summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/net
diff options
context:
space:
mode:
Diffstat (limited to 'fw/fe310/eos/net')
-rw-r--r--fw/fe310/eos/net/Makefile19
-rw-r--r--fw/fe310/eos/net/cell.c51
-rw-r--r--fw/fe310/eos/net/cell.h61
-rw-r--r--fw/fe310/eos/net/pwr.c69
-rw-r--r--fw/fe310/eos/net/pwr.h10
-rw-r--r--fw/fe310/eos/net/rng.c27
-rw-r--r--fw/fe310/eos/net/rng.h5
-rw-r--r--fw/fe310/eos/net/sock.c163
-rw-r--r--fw/fe310/eos/net/sock.h28
-rw-r--r--fw/fe310/eos/net/wifi.c179
-rw-r--r--fw/fe310/eos/net/wifi.h27
11 files changed, 639 insertions, 0 deletions
diff --git a/fw/fe310/eos/net/Makefile b/fw/fe310/eos/net/Makefile
new file mode 100644
index 0000000..fc65454
--- /dev/null
+++ b/fw/fe310/eos/net/Makefile
@@ -0,0 +1,19 @@
+include ../../common.mk
+
+obj = rng.o pwr.o wifi.o sock.o cell.o
+lib = ../../libeos-net.a
+
+
+%.o: %.c %.h
+ $(CC) $(CFLAGS) -c $<
+
+%.o: %.S
+ $(CC) $(CFLAGS) -c $<
+
+all: $(lib)
+
+$(lib): $(obj)
+ $(AR) rcs $@ $(obj)
+
+clean:
+ rm -f *.o $(lib)
diff --git a/fw/fe310/eos/net/cell.c b/fw/fe310/eos/net/cell.c
new file mode 100644
index 0000000..4bfbb35
--- /dev/null
+++ b/fw/fe310/eos/net/cell.c
@@ -0,0 +1,51 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "eos.h"
+#include "event.h"
+#include "dev/net.h"
+
+#include "cell.h"
+
+static eos_evt_handler_t evt_handler[EOS_CELL_MAX_MTYPE];
+
+static void cell_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) {
+ unsigned char mtype;
+ unsigned char idx;
+
+ if ((buffer == NULL) || (len < 1)) {
+ eos_net_bad_handler(type, buffer, len);
+ return;
+ }
+
+ mtype = buffer[0];
+ idx = (mtype & EOS_CELL_MTYPE_MASK) >> 4;
+ if ((idx < EOS_CELL_MAX_MTYPE) && evt_handler[idx]) {
+ evt_handler[idx](mtype & ~EOS_CELL_MTYPE_MASK, buffer, len);
+ } else {
+ eos_net_bad_handler(type, buffer, len);
+ }
+}
+
+void eos_cell_init(void) {
+ int i;
+
+ for (i=0; i<EOS_CELL_MAX_MTYPE; i++) {
+ evt_handler[i] = NULL;
+ }
+ eos_net_set_handler(EOS_NET_MTYPE_CELL, cell_handle_msg);
+}
+
+void eos_cell_set_handler(unsigned char mtype, eos_evt_handler_t handler) {
+ unsigned char idx = (mtype & EOS_CELL_MTYPE_MASK) >> 4;
+
+ if (idx < EOS_CELL_MAX_MTYPE) evt_handler[idx] = handler;
+}
+
+eos_evt_handler_t eos_cell_get_handler(unsigned char mtype) {
+ unsigned char idx = (mtype & EOS_CELL_MTYPE_MASK) >> 4;
+
+ if (idx < EOS_CELL_MAX_MTYPE) return evt_handler[idx];
+ return NULL;
+}
diff --git a/fw/fe310/eos/net/cell.h b/fw/fe310/eos/net/cell.h
new file mode 100644
index 0000000..f04eef2
--- /dev/null
+++ b/fw/fe310/eos/net/cell.h
@@ -0,0 +1,61 @@
+#include <stdint.h>
+#include "../event.h"
+
+#define EOS_CELL_MTYPE_DEV 0x10
+#define EOS_CELL_MTYPE_VOICE 0x20
+#define EOS_CELL_MTYPE_SMS 0x30
+#define EOS_CELL_MTYPE_CBS 0x40
+#define EOS_CELL_MTYPE_USSD 0x50
+#define EOS_CELL_MTYPE_PDP 0x60
+
+#define EOS_CELL_MTYPE_MASK 0xf0
+#define EOS_CELL_MAX_MTYPE 8
+
+/* EOS_CELL_MTYPE_DEV subtypes */
+#define EOS_CELL_MTYPE_READY 1
+#define EOS_CELL_MTYPE_UART_DATA 2
+#define EOS_CELL_MTYPE_UART_TAKE 3
+#define EOS_CELL_MTYPE_UART_GIVE 4
+#define EOS_CELL_MTYPE_RESET 5
+
+#define EOS_CELL_MTYPE_VOICE_PCM 1
+#define EOS_CELL_MTYPE_VOICE_DIAL 2
+#define EOS_CELL_MTYPE_VOICE_RING 3
+#define EOS_CELL_MTYPE_VOICE_ANSWER 4
+#define EOS_CELL_MTYPE_VOICE_HANGUP 5
+#define EOS_CELL_MTYPE_VOICE_BEGIN 6
+#define EOS_CELL_MTYPE_VOICE_END 7
+#define EOS_CELL_MTYPE_VOICE_MISS 8
+#define EOS_CELL_MTYPE_VOICE_BUSY 9
+#define EOS_CELL_MTYPE_VOICE_ERR 10
+
+#define EOS_CELL_MTYPE_SMS_LIST 1
+#define EOS_CELL_MTYPE_SMS_SEND 2
+#define EOS_CELL_MTYPE_SMS_MSG_NEW 3
+#define EOS_CELL_MTYPE_SMS_MSG_ITEM 4
+
+#define EOS_CELL_MTYPE_USSD_REQUEST 1
+#define EOS_CELL_MTYPE_USSD_REPLY 2
+#define EOS_CELL_MTYPE_USSD_CANCEL 3
+
+#define EOS_CELL_MTYPE_PDP_GET_APN 1
+#define EOS_CELL_MTYPE_PDP_GET_USR 2
+#define EOS_CELL_MTYPE_PDP_GET_PWD 3
+#define EOS_CELL_MTYPE_PDP_SET_APN 4
+#define EOS_CELL_MTYPE_PDP_SET_USR 5
+#define EOS_CELL_MTYPE_PDP_SET_PWD 6
+#define EOS_CELL_MTYPE_PDP_CONNECT 7
+#define EOS_CELL_MTYPE_PDP_DISCONNECT 8
+
+#define EOS_CELL_SMS_ADDRTYPE_INTL 1
+#define EOS_CELL_SMS_ADDRTYPE_ALPHA 2
+#define EOS_CELL_SMS_ADDRTYPE_OTHER 3
+
+#define EOS_CELL_PDP_SIZE_APN 64
+#define EOS_CELL_PDP_SIZE_USR 64
+#define EOS_CELL_PDP_SIZE_PWD 64
+#define EOS_CELL_PDP_SIZE_ARG 64
+
+void eos_cell_init(void);
+void eos_cell_set_handler(unsigned char mtype, eos_evt_handler_t handler);
+eos_evt_handler_t eos_cell_get_handler(unsigned char mtype); \ No newline at end of file
diff --git a/fw/fe310/eos/net/pwr.c b/fw/fe310/eos/net/pwr.c
new file mode 100644
index 0000000..734e3cd
--- /dev/null
+++ b/fw/fe310/eos/net/pwr.c
@@ -0,0 +1,69 @@
+#include <stdlib.h>
+#include <stdint.h>
+
+#include "eos.h"
+#include "event.h"
+#include "dev/net.h"
+
+#include "soc/pwr.h"
+#include "soc/spi.h"
+#include "dev/spi.h"
+#include "eve/eve.h"
+
+#include "pwr.h"
+
+static eos_evt_handler_t evt_handler[EOS_PWR_MAX_MTYPE];
+static unsigned char power_btn_down;
+
+static void pwr_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_PWR_MAX_MTYPE) && evt_handler[mtype]) {
+ evt_handler[mtype](mtype, buffer, len);
+ } else {
+ eos_net_bad_handler(type, buffer, len);
+ }
+}
+
+static void pwr_handle_btn(unsigned char type, unsigned char *buffer, uint16_t len) {
+ int rv;
+ unsigned char level = buffer[1];
+
+ eos_net_free(buffer, 0);
+ if (!level) {
+ power_btn_down = 1;
+ return;
+ }
+ if (!power_btn_down) return;
+
+ eos_spi_select(EOS_SPI_DEV_EVE);
+ eve_pwr_sleep();
+ eos_spi_deselect();
+
+ rv = eos_pwr_sleep();
+}
+
+void eos_pwr_net_init(void) {
+ int i;
+
+ for (i=0; i<EOS_PWR_MAX_MTYPE; i++) {
+ evt_handler[i] = NULL;
+ }
+ eos_net_set_handler(EOS_NET_MTYPE_POWER, pwr_handle_msg);
+ eos_pwr_set_handler(EOS_PWR_MTYPE_BUTTON, pwr_handle_btn);
+}
+
+void eos_pwr_set_handler(unsigned char mtype, eos_evt_handler_t handler) {
+ if (mtype < EOS_PWR_MAX_MTYPE) evt_handler[mtype] = handler;
+}
+
+eos_evt_handler_t eos_pwr_get_handler(unsigned char mtype) {
+ if (mtype < EOS_PWR_MAX_MTYPE) return evt_handler[mtype];
+ return NULL;
+}
diff --git a/fw/fe310/eos/net/pwr.h b/fw/fe310/eos/net/pwr.h
new file mode 100644
index 0000000..b82a96b
--- /dev/null
+++ b/fw/fe310/eos/net/pwr.h
@@ -0,0 +1,10 @@
+#include <stdint.h>
+#include "../event.h"
+
+#define EOS_PWR_MTYPE_BUTTON 1
+
+#define EOS_PWR_MAX_MTYPE 2
+
+void eos_pwr_net_init(void);
+void eos_pwr_set_handler(unsigned char mtype, eos_evt_handler_t handler);
+eos_evt_handler_t eos_pwr_get_handler(unsigned char mtype);
diff --git a/fw/fe310/eos/net/rng.c b/fw/fe310/eos/net/rng.c
new file mode 100644
index 0000000..7d05a81
--- /dev/null
+++ b/fw/fe310/eos/net/rng.c
@@ -0,0 +1,27 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "dev/net.h"
+
+int getentropy(unsigned char *b, size_t sz) {
+ unsigned char type;
+ unsigned char *buffer;
+ uint16_t len;
+ int rv;
+
+ buffer = eos_net_alloc();
+
+ type = EOS_NET_MTYPE_RNG;
+ len = sizeof(uint16_t);
+ buffer[0] = sz >> 8;
+ buffer[1] = sz;
+
+ rv = eos_net_xchg(&type, buffer, &len);
+ if (rv || (len != sz)) rv = -1;
+
+ if (!rv) memcpy(b, buffer, sz);
+ eos_net_free(buffer, 1);
+
+ return rv;
+}
diff --git a/fw/fe310/eos/net/rng.h b/fw/fe310/eos/net/rng.h
new file mode 100644
index 0000000..27bbf74
--- /dev/null
+++ b/fw/fe310/eos/net/rng.h
@@ -0,0 +1,5 @@
+#include <stdlib.h>
+
+int getentropy(unsigned char *b, size_t sz);
+int arc4random_alloc(void **rsp, size_t rsp_size, void **rsxp, size_t rsxp_size);
+void arc4random_close(void **rsp, void **rsxp); \ No newline at end of file
diff --git a/fw/fe310/eos/net/sock.c b/fw/fe310/eos/net/sock.c
new file mode 100644
index 0000000..1db0cd9
--- /dev/null
+++ b/fw/fe310/eos/net/sock.c
@@ -0,0 +1,163 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "eos.h"
+#include "event.h"
+#include "dev/net.h"
+
+#include "sock.h"
+
+static eos_evt_handler_t evt_handler[EOS_SOCK_MAX_SOCK];
+
+static void sock_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) {
+ unsigned char sock;
+
+ if ((buffer == NULL) || (len < 2)) {
+ eos_net_bad_handler(type, buffer, len);
+ return;
+ }
+
+ sock = buffer[1];
+ if ((sock == 0) || (sock > EOS_SOCK_MAX_SOCK) || (evt_handler[sock - 1] == NULL)) {
+ eos_net_bad_handler(type, buffer, len);
+ return;
+ }
+
+ switch(buffer[0]) {
+ case EOS_SOCK_MTYPE_PKT:
+ evt_handler[sock - 1](type, buffer, len);
+ break;
+ default:
+ eos_net_bad_handler(type, buffer, len);
+ break;
+ }
+}
+
+void eos_sock_init(void) {
+ int i;
+
+ for (i=0; i<EOS_SOCK_MAX_SOCK; i++) {
+ evt_handler[i] = NULL;
+ }
+ eos_net_set_handler(EOS_NET_MTYPE_SOCK, sock_handle_msg);
+}
+
+void eos_sock_set_handler(unsigned char sock, eos_evt_handler_t handler) {
+ if (sock && (sock <= EOS_SOCK_MAX_SOCK)) evt_handler[sock - 1] = handler;
+}
+
+eos_evt_handler_t eos_sock_get_handler(unsigned char sock) {
+ if (sock && (sock <= EOS_SOCK_MAX_SOCK)) return evt_handler[sock - 1];
+ return NULL;
+}
+
+int eos_sock_open_udp(eos_evt_handler_t handler, unsigned char *buffer) {
+ unsigned char type;
+ uint16_t len;
+ int do_release;
+ int rv, sock;
+
+ do_release = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ do_release = 1;
+ }
+
+ type = EOS_NET_MTYPE_SOCK;
+ len = 1;
+ buffer[0] = EOS_SOCK_MTYPE_OPEN_DGRAM;
+
+ rv = eos_net_xchg(&type, buffer, &len);
+ if (rv) goto sock_open_udp_fin;
+
+ if (type != EOS_NET_MTYPE_SOCK) {
+ rv = EOS_ERR_NET;
+ goto sock_open_udp_fin;
+ }
+ if (len < 2) {
+ rv = EOS_ERR_SIZE;
+ goto sock_open_udp_fin;
+ }
+
+ sock = buffer[1];
+ if (sock == 0) {
+ rv = EOS_ERR_NET;
+ goto sock_open_udp_fin;
+ }
+
+ rv = sock;
+ eos_sock_set_handler(sock, handler);
+
+sock_open_udp_fin:
+ if (do_release) eos_net_free(buffer, 1);
+ return rv;
+}
+
+void eos_sock_close(unsigned char sock, unsigned char *buffer) {
+ int async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ buffer[0] = EOS_SOCK_MTYPE_CLOSE;
+ buffer[1] = sock;
+ _eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 2, async, 1);
+ eos_sock_set_handler(sock, NULL);
+}
+
+static int sock_send(unsigned char sock, unsigned char *msg, uint16_t msg_len, EOSNetAddr *addr, unsigned char *buffer) {
+ buffer[0] = EOS_SOCK_MTYPE_PKT;
+ buffer[1] = sock;
+ buffer += 2;
+ memcpy(buffer, addr->host, sizeof(addr->host));
+ buffer += sizeof(addr->host);
+ buffer[0] = addr->port >> 8;
+ buffer[1] = addr->port;
+ buffer += sizeof(addr->port);
+ if (msg) {
+ if (msg_len + EOS_SOCK_SIZE_UDP_HDR > EOS_NET_MTU) return EOS_ERR_SIZE;
+ memcpy(buffer, msg, msg_len);
+ }
+
+ return EOS_OK;
+}
+
+int eos_sock_sendto(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer) {
+ int rv;
+
+ rv = sock_send(sock, msg, msg_len, addr, buffer);
+ if (rv) return rv;
+
+ return eos_net_send(EOS_NET_MTYPE_SOCK, buffer, msg_len + EOS_SOCK_SIZE_UDP_HDR);
+}
+
+int eos_sock_sendto_async(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer, unsigned char more) {
+ int rv;
+
+ rv = sock_send(sock, msg, msg_len, addr, buffer);
+ if (rv) return rv;
+
+ return eos_net_send_async(EOS_NET_MTYPE_SOCK, buffer, msg_len + EOS_SOCK_SIZE_UDP_HDR, more);
+}
+
+int eos_sock_recvfrom(unsigned char *buffer, uint16_t len, unsigned char *msg, size_t msg_size, EOSNetAddr *addr) {
+ if (len < EOS_SOCK_SIZE_UDP_HDR) return EOS_ERR_SIZE;
+
+ if (buffer[0] != EOS_SOCK_MTYPE_PKT) return EOS_ERR_NET;
+
+ buffer += 2;
+ memcpy(addr->host, buffer, sizeof(addr->host));
+ buffer += sizeof(addr->host);
+ addr->port = (uint16_t)buffer[0] << 8;
+ addr->port |= (uint16_t)buffer[1];
+ buffer += sizeof(addr->port);
+ if (msg) {
+ if (msg_size < len - EOS_SOCK_SIZE_UDP_HDR) return EOS_ERR_SIZE;
+ memcpy(msg, buffer, len - EOS_SOCK_SIZE_UDP_HDR);
+ }
+
+ return EOS_OK;
+}
diff --git a/fw/fe310/eos/net/sock.h b/fw/fe310/eos/net/sock.h
new file mode 100644
index 0000000..e2f8637
--- /dev/null
+++ b/fw/fe310/eos/net/sock.h
@@ -0,0 +1,28 @@
+#include <stdint.h>
+#include "../event.h"
+
+#define EOS_SOCK_MTYPE_PKT 0
+#define EOS_SOCK_MTYPE_OPEN_DGRAM 1
+#define EOS_SOCK_MTYPE_CLOSE 127
+
+#define EOS_SOCK_MAX_SOCK 2
+
+#define EOS_SOCK_SIZE_UDP_HDR 8
+
+#define EOS_IPv4_ADDR_SIZE 4
+
+typedef struct EOSNetAddr {
+ unsigned char host[EOS_IPv4_ADDR_SIZE];
+ uint16_t port;
+} EOSNetAddr;
+
+void eos_sock_init(void);
+void eos_sock_set_handler(unsigned char sock, eos_evt_handler_t handler);
+eos_evt_handler_t eos_sock_get_handler(unsigned char sock);
+
+int eos_sock_open_udp(eos_evt_handler_t handler, unsigned char *buffer);
+void eos_sock_close(unsigned char sock, unsigned char *buffer);
+
+int eos_sock_sendto(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer);
+int eos_sock_sendto_async(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer, unsigned char more);
+int eos_sock_recvfrom(unsigned char *buffer, uint16_t len, unsigned char *msg, size_t msg_size, EOSNetAddr *addr);
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);
+}
diff --git a/fw/fe310/eos/net/wifi.h b/fw/fe310/eos/net/wifi.h
new file mode 100644
index 0000000..2100144
--- /dev/null
+++ b/fw/fe310/eos/net/wifi.h
@@ -0,0 +1,27 @@
+#include <stdint.h>
+#include "../event.h"
+
+#define EOS_WIFI_MTYPE_STATUS 0
+#define EOS_WIFI_MTYPE_SCAN 1
+#define EOS_WIFI_MTYPE_START 2
+#define EOS_WIFI_MTYPE_STOP 3
+#define EOS_WIFI_MTYPE_CONNECT 4
+#define EOS_WIFI_MTYPE_DISCONNECT 5
+
+#define EOS_WIFI_MAX_MTYPE 2
+
+#define EOS_WIFI_STATUS_OFF 0
+#define EOS_WIFI_STATUS_DISCONNECTED 1
+#define EOS_WIFI_STATUS_CONNECTED 2
+#define EOS_WIFI_STATUS_GOT_IP 3
+
+void eos_wifi_init(void);
+void eos_wifi_set_handler(unsigned char mtype, eos_evt_handler_t handler);
+eos_evt_handler_t eos_wifi_get_handler(unsigned char mtype);
+
+int eos_wifi_status(unsigned char *buffer, uint8_t *status, uint8_t ip_addr[], char *ssid);
+int eos_wifi_start(unsigned char *buffer);
+int eos_wifi_stop(unsigned char *buffer);
+int eos_wifi_scan(unsigned char *buffer);
+int eos_wifi_connect(const char *ssid, const char *pwd, unsigned char *buffer);
+int eos_wifi_disconnect(unsigned char *buffer); \ No newline at end of file