Friendly LWM2M client
WppConnection.cpp
Go to the documentation of this file.
1 /*
2  * WppConnection.cpp
3  *
4  * Created on: 22 Jul 2023
5  * Author: valentin
6  */
7 
8 #include "WppConnection.h"
9 
10 #include "WppClient.h"
11 #include "WppRegistry.h"
12 #include "WppLogs.h"
13 
14 namespace wpp {
15 
17 
20 }
21 
22 /* ------------- Connection abilities ------------- */
23 
25  Packet tmpPkt = packet;
26 
27  WPP_LOGD(TAG_WPP_CONN, "Retrieved new packet from server: session-> %p, length->%d", packet.session, packet.length);
28  tmpPkt.buffer = new uint8_t[packet.length];
29  memcpy(tmpPkt.buffer, packet.buffer, packet.length);
30 
31  if (!packets.push(&tmpPkt)) {
32  WPP_LOGE(TAG_WPP_CONN, "Error ocured during add packet to queue: queue size -> %d, max queue size -> %d", getPacketQueueSize(), WPP_CONN_I_PACKETS_QUEUE_SIZE);
33  delete [] tmpPkt.buffer;
34  return false;
35  }
36 
37  return true;
38 }
39 
41  return packets.size();
42 }
43 
45  WPP_LOGD(TAG_WPP_CONN, "Clearing queue: queue size -> %d", getPacketQueueSize());
46  while (packets.size()) {
47  Packet *pkt = packets.front();
48  if (pkt && pkt->buffer) {
49  delete [] pkt->buffer;
50  }
51  packets.pop();
52  }
53 }
54 
55 bool WppConnection::setDataBlockSize(uint16_t size) {
56  WPP_LOGD(TAG_WPP_CONN, "New data block size -> %d", size);
57  return lwm2m_set_coap_block_size(size);
58 }
59 
61  return lwm2m_get_coap_block_size();
62 }
63 
65  WPP_LOGD(TAG_WPP_CONN, "Handling packets in queue: packets count -> %d", getPacketQueueSize());
66  while (packets.size()) {
67  Packet *pkt = packets.front();
68  if (pkt && pkt->buffer) {
69  lwm2m_handle_packet(&client.getContext(), pkt->buffer, pkt->length, pkt->session);
70  delete [] pkt->buffer;
71  }
72  packets.pop();
73  }
74  WPP_LOGD(TAG_WPP_CONN, "Done");
75 }
76 
77 extern "C" {
78  /*---------- Connection bindings ----------*/
79  void * lwm2m_connect_server(uint16_t secObjInstID, void * userData) {
80  if (!userData) {
81  WPP_LOGE(TAG_WPP_CONN, "User data is NULL");
82  return NULL;
83  }
84 
85  wpp::WppClient *client = (wpp::WppClient *)userData;
86  WPP_LOGD(TAG_WPP_CONN, "Connecting to server: security obj ID -> %d", secObjInstID);
87  wpp::Lwm2mSecurity *security = Lwm2mSecurity::instance(*client, secObjInstID);
88  if (!security) {
89  WPP_LOGE(TAG_WPP_CONN, "Lwm2mSecurity obj with ID -> %d not found", secObjInstID);
90  return NULL;
91  }
92 
93  wpp::WppConnection::SESSION_T session = client->connection().connect(*security);
94  if (!session) {
95  WPP_LOGE(TAG_WPP_CONN, "Not posible connect to server: security obj ID-> %d, session -> %p", secObjInstID, session);
96  } else {
97  WPP_LOGI(TAG_WPP_CONN, "Connected to server: security obj ID-> %d, session -> %p", secObjInstID, session);
98  }
99 
100  return session;
101  }
102 
103  void lwm2m_close_connection(void * sessionH, void * userData) {
104  if (!userData) {
105  WPP_LOGE(TAG_WPP_CONN, "User data is NULL");
106  return;
107  }
108 
109  wpp::WppClient *client = (wpp::WppClient *)userData;
110  WPP_LOGI(TAG_WPP_CONN, "Close connection: session -> %p", sessionH);
111  client->connection().disconnect(sessionH);
112  }
113 
114  uint8_t lwm2m_buffer_send(void * sessionH, uint8_t * buffer, size_t length, void * userData) {
115  if (!userData) {
116  WPP_LOGE(TAG_WPP_CONN, "User data is NULL");
117  return COAP_500_INTERNAL_SERVER_ERROR;
118  }
119 
120  wpp::WppClient *client = (wpp::WppClient *)userData;
121  WPP_LOGD(TAG_WPP_CONN, "Sending buffer to server: session -> %p, size -> %d", sessionH, length);
122  bool result = client->connection().sendPacket({sessionH, length, buffer});
123  WPP_LOGD(TAG_WPP_CONN, "Sending buffer to server: session -> %p, result -> %d", sessionH, result);
124  return result? COAP_NO_ERROR : COAP_500_INTERNAL_SERVER_ERROR;
125  }
126 
127  bool lwm2m_session_is_equal(void * session1, void * session2, void * userData) {
128  if (!userData) {
129  WPP_LOGE(TAG_WPP_CONN, "User data is NULL");
130  return false;
131  }
132 
133  wpp::WppClient *client = (wpp::WppClient *)userData;
134  return client->connection().sessionCmp(session1, session2);
135  }
136 }
137 
138 } /* namespace wpp */
#define WPP_CONN_I_PACKETS_QUEUE_SIZE
Definition: WppConnection.h:16
#define WPP_LOGE(TAG, FMT,...)
Definition: WppLogs.h:49
#define WPP_LOGD(TAG, FMT,...)
Definition: WppLogs.h:31
#define WPP_LOGI(TAG, FMT,...)
Definition: WppLogs.h:37
#define TAG_WPP_CONN
Definition: WppLogs.h:9
static Lwm2mSecurity * instance(WppClient &ctx, ID_T instId=ID_T_MAX_VAL)
Gets an instance of the object.
Represents a client interface for Wpp library.
Definition: WppClient.h:37
lwm2m_context_t & getContext()
Gets the LwM2M context associated with the WppClient.
Definition: WppClient.cpp:102
WppConnection & connection()
Gets the WppConnection associated with the WppClient.
Definition: WppClient.cpp:89
virtual SESSION_T connect(Lwm2mSecurity &security)=0
Establishes a connection using the provided Lwm2mSecurity object.
uint16_t getDataBlockSize()
Retrieves the COAP block size.
virtual void disconnect(SESSION_T session)=0
Disconnects from the specified session.
virtual bool sendPacket(const Packet &packet)=0
Sends a packet over the connection.
void clearPacketQueue()
Clears the packet queue.
virtual bool sessionCmp(SESSION_T session1, SESSION_T session2)=0
Compares two session identifiers.
bool setDataBlockSize(uint16_t size)
Sets the COAP block size.
bool addPacketToQueue(const Packet &packet)
Adds a packet to the packet queue.
virtual ~WppConnection()
uint8_t getPacketQueueSize()
Retrieves the size of the packet queue.
void handlePacketsInQueue(WppClient &client)
Processes the packets in the packet queue.
The WppConnection class represents a connection interface for the Wpp library.
Definition: WppClient.cpp:14
void lwm2m_close_connection(void *sessionH, void *userData)
bool lwm2m_session_is_equal(void *session1, void *session2, void *userData)
void * lwm2m_connect_server(uint16_t secObjInstID, void *userData)
uint8_t lwm2m_buffer_send(void *sessionH, uint8_t *buffer, size_t length, void *userData)