lib: Move some common mod_network_* functions to lib/netutils.
This commit is contained in:
parent
47b9809d23
commit
04ee5983fe
@ -20,6 +20,7 @@ APP_INC += -I$(BUILD)
|
|||||||
APP_INC += -I$(BUILD)/genhdr
|
APP_INC += -I$(BUILD)/genhdr
|
||||||
APP_INC += -I../lib/fatfs
|
APP_INC += -I../lib/fatfs
|
||||||
APP_INC += -I../lib/mp-readline
|
APP_INC += -I../lib/mp-readline
|
||||||
|
APP_INC += -I../lib/netutils
|
||||||
APP_INC += -I../stmhal
|
APP_INC += -I../stmhal
|
||||||
|
|
||||||
APP_CPPDEFINES = -Dgcc -DTARGET_IS_CC3200 -DSL_FULL -DUSE_FREERTOS
|
APP_CPPDEFINES = -Dgcc -DTARGET_IS_CC3200 -DSL_FULL -DUSE_FREERTOS
|
||||||
@ -142,6 +143,7 @@ APP_LIB_SRC_C = $(addprefix lib/,\
|
|||||||
fatfs/option/ccsbcs.c \
|
fatfs/option/ccsbcs.c \
|
||||||
libc/string0.c \
|
libc/string0.c \
|
||||||
mp-readline/readline.c \
|
mp-readline/readline.c \
|
||||||
|
netutils/netutils.c \
|
||||||
)
|
)
|
||||||
|
|
||||||
APP_STM_SRC_C = $(addprefix stmhal/,\
|
APP_STM_SRC_C = $(addprefix stmhal/,\
|
||||||
|
@ -114,60 +114,3 @@ const mp_obj_module_t mp_module_network = {
|
|||||||
.name = MP_QSTR_network,
|
.name = MP_QSTR_network,
|
||||||
.globals = (mp_obj_dict_t*)&mp_module_network_globals,
|
.globals = (mp_obj_dict_t*)&mp_module_network_globals,
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
// Miscellaneous helpers
|
|
||||||
|
|
||||||
// Takes an address of the form '192.168.0.1' and converts it to integer
|
|
||||||
// in out_ip (little endian, so the 192 is the last byte).
|
|
||||||
void mod_network_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip) {
|
|
||||||
mp_uint_t addr_len;
|
|
||||||
const char *addr_str = mp_obj_str_get_data(addr_in, &addr_len);
|
|
||||||
if (addr_len == 0) {
|
|
||||||
// special case of no address given
|
|
||||||
memset(out_ip, 0, MOD_NETWORK_IPV4ADDR_BUF_SIZE);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const char *s = addr_str;
|
|
||||||
const char *s_top = addr_str + addr_len;
|
|
||||||
for (mp_uint_t i = 3 ; ; i--) {
|
|
||||||
mp_uint_t val = 0;
|
|
||||||
for (; s < s_top && *s != '.'; s++) {
|
|
||||||
val = val * 10 + *s - '0';
|
|
||||||
}
|
|
||||||
out_ip[i] = val;
|
|
||||||
if (i == 0 && s == s_top) {
|
|
||||||
return;
|
|
||||||
} else if (i > 0 && s < s_top && *s == '.') {
|
|
||||||
s++;
|
|
||||||
} else {
|
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Takes an address of the form ('192.168.0.1', 8080), returns the port and
|
|
||||||
// puts IP in out_ip (which must take at least IPADDR_BUF_SIZE bytes).
|
|
||||||
mp_uint_t mod_network_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip) {
|
|
||||||
mp_obj_t *addr_items;
|
|
||||||
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
|
|
||||||
mod_network_parse_ipv4_addr(addr_items[0], out_ip);
|
|
||||||
return mp_obj_get_int(addr_items[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Takes an array with a raw IPv4 address and returns something like '192.168.0.1'.
|
|
||||||
mp_obj_t mod_network_format_ipv4_addr(uint8_t *ip) {
|
|
||||||
char ip_str[16];
|
|
||||||
mp_uint_t ip_len = snprintf(ip_str, 16, "%u.%u.%u.%u", ip[3], ip[2], ip[1], ip[0]);
|
|
||||||
return mp_obj_new_str(ip_str, ip_len, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Takes an array with a raw IP address, and a port, and returns a net-address
|
|
||||||
// tuple such as ('192.168.0.1', 8080).
|
|
||||||
mp_obj_t mod_network_format_inet_addr(uint8_t *ip, mp_uint_t port) {
|
|
||||||
mp_obj_t tuple[2] = {
|
|
||||||
tuple[0] = mod_network_format_ipv4_addr(ip),
|
|
||||||
tuple[1] = mp_obj_new_int(port),
|
|
||||||
};
|
|
||||||
return mp_obj_new_tuple(2, tuple);
|
|
||||||
}
|
|
||||||
|
@ -77,9 +77,4 @@ void mod_network_init0(void);
|
|||||||
void mod_network_register_nic(mp_obj_t nic);
|
void mod_network_register_nic(mp_obj_t nic);
|
||||||
mp_obj_t mod_network_find_nic(void);
|
mp_obj_t mod_network_find_nic(void);
|
||||||
|
|
||||||
void mod_network_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip);
|
|
||||||
mp_uint_t mod_network_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip);
|
|
||||||
mp_obj_t mod_network_format_ipv4_addr(uint8_t *ip);
|
|
||||||
mp_obj_t mod_network_format_inet_addr(uint8_t *ip, mp_uint_t port);
|
|
||||||
|
|
||||||
#endif // MODNETWORK_H_
|
#endif // MODNETWORK_H_
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
#include "py/mpstate.h"
|
#include "py/mpstate.h"
|
||||||
#include MICROPY_HAL_H
|
#include MICROPY_HAL_H
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
|
#include "netutils.h"
|
||||||
#include "modnetwork.h"
|
#include "modnetwork.h"
|
||||||
#include "mpexception.h"
|
#include "mpexception.h"
|
||||||
|
|
||||||
@ -99,7 +100,7 @@ STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
|
|||||||
|
|
||||||
// get address
|
// get address
|
||||||
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
||||||
mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip);
|
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE);
|
||||||
|
|
||||||
// call the NIC to bind the socket
|
// call the NIC to bind the socket
|
||||||
int _errno;
|
int _errno;
|
||||||
@ -155,7 +156,7 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
|
|||||||
// make the return value
|
// make the return value
|
||||||
mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
|
mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
|
||||||
client->items[0] = socket2;
|
client->items[0] = socket2;
|
||||||
client->items[1] = mod_network_format_inet_addr(ip, port);
|
client->items[1] = netutils_format_inet_addr(ip, port, NETUTILS_LITTLE);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
@ -167,7 +168,7 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
|
|||||||
|
|
||||||
// get address
|
// get address
|
||||||
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
||||||
mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip);
|
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE);
|
||||||
|
|
||||||
// call the NIC to connect the socket
|
// call the NIC to connect the socket
|
||||||
int _errno;
|
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_
|
|||||||
|
|
||||||
// get address
|
// get address
|
||||||
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
|
||||||
mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip);
|
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE);
|
||||||
|
|
||||||
// call the NIC to sendto
|
// call the NIC to sendto
|
||||||
int _errno;
|
int _errno;
|
||||||
@ -268,7 +269,7 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
|
|||||||
vstr.buf[vstr.len] = '\0';
|
vstr.buf[vstr.len] = '\0';
|
||||||
tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||||
}
|
}
|
||||||
tuple[1] = mod_network_format_inet_addr(ip, port);
|
tuple[1] = netutils_format_inet_addr(ip, port, NETUTILS_LITTLE);
|
||||||
return mp_obj_new_tuple(2, tuple);
|
return mp_obj_new_tuple(2, tuple);
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recvfrom_obj, socket_recvfrom);
|
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) {
|
|||||||
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCK_STREAM);
|
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCK_STREAM);
|
||||||
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
|
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
|
||||||
tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
|
tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
|
||||||
tuple->items[4] = mod_network_format_inet_addr(out_ip, port);
|
tuple->items[4] = netutils_format_inet_addr(out_ip, port, NETUTILS_LITTLE);
|
||||||
return mp_obj_new_list(1, (mp_obj_t*)&tuple);
|
return mp_obj_new_list(1, (mp_obj_t*)&tuple);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
#include "py/obj.h"
|
#include "py/obj.h"
|
||||||
#include "py/objstr.h"
|
#include "py/objstr.h"
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
|
#include "netutils.h"
|
||||||
#include "modnetwork.h"
|
#include "modnetwork.h"
|
||||||
#include "modwlan.h"
|
#include "modwlan.h"
|
||||||
#include "pybioctl.h"
|
#include "pybioctl.h"
|
||||||
@ -836,10 +837,10 @@ STATIC mp_obj_t wlan_ifconfig (mp_obj_t self_in) {
|
|||||||
ifconfig[1] = mp_obj_new_str((const char *)wlan_obj.ssid, strlen((const char *)wlan_obj.ssid), false);
|
ifconfig[1] = mp_obj_new_str((const char *)wlan_obj.ssid, strlen((const char *)wlan_obj.ssid), false);
|
||||||
ifconfig[2] = mp_obj_new_bytes((const byte *)wlan_obj.bssid, SL_BSSID_LENGTH);
|
ifconfig[2] = mp_obj_new_bytes((const byte *)wlan_obj.bssid, SL_BSSID_LENGTH);
|
||||||
ifconfig[3] = mp_obj_new_bytes((const byte *)wlan_obj.mac, SL_BSSID_LENGTH);
|
ifconfig[3] = mp_obj_new_bytes((const byte *)wlan_obj.mac, SL_BSSID_LENGTH);
|
||||||
ifconfig[4] = mod_network_format_ipv4_addr((uint8_t *)&wlan_obj.ip);
|
ifconfig[4] = netutils_format_ipv4_addr((uint8_t *)&wlan_obj.ip, NETUTILS_LITTLE);
|
||||||
ifconfig[5] = mod_network_format_ipv4_addr((uint8_t *)&ipV4.ipV4Mask);
|
ifconfig[5] = netutils_format_ipv4_addr((uint8_t *)&ipV4.ipV4Mask, NETUTILS_LITTLE);
|
||||||
ifconfig[6] = mod_network_format_ipv4_addr((uint8_t *)&wlan_obj.gateway);
|
ifconfig[6] = netutils_format_ipv4_addr((uint8_t *)&wlan_obj.gateway, NETUTILS_LITTLE);
|
||||||
ifconfig[7] = mod_network_format_ipv4_addr((uint8_t *)&wlan_obj.dns);
|
ifconfig[7] = netutils_format_ipv4_addr((uint8_t *)&wlan_obj.dns, NETUTILS_LITTLE);
|
||||||
|
|
||||||
return mp_obj_new_attrtuple(wlan_ifconfig_fields, 8, ifconfig);
|
return mp_obj_new_attrtuple(wlan_ifconfig_fields, 8, ifconfig);
|
||||||
}
|
}
|
||||||
@ -992,10 +993,10 @@ STATIC mp_obj_t wlan_config_ip (mp_uint_t n_args, const mp_obj_t *pos_args, mp_m
|
|||||||
}
|
}
|
||||||
|
|
||||||
SlNetCfgIpV4Args_t ipV4;
|
SlNetCfgIpV4Args_t ipV4;
|
||||||
mod_network_parse_ipv4_addr(args[1].u_obj, (uint8_t *)&ipV4.ipV4);
|
netutils_parse_ipv4_addr(args[1].u_obj, (uint8_t *)&ipV4.ipV4, NETUTILS_LITTLE);
|
||||||
mod_network_parse_ipv4_addr(args[2].u_obj, (uint8_t *)&ipV4.ipV4Mask);
|
netutils_parse_ipv4_addr(args[2].u_obj, (uint8_t *)&ipV4.ipV4Mask, NETUTILS_LITTLE);
|
||||||
mod_network_parse_ipv4_addr(args[3].u_obj, (uint8_t *)&ipV4.ipV4Gateway);
|
netutils_parse_ipv4_addr(args[3].u_obj, (uint8_t *)&ipV4.ipV4Gateway, NETUTILS_LITTLE);
|
||||||
mod_network_parse_ipv4_addr(args[4].u_obj, (uint8_t *)&ipV4.ipV4DnsServer);
|
netutils_parse_ipv4_addr(args[4].u_obj, (uint8_t *)&ipV4.ipV4DnsServer, NETUTILS_LITTLE);
|
||||||
|
|
||||||
wlan_servers_stop();
|
wlan_servers_stop();
|
||||||
|
|
||||||
|
95
lib/netutils/netutils.c
Normal file
95
lib/netutils/netutils.c
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Micro Python project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013, 2014 Damien P. George
|
||||||
|
* Copyright (c) 2015 Daniel Campora
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "py/obj.h"
|
||||||
|
#include "py/nlr.h"
|
||||||
|
#include "netutils.h"
|
||||||
|
|
||||||
|
// Takes an array with a raw IPv4 address and returns something like '192.168.0.1'.
|
||||||
|
mp_obj_t netutils_format_ipv4_addr(uint8_t *ip, netutils_endian_t endian) {
|
||||||
|
char ip_str[16];
|
||||||
|
mp_uint_t ip_len;
|
||||||
|
if (endian == NETUTILS_LITTLE) {
|
||||||
|
ip_len = snprintf(ip_str, 16, "%u.%u.%u.%u", ip[3], ip[2], ip[1], ip[0]);
|
||||||
|
} else {
|
||||||
|
ip_len = snprintf(ip_str, 16, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
|
||||||
|
}
|
||||||
|
return mp_obj_new_str(ip_str, ip_len, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Takes an array with a raw IP address, and a port, and returns a net-address
|
||||||
|
// tuple such as ('192.168.0.1', 8080).
|
||||||
|
mp_obj_t netutils_format_inet_addr(uint8_t *ip, mp_uint_t port, netutils_endian_t endian) {
|
||||||
|
mp_obj_t tuple[2] = {
|
||||||
|
tuple[0] = netutils_format_ipv4_addr(ip, endian),
|
||||||
|
tuple[1] = mp_obj_new_int(port),
|
||||||
|
};
|
||||||
|
return mp_obj_new_tuple(2, tuple);
|
||||||
|
}
|
||||||
|
|
||||||
|
void netutils_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip, netutils_endian_t endian) {
|
||||||
|
mp_uint_t addr_len;
|
||||||
|
const char *addr_str = mp_obj_str_get_data(addr_in, &addr_len);
|
||||||
|
if (addr_len == 0) {
|
||||||
|
// special case of no address given
|
||||||
|
memset(out_ip, 0, NETUTILS_IPV4ADDR_BUFSIZE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const char *s = addr_str;
|
||||||
|
const char *s_top = addr_str + addr_len;
|
||||||
|
for (mp_uint_t i = 3 ; ; i--) {
|
||||||
|
mp_uint_t val = 0;
|
||||||
|
for (; s < s_top && *s != '.'; s++) {
|
||||||
|
val = val * 10 + *s - '0';
|
||||||
|
}
|
||||||
|
if (endian == NETUTILS_LITTLE) {
|
||||||
|
out_ip[i] = val;
|
||||||
|
} else {
|
||||||
|
out_ip[NETUTILS_IPV4ADDR_BUFSIZE - 1 - i] = val;
|
||||||
|
}
|
||||||
|
if (i == 0 && s == s_top) {
|
||||||
|
return;
|
||||||
|
} else if (i > 0 && s < s_top && *s == '.') {
|
||||||
|
s++;
|
||||||
|
} else {
|
||||||
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid arguments"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Takes an address of the form ('192.168.0.1', 8080), returns the port and
|
||||||
|
// puts IP in out_ip (which must take at least IPADDR_BUF_SIZE bytes).
|
||||||
|
mp_uint_t netutils_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip, netutils_endian_t endian) {
|
||||||
|
mp_obj_t *addr_items;
|
||||||
|
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
|
||||||
|
netutils_parse_ipv4_addr(addr_items[0], out_ip, endian);
|
||||||
|
return mp_obj_get_int(addr_items[1]);
|
||||||
|
}
|
50
lib/netutils/netutils.h
Normal file
50
lib/netutils/netutils.h
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Micro Python project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013, 2014 Damien P. George
|
||||||
|
* Copyright (c) 2015 Daniel Campora
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef __MICROPY_INCLUDED_LIB_NETUTILS_H__
|
||||||
|
#define __MICROPY_INCLUDED_LIB_NETUTILS_H__
|
||||||
|
|
||||||
|
#define NETUTILS_IPV4ADDR_BUFSIZE 4
|
||||||
|
|
||||||
|
typedef enum _netutils_endian_t {
|
||||||
|
NETUTILS_LITTLE,
|
||||||
|
NETUTILS_BIG,
|
||||||
|
} netutils_endian_t;
|
||||||
|
|
||||||
|
// Takes an array with a raw IPv4 address and returns something like '192.168.0.1'.
|
||||||
|
mp_obj_t netutils_format_ipv4_addr(uint8_t *ip, netutils_endian_t endian);
|
||||||
|
|
||||||
|
// Takes an array with a raw IP address, and a port, and returns a net-address
|
||||||
|
// tuple such as ('192.168.0.1', 8080).
|
||||||
|
mp_obj_t netutils_format_inet_addr(uint8_t *ip, mp_uint_t port, netutils_endian_t endian);
|
||||||
|
|
||||||
|
void netutils_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip, netutils_endian_t endian);
|
||||||
|
|
||||||
|
// Takes an address of the form ('192.168.0.1', 8080), returns the port and
|
||||||
|
// puts IP in out_ip (which must take at least IPADDR_BUF_SIZE bytes).
|
||||||
|
mp_uint_t netutils_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip, netutils_endian_t endian);
|
||||||
|
|
||||||
|
#endif // __MICROPY_INCLUDED_LIB_NETUTILS_H__
|
@ -41,6 +41,7 @@ INC += -I$(HAL_DIR)/inc
|
|||||||
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/inc
|
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/inc
|
||||||
#INC += -I$(USBHOST_DIR)
|
#INC += -I$(USBHOST_DIR)
|
||||||
INC += -I../lib/mp-readline
|
INC += -I../lib/mp-readline
|
||||||
|
INC += -I../lib/netutils
|
||||||
|
|
||||||
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
|
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
|
||||||
CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_CORTEX_M4) $(COPT)
|
CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_MOD) $(CFLAGS_CORTEX_M4) $(COPT)
|
||||||
@ -92,6 +93,7 @@ SRC_LIB = $(addprefix lib/,\
|
|||||||
fatfs/ff.c \
|
fatfs/ff.c \
|
||||||
fatfs/option/ccsbcs.c \
|
fatfs/option/ccsbcs.c \
|
||||||
mp-readline/readline.c \
|
mp-readline/readline.c \
|
||||||
|
netutils/netutils.c \
|
||||||
)
|
)
|
||||||
|
|
||||||
SRC_C = \
|
SRC_C = \
|
||||||
|
@ -90,65 +90,3 @@ const mp_obj_module_t mp_module_network = {
|
|||||||
.name = MP_QSTR_network,
|
.name = MP_QSTR_network,
|
||||||
.globals = (mp_obj_dict_t*)&mp_module_network_globals,
|
.globals = (mp_obj_dict_t*)&mp_module_network_globals,
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
// Miscellaneous helpers
|
|
||||||
|
|
||||||
void mod_network_convert_ipv4_endianness(uint8_t *ip) {
|
|
||||||
uint8_t ip0 = ip[0]; ip[0] = ip[3]; ip[3] = ip0;
|
|
||||||
uint8_t ip1 = ip[1]; ip[1] = ip[2]; ip[2] = ip1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Takes an address of the form '192.168.0.1' and converts it to network format
|
|
||||||
// in out_ip (big endian, so the 192 is the first byte).
|
|
||||||
void mod_network_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip) {
|
|
||||||
mp_uint_t addr_len;
|
|
||||||
const char *addr_str = mp_obj_str_get_data(addr_in, &addr_len);
|
|
||||||
if (addr_len == 0) {
|
|
||||||
// special case of no address given
|
|
||||||
memset(out_ip, 0, MOD_NETWORK_IPADDR_BUF_SIZE);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const char *s = addr_str;
|
|
||||||
const char *s_top = addr_str + addr_len;
|
|
||||||
for (mp_uint_t i = 0;; i++) {
|
|
||||||
mp_uint_t val = 0;
|
|
||||||
for (; s < s_top && *s != '.'; s++) {
|
|
||||||
val = val * 10 + *s - '0';
|
|
||||||
}
|
|
||||||
out_ip[i] = val;
|
|
||||||
if (i == 3 && s == s_top) {
|
|
||||||
return;
|
|
||||||
} else if (i < 3 && s < s_top && *s == '.') {
|
|
||||||
s++;
|
|
||||||
} else {
|
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid IP address"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Takes an address of the form ('192.168.0.1', 8080), returns the port and
|
|
||||||
// puts IP in out_ip (which must take at least IPADDR_BUF_SIZE bytes).
|
|
||||||
mp_uint_t mod_network_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip) {
|
|
||||||
mp_obj_t *addr_items;
|
|
||||||
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
|
|
||||||
mod_network_parse_ipv4_addr(addr_items[0], out_ip);
|
|
||||||
return mp_obj_get_int(addr_items[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Takes an array with a raw IPv4 address and returns something like '192.168.0.1'.
|
|
||||||
mp_obj_t mod_network_format_ipv4_addr(uint8_t *ip) {
|
|
||||||
char ip_str[16];
|
|
||||||
mp_uint_t ip_len = snprintf(ip_str, 16, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
|
|
||||||
return mp_obj_new_str(ip_str, ip_len, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Takes an array with a raw IP address, and a port, and returns a net-address
|
|
||||||
// tuple such as ('192.168.0.1', 8080).
|
|
||||||
mp_obj_t mod_network_format_inet_addr(uint8_t *ip, mp_uint_t port) {
|
|
||||||
mp_obj_t tuple[2] = {
|
|
||||||
tuple[0] = mod_network_format_ipv4_addr(ip),
|
|
||||||
tuple[1] = mp_obj_new_int(port),
|
|
||||||
};
|
|
||||||
return mp_obj_new_tuple(2, tuple);
|
|
||||||
}
|
|
||||||
|
@ -77,9 +77,3 @@ extern const mod_network_nic_type_t mod_network_nic_type_cc3k;
|
|||||||
void mod_network_init(void);
|
void mod_network_init(void);
|
||||||
void mod_network_register_nic(mp_obj_t nic);
|
void mod_network_register_nic(mp_obj_t nic);
|
||||||
mp_obj_t mod_network_find_nic(const uint8_t *ip);
|
mp_obj_t mod_network_find_nic(const uint8_t *ip);
|
||||||
|
|
||||||
void mod_network_convert_ipv4_endianness(uint8_t *ip);
|
|
||||||
void mod_network_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip);
|
|
||||||
mp_uint_t mod_network_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip);
|
|
||||||
mp_obj_t mod_network_format_ipv4_addr(uint8_t *ip);
|
|
||||||
mp_obj_t mod_network_format_inet_addr(uint8_t *ip, mp_uint_t port);
|
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
#include "py/objlist.h"
|
#include "py/objlist.h"
|
||||||
#include "py/stream.h"
|
#include "py/stream.h"
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
|
#include "netutils.h"
|
||||||
#include "modnetwork.h"
|
#include "modnetwork.h"
|
||||||
#include "pin.h"
|
#include "pin.h"
|
||||||
#include "genhdr/pins.h"
|
#include "genhdr/pins.h"
|
||||||
@ -519,13 +520,6 @@ STATIC mp_obj_t cc3k_ifconfig(mp_obj_t self_in) {
|
|||||||
tNetappIpconfigRetArgs ipconfig;
|
tNetappIpconfigRetArgs ipconfig;
|
||||||
netapp_ipconfig(&ipconfig);
|
netapp_ipconfig(&ipconfig);
|
||||||
|
|
||||||
// CC3000 returns little endian, but we want big endian
|
|
||||||
mod_network_convert_ipv4_endianness(ipconfig.aucIP);
|
|
||||||
mod_network_convert_ipv4_endianness(ipconfig.aucSubnetMask);
|
|
||||||
mod_network_convert_ipv4_endianness(ipconfig.aucDefaultGateway);
|
|
||||||
mod_network_convert_ipv4_endianness(ipconfig.aucDNSServer);
|
|
||||||
mod_network_convert_ipv4_endianness(ipconfig.aucDHCPServer);
|
|
||||||
|
|
||||||
// render MAC address
|
// render MAC address
|
||||||
VSTR_FIXED(mac_vstr, 18);
|
VSTR_FIXED(mac_vstr, 18);
|
||||||
const uint8_t *mac = ipconfig.uaMacAddr;
|
const uint8_t *mac = ipconfig.uaMacAddr;
|
||||||
@ -533,11 +527,11 @@ STATIC mp_obj_t cc3k_ifconfig(mp_obj_t self_in) {
|
|||||||
|
|
||||||
// create and return tuple with ifconfig info
|
// create and return tuple with ifconfig info
|
||||||
mp_obj_t tuple[7] = {
|
mp_obj_t tuple[7] = {
|
||||||
mod_network_format_ipv4_addr(ipconfig.aucIP),
|
netutils_format_ipv4_addr(ipconfig.aucIP, NETUTILS_LITTLE),
|
||||||
mod_network_format_ipv4_addr(ipconfig.aucSubnetMask),
|
netutils_format_ipv4_addr(ipconfig.aucSubnetMask, NETUTILS_LITTLE),
|
||||||
mod_network_format_ipv4_addr(ipconfig.aucDefaultGateway),
|
netutils_format_ipv4_addr(ipconfig.aucDefaultGateway, NETUTILS_LITTLE),
|
||||||
mod_network_format_ipv4_addr(ipconfig.aucDNSServer),
|
netutils_format_ipv4_addr(ipconfig.aucDNSServer, NETUTILS_LITTLE),
|
||||||
mod_network_format_ipv4_addr(ipconfig.aucDHCPServer),
|
netutils_format_ipv4_addr(ipconfig.aucDHCPServer, NETUTILS_LITTLE),
|
||||||
mp_obj_new_str(mac_vstr.buf, mac_vstr.len, false),
|
mp_obj_new_str(mac_vstr.buf, mac_vstr.len, false),
|
||||||
mp_obj_new_str((const char*)ipconfig.uaSSID, strlen((const char*)ipconfig.uaSSID), false),
|
mp_obj_new_str((const char*)ipconfig.uaSSID, strlen((const char*)ipconfig.uaSSID), false),
|
||||||
};
|
};
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
#include "py/nlr.h"
|
#include "py/nlr.h"
|
||||||
#include "py/objlist.h"
|
#include "py/objlist.h"
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
|
#include "netutils.h"
|
||||||
#include "modnetwork.h"
|
#include "modnetwork.h"
|
||||||
#include "pin.h"
|
#include "pin.h"
|
||||||
#include "genhdr/pins.h"
|
#include "genhdr/pins.h"
|
||||||
@ -422,20 +423,20 @@ STATIC mp_obj_t wiznet5k_ifconfig(mp_uint_t n_args, const mp_obj_t *args) {
|
|||||||
if (n_args == 1) {
|
if (n_args == 1) {
|
||||||
// get
|
// get
|
||||||
mp_obj_t tuple[4] = {
|
mp_obj_t tuple[4] = {
|
||||||
mod_network_format_ipv4_addr(netinfo.ip),
|
netutils_format_ipv4_addr(netinfo.ip, NETUTILS_BIG),
|
||||||
mod_network_format_ipv4_addr(netinfo.sn),
|
netutils_format_ipv4_addr(netinfo.sn, NETUTILS_BIG),
|
||||||
mod_network_format_ipv4_addr(netinfo.gw),
|
netutils_format_ipv4_addr(netinfo.gw, NETUTILS_BIG),
|
||||||
mod_network_format_ipv4_addr(netinfo.dns),
|
netutils_format_ipv4_addr(netinfo.dns, NETUTILS_BIG),
|
||||||
};
|
};
|
||||||
return mp_obj_new_tuple(4, tuple);
|
return mp_obj_new_tuple(4, tuple);
|
||||||
} else {
|
} else {
|
||||||
// set
|
// set
|
||||||
mp_obj_t *items;
|
mp_obj_t *items;
|
||||||
mp_obj_get_array_fixed_n(args[1], 4, &items);
|
mp_obj_get_array_fixed_n(args[1], 4, &items);
|
||||||
mod_network_parse_ipv4_addr(items[0], netinfo.ip);
|
netutils_parse_ipv4_addr(items[0], netinfo.ip, NETUTILS_BIG);
|
||||||
mod_network_parse_ipv4_addr(items[1], netinfo.sn);
|
netutils_parse_ipv4_addr(items[1], netinfo.sn, NETUTILS_BIG);
|
||||||
mod_network_parse_ipv4_addr(items[2], netinfo.gw);
|
netutils_parse_ipv4_addr(items[2], netinfo.gw, NETUTILS_BIG);
|
||||||
mod_network_parse_ipv4_addr(items[3], netinfo.dns);
|
netutils_parse_ipv4_addr(items[3], netinfo.dns, NETUTILS_BIG);
|
||||||
ctlnetwork(CN_SET_NETINFO, &netinfo);
|
ctlnetwork(CN_SET_NETINFO, &netinfo);
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
#include "py/objtuple.h"
|
#include "py/objtuple.h"
|
||||||
#include "py/objlist.h"
|
#include "py/objlist.h"
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
|
#include "netutils.h"
|
||||||
#include "modnetwork.h"
|
#include "modnetwork.h"
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
@ -93,7 +94,7 @@ STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
|
|||||||
|
|
||||||
// get address
|
// get address
|
||||||
uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
|
uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
|
||||||
mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip);
|
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
|
||||||
|
|
||||||
// check if we need to select a NIC
|
// check if we need to select a NIC
|
||||||
socket_select_nic(self, ip);
|
socket_select_nic(self, ip);
|
||||||
@ -153,7 +154,7 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
|
|||||||
// make the return value
|
// make the return value
|
||||||
mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
|
mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
|
||||||
client->items[0] = socket2;
|
client->items[0] = socket2;
|
||||||
client->items[1] = mod_network_format_inet_addr(ip, port);
|
client->items[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
@ -165,7 +166,7 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
|
|||||||
|
|
||||||
// get address
|
// get address
|
||||||
uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
|
uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
|
||||||
mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip);
|
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
|
||||||
|
|
||||||
// check if we need to select a NIC
|
// check if we need to select a NIC
|
||||||
socket_select_nic(self, ip);
|
socket_select_nic(self, ip);
|
||||||
@ -231,7 +232,7 @@ STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_
|
|||||||
|
|
||||||
// get address
|
// get address
|
||||||
uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
|
uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
|
||||||
mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip);
|
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
|
||||||
|
|
||||||
// check if we need to select a NIC
|
// check if we need to select a NIC
|
||||||
socket_select_nic(self, ip);
|
socket_select_nic(self, ip);
|
||||||
@ -270,7 +271,7 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
|
|||||||
vstr.len = ret;
|
vstr.len = ret;
|
||||||
tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||||
}
|
}
|
||||||
tuple[1] = mod_network_format_inet_addr(ip, port);
|
tuple[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
|
||||||
return mp_obj_new_tuple(2, tuple);
|
return mp_obj_new_tuple(2, tuple);
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recvfrom_obj, socket_recvfrom);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recvfrom_obj, socket_recvfrom);
|
||||||
@ -404,7 +405,7 @@ STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) {
|
|||||||
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_SOCK_STREAM);
|
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_SOCK_STREAM);
|
||||||
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
|
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
|
||||||
tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
|
tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
|
||||||
tuple->items[4] = mod_network_format_inet_addr(out_ip, port);
|
tuple->items[4] = netutils_format_inet_addr(out_ip, port, NETUTILS_BIG);
|
||||||
return mp_obj_new_list(1, (mp_obj_t*)&tuple);
|
return mp_obj_new_list(1, (mp_obj_t*)&tuple);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user