From 0ca91a2b6a1b112a6e67306e7dbedaeec0225c5b Mon Sep 17 00:00:00 2001
From: Uros Majstorovic <majstor@majstor.org>
Date: Tue, 11 Aug 2020 19:04:15 +0200
Subject: cell messages are handled in separate task

---
 fw/esp32/components/eos/cell.c        | 64 ++++++++++++++++++++++++++++++++++-
 fw/esp32/components/eos/include/eos.h |  2 ++
 2 files changed, 65 insertions(+), 1 deletion(-)

(limited to 'fw/esp32/components/eos')

diff --git a/fw/esp32/components/eos/cell.c b/fw/esp32/components/eos/cell.c
index c2e03e1..11a96e8 100644
--- a/fw/esp32/components/eos/cell.c
+++ b/fw/esp32/components/eos/cell.c
@@ -1,15 +1,31 @@
 #include <stdlib.h>
 #include <stdint.h>
+#include <string.h>
 
+#include <freertos/FreeRTOS.h>
+#include <freertos/semphr.h>
+#include <freertos/task.h>
+#include <freertos/queue.h>
 #include <esp_log.h>
 
 #include "eos.h"
+#include "msgq.h"
 #include "net.h"
 #include "cell.h"
 
+#define CELL_SIZE_QUEUE     2
+
+static const char *TAG = "EOS CELL";
+
 static uint8_t cell_mode;
 
-static void cell_handler(unsigned char _mtype, unsigned char *buffer, uint16_t size) {
+static EOSBufQ cell_buf_q;
+static unsigned char *cell_bufq_array[CELL_SIZE_QUEUE];
+
+static SemaphoreHandle_t mutex;
+static QueueHandle_t cell_queue;
+
+static void _cell_handler(unsigned char _mtype, unsigned char *buffer, uint16_t size) {
     uint8_t mtype;
 
     if (size < 1) return;
@@ -62,7 +78,53 @@ static void cell_handler(unsigned char _mtype, unsigned char *buffer, uint16_t s
     }
 }
 
+static void cell_handler_task(void *pvParameters) {
+    EOSMsgItem mi;
+
+    while (1) {
+        if (xQueueReceive(cell_queue, &mi, portMAX_DELAY)) {
+            _cell_handler(mi.type, mi.buffer, mi.len);
+            xSemaphoreTake(mutex, portMAX_DELAY);
+            eos_bufq_push(&cell_buf_q, mi.buffer);
+            xSemaphoreGive(mutex);
+        }
+    }
+    vTaskDelete(NULL);
+}
+
+static void cell_handler(unsigned char type, unsigned char *buffer, uint16_t len) {
+    EOSMsgItem mi;
+    unsigned char *buf;
+
+    xSemaphoreTake(mutex, portMAX_DELAY);
+    buf = eos_bufq_pop(&cell_buf_q);
+    xSemaphoreGive(mutex);
+
+    if (buf == NULL) {
+        ESP_LOGE(TAG, "Cell message NOT handled: %2x", type);
+        return;
+    }
+
+    memcpy(buf, buffer, len);
+    mi.type = type;
+    mi.buffer = buf;
+    mi.len = len;
+    xQueueSend(cell_queue, &mi, portMAX_DELAY);
+}
+
 void eos_cell_init(void) {
+    int i;
+
+    eos_bufq_init(&cell_buf_q, cell_bufq_array, CELL_SIZE_QUEUE);
+    for (i=0; i<CELL_SIZE_QUEUE; i++) {
+        eos_bufq_push(&cell_buf_q, malloc(EOS_NET_SIZE_BUF));
+    }
+
+    mutex = xSemaphoreCreateBinary();
+    xSemaphoreGive(mutex);
+    cell_queue = xQueueCreate(CELL_SIZE_QUEUE, sizeof(EOSMsgItem));
+    xTaskCreate(cell_handler_task, "cell_handler", EOS_TASK_SSIZE_CELL, NULL, EOS_TASK_PRIORITY_CELL, NULL);
+
     eos_net_set_handler(EOS_NET_MTYPE_CELL, cell_handler);
 }
 
diff --git a/fw/esp32/components/eos/include/eos.h b/fw/esp32/components/eos/include/eos.h
index 0e660fb..7240c83 100644
--- a/fw/esp32/components/eos/include/eos.h
+++ b/fw/esp32/components/eos/include/eos.h
@@ -13,6 +13,7 @@
 #define EOS_TASK_PRIORITY_NET_XCHG  1
 #define EOS_TASK_PRIORITY_UDP_RCVR  1
 #define EOS_TASK_PRIORITY_PWR       1
+#define EOS_TASK_PRIORITY_CELL      1
 
 #define EOS_TASK_SSIZE_UART         4096
 #define EOS_TASK_SSIZE_MODEM        4096
@@ -20,4 +21,5 @@
 #define EOS_TASK_SSIZE_NET_XCHG     8192
 #define EOS_TASK_SSIZE_UDP_RCVR     4096
 #define EOS_TASK_SSIZE_PWR          4096
+#define EOS_TASK_SSIZE_CELL         4096
 
-- 
cgit v1.2.3