Skip to content

Commit 88dcf27

Browse files
authored
Merge pull request #11246 from AriParkkila/cell-bg96-ipv6
Cellular: Fix BG96 AT driver for IPv6
2 parents 28daa3f + 110fb7d commit 88dcf27

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include <string.h>
1819
#include "QUECTEL/BG96/QUECTEL_BG96_CellularStack.h"
1920
#include "CellularLog.h"
2021

@@ -60,8 +61,10 @@ nsapi_error_t QUECTEL_BG96_CellularStack::socket_connect(nsapi_socket_t handle,
6061

6162
_at.lock();
6263
if (socket->proto == NSAPI_TCP) {
64+
char ipdot[NSAPI_IP_SIZE];
65+
ip2dot(address, ipdot);
6366
_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);
6568

6669
handle_open_socket_response(modem_connect_id, err);
6770

@@ -74,7 +77,7 @@ nsapi_error_t QUECTEL_BG96_CellularStack::socket_connect(nsapi_socket_t handle,
7477
_at.at_cmd_discard("+QICLOSE", "=", "%d", modem_connect_id);
7578

7679
_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);
7881

7982
handle_open_socket_response(modem_connect_id, err);
8083
}
@@ -228,8 +231,10 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
228231
handle_open_socket_response(modem_connect_id, err);
229232
}
230233
} else if (socket->proto == NSAPI_UDP && socket->connected) {
234+
char ipdot[NSAPI_IP_SIZE];
235+
ip2dot(socket->remoteAddress, ipdot);
231236
_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());
233238

234239
handle_open_socket_response(modem_connect_id, err);
235240

@@ -241,7 +246,7 @@ nsapi_error_t QUECTEL_BG96_CellularStack::create_socket_impl(CellularSocket *soc
241246
socket_close_impl(modem_connect_id);
242247

243248
_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());
245250

246251
handle_open_socket_response(modem_connect_id, err);
247252
}
@@ -277,8 +282,10 @@ nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl(CellularSoc
277282

278283
// Send
279284
if (socket->proto == NSAPI_UDP) {
285+
char ipdot[NSAPI_IP_SIZE];
286+
ip2dot(address, ipdot);
280287
_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());
282289
} else {
283290
_at.cmd_start_stop("+QISEND", "=", "%d%d", socket->id, size);
284291
}
@@ -401,3 +408,21 @@ nsapi_error_t QUECTEL_BG96_CellularStack::gethostbyname_async_cancel(int id)
401408
return NSAPI_ERROR_OK;
402409
}
403410
#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+
}

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ class QUECTEL_BG96_CellularStack : public AT_CellularStack {
8989
hostbyname_cb_t _dns_callback;
9090
nsapi_version_t _dns_version;
9191
#endif
92+
/** Convert IP address to dotted string representation
93+
*
94+
* BG96 requires consecutive zeros so can't use get_ip_address or ip6tos directly.
95+
*
96+
* @param ip address
97+
* @param dot buffer with size NSAPI_IPv6, where address is written zero terminated
98+
*/
99+
void ip2dot(const SocketAddress &ip, char *dot);
92100
};
93101
} // namespace mbed
94102
#endif /* QUECTEL_BG96_CELLULARSTACK_H_ */

0 commit comments

Comments
 (0)