From 979d6a97a53b5c9738cfb7489ca05a3874042c8d Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Sun, 4 Sep 2022 18:26:23 +0200 Subject: wifi/cell driver improved --- fw/fe310/eos/net/wifi.c | 92 ++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) (limited to 'fw/fe310/eos/net/wifi.c') diff --git a/fw/fe310/eos/net/wifi.c b/fw/fe310/eos/net/wifi.c index 4db49f8..113321b 100644 --- a/fw/fe310/eos/net/wifi.c +++ b/fw/fe310/eos/net/wifi.c @@ -8,9 +8,6 @@ #include "wifi.h" -#define WIFI_SIZE_SSID 33 -#define WIFI_SIZE_PWD 64 - static eos_evt_handler_t evt_handler[EOS_WIFI_MAX_MTYPE]; static void wifi_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) { @@ -47,8 +44,33 @@ eos_evt_handler_t eos_wifi_get_handler(unsigned char mtype) { return NULL; } -int eos_wifi_status(unsigned char *buffer, uint8_t *status, uint8_t ip_addr[], char *ssid) { - unsigned char *buf; +int eos_wifi_status_parse(unsigned char *buffer, uint16_t len, uint8_t *status, uint8_t ip_addr[], char *ssid) { + if (len < 2) return EOS_ERR_SIZE; + + if (buffer[0] != EOS_WIFI_MTYPE_STATUS) return EOS_ERR_NET; + buffer++; + len -= 1; + + *status = buffer[0]; + buffer++; + len -= 1; + + switch (*status) { + case EOS_WIFI_STATUS_GOT_IP: + if (len < sizeof(uint32_t)) return EOS_ERR_SIZE; + if (ip_addr) memcpy(ip_addr, buffer, sizeof(uint32_t)); + buffer += sizeof(uint32_t); + len -= sizeof(uint32_t); + case EOS_WIFI_STATUS_CONNECTED: + if ((len == 0) || (len > EOS_WIFI_SIZE_SSID)) return EOS_ERR_SIZE; + buffer[len - 1] = '\0'; + if (ssid) strcpy(ssid, buffer); + break; + } + return EOS_OK; +} + +int eos_wifi_status(uint8_t *status, uint8_t ip_addr[], char *ssid, unsigned char *buffer) { unsigned char type; uint16_t len; int do_release; @@ -71,89 +93,67 @@ int eos_wifi_status(unsigned char *buffer, uint8_t *status, uint8_t ip_addr[], c rv = EOS_ERR_NET; goto wifi_status_fin; } - if (len < 2) { - rv = EOS_ERR_SIZE; - goto wifi_status_fin; - } - - *status = buffer[1]; - buf = buffer + 2; - len -= 2; - - switch (*status) { - case EOS_WIFI_STATUS_GOT_IP: - if (len < sizeof(uint32_t)) { - rv = EOS_ERR_SIZE; - goto wifi_status_fin; - } - if (ip_addr) memcpy(ip_addr, buf, sizeof(uint32_t)); - buf += sizeof(uint32_t); - len -= sizeof(uint32_t); - case EOS_WIFI_STATUS_CONNECTED: - if ((len == 0) || (len > WIFI_SIZE_SSID)) { - rv = EOS_ERR_SIZE; - goto wifi_status_fin; - } - buf[len - 1] = '\0'; - if (ssid) strcpy(ssid, buf); - break; - } + rv = eos_wifi_status_parse(buffer, len, status, ip_addr, ssid); wifi_status_fin: if (do_release) eos_net_free(buffer, 1); return rv; } -int eos_wifi_start(unsigned char *buffer) { +int eos_wifi_start(unsigned char *buffer, int sync) { int async; - async = 0; if (buffer == NULL) { buffer = eos_net_alloc(); async = 1; + } else { + async = !sync; } buffer[0] = EOS_WIFI_MTYPE_START; return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); } -int eos_wifi_stop(unsigned char *buffer) { +int eos_wifi_stop(unsigned char *buffer, int sync) { int async; - async = 0; if (buffer == NULL) { buffer = eos_net_alloc(); async = 1; + } else { + async = !sync; } buffer[0] = EOS_WIFI_MTYPE_STOP; return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); } -int eos_wifi_scan(unsigned char *buffer) { +int eos_wifi_scan(unsigned char *buffer, int sync) { int async; - async = 0; if (buffer == NULL) { buffer = eos_net_alloc(); async = 1; + } else { + async = !sync; } buffer[0] = EOS_WIFI_MTYPE_SCAN; return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); } -int eos_wifi_connect(const char *ssid, const char *pwd, unsigned char *buffer) { +int eos_wifi_connect(char *ssid, char *pwd, unsigned char *buffer, int sync) { unsigned char *buf; size_t ssid_len, pwd_len; int async; ssid_len = strlen(ssid); pwd_len = strlen(pwd); - if (ssid_len > WIFI_SIZE_SSID - 1) return EOS_ERR_SIZE; - if (pwd_len > WIFI_SIZE_PWD - 1) return EOS_ERR_SIZE; + if (ssid_len > EOS_WIFI_SIZE_SSID - 1) return EOS_ERR_SIZE; + if (pwd_len > EOS_WIFI_SIZE_PWD - 1) return EOS_ERR_SIZE; - async = 0; if (buffer == NULL) { buffer = eos_net_alloc(); async = 1; + } else { + async = !sync; } buf = buffer; buf[0] = EOS_WIFI_MTYPE_CONNECT; @@ -162,17 +162,17 @@ int eos_wifi_connect(const char *ssid, const char *pwd, unsigned char *buffer) { buf += ssid_len + 1; strcpy(buf, pwd); buf += pwd_len + 1; - - return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); + return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 3 + ssid_len + pwd_len, async, 1); } -int eos_wifi_disconnect(unsigned char *buffer) { +int eos_wifi_disconnect(unsigned char *buffer, int sync) { int async; - async = 0; if (buffer == NULL) { buffer = eos_net_alloc(); async = 1; + } else { + async = !sync; } buffer[0] = EOS_WIFI_MTYPE_DISCONNECT; return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); -- cgit v1.2.3