Skip to content

Commit 04ee598

Browse files
atxdpgeorge
authored andcommitted
lib: Move some common mod_network_* functions to lib/netutils.
1 parent 47b9809 commit 04ee598

13 files changed

Lines changed: 187 additions & 170 deletions

File tree

cc3200/application.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ APP_INC += -I$(BUILD)
2020
APP_INC += -I$(BUILD)/genhdr
2121
APP_INC += -I../lib/fatfs
2222
APP_INC += -I../lib/mp-readline
23+
APP_INC += -I../lib/netutils
2324
APP_INC += -I../stmhal
2425

2526
APP_CPPDEFINES = -Dgcc -DTARGET_IS_CC3200 -DSL_FULL -DUSE_FREERTOS
@@ -142,6 +143,7 @@ APP_LIB_SRC_C = $(addprefix lib/,\
142143
fatfs/option/ccsbcs.c \
143144
libc/string0.c \
144145
mp-readline/readline.c \
146+
netutils/netutils.c \
145147
)
146148

147149
APP_STM_SRC_C = $(addprefix stmhal/,\

cc3200/mods/modnetwork.c

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -114,60 +114,3 @@ const mp_obj_module_t mp_module_network = {
114114
.name = MP_QSTR_network,
115115
.globals = (mp_obj_dict_t*)&mp_module_network_globals,
116116
};
117-
118-
/******************************************************************************/
119-
// Miscellaneous helpers
120-
121-
// Takes an address of the form '192.168.0.1' and converts it to integer
122-
// in out_ip (little endian, so the 192 is the last byte).
123-
void mod_network_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip) {
124-
mp_uint_t addr_len;
125-
const char *addr_str = mp_obj_str_get_data(addr_in, &addr_len);
126-
if (addr_len == 0) {
127-
// special case of no address given
128-
memset(out_ip, 0, MOD_NETWORK_IPV4ADDR_BUF_SIZE);
129-
return;
130-
}
131-
const char *s = addr_str;
132-
const char *s_top = addr_str + addr_len;
133-
for (mp_uint_t i = 3 ; ; i--) {
134-
mp_uint_t val = 0;
135-
for (; s < s_top && *s != '.'; s++) {
136-
val = val * 10 + *s - '0';
137-
}
138-
out_ip[i] = val;
139-
if (i == 0 && s == s_top) {
140-
return;
141-
} else if (i > 0 && s < s_top && *s == '.') {
142-
s++;
143-
} else {
144-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
145-
}
146-
}
147-
}
148-
149-
// Takes an address of the form ('192.168.0.1', 8080), returns the port and
150-
// puts IP in out_ip (which must take at least IPADDR_BUF_SIZE bytes).
151-
mp_uint_t mod_network_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip) {
152-
mp_obj_t *addr_items;
153-
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
154-
mod_network_parse_ipv4_addr(addr_items[0], out_ip);
155-
return mp_obj_get_int(addr_items[1]);
156-
}
157-
158-
// Takes an array with a raw IPv4 address and returns something like '192.168.0.1'.
159-
mp_obj_t mod_network_format_ipv4_addr(uint8_t *ip) {
160-
char ip_str[16];
161-
mp_uint_t ip_len = snprintf(ip_str, 16, "%u.%u.%u.%u", ip[3], ip[2], ip[1], ip[0]);
162-
return mp_obj_new_str(ip_str, ip_len, false);
163-
}
164-
165-
// Takes an array with a raw IP address, and a port, and returns a net-address
166-
// tuple such as ('192.168.0.1', 8080).
167-
mp_obj_t mod_network_format_inet_addr(uint8_t *ip, mp_uint_t port) {
168-
mp_obj_t tuple[2] = {
169-
tuple[0] = mod_network_format_ipv4_addr(ip),
170-
tuple[1] = mp_obj_new_int(port),
171-
};
172-
return mp_obj_new_tuple(2, tuple);
173-
}

cc3200/mods/modnetwork.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,4 @@ void mod_network_init0(void);
7777
void mod_network_register_nic(mp_obj_t nic);
7878
mp_obj_t mod_network_find_nic(void);
7979

80-
void mod_network_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip);
81-
mp_uint_t mod_network_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip);
82-
mp_obj_t mod_network_format_ipv4_addr(uint8_t *ip);
83-
mp_obj_t mod_network_format_inet_addr(uint8_t *ip, mp_uint_t port);
84-
8580
#endif // MODNETWORK_H_

cc3200/mods/modusocket.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "py/mpstate.h"
3333
#include MICROPY_HAL_H
3434
#include "py/runtime.h"
35+
#include "netutils.h"
3536
#include "modnetwork.h"
3637
#include "mpexception.h"
3738

@@ -99,7 +100,7 @@ STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
99100

100101
// get address
101102
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
102-
mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip);
103+
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE);
103104

104105
// call the NIC to bind the socket
105106
int _errno;
@@ -155,7 +156,7 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
155156
// make the return value
156157
mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
157158
client->items[0] = socket2;
158-
client->items[1] = mod_network_format_inet_addr(ip, port);
159+
client->items[1] = netutils_format_inet_addr(ip, port, NETUTILS_LITTLE);
159160

160161
return client;
161162
}
@@ -167,7 +168,7 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
167168

168169
// get address
169170
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
170-
mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip);
171+
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE);
171172

172173
// call the NIC to connect the socket
173174
int _errno;
@@ -231,7 +232,7 @@ STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_
231232

232233
// get address
233234
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
234-
mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip);
235+
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE);
235236

236237
// call the NIC to sendto
237238
int _errno;
@@ -268,7 +269,7 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
268269
vstr.buf[vstr.len] = '\0';
269270
tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
270271
}
271-
tuple[1] = mod_network_format_inet_addr(ip, port);
272+
tuple[1] = netutils_format_inet_addr(ip, port, NETUTILS_LITTLE);
272273
return mp_obj_new_tuple(2, tuple);
273274
}
274275
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recvfrom_obj, socket_recvfrom);
@@ -399,7 +400,7 @@ STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) {
399400
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCK_STREAM);
400401
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
401402
tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
402-
tuple->items[4] = mod_network_format_inet_addr(out_ip, port);
403+
tuple->items[4] = netutils_format_inet_addr(out_ip, port, NETUTILS_LITTLE);
403404
return mp_obj_new_list(1, (mp_obj_t*)&tuple);
404405
}
405406
}

cc3200/mods/modwlan.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "py/obj.h"
3535
#include "py/objstr.h"
3636
#include "py/runtime.h"
37+
#include "netutils.h"
3738
#include "modnetwork.h"
3839
#include "modwlan.h"
3940
#include "pybioctl.h"
@@ -836,10 +837,10 @@ STATIC mp_obj_t wlan_ifconfig (mp_obj_t self_in) {
836837
ifconfig[1] = mp_obj_new_str((const char *)wlan_obj.ssid, strlen((const char *)wlan_obj.ssid), false);
837838
ifconfig[2] = mp_obj_new_bytes((const byte *)wlan_obj.bssid, SL_BSSID_LENGTH);
838839
ifconfig[3] = mp_obj_new_bytes((const byte *)wlan_obj.mac, SL_BSSID_LENGTH);
839-
ifconfig[4] = mod_network_format_ipv4_addr((uint8_t *)&wlan_obj.ip);
840-
ifconfig[5] = mod_network_format_ipv4_addr((uint8_t *)&ipV4.ipV4Mask);
841-
ifconfig[6] = mod_network_format_ipv4_addr((uint8_t *)&wlan_obj.gateway);
842-
ifconfig[7] = mod_network_format_ipv4_addr((uint8_t *)&wlan_obj.dns);
840+
ifconfig[4] = netutils_format_ipv4_addr((uint8_t *)&wlan_obj.ip, NETUTILS_LITTLE);
841+
ifconfig[5] = netutils_format_ipv4_addr((uint8_t *)&ipV4.ipV4Mask, NETUTILS_LITTLE);
842+
ifconfig[6] = netutils_format_ipv4_addr((uint8_t *)&wlan_obj.gateway, NETUTILS_LITTLE);
843+
ifconfig[7] = netutils_format_ipv4_addr((uint8_t *)&wlan_obj.dns, NETUTILS_LITTLE);
843844

844845
return mp_obj_new_attrtuple(wlan_ifconfig_fields, 8, ifconfig);
845846
}
@@ -992,10 +993,10 @@ STATIC mp_obj_t wlan_config_ip (mp_uint_t n_args, const mp_obj_t *pos_args, mp_m
992993
}
993994

994995
SlNetCfgIpV4Args_t ipV4;
995-
mod_network_parse_ipv4_addr(args[1].u_obj, (uint8_t *)&ipV4.ipV4);
996-
mod_network_parse_ipv4_addr(args[2].u_obj, (uint8_t *)&ipV4.ipV4Mask);
997-
mod_network_parse_ipv4_addr(args[3].u_obj, (uint8_t *)&ipV4.ipV4Gateway);
998-
mod_network_parse_ipv4_addr(args[4].u_obj, (uint8_t *)&ipV4.ipV4DnsServer);
996+
netutils_parse_ipv4_addr(args[1].u_obj, (uint8_t *)&ipV4.ipV4, NETUTILS_LITTLE);
997+
netutils_parse_ipv4_addr(args[2].u_obj, (uint8_t *)&ipV4.ipV4Mask, NETUTILS_LITTLE);
998+
netutils_parse_ipv4_addr(args[3].u_obj, (uint8_t *)&ipV4.ipV4Gateway, NETUTILS_LITTLE);
999+
netutils_parse_ipv4_addr(args[4].u_obj, (uint8_t *)&ipV4.ipV4DnsServer, NETUTILS_LITTLE);
9991000

10001001
wlan_servers_stop();
10011002

lib/netutils/netutils.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
* Copyright (c) 2015 Daniel Campora
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include <stdint.h>
29+
#include <stdio.h>
30+
#include <string.h>
31+
32+
#include "py/obj.h"
33+
#include "py/nlr.h"
34+
#include "netutils.h"
35+
36+
// Takes an array with a raw IPv4 address and returns something like '192.168.0.1'.
37+
mp_obj_t netutils_format_ipv4_addr(uint8_t *ip, netutils_endian_t endian) {
38+
char ip_str[16];
39+
mp_uint_t ip_len;
40+
if (endian == NETUTILS_LITTLE) {
41+
ip_len = snprintf(ip_str, 16, "%u.%u.%u.%u", ip[3], ip[2], ip[1], ip[0]);
42+
} else {
43+
ip_len = snprintf(ip_str, 16, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
44+
}
45+
return mp_obj_new_str(ip_str, ip_len, false);
46+
}
47+
48+
// Takes an array with a raw IP address, and a port, and returns a net-address
49+
// tuple such as ('192.168.0.1', 8080).
50+
mp_obj_t netutils_format_inet_addr(uint8_t *ip, mp_uint_t port, netutils_endian_t endian) {
51+
mp_obj_t tuple[2] = {
52+
tuple[0] = netutils_format_ipv4_addr(ip, endian),
53+
tuple[1] = mp_obj_new_int(port),
54+
};
55+
return mp_obj_new_tuple(2, tuple);
56+
}
57+
58+
void netutils_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip, netutils_endian_t endian) {
59+
mp_uint_t addr_len;
60+
const char *addr_str = mp_obj_str_get_data(addr_in, &addr_len);
61+
if (addr_len == 0) {
62+
// special case of no address given
63+
memset(out_ip, 0, NETUTILS_IPV4ADDR_BUFSIZE);
64+
return;
65+
}
66+
const char *s = addr_str;
67+
const char *s_top = addr_str + addr_len;
68+
for (mp_uint_t i = 3 ; ; i--) {
69+
mp_uint_t val = 0;
70+
for (; s < s_top && *s != '.'; s++) {
71+
val = val * 10 + *s - '0';
72+
}
73+
if (endian == NETUTILS_LITTLE) {
74+
out_ip[i] = val;
75+
} else {
76+
out_ip[NETUTILS_IPV4ADDR_BUFSIZE - 1 - i] = val;
77+
}
78+
if (i == 0 && s == s_top) {
79+
return;
80+
} else if (i > 0 && s < s_top && *s == '.') {
81+
s++;
82+
} else {
83+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid arguments"));
84+
}
85+
}
86+
}
87+
88+
// Takes an address of the form ('192.168.0.1', 8080), returns the port and
89+
// puts IP in out_ip (which must take at least IPADDR_BUF_SIZE bytes).
90+
mp_uint_t netutils_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip, netutils_endian_t endian) {
91+
mp_obj_t *addr_items;
92+
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
93+
netutils_parse_ipv4_addr(addr_items[0], out_ip, endian);
94+
return mp_obj_get_int(addr_items[1]);
95+
}

lib/netutils/netutils.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
* Copyright (c) 2015 Daniel Campora
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
#ifndef __MICROPY_INCLUDED_LIB_NETUTILS_H__
28+
#define __MICROPY_INCLUDED_LIB_NETUTILS_H__
29+
30+
#define NETUTILS_IPV4ADDR_BUFSIZE 4
31+
32+
typedef enum _netutils_endian_t {
33+
NETUTILS_LITTLE,
34+
NETUTILS_BIG,
35+
} netutils_endian_t;
36+
37+
// Takes an array with a raw IPv4 address and returns something like '192.168.0.1'.
38+
mp_obj_t netutils_format_ipv4_addr(uint8_t *ip, netutils_endian_t endian);
39+
40+
// Takes an array with a raw IP address, and a port, and returns a net-address
41+
// tuple such as ('192.168.0.1', 8080).
42+
mp_obj_t netutils_format_inet_addr(uint8_t *ip, mp_uint_t port, netutils_endian_t endian);
43+
44+
void netutils_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip, netutils_endian_t endian);
45+
46+
// Takes an address of the form ('192.168.0.1', 8080), returns the port and
47+
// puts IP in out_ip (which must take at least IPADDR_BUF_SIZE bytes).
48+
mp_uint_t netutils_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip, netutils_endian_t endian);
49+
50+
#endif // __MICROPY_INCLUDED_LIB_NETUTILS_H__

stmhal/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ INC += -I$(HAL_DIR)/inc
4141
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/inc
4242
#INC += -I$(USBHOST_DIR)
4343
INC += -I../lib/mp-readline
44+
INC += -I../lib/netutils
4445

4546
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion
4647
CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_CORTEX_M4) $(COPT)
@@ -92,6 +93,7 @@ SRC_LIB = $(addprefix lib/,\
9293
fatfs/ff.c \
9394
fatfs/option/ccsbcs.c \
9495
mp-readline/readline.c \
96+
netutils/netutils.c \
9597
)
9698

9799
SRC_C = \

0 commit comments

Comments
 (0)