diff options
author | Damien <damien.p.george@gmail.com> | 2013-12-21 18:53:45 +0000 |
---|---|---|
committer | Damien <damien.p.george@gmail.com> | 2013-12-21 18:53:45 +0000 |
commit | 5bbc3cdc0e081114e9fea0c71766e98a8ec8c8b7 (patch) | |
tree | 55076f9ad5e6d615a6fcf0969c9f4100359ae024 | |
parent | 6f95432407d0d7d54b1f50ef578e7273dc1b9787 (diff) | |
download | micropython-5bbc3cdc0e081114e9fea0c71766e98a8ec8c8b7.tar.gz micropython-5bbc3cdc0e081114e9fea0c71766e98a8ec8c8b7.zip |
stm: Tidy up some wlan code.
-rw-r--r-- | stm/pybwlan.c | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/stm/pybwlan.c b/stm/pybwlan.c index 863c241b60..c078ecfe14 100644 --- a/stm/pybwlan.c +++ b/stm/pybwlan.c @@ -49,39 +49,41 @@ mp_obj_t pyb_wlan_disconnect(void) { return mp_obj_new_int(ret); } -mp_obj_t decode_addr(unsigned char *ip, int nBytes) { +mp_obj_t decode_addr(unsigned char *ip, int n_bytes) { char data[64] = ""; - if (nBytes == 4) { + if (n_bytes == 4) { snprintf(data, 64, "%u.%u.%u.%u", ip[3], ip[2], ip[1], ip[0]); - } else if (nBytes == 6) { + } else if (n_bytes == 6) { snprintf(data, 64, "%02x:%02x:%02x:%02x:%02x:%02x", ip[5], ip[4], ip[3], ip[2], ip[1], ip[0]); - } else if (nBytes == 32) { + } else if (n_bytes == 32) { snprintf(data, 64, "%s", ip); } return mp_obj_new_str(qstr_from_strn_copy(data, strlen(data))); } -void _wlan_getIP_get_address(mp_obj_t object, qstr q_attr, unsigned char *ip, int nBytes) { - rt_store_attr(object, q_attr, decode_addr(ip, nBytes)); +void decode_addr_and_store(mp_obj_t object, qstr q_attr, unsigned char *ip, int n_bytes) { + rt_store_attr(object, q_attr, decode_addr(ip, n_bytes)); } mp_obj_t pyb_wlan_get_ip(void) { - tNetappIpconfigRetArgs ipconfig; - netapp_ipconfig(&ipconfig); - - /* If byte 1 is 0 we don't have a valid address */ - if (ipconfig.aucIP[3] == 0) return mp_const_none; - - mp_obj_t data = mp_module_new(); // TODO should really be a class - _wlan_getIP_get_address(data, qstr_from_str_static("ip"), &ipconfig.aucIP[0], 4); - _wlan_getIP_get_address(data, qstr_from_str_static("subnet"), &ipconfig.aucSubnetMask[0], 4); - _wlan_getIP_get_address(data, qstr_from_str_static("gateway"), &ipconfig.aucDefaultGateway[0], 4); - _wlan_getIP_get_address(data, qstr_from_str_static("dhcp"), &ipconfig.aucDHCPServer[0], 4); - _wlan_getIP_get_address(data, qstr_from_str_static("dns"), &ipconfig.aucDNSServer[0], 4); - _wlan_getIP_get_address(data, qstr_from_str_static("mac"), &ipconfig.uaMacAddr[0], 6); - _wlan_getIP_get_address(data, qstr_from_str_static("ssid"), &ipconfig.uaSSID[0], 32); - - return data; + tNetappIpconfigRetArgs ipconfig; + netapp_ipconfig(&ipconfig); + + // If byte 1 is 0 we don't have a valid address + if (ipconfig.aucIP[3] == 0) { + return mp_const_none; + } + + mp_obj_t data = mp_module_new(); // TODO should really be a class + decode_addr_and_store(data, qstr_from_str_static("ip"), &ipconfig.aucIP[0], 4); + decode_addr_and_store(data, qstr_from_str_static("subnet"), &ipconfig.aucSubnetMask[0], 4); + decode_addr_and_store(data, qstr_from_str_static("gateway"), &ipconfig.aucDefaultGateway[0], 4); + decode_addr_and_store(data, qstr_from_str_static("dhcp"), &ipconfig.aucDHCPServer[0], 4); + decode_addr_and_store(data, qstr_from_str_static("dns"), &ipconfig.aucDNSServer[0], 4); + decode_addr_and_store(data, qstr_from_str_static("mac"), &ipconfig.uaMacAddr[0], 6); + decode_addr_and_store(data, qstr_from_str_static("ssid"), &ipconfig.uaSSID[0], 32); + + return data; } uint32_t last_ip = 0; // XXX such a hack! |