ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
wifi.c
Go to the documentation of this file.
1#include "wifi.h"
2
3#include "../../proto/transcoder.h"
4#include "../communication.h"
5
6ControllerStatus WiFiCommandTransaction(const WiFiCommand* input,
7 WiFiCommand* output);
8
9ControllerStatus WiFiCommandTransaction(const WiFiCommand* input,
10 WiFiCommand* output) {
11 // get reference to tx and rx buffers
12 Buffer* tx = ControllerTx();
13 Buffer* rx = ControllerRx();
14
15 // encode command
16 tx->len = EncodeWiFiCommand(input, tx->data, tx->size);
17
18 // send transaction
20 status = ControllerTransaction();
21 if (status != CONTROLLER_SUCCESS) {
22 return status;
23 }
24
25 // check for errors
26 if (rx->len == 0) {
27 return CONTROLLER_ERROR;
28 }
29
30 // decode command
31 Esp32Command cmd = Esp32Command_init_default;
32 cmd = DecodeEsp32Command(rx->data, rx->len);
33
34 // copy output data
35 memcpy(output, &cmd.command.wifi_command, sizeof(WiFiCommand));
36
37 return CONTROLLER_SUCCESS;
38}
39
40bool ControllerWiFiConnect(const char* ssid, const char* passwd) {
41 // format input command
42 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
43 wifi_cmd.type = WiFiCommand_Type_CONNECT;
44 strncpy(wifi_cmd.ssid, ssid, sizeof(wifi_cmd.ssid));
45 strncpy(wifi_cmd.passwd, passwd, sizeof(wifi_cmd.passwd));
46
47 WiFiCommand resp = WiFiCommand_init_zero;
48
49 if (WiFiCommandTransaction(&wifi_cmd, &resp) != CONTROLLER_SUCCESS) {
50 return false;
51 }
52
53 return true;
54}
55
57 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
58 wifi_cmd.type = WiFiCommand_Type_DISCONNECT;
59
60 WiFiCommand resp = WiFiCommand_init_zero;
61
62 if (WiFiCommandTransaction(&wifi_cmd, &resp) != CONTROLLER_SUCCESS) {
63 return false;
64 }
65
66 return true;
67}
68
70 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
71 wifi_cmd.type = WiFiCommand_Type_CHECK_WIFI;
72
73 WiFiCommand resp = WiFiCommand_init_zero;
74
75 WiFiCommandTransaction(&wifi_cmd, &resp);
76
77 return resp.rc;
78}
79
81 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
82 wifi_cmd.type = WiFiCommand_Type_NTP_SYNC;
83
84 WiFiCommand resp = WiFiCommand_init_zero;
85
86 if (WiFiCommandTransaction(&wifi_cmd, &resp) != CONTROLLER_SUCCESS) {
87 return false;
88 }
89
90 return true;
91}
92
93uint32_t ControllerWiFiTime(void) {
94 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
95 wifi_cmd.type = WiFiCommand_Type_TIME;
96
97 WiFiCommand resp = WiFiCommand_init_zero;
98
99 WiFiCommandTransaction(&wifi_cmd, &resp);
100
101 return resp.ts;
102}
103
104bool ControllerWiFiCheckApi(const char* url) {
105 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
106 wifi_cmd.type = WiFiCommand_Type_CHECK_API;
107 strncpy(wifi_cmd.url, url, sizeof(wifi_cmd.url));
108
109 WiFiCommand resp = WiFiCommand_init_zero;
110
111 if (WiFiCommandTransaction(&wifi_cmd, &resp) != CONTROLLER_SUCCESS) {
112 return false;
113 }
114
115 return true;
116}
117
118bool ControllerWiFiPost(const uint8_t* data, size_t data_len) {
119 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
120 wifi_cmd.type = WiFiCommand_Type_POST;
121 wifi_cmd.resp.size = data_len;
122 if (data_len > 0) {
123 memcpy(wifi_cmd.resp.bytes, data, data_len);
124 }
125
126 WiFiCommand resp = WiFiCommand_init_zero;
127
128 if (WiFiCommandTransaction(&wifi_cmd, &resp) != CONTROLLER_SUCCESS) {
129 return false;
130 }
131
132 return true;
133}
134
136 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
137 wifi_cmd.type = WiFiCommand_Type_CHECK;
138
139 // naming here doesn't really make sense since resp has a resp field
140 WiFiCommand resp = WiFiCommand_init_zero;
141
142 WiFiCommandTransaction(&wifi_cmd, &resp);
143
144 // copy the response
145 ControllerWiFiResponse http_resp = {};
146 http_resp.http_code = resp.rc;
147 http_resp.size = resp.resp.size;
148 if (http_resp.size > 0) {
149 memcpy(http_resp.bytes, resp.resp.bytes, http_resp.size);
150 }
151
152 // return timestamp
153 return http_resp;
154}
155
156bool ControllerWiFiHost(const char* ssid, const char* passwd) {
157 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
158 wifi_cmd.type = WiFiCommand_Type_HOST;
159
160 strncpy(wifi_cmd.ssid, ssid, sizeof(wifi_cmd.ssid));
161 strncpy(wifi_cmd.passwd, passwd, sizeof(wifi_cmd.passwd));
162
163 WiFiCommand resp = WiFiCommand_init_zero;
164
165 if (WiFiCommandTransaction(&wifi_cmd, &resp) != CONTROLLER_SUCCESS) {
166 // ip = NULL;
167 return false;
168 }
169
170 return true;
171}
172
174 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
175 wifi_cmd.type = WiFiCommand_Type_STOP_HOST;
176
177 WiFiCommand resp = WiFiCommand_init_zero;
178
179 if (WiFiCommandTransaction(&wifi_cmd, &resp) != CONTROLLER_SUCCESS) {
180 return false;
181 }
182
183 return true;
184}
185
186void ControllerWiFiHostInfo(char* ssid, char* ip, char* mac, uint8_t* clients) {
187 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
188 wifi_cmd.type = WiFiCommand_Type_HOST_INFO;
189
190 WiFiCommand resp = WiFiCommand_init_zero;
191
192 WiFiCommandTransaction(&wifi_cmd, &resp);
193
194 if (ssid != NULL) {
195 strcpy(ssid, resp.ssid);
196 }
197
198 if (ip != NULL) {
199 strcpy(ip, resp.url);
200 }
201
202 if (mac != NULL) {
203 strcpy(mac, resp.mac);
204 }
205
206 if (clients != NULL) {
207 *clients = resp.clients;
208 }
209}
Buffer * ControllerTx(void)
Get reference to transmit buffer.
Buffer * ControllerRx(void)
Get reference to receive buffer.
ControllerStatus ControllerTransaction(void)
Send message and receive response from esp32.
const char ssid[]
const size_t data_len
const char data[]
ControllerStatus
@ CONTROLLER_ERROR
@ CONTROLLER_SUCCESS
bool ControllerWiFiHost(const char *ssid, const char *passwd)
Sets the esp32 as a WiFi access point.
Definition wifi.c:156
void ControllerWiFiHostInfo(char *ssid, char *ip, char *mac, uint8_t *clients)
Gets information about the current access point.
Definition wifi.c:186
bool ControllerWiFiConnect(const char *ssid, const char *passwd)
Initialize WiFi settings on the esp32.
Definition wifi.c:40
ControllerWiFiStatus ControllerWiFiCheckWiFi(void)
Checks the current status of WiFi.
Definition wifi.c:69
bool ControllerWiFiCheckApi(const char *url)
Queries the API endpoint to check if it is reachable.
Definition wifi.c:104
bool ControllerWiFiDisconnect(void)
Disconnects from the WiFi network.
Definition wifi.c:56
bool ControllerWiFiNtpSync(void)
Triggers a NTP sync with the server.
Definition wifi.c:80
ControllerWiFiStatus
WiFi status.
Definition wifi.h:57
uint32_t ControllerWiFiTime(void)
Get the current time from the NTP server.
Definition wifi.c:93
bool ControllerWiFiPost(const uint8_t *data, size_t data_len)
Post data to the configured endpoint.
Definition wifi.c:118
bool ControllerWiFiStopHost(void)
Stops the esp32 from acting as a WiFi access point.
Definition wifi.c:173
ControllerWiFiResponse ControllerWiFiCheckRequest(void)
Checks the status of the most recent HTTP request.
Definition wifi.c:135
size_t EncodeWiFiCommand(const WiFiCommand *wifi_cmd, uint8_t *buffer, size_t size)
Encodes a WiFiCommand.
Definition transcoder.c:290
Esp32Command DecodeEsp32Command(const uint8_t *data, const size_t len)
Decodes an Esp32Command message.
Definition transcoder.c:246
ControllerStatus WiFiCommandTransaction(const WiFiCommand *input, WiFiCommand *output)
Definition wifi.c:9
size_t len
size_t size
uint8_t * data
WiFi response.
Definition wifi.h:75
uint8_t bytes[256]
Definition wifi.h:79
unsigned int http_code
Definition wifi.h:77