From 21dbc40e58a79f487d1ee34129f16e26cb6ba120 Mon Sep 17 00:00:00 2001
From: Uros Majstorovic <majstor@majstor.org>
Date: Sun, 9 Aug 2020 02:46:08 +0200
Subject: message handling iface updated

---
 fw/fe310/eos/cell.c  | 12 +++++++-----
 fw/fe310/eos/power.c | 11 ++++++-----
 fw/fe310/eos/sock.c  | 14 ++++++++------
 fw/fe310/eos/sock.h  |  2 +-
 fw/fe310/eos/wifi.c  | 11 ++++++-----
 5 files changed, 28 insertions(+), 22 deletions(-)

(limited to 'fw/fe310')

diff --git a/fw/fe310/eos/cell.c b/fw/fe310/eos/cell.c
index 4b4fcc0..452fb47 100644
--- a/fw/fe310/eos/cell.c
+++ b/fw/fe310/eos/cell.c
@@ -11,14 +11,17 @@
 static eos_evt_handler_t evt_handler[EOS_CELL_MAX_MTYPE];
 
 static void cell_handle_evt(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;
     }
-    unsigned char mtype = buffer[0];
-    unsigned char idx = (mtype & EOS_CELL_MTYPE_MASK) >> 4;
 
-    if (idx < EOS_CELL_MAX_MTYPE) {
+    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);
@@ -34,7 +37,7 @@ void eos_cell_init(void) {
     int i;
 
     for (i=0; i<EOS_CELL_MAX_MTYPE; i++) {
-        evt_handler[i] = eos_net_bad_handler;
+        evt_handler[i] = NULL;
     }
     eos_net_set_handler(EOS_NET_MTYPE_CELL, cell_handle_evt);
     eos_cell_set_handler(EOS_CELL_MTYPE_READY, cell_handle_rdy);
@@ -43,6 +46,5 @@ void eos_cell_init(void) {
 void eos_cell_set_handler(unsigned char mtype, eos_evt_handler_t handler) {
     unsigned char idx = (mtype & EOS_CELL_MTYPE_MASK) >> 4;
 
-    if (handler == NULL) handler = eos_net_bad_handler;
     if (idx < EOS_CELL_MAX_MTYPE) evt_handler[idx] = handler;
 }
diff --git a/fw/fe310/eos/power.c b/fw/fe310/eos/power.c
index 5c874e0..2b13c9f 100644
--- a/fw/fe310/eos/power.c
+++ b/fw/fe310/eos/power.c
@@ -19,14 +19,16 @@ static eos_evt_handler_t evt_handler[EOS_PWR_MAX_MTYPE];
 static unsigned char power_btn_down;
 
 static void power_handle_evt(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;
     }
 
-    unsigned char mtype = buffer[0];
-    if (mtype < EOS_PWR_MAX_MTYPE) {
-        evt_handler[mtype](type, buffer, len);
+    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);
     }
@@ -49,7 +51,7 @@ void eos_power_init(void) {
     int i;
 
     for (i=0; i<EOS_PWR_MAX_MTYPE; i++) {
-        evt_handler[i] = eos_net_bad_handler;
+        evt_handler[i] = NULL;
     }
     eos_net_set_handler(EOS_NET_MTYPE_POWER, power_handle_evt);
     eos_power_set_handler(EOS_PWR_MTYPE_BUTTON, power_handle_btn);
@@ -106,6 +108,5 @@ void eos_power_wake_disable(void) {
 }
 
 void eos_power_set_handler(unsigned char mtype, eos_evt_handler_t handler) {
-    if (handler == NULL) handler = eos_net_bad_handler;
     if (mtype < EOS_PWR_MAX_MTYPE) evt_handler[mtype] = handler;
 }
diff --git a/fw/fe310/eos/sock.c b/fw/fe310/eos/sock.c
index 720e620..9abb9e8 100644
--- a/fw/fe310/eos/sock.c
+++ b/fw/fe310/eos/sock.c
@@ -12,21 +12,21 @@ static eos_evt_handler_t evt_handler[EOS_SOCK_MAX_SOCK];
 
 static void sock_handle_evt(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)) {
+    if ((sock == 0) || (sock > EOS_SOCK_MAX_SOCK) || (evt_handler[sock - 1] == NULL)) {
         eos_net_bad_handler(type, buffer, len);
         return;
     }
 
-    sock--;
     switch(buffer[0]) {
         case EOS_SOCK_MTYPE_PKT:
-            evt_handler[sock](type, buffer, len);
+            evt_handler[sock - 1](type, buffer, len);
             break;
         default:
             eos_net_bad_handler(type, buffer, len);
@@ -38,17 +38,16 @@ void eos_sock_init(void) {
     int i;
 
     for (i=0; i<EOS_SOCK_MAX_SOCK; i++) {
-        evt_handler[i] = eos_net_bad_handler;
+        evt_handler[i] = NULL;
     }
     eos_net_set_handler(EOS_NET_MTYPE_SOCK, sock_handle_evt);
 }
 
 void eos_sock_set_handler(unsigned char sock, eos_evt_handler_t handler) {
-    if (handler == NULL) handler = eos_net_bad_handler;
     if (sock && (sock <= EOS_SOCK_MAX_SOCK)) evt_handler[sock - 1] = handler;
 }
 
-int eos_sock_open_udp(void) {
+int eos_sock_open_udp(eos_evt_handler_t handler) {
     unsigned char type = EOS_SOCK_MTYPE_OPEN_DGRAM;
     unsigned char *buffer = eos_net_alloc();
     uint16_t buf_size;
@@ -68,6 +67,8 @@ int eos_sock_open_udp(void) {
     eos_net_free(buffer, 1);
 
     if (sock == 0) return EOS_ERR_NET;
+
+    eos_sock_set_handler(sock, handler);
     return sock;
 }
 
@@ -76,6 +77,7 @@ void eos_sock_close(unsigned char sock) {
     buffer[0] = EOS_SOCK_MTYPE_CLOSE;
     buffer[1] = sock;
     eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 2, 1);
+    eos_sock_set_handler(sock, NULL);
 }
 
 int eos_sock_sendto(unsigned char sock, unsigned char *buffer, uint16_t size, unsigned char more, EOSNetAddr *addr) {
diff --git a/fw/fe310/eos/sock.h b/fw/fe310/eos/sock.h
index d647381..cec8aae 100644
--- a/fw/fe310/eos/sock.h
+++ b/fw/fe310/eos/sock.h
@@ -19,7 +19,7 @@ typedef struct EOSNetAddr {
 void eos_sock_init(void);
 void eos_sock_set_handler(unsigned char sock, eos_evt_handler_t handler);
 
-int eos_sock_open_udp(void);
+int eos_sock_open_udp(eos_evt_handler_t handler);
 void eos_sock_close(unsigned char sock);
 int eos_sock_sendto(unsigned char sock, unsigned char *buffer, uint16_t size, unsigned char more, EOSNetAddr *addr);
 void eos_sock_getfrom(unsigned char *buffer, EOSNetAddr *addr);
diff --git a/fw/fe310/eos/wifi.c b/fw/fe310/eos/wifi.c
index d58c0eb..6d39332 100644
--- a/fw/fe310/eos/wifi.c
+++ b/fw/fe310/eos/wifi.c
@@ -11,14 +11,16 @@
 static eos_evt_handler_t evt_handler[EOS_WIFI_MAX_MTYPE];
 
 static void wifi_handle_evt(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;
     }
 
-    unsigned char mtype = buffer[0];
-    if (mtype < EOS_WIFI_MAX_MTYPE) {
-        evt_handler[mtype](type, buffer, len);
+    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);
     }
@@ -28,13 +30,12 @@ void eos_wifi_init(void) {
     int i;
 
     for (i=0; i<EOS_WIFI_MAX_MTYPE; i++) {
-        evt_handler[i] = eos_net_bad_handler;
+        evt_handler[i] = NULL;
     }
     eos_net_set_handler(EOS_NET_MTYPE_WIFI, wifi_handle_evt);
 }
 
 void eos_wifi_set_handler(unsigned char mtype, eos_evt_handler_t handler) {
-    if (handler == NULL) handler = eos_net_bad_handler;
     if (mtype < EOS_WIFI_MAX_MTYPE) evt_handler[mtype] = handler;
 }
 
-- 
cgit v1.2.3