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/cell.c51
-rw-r--r--fw/fe310/eos/net/cell.h51
-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.c154
-rw-r--r--fw/fe310/eos/net/sock.h28
-rw-r--r--fw/fe310/eos/net/wifi.c106
-rw-r--r--fw/fe310/eos/net/wifi.h18
8 files changed, 440 insertions, 0 deletions
diff --git a/fw/fe310/eos/net/cell.c b/fw/fe310/eos/net/cell.c
new file mode 100644
index 0000000..20a9f42
--- /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 "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_netinit(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..b01d4cf
--- /dev/null
+++ b/fw/fe310/eos/net/cell.h
@@ -0,0 +1,51 @@
+#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_CONFIG 1
+#define EOS_CELL_MTYPE_PDP_CONNECT 2
+#define EOS_CELL_MTYPE_PDP_DISCONNECT 3
+
+#define EOS_CELL_SMS_ADDRTYPE_INTL 1
+#define EOS_CELL_SMS_ADDRTYPE_ALPHA 2
+#define EOS_CELL_SMS_ADDRTYPE_OTHER 3
+
+void eos_cell_netinit(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/rng.c b/fw/fe310/eos/net/rng.c
new file mode 100644
index 0000000..317941d
--- /dev/null
+++ b/fw/fe310/eos/net/rng.c
@@ -0,0 +1,27 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "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..7365c97
--- /dev/null
+++ b/fw/fe310/eos/net/sock.c
@@ -0,0 +1,154 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "eos.h"
+#include "event.h"
+#include "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_netinit(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) return rv;
+
+ if (type != EOS_NET_MTYPE_SOCK) return EOS_ERR_NET;
+ if (len < 2) return EOS_ERR_SIZE;
+
+ sock = buffer[1];
+ if (sock == 0) return EOS_ERR_NET;
+
+ if (do_release) {
+ eos_net_free(buffer, 1);
+ }
+ eos_sock_set_handler(sock, handler);
+
+ return sock;
+}
+
+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..5ef42e3
--- /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_netinit(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..0663582
--- /dev/null
+++ b/fw/fe310/eos/net/wifi.c
@@ -0,0 +1,106 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "eos.h"
+#include "event.h"
+#include "net.h"
+
+#include "wifi.h"
+
+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_netinit(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_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_auth(const char *ssid, const char *pass, unsigned char *buffer) {
+ unsigned char *buf;
+ size_t ssid_len, pass_len;
+ int async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ ssid_len = strlen(ssid) + 1;
+ pass_len = strlen(pass) + 1;
+ if ((1 + ssid_len + pass_len) > EOS_NET_MTU) return EOS_ERR_SIZE;
+
+ buf = buffer;
+ buf[0] = EOS_WIFI_MTYPE_AUTH;
+ buf++;
+ strcpy(buf, ssid);
+ buf += ssid_len;
+ strcpy(buf, pass);
+ buf += pass_len;
+
+ return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1 + ssid_len + pass_len, async, 1);
+}
+
+int eos_wifi_connect(unsigned char *buffer) {
+ int async;
+
+ async = 0;
+ if (buffer == NULL) {
+ buffer = eos_net_alloc();
+ async = 1;
+ }
+ buffer[0] = EOS_WIFI_MTYPE_CONNECT;
+ 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..4a49518
--- /dev/null
+++ b/fw/fe310/eos/net/wifi.h
@@ -0,0 +1,18 @@
+#include <stdint.h>
+#include "event.h"
+
+#define EOS_WIFI_MTYPE_SCAN 1
+#define EOS_WIFI_MTYPE_AUTH 2
+#define EOS_WIFI_MTYPE_CONNECT 3
+#define EOS_WIFI_MTYPE_DISCONNECT 4
+
+#define EOS_WIFI_MAX_MTYPE 5
+
+void eos_wifi_netinit(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_scan(unsigned char *buffer);
+int eos_wifi_auth(const char *ssid, const char *pass, unsigned char *buffer);
+int eos_wifi_connect(unsigned char *buffer);
+int eos_wifi_disconnect(unsigned char *buffer);