mesh_node_module.c 2.73 KB
Newer Older
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
1 2
#include "esp_err.h"
#include "esp_log.h"
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
3
#include "esp_mesh.h"
4

Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
5
#include "modules/mesh_module/mesh_module.h"
6
#include "modules/protocol_module/protocol_module.h"
7
#include "modules/protocol_module/message_handler/message_handler_module.h"
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
8
#include <string.h>
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
9
#define QUEUE_LENGHT 5
10

Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
11
__attribute__((unused)) static const char *TAG = "MESH MODULE NODE";
12 13 14 15

static void node_write_task(void *arg);

static QueueHandle_t node_send_queue    = NULL;
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
16
static mesh_addr_t root_addr;
17 18 19

int mesh_node_module_init(void)
{
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
20
    message_handle_module_init();
21
    node_send_queue    = xQueueCreate(QUEUE_LENGHT, sizeof(light_mesh_message_t));
22

23
    xTaskCreate(node_write_task, "node_write_task", 4 * 1024,
24
                NULL, 20, NULL);
25 26 27
    return 0;
}

Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
28
void node_read_task(void)
29
{
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
30
    uint8_t buf[1500];
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
31
    mesh_data_t data;
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
32 33 34 35
    data.data  = buf;
    data.size  = 1500;
    data.proto = MESH_PROTO_BIN;
    data.tos   = MESH_TOS_P2P;
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
36
    int  flag = 0;
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
37 38 39 40
    light_mesh_message_t light_mesh_msg;

    while(1) {
        if (!mesh_is_connected()) {
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
41
            return;
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
42 43
        }

Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
44 45 46 47
        if(esp_mesh_recv(&root_addr, &data, 0, &flag, NULL, 0) == ESP_ERR_MESH_TIMEOUT)
        {
            return;
        }
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
48

Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
49
        protocol_module_esp_mesh_to_light_mesh(&light_mesh_msg, &root_addr, data.size, data.data);
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
50 51
        message_handler_module_handle_message(&light_mesh_msg);
    }
52 53 54 55
}

void node_write_task(void *arg)
{
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
56
    mesh_data_t data;
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
57
    data.data                        = malloc(1500);
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
58 59
    data.size                        = 1500;
    data.proto                       = MESH_PROTO_BIN;
60
    data.tos                         = MESH_TOS_P2P;
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
61

Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
62
    mesh_addr_t dest_addr;
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
63 64 65 66 67 68 69 70 71
    light_mesh_message_t light_mesh_msg;

    for (;;) {
        xQueueReceive(node_send_queue, &light_mesh_msg, portMAX_DELAY);

        if (!mesh_is_connected()) {
            continue;
        }

Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
72 73
        protocol_module_light_mesh_to_esp_mesh(&light_mesh_msg, &dest_addr, &data.size, data.data);
        free(light_mesh_msg.payload);
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
74 75
        if(mesh_is_connected())
        {
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
76
            esp_mesh_send(NULL, &data, 0, NULL, 0);
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
77 78 79
        }
    }

Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
80
    ESP_LOGW(TAG, "Node write task is exit");
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
81

Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
82
    free(data.data);
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
83
    vTaskDelete(NULL);
84 85 86 87
}

void send_to_root(light_mesh_message_type_t message_type, void *payload, uint16_t payload_len)
{
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
88 89 90 91
    light_mesh_message_t light_mesh_msg;
    uint8_t root_addr[6] = {0};
    memcpy(light_mesh_msg.dest_id, root_addr, 6);
    light_mesh_msg.type                    = (uint32_t)message_type;
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
92
    light_mesh_msg.payload                 = malloc(payload_len);
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
93 94 95 96
    memcpy(light_mesh_msg.payload, payload, payload_len);
    light_mesh_msg.payload_len             = payload_len;
    if(xQueueSendToBack(node_send_queue,  &light_mesh_msg, 0) != pdTRUE)
    {
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
97
        free(light_mesh_msg.payload);
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
98
    }
Stefan Ravn van Overeem's avatar
Stefan Ravn van Overeem committed
99
}