15
15
* limitations under the License.
16
16
*/
17
17
18
+ #include < string.h>
18
19
#include " QUECTEL/BG96/QUECTEL_BG96_CellularStack.h"
19
20
#include " CellularLog.h"
20
21
@@ -60,8 +61,10 @@ nsapi_error_t QUECTEL_BG96_CellularStack::socket_connect(nsapi_socket_t handle,
60
61
61
62
_at.lock ();
62
63
if (socket->proto == NSAPI_TCP) {
64
+ char ipdot[NSAPI_IP_SIZE];
65
+ ip2dot (address, ipdot);
63
66
_at.at_cmd_discard (" +QIOPEN" , " =" , " %d%d%s%s%d%d%d" , _cid, request_connect_id, " TCP" ,
64
- address. get_ip_address () , address.get_port (), socket->localAddress .get_port (), 0 );
67
+ ipdot , address.get_port (), socket->localAddress .get_port (), 0 );
65
68
66
69
handle_open_socket_response (modem_connect_id, err);
67
70
@@ -74,7 +77,7 @@ nsapi_error_t QUECTEL_BG96_CellularStack::socket_connect(nsapi_socket_t handle,
74
77
_at.at_cmd_discard (" +QICLOSE" , " =" , " %d" , modem_connect_id);
75
78
76
79
_at.at_cmd_discard (" +QIOPEN" , " =" , " %d%d%s%s%d%d%d" , _cid, request_connect_id, " TCP" ,
77
- address. get_ip_address () , address.get_port (), socket->localAddress .get_port (), 0 );
80
+ ipdot , address.get_port (), socket->localAddress .get_port (), 0 );
78
81
79
82
handle_open_socket_response (modem_connect_id, err);
80
83
}
@@ -228,8 +231,10 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
228
231
handle_open_socket_response (modem_connect_id, err);
229
232
}
230
233
} else if (socket->proto == NSAPI_UDP && socket->connected ) {
234
+ char ipdot[NSAPI_IP_SIZE];
235
+ ip2dot (socket->remoteAddress , ipdot);
231
236
_at.at_cmd_discard (" +QIOPEN" , " =" , " %d%d%s%s%d" , _cid, request_connect_id, " UDP" ,
232
- socket-> remoteAddress . get_ip_address () , socket->remoteAddress .get_port ());
237
+ ipdot , socket->remoteAddress .get_port ());
233
238
234
239
handle_open_socket_response (modem_connect_id, err);
235
240
@@ -241,7 +246,7 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
241
246
socket_close_impl (modem_connect_id);
242
247
243
248
_at.at_cmd_discard (" +QIOPEN" , " =" , " %d%d%s%s%d" , _cid, request_connect_id, " UDP" ,
244
- socket-> remoteAddress . get_ip_address () , socket->remoteAddress .get_port ());
249
+ ipdot , socket->remoteAddress .get_port ());
245
250
246
251
handle_open_socket_response (modem_connect_id, err);
247
252
}
@@ -277,8 +282,10 @@ nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl(CellularSoc
277
282
278
283
// Send
279
284
if (socket->proto == NSAPI_UDP) {
285
+ char ipdot[NSAPI_IP_SIZE];
286
+ ip2dot (address, ipdot);
280
287
_at.cmd_start_stop (" +QISEND" , " =" , " %d%d%s%d" , socket->id , size,
281
- address. get_ip_address () , address.get_port ());
288
+ ipdot , address.get_port ());
282
289
} else {
283
290
_at.cmd_start_stop (" +QISEND" , " =" , " %d%d" , socket->id , size);
284
291
}
@@ -401,3 +408,21 @@ nsapi_error_t QUECTEL_BG96_CellularStack::gethostbyname_async_cancel(int id)
401
408
return NSAPI_ERROR_OK;
402
409
}
403
410
#endif
411
+
412
+ void QUECTEL_BG96_CellularStack::ip2dot (const SocketAddress &ip, char *dot)
413
+ {
414
+ if (ip.get_ip_version () == NSAPI_IPv6) {
415
+ const uint8_t *bytes = (uint8_t *)ip.get_ip_bytes ();
416
+ char *p = dot;
417
+ for (int i = 0 ; i < NSAPI_IPv6_BYTES; i += 2 ) {
418
+ if (i != 0 ) {
419
+ *dot++ = ' :' ;
420
+ }
421
+ dot += sprintf (dot, " %x" , (*(bytes + i) << 8 | *(bytes + i + 1 )));
422
+ }
423
+ } else if (ip.get_ip_version () == NSAPI_IPv4) {
424
+ strcpy (dot, ip.get_ip_address ());
425
+ } else {
426
+ *dot = ' \0 ' ;
427
+ }
428
+ }
0 commit comments