forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCEspControl.h
183 lines (130 loc) · 6.24 KB
/
CEspControl.h
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* ########################################################################## */
/* - File: CEspControl.h
- Copyright (c): 2023 Arduino srl.
- Author: Daniele Aimo ([email protected])
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc.,51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* ########################################################################## */
/* SHORT DESCRIPTION:
* This class manage all the 'Control' requests that can be send to ESP32 fw
* It is conceived as a singletone object */
#ifndef _ARDUINO_ESP_CONTROL_CLASS_H
#define _ARDUINO_ESP_CONTROL_CLASS_H
/*
* configuration defines
*/
#include <string>
#include "CCtrlWrapper.h"
#include "CEspCommunication.h"
#include "EspSpiDriver.h"
#include "CNetIf.h"
#include "CEspCbks.h"
#include <string>
#define WIFI_MAC_ADDRESS_DIM 6
class CNetUtilities {
public:
/* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
X X : X X : X X : X X : X X : X X */
static bool macStr2macArray(uint8_t *mac_out, const char *mac_in) {
if(mac_in[2] != ':' ||
mac_in[5] != ':' ||
mac_in[8] != ':' ||
mac_in[11] != ':' ||
mac_in[14] != ':') {
return false;
}
for(int i = 0; i < 6; i++) {
std::string str_num(mac_in+(i*3),2);
*(mac_out+i) = std::stoul(str_num,nullptr,16);
}
return true;
}
static void macArray2macStr(char *mac_out, const uint8_t *mac_in) {
std::string MAC = "";
for(int i = 0; i < WIFI_MAC_ADDRESS_DIM; i++) {
MAC += std::to_string(*(mac_in + i));
if(i < WIFI_MAC_ADDRESS_DIM - 1) {
MAC += ":";
}
}
for(unsigned int i = 0; i < MAC.size(); i++ ) {
*(mac_out + i) = MAC[i];
}
}
};
class CEspControl {
public:
static CEspControl& getInstance();
CEspControl(CEspControl const&) = delete;
void operator=(CEspControl const&) = delete;
~CEspControl();
/* The following functions allow the user to control the ESP32 behaviour
Each of this function has the last one parameter which is function callback
If that parameter is different from nullptr then a callback is automatically
set up and will be called when the answer to the request will arrive
If that parameter is a nullptr then the function will wait for the answer
to arrive and the behaviour will be synchronous.
! IMPORTANT: It turns out that when callback are used the "get" parameters of
the function are unavailable when the function returns.
! IMPORTANT: when callbacks are used it is user responsability to call
communicateWithEsp() in order make the communication happen and
get the callback to eventually be called! */
int getWifiMacAddress(WifiMac_t& mac, EspCallback_f cb = nullptr);
int setWifiMacAddress(WifiMac_t& mac, EspCallback_f cb = nullptr);
int getWifiMode(WifiMode_t &mode, EspCallback_f cb = nullptr);
int setWifiMode(WifiMode_t mode, EspCallback_f cb = nullptr);
int getAccessPointScanList(std::vector<AccessPoint_t>& l, EspCallback_f cb = nullptr);
int connectAccessPoint(WifiApCfg_t &ap_cfg, EspCallback_f cb = nullptr);
int getAccessPointConfig(WifiApCfg_t &ap, EspCallback_f cb = nullptr);
int disconnectAccessPoint(EspCallback_f cb = nullptr);
int getSoftAccessPointConfig(SoftApCfg_t &sap_cfg, EspCallback_f cb = nullptr);
int getSoftConnectedStationList(std::vector<WifiConnectedSta_t>& l, EspCallback_f cb = nullptr);
int setSoftAccessPointVndIe(WifiVendorSoftApIe_t &vendor_ie, EspCallback_f cb = nullptr);
int startSoftAccessPoint(SoftApCfg_t &cfg, EspCallback_f cb = nullptr);
int stopSoftAccessPoint(EspCallback_f cb = nullptr);
/* heartbeat "answer" is always an event, a callback must be provide in any case */
int configureHeartbeat(HeartBeat_t &hb, EspCallback_f cb);
void listenForInitEvent(EspCallback_f cb);
void listenForStationDisconnectEvent(EspCallback_f cb);
void listenForDisconnectionFromSoftApEvent(EspCallback_f cb);
int getPowerSaveMode(int &power_save_mode, EspCallback_f cb = nullptr);
int setPowerSaveMode(int power_save_mode, EspCallback_f cb = nullptr);
int beginOTA(EspCallback_f cb = nullptr);
int endOTA(EspCallback_f cb = nullptr);
int otaWrite(OtaWrite_t &ow, EspCallback_f cb = nullptr);
int getWifiCurrentTxPower(uint32_t &max_power, EspCallback_f cb = nullptr);
int setWifiMaxTxPower(uint32_t max_power, EspCallback_f cb = nullptr);
int sendBuffer(ESP_INTERFACE_TYPE type, uint8_t num, uint8_t *buf, uint16_t dim);
uint8_t *getStationRx(uint8_t &if_num, uint16_t &dim);
uint16_t peekStationRxMsgSize();
uint8_t *getSoftApRx(uint8_t &if_num, uint16_t &dim);
int initSpiDriver() { return esp_host_spi_init(); }
void communicateWithEsp();
/* timeout for synchronous answer */
void setTimeout_ms(uint32_t tout) {timeout_ms = tout; }
uint32_t getTimeout_ms() { return timeout_ms; }
private:
uint32_t timeout_ms;
CEspControl();
void prepare_and_send_request(AppMsgId_e id, int& res, void *arg, EspCallback_f cb, CCtrlMsgWrapper& rv);
/* dispatches the received message to one of the other handler function below*/
int process_msgs_received(CCtrlMsgWrapper *ans);
/* process ctrl messages */
int process_ctrl_messages(CMsg& msg, CCtrlMsgWrapper *ans);
/* process priv messages */
int process_priv_messages(CCtrlMsgWrapper *ans);
/* process test messages */
int process_test_messages(CCtrlMsgWrapper* ans);
int wait_for_answer(CCtrlMsgWrapper* ans);
int send_net_packet(CMsg& msg);
};
#endif