|
19 | 19 | #include "rtos.h"
|
20 | 20 |
|
21 | 21 |
|
| 22 | +// Portable big -> little endian |
| 23 | +static inline MDMParser::IP ntohl(MDMParser::IP ip) { |
| 24 | + ip = ((0xff & (ip >> 24)) << 0) |
| 25 | + | ((0xff & (ip >> 16)) << 8) |
| 26 | + | ((0xff & (ip >> 8)) << 16) |
| 27 | + | ((0xff & (ip >> 0)) << 24); |
| 28 | + return ip; |
| 29 | +} |
| 30 | + |
| 31 | +static inline MDMParser::IP htonl(MDMParser::IP ip) { |
| 32 | + return ntohl(ip); |
| 33 | +} |
| 34 | + |
| 35 | + |
22 | 36 | // C027Interface implementation
|
23 | 37 | C027Interface::C027Interface(const char *simpin, bool debug)
|
24 | 38 | : _debug(debug)
|
@@ -54,7 +68,7 @@ int C027Interface::connect(const char *apn, const char *username, const char *pa
|
54 | 68 |
|
55 | 69 | if (mdmOk) {
|
56 | 70 | // join the internet connection
|
57 |
| - MDMParser::IP ip = _mdm->join(apn, username, password); |
| 71 | + MDMParser::IP ip = htonl(_mdm->join(apn, username, password)); |
58 | 72 | _ip_address.set_ip_bytes(&ip, NSAPI_IPv4);
|
59 | 73 | mdmOk = (ip != NOIP);
|
60 | 74 | }
|
@@ -228,7 +242,7 @@ int C027Interface::socket_sendto(void *handle, const SocketAddress &addr, const
|
228 | 242 |
|
229 | 243 | socket->mutex.lock();
|
230 | 244 | int sent = _mdm->socketSendTo(socket->socket,
|
231 |
| - *(MDMParser::IP *)addr.get_ip_bytes(), addr.get_port(), |
| 245 | + ntohl(*(MDMParser::IP *)addr.get_ip_bytes()), addr.get_port(), |
232 | 246 | (const char *)data, size);
|
233 | 247 |
|
234 | 248 | if (socket->callback) {
|
@@ -268,6 +282,7 @@ int C027Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data
|
268 | 282 | }
|
269 | 283 |
|
270 | 284 | if (addr) {
|
| 285 | + ip = htonl(ip); |
271 | 286 | addr->set_ip_bytes(&ip, NSAPI_IPv4);
|
272 | 287 | addr->set_port(port);
|
273 | 288 | }
|
|
0 commit comments