ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
wifi_userconfig.c
Go to the documentation of this file.
1#include "wifi_userconfig.h"
2
3#include "../../proto/transcoder.h"
4#include "../communication.h"
5
7 Buffer* tx = ControllerTx();
8 Buffer* rx = ControllerRx();
9
10 // Clear buffers
11 memset(tx->data, 0, tx->size);
12 memset(rx->data, 0, rx->size);
13 tx->len = 0;
14 rx->len = 0;
15 // Encode and send request
17 UserConfigCommand_RequestType_REQUEST_CONFIG, NULL, tx->data, tx->size);
18 if (tx->len == 0) {
19 // APP_LOG(TS_OFF, VLEVEL_M, "Failed to encode config request\r\n");
21 }
22
24 if (status != CONTROLLER_SUCCESS) {
25 // APP_LOG(TS_OFF, VLEVEL_M, "Config request failed: %d\r\n", status);
27 }
28
29 // Check if we got a response
30 if (rx->len == 0) {
31 // APP_LOG(TS_OFF, VLEVEL_M, "Empty config response\r\n");
33 }
34
35 // Decode the response
36 Esp32Command cmd = DecodeEsp32Command(rx->data, rx->len);
37 if (cmd.which_command != Esp32Command_user_config_command_tag) {
38 // APP_LOG(TS_OFF, VLEVEL_M, "Invalid response type\r\n");
40 }
41
42 if (cmd.command.user_config_command.type ==
43 UserConfigCommand_RequestType_RESPONSE_CONFIG) {
44 if (cmd.command.user_config_command.has_config_data) {
45 const UserConfiguration* config =
46 &cmd.command.user_config_command.config_data;
47 // Check if config is all zeros (uninitialized)
48 if (isConfigEmpty(config)) {
49 // APP_LOG(TS_OFF, VLEVEL_M, "Received empty config from ESP32\r\n");
51 }
52
53 if (UserConfigSave(config)) {
54 // APP_LOG(TS_OFF, VLEVEL_M, "Failed to save config to FRAM\r\n");
56 }
57
58 return USERCONFIG_OK;
59 }
60 }
61
62 // APP_LOG(TS_OFF, VLEVEL_M, "Invalid config response format\r\n");
64}
65
67 const UserConfiguration* config = UserConfigGet();
68 if (config == NULL) {
69 // APP_LOG(TS_OFF, VLEVEL_M, "Null config received from FRAM\r\n");
71 }
72
73 // Print config being sent
74 /*
75 APP_LOG(
76 TS_OFF, VLEVEL_M,
77 "-----------------Sending configuration to ESP32-----------------:\r\n");
78 UserConfigPrintAny(config);
79 */
80
81 Buffer* tx = ControllerTx();
82 Buffer* rx = ControllerRx();
83
84 // Clear buffers
85 memset(tx->data, 0, tx->size);
86 memset(rx->data, 0, rx->size);
87 tx->len = 0;
88 rx->len = 0;
89
90 // Prepare response
91 UserConfigCommand response = {0};
92 response.type = UserConfigCommand_RequestType_RESPONSE_CONFIG;
93 response.has_config_data = true;
94 memcpy(&response.config_data, config, sizeof(UserConfiguration));
95
96 // Encode and send
97 tx->len = EncodeUserConfigCommand(response.type, &response.config_data,
98 tx->data, tx->size);
99 if (tx->len == 0) {
100 // APP_LOG(TS_OFF, VLEVEL_M, "Failed to encode config response\r\n");
102 }
103
105 if (status != CONTROLLER_SUCCESS) {
106 // APP_LOG(TS_OFF, VLEVEL_M, "Failed to send config: %d\r\n", status);
108 }
109
110 // APP_LOG(TS_OFF, VLEVEL_M, "Configuration successfully sent to ESP32\r\n");
111 return USERCONFIG_OK;
112}
113
115 Buffer* tx = ControllerTx();
116 Buffer* rx = ControllerRx();
117
118 // Clear buffers
119 memset(tx->data, 0, tx->size);
120 memset(rx->data, 0, rx->size);
121 tx->len = 0;
122 rx->len = 0;
123
124 UserConfigCommand cmd = UserConfigCommand_init_zero;
125 cmd.type = UserConfigCommand_RequestType_START;
126
127 tx->len =
128 EncodeUserConfigCommand(cmd.type, &cmd.config_data, tx->data, tx->size);
129 if (tx->len == 0) {
130 // APP_LOG(TS_OFF, VLEVEL_M, "Failed to encode config response\r\n");
131 return false;
132 }
133
135 if (status != CONTROLLER_SUCCESS) {
136 // APP_LOG(TS_OFF, VLEVEL_M, "Failed to send start request: %d\r\n",
137 // status);
138 return false;
139 }
140
141 return true;
142}
143
144bool isConfigEmpty(const UserConfiguration* config) {
145 // Check if logger_id & cell_id fields are zero/default
146 return (config->logger_id == 0 && config->cell_id == 0);
147}
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.
ControllerStatus
@ CONTROLLER_SUCCESS
Esp32Command DecodeEsp32Command(const uint8_t *data, const size_t len)
Decodes an Esp32Command message.
Definition transcoder.c:246
size_t EncodeUserConfigCommand(UserConfigCommand_RequestType type, const UserConfiguration *config_data, uint8_t *buffer, size_t size)
Encodes a UserConfigCommand into a protobuf message.
Definition transcoder.c:302
UserConfigStatus UserConfigSave(const UserConfiguration *config)
Saves the configuration to FRAM.
const UserConfiguration * UserConfigGet(void)
Gets a reference to the loaded user configuration data in RAM.
UserConfigStatus
Definition user_config.h:51
@ USERCONFIG_NO_RESPONSE
Definition user_config.h:55
@ USERCONFIG_ENCODE_ERROR
Definition user_config.h:60
@ USERCONFIG_OK
Definition user_config.h:52
@ USERCONFIG_EMPTY_CONFIG
Definition user_config.h:59
@ USERCONFIG_INVALID_RESPONSE
Definition user_config.h:57
@ USERCONFIG_NULL_CONFIG
Definition user_config.h:61
@ USERCONFIG_COMM_ERROR
Definition user_config.h:56
@ USERCONFIG_FRAM_ERROR
Definition user_config.h:53
size_t len
size_t size
uint8_t * data
UserConfigStatus ControllerUserConfigSend(void)
Send user configuration to ESP32.
bool ControllerUserConfigStart(void)
Start the user configuration website.
bool isConfigEmpty(const UserConfiguration *config)
Check if configuration is empty/uninitialized.
UserConfigStatus ControllerUserConfigRequest(void)
Request user configuration from ESP32.
User configuration WiFi interface.