telemesh.cpp
2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "FreematicsPlus.h"
#include "config.h"
#include "telemesh.h"
#if NET_DEVICE == NET_WIFI_MESH
#include <mdf_common.h>
#include <mwifi.h>
static mdf_err_t event_loop_cb(mdf_event_loop_t event, void *ctx)
{
switch (event) {
case MDF_EVENT_MWIFI_STARTED:
Serial.println("MESH is started");
break;
case MDF_EVENT_MWIFI_PARENT_CONNECTED:
Serial.println("Parent is connected on station interface");
break;
case MDF_EVENT_MWIFI_PARENT_DISCONNECTED:
Serial.println("Parent is disconnected on station interface");
break;
default:
break;
}
return MDF_OK;
}
static mdf_err_t wifi_init()
{
mdf_err_t ret = nvs_flash_init();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
nvs_flash_erase();
ret = nvs_flash_init();
}
//MDF_ERROR_ASSERT(ret);
tcpip_adapter_init();
esp_event_loop_init(NULL, NULL);
esp_wifi_init(&cfg);
esp_wifi_set_storage(WIFI_STORAGE_FLASH);
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_ps(WIFI_PS_NONE);
esp_mesh_set_6m_rate(false);
return esp_wifi_start();
}
bool ClientWiFiMesh::begin(CFreematics* device)
{
if (m_inited) return true;
mwifi_init_config_t cfg = MWIFI_INIT_CONFIG_DEFAULT();
mwifi_config_t config = {0};
config.channel = WIFI_MESH_CHANNEL;
config.mesh_type = MWIFI_MESH_NODE;
memcpy(config.mesh_id, WIFI_MESH_ID, sizeof(WIFI_MESH_ID) - 1);
mdf_event_loop_init(event_loop_cb);
wifi_init();
mwifi_init(&cfg);
mwifi_set_config(&config);
if (mwifi_start() == MDF_OK) {
m_inited = true;
return true;
}
return false;
}
bool ClientWiFiMesh::open(const char* host, uint16_t port)
{
return mwifi_is_connected();
}
bool ClientWiFiMesh::send(const char* data, unsigned int len)
{
if (mwifi_is_connected()) {
mwifi_data_type_t data_type = {0x0};
if (mwifi_write(NULL, &data_type, data, len, true) == MDF_OK)
return true;
}
return false;
}
char* ClientWiFiMesh::receive(int* pbytes, unsigned int timeout)
{
if (mwifi_is_connected()) {
uint8_t src_addr[MWIFI_ADDR_LEN] = {0x0};
mwifi_data_type_t data_type = {0x0};
size_t size = MESH_RECV_BUF_SIZE - 1;
if (mwifi_read(src_addr, &data_type, m_buffer, &size, timeout / portTICK_PERIOD_MS) == MDF_OK) {
m_buffer[size] = 0;
if (pbytes) *pbytes = size;
return m_buffer;
}
}
return 0;
}
ClientWiFiMesh::ClientWiFiMesh()
{
m_buffer = (char*)malloc(MESH_RECV_BUF_SIZE);
}
ClientWiFiMesh::~ClientWiFiMesh()
{
free(m_buffer);
}
#endif