diff options
author | Daniel Campora <daniel@wipy.io> | 2015-04-12 23:55:34 +0200 |
---|---|---|
committer | Daniel Campora <daniel@wipy.io> | 2015-04-13 00:02:56 +0200 |
commit | c69b4310c85b7f09789f75b49e706fea58e8aa7a (patch) | |
tree | 7680324c6d6d440b369778c132e5ece1108e1c8b /cc3200/mods/modnetwork.c | |
parent | b21786947fabbdef76824d899263bec51779738b (diff) | |
download | micropython-c69b4310c85b7f09789f75b49e706fea58e8aa7a.tar.gz micropython-c69b4310c85b7f09789f75b49e706fea58e8aa7a.zip |
cc3200: Add WLAN.config_ip().
This new method allows to assign an static IP to the device.
Diffstat (limited to 'cc3200/mods/modnetwork.c')
-rw-r--r-- | cc3200/mods/modnetwork.c | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/cc3200/mods/modnetwork.c b/cc3200/mods/modnetwork.c index 94e098dbcc..fa45e57157 100644 --- a/cc3200/mods/modnetwork.c +++ b/cc3200/mods/modnetwork.c @@ -120,13 +120,8 @@ const mp_obj_module_t mp_module_network = { /******************************************************************************/ // 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). +// 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); @@ -137,15 +132,15 @@ void mod_network_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip) { } const char *s = addr_str; const char *s_top = addr_str + addr_len; - for (mp_uint_t i = 0;; i++) { + 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 == 3 && s == s_top) { + if (i == 0 && s == s_top) { return; - } else if (i < 3 && s < s_top && *s == '.') { + } else if (i > 0 && s < s_top && *s == '.') { s++; } else { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); @@ -165,7 +160,7 @@ mp_uint_t mod_network_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip) { // 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]); + 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); } |