summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/net/wifi.c
diff options
context:
space:
mode:
Diffstat (limited to 'fw/fe310/eos/net/wifi.c')
-rw-r--r--fw/fe310/eos/net/wifi.c179
1 files changed, 179 insertions, 0 deletions
diff --git a/fw/fe310/eos/net/wifi.c b/fw/fe310/eos/net/wifi.c
new file mode 100644
index 0000000..4db49f8
--- /dev/null
+++ b/fw/fe310/eos/net/wifi.c
@@ -0,0 +1,179 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "eos.h"
+#include "event.h"
+#include "dev/net.h"
+
+#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) {
+ unsigned char mtype;
+
+ if ((buffer == NULL) || (len < 1)) {
+ eos_net_bad_handler(type, buffer, len);
+ return;
+ }
+
+ mtype = buffer[0];
+ if ((mtype < EOS_WIFI_MAX_MTYPE) && evt_handler[mtype]) {
+ evt_handler[mtype](mtype, buffer, len);
+ } else {
+ eos_net_bad_handler(type, buffer, len);
+ }
+}
+
+void eos_wifi_init(void) {
+ int i;
+
+ for (i=0; i<EOS_WIFI_MAX_MTYPE; i++) {
+ evt_handler[i] = NULL;
+ }
+ eos_net_set_handler(EOS_NET_MTYPE_WIFI, wifi_handle_msg);
+}
+
+void eos_wifi_set_handler(unsigned char mtype, eos_evt_handler_t handler) {
+ if (mtype < EOS_WIFI_MAX_MTYPE) evt_handler[mtype] = handler;
+}
+
+eos_evt_handler_t eos_wifi_get_handler(unsigned char mtype) {
+ if (mtype < EOS_WIFI_MAX_MTYPE) return evt_handler[mtype];
+ return NULL;
+}
+
+int eos_wifi_status(unsigned char *buffer, uint8_t *status, uint8_t ip_addr[], char *ssid) {
+ unsigned char *buf;
+ unsigned char type;
+ uint16_t len;
+ int do_release;
+ int rv;
+
+ do_release = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ do_release = 1;
+ }
+
+ type = EOS_NET_MTYPE_WIFI;
+ len = 1;
+ buffer[0] = EOS_WIFI_MTYPE_STATUS;
+
+ rv = eos_net_xchg(&type, buffer, &len);
+ if (rv) goto wifi_status_fin;
+
+ if (type != EOS_NET_MTYPE_WIFI) {
+ 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;
+ }
+
+wifi_status_fin:
+ if (do_release) eos_net_free(buffer, 1);
+ return rv;
+}
+
+int eos_wifi_start(unsigned char *buffer) {
+ int async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ 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 async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ 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 async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ 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) {
+ 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;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ buf = buffer;
+ buf[0] = EOS_WIFI_MTYPE_CONNECT;
+ buf++;
+ strcpy(buf, ssid);
+ buf += ssid_len + 1;
+ strcpy(buf, pwd);
+ buf += pwd_len + 1;
+
+ return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1);
+}
+
+int eos_wifi_disconnect(unsigned char *buffer) {
+ int async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ buffer[0] = EOS_WIFI_MTYPE_DISCONNECT;
+ return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1);
+}