Skip to content

Commit 5bbc3cd

Browse files
committed
stm: Tidy up some wlan code.
1 parent 6f95432 commit 5bbc3cd

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

stm/pybwlan.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,41 @@ mp_obj_t pyb_wlan_disconnect(void) {
4949
return mp_obj_new_int(ret);
5050
}
5151

52-
mp_obj_t decode_addr(unsigned char *ip, int nBytes) {
52+
mp_obj_t decode_addr(unsigned char *ip, int n_bytes) {
5353
char data[64] = "";
54-
if (nBytes == 4) {
54+
if (n_bytes == 4) {
5555
snprintf(data, 64, "%u.%u.%u.%u", ip[3], ip[2], ip[1], ip[0]);
56-
} else if (nBytes == 6) {
56+
} else if (n_bytes == 6) {
5757
snprintf(data, 64, "%02x:%02x:%02x:%02x:%02x:%02x", ip[5], ip[4], ip[3], ip[2], ip[1], ip[0]);
58-
} else if (nBytes == 32) {
58+
} else if (n_bytes == 32) {
5959
snprintf(data, 64, "%s", ip);
6060
}
6161
return mp_obj_new_str(qstr_from_strn_copy(data, strlen(data)));
6262
}
6363

64-
void _wlan_getIP_get_address(mp_obj_t object, qstr q_attr, unsigned char *ip, int nBytes) {
65-
rt_store_attr(object, q_attr, decode_addr(ip, nBytes));
64+
void decode_addr_and_store(mp_obj_t object, qstr q_attr, unsigned char *ip, int n_bytes) {
65+
rt_store_attr(object, q_attr, decode_addr(ip, n_bytes));
6666
}
6767

6868
mp_obj_t pyb_wlan_get_ip(void) {
69-
tNetappIpconfigRetArgs ipconfig;
70-
netapp_ipconfig(&ipconfig);
71-
72-
/* If byte 1 is 0 we don't have a valid address */
73-
if (ipconfig.aucIP[3] == 0) return mp_const_none;
74-
75-
mp_obj_t data = mp_module_new(); // TODO should really be a class
76-
_wlan_getIP_get_address(data, qstr_from_str_static("ip"), &ipconfig.aucIP[0], 4);
77-
_wlan_getIP_get_address(data, qstr_from_str_static("subnet"), &ipconfig.aucSubnetMask[0], 4);
78-
_wlan_getIP_get_address(data, qstr_from_str_static("gateway"), &ipconfig.aucDefaultGateway[0], 4);
79-
_wlan_getIP_get_address(data, qstr_from_str_static("dhcp"), &ipconfig.aucDHCPServer[0], 4);
80-
_wlan_getIP_get_address(data, qstr_from_str_static("dns"), &ipconfig.aucDNSServer[0], 4);
81-
_wlan_getIP_get_address(data, qstr_from_str_static("mac"), &ipconfig.uaMacAddr[0], 6);
82-
_wlan_getIP_get_address(data, qstr_from_str_static("ssid"), &ipconfig.uaSSID[0], 32);
83-
84-
return data;
69+
tNetappIpconfigRetArgs ipconfig;
70+
netapp_ipconfig(&ipconfig);
71+
72+
// If byte 1 is 0 we don't have a valid address
73+
if (ipconfig.aucIP[3] == 0) {
74+
return mp_const_none;
75+
}
76+
77+
mp_obj_t data = mp_module_new(); // TODO should really be a class
78+
decode_addr_and_store(data, qstr_from_str_static("ip"), &ipconfig.aucIP[0], 4);
79+
decode_addr_and_store(data, qstr_from_str_static("subnet"), &ipconfig.aucSubnetMask[0], 4);
80+
decode_addr_and_store(data, qstr_from_str_static("gateway"), &ipconfig.aucDefaultGateway[0], 4);
81+
decode_addr_and_store(data, qstr_from_str_static("dhcp"), &ipconfig.aucDHCPServer[0], 4);
82+
decode_addr_and_store(data, qstr_from_str_static("dns"), &ipconfig.aucDNSServer[0], 4);
83+
decode_addr_and_store(data, qstr_from_str_static("mac"), &ipconfig.uaMacAddr[0], 6);
84+
decode_addr_and_store(data, qstr_from_str_static("ssid"), &ipconfig.uaSSID[0], 32);
85+
86+
return data;
8587
}
8688

8789
uint32_t last_ip = 0; // XXX such a hack!

0 commit comments

Comments
 (0)