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
}
@@ -264,7 +269,9 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
264
269
nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl (CellularSocket *socket, const SocketAddress &address,
265
270
const void *data, nsapi_size_t size)
266
271
{
267
- if (size > BG96_MAX_SEND_SIZE) {
272
+ if (size > BG96_MAX_SEND_SIZE ||
273
+ (_stack_type == IPV4_STACK && address.get_ip_version () != NSAPI_IPv4) ||
274
+ (_stack_type == IPV6_STACK && address.get_ip_version () != NSAPI_IPv6)) {
268
275
return NSAPI_ERROR_PARAMETER;
269
276
}
270
277
@@ -277,8 +284,10 @@ nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl(CellularSoc
277
284
278
285
// Send
279
286
if (socket->proto == NSAPI_UDP) {
287
+ char ipdot[NSAPI_IP_SIZE];
288
+ ip2dot (address, ipdot);
280
289
_at.cmd_start_stop (" +QISEND" , " =" , " %d%d%s%d" , socket->id , size,
281
- address. get_ip_address () , address.get_port ());
290
+ ipdot , address.get_port ());
282
291
} else {
283
292
_at.cmd_start_stop (" +QISEND" , " =" , " %d%d" , socket->id , size);
284
293
}
@@ -401,3 +410,21 @@ nsapi_error_t QUECTEL_BG96_CellularStack::gethostbyname_async_cancel(int id)
401
410
return NSAPI_ERROR_OK;
402
411
}
403
412
#endif
413
+
414
+ void QUECTEL_BG96_CellularStack::ip2dot (const SocketAddress &ip, char *dot)
415
+ {
416
+ if (ip.get_ip_version () == NSAPI_IPv6) {
417
+ const uint8_t *bytes = (uint8_t *)ip.get_ip_bytes ();
418
+ char *p = dot;
419
+ for (int i = 0 ; i < NSAPI_IPv6_BYTES; i += 2 ) {
420
+ if (i != 0 ) {
421
+ *dot++ = ' :' ;
422
+ }
423
+ dot += sprintf (dot, " %x" , (*(bytes + i) << 8 | *(bytes + i + 1 )));
424
+ }
425
+ } else if (ip.get_ip_version () == NSAPI_IPv4) {
426
+ strcpy (dot, ip.get_ip_address ());
427
+ } else {
428
+ *dot = ' \0 ' ;
429
+ }
430
+ }
0 commit comments