ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
user_config.c
Go to the documentation of this file.
1#include "user_config.h"
2
3#include <stdio.h>
4
6#include "./storage/fram.h"
7
8// Static variable to store the loaded user configuration in RAM
9static UserConfiguration loadedConfig = {};
10
11#ifdef TEST_USER_CONFIG
12const static UserConfiguration testConfig = {
13 .logger_id = 200,
14 .cell_id = 200,
15 .Upload_method = Uploadmethod_WiFi,
16 .Upload_interval = 10,
17 .enabled_sensors_count = 2,
18 .enabled_sensors = {EnabledSensor_Voltage, EnabledSensor_Current},
19 // calibration values are taken from 2.2.3-033
20 .Voltage_Slope = -0.00039326,
21 .Voltage_Offset = 4.92916378e-05,
22 .Current_Slope = -1.18693164e-10,
23 .Current_Offset = 4.14518594e-05,
24 .WiFi_SSID = "HARE_Lab",
25 .WiFi_Password = "",
26 .API_Endpoint_URL = "http://dirtviz.jlab.ucsc.edu/api/sensor/",
27 // port is not used
28 .API_Endpoint_Port = 80};
29#endif // TEST_USER_CONFIG
30
42UserConfigStatus UserConfig_WriteToFRAM(uint16_t addr, uint8_t* data,
43 uint16_t length);
44
56UserConfigStatus UserConfig_ReadFromFRAM(uint16_t addr, uint16_t length,
57 uint8_t* data);
58
60 uint16_t length) {
61 fram_status status = fram_write(addr, data, length);
62 if (status != FRAM_OK) {
64 }
65
66 return USERCONFIG_OK;
67}
68
69UserConfigStatus UserConfig_ReadFromFRAM(uint16_t addr, uint16_t length,
70 uint8_t* data) {
71 fram_status status = fram_read(addr, length, data);
72 if (status != FRAM_OK) {
74 }
75
76 return USERCONFIG_OK;
77}
78
79UserConfigStatus UserConfigBytes(uint8_t* buffer, uint16_t* length) {
80#ifdef TEST_USER_CONFIG
81 return USERCONFIG_OK;
82#else
83 uint8_t length_buf[2];
84
85 // Read the length of the user configuration data from FRAM
89 }
90
91 // Convert length bytes to integer
92 *length = (length_buf[0] << 8) | length_buf[1];
93
94 if (*length == 0) {
96 }
97
98 // check the length for errors
99 if (*length > UserConfiguration_size) {
101 }
102
103 // Read the encoded configuration data from FRAM into RX_Buffer
107 }
108
109 return USERCONFIG_OK;
110#endif // TEST_USER_CONFIG
111}
112
113UserConfigStatus UserConfigLoadBytes(uint8_t* buffer, uint16_t length) {
114 // Decode the user configuration from RX_Buffer into loadedConfig struct
115 if (DecodeUserConfiguration(buffer, length, &loadedConfig) != USERCONFIG_OK) {
116 // Return an error if decoding fails
118 }
119
120 return USERCONFIG_OK;
121}
122
123// Load user configuration data from FRAM to RAM
125#ifdef TEST_USER_CONFIG
126 return USERCONFIG_OK;
127#else
128
129 uint8_t buffer[UserConfiguration_size] = {};
130 uint16_t data_length = 0;
131
133
135 status = UserConfigBytes(buffer, &data_length);
136 if (status != USERCONFIG_OK) {
137 return status;
138 }
139
140 // Decode the user configuration from RX_Buffer into loadedConfig struct
141 status = UserConfigLoadBytes(buffer, data_length);
142 if (status != USERCONFIG_OK) {
143 return status;
144 }
145
146 return USERCONFIG_OK; // Return success if decoding is successful
147#endif // TEST_USER_CONFIG
148}
149
150// Get a reference to the loaded user configuration data in RAM.
151const UserConfiguration* UserConfigGet(void) {
152#ifdef TEST_USER_CONFIG
153 return &testConfig;
154#else
155 return &loadedConfig;
156#endif // TEST_USER_CONFIG
157}
158
159UserConfigStatus UserConfigSave(const UserConfiguration* config) {
160 if (config == NULL) {
162 }
163
164 uint8_t encoded_data[UserConfiguration_size];
165 size_t encoded_length = EncodeUserConfiguration(config, encoded_data);
166 if (encoded_length == 0) {
168 }
169
170 // Write the length of the encoded data to FRAM
171 uint8_t length_buf[2] = {(encoded_length >> 8) & 0xFF, encoded_length & 0xFF};
172 UserConfigStatus status =
174 if (status != USERCONFIG_OK) {
175 return status;
176 }
177
178 // Write the encoded data to FRAM
180 encoded_length);
181 if (status != USERCONFIG_OK) {
182 return status;
183 }
184
185 return USERCONFIG_OK;
186}
187
188void UserConfigPrintAny(const UserConfiguration* config) {
189 // Print each member of the UserConfiguration
190 printf("Logger ID: %lu\r\n", config->logger_id);
191 printf("Cell ID: %lu\r\n", config->cell_id);
192
193 if (config->Upload_method == 0) {
194 printf("Upload Method: %u \"LoRa\"\r\n", config->Upload_method);
195 } else {
196 printf("Upload Method: %u \"WiFi\"\r\n", config->Upload_method);
197 }
198
199 printf("Upload Interval: %lu\r\n", config->Upload_interval);
200
201 for (int i = 0; i < config->enabled_sensors_count; i++) {
202 const char* sensor_name = NULL;
203 switch (config->enabled_sensors[i]) {
204 case EnabledSensor_Voltage:
205 sensor_name = "Voltage";
206 break;
207 case EnabledSensor_Current:
208 sensor_name = "Current";
209 break;
210 case EnabledSensor_Teros12:
211 sensor_name = "Teros12";
212 break;
213 case EnabledSensor_Teros21:
214 sensor_name = "Teros21";
215 break;
216 case EnabledSensor_BME280:
217 sensor_name = "BME280";
218 break;
219 case EnabledSensor_Phytos31:
220 sensor_name = "Phytos31";
221 break;
222 case EnabledSensor_SEN0308:
223 sensor_name = "SEN0308";
224 break;
225 case EnabledSensor_SEN0257:
226 sensor_name = "SEN0257";
227 break;
228 case EnabledSensor_YFS210C:
229 sensor_name = "YFS210C";
230 break;
231 case EnabledSensor_PCAP02:
232 sensor_name = "PCAP02";
233 break;
234 default:
235 sensor_name = "Unknown Sensor";
236 }
237 printf("Enabled Sensor %d: %s\r\n", i + 1, sensor_name);
238 }
239
240 char float_str[100];
241
242 snprintf(float_str, sizeof(float_str), "%e", config->Voltage_Slope);
243 printf("Calibration V Slope: %s\r\n", float_str);
244
245 snprintf(float_str, sizeof(float_str), "%e", config->Voltage_Offset);
246 printf("Calibration V Offset: %s\r\n", float_str);
247
248 snprintf(float_str, sizeof(float_str), "%e", config->Current_Slope);
249 printf("Calibration I Slope: %s\r\n", float_str);
250
251 snprintf(float_str, sizeof(float_str), "%e", config->Current_Offset);
252 printf("Calibration I Offset: %s\r\n", float_str);
253
254 printf("WiFi SSID: %s\r\n", config->WiFi_SSID);
255
256 printf("WiFi Password: %s\r\n", config->WiFi_Password);
257
258 printf("API Endpoint URL: %s\r\n", config->API_Endpoint_URL);
259
260 printf("API Port: %lu\r\n", config->API_Endpoint_Port);
261}
262
263void UserConfigPrint(void) {
264 const UserConfiguration* config = UserConfigGet();
265
266 UserConfigPrintAny(config);
267}
268
270 fram_status status = FRAM_OK;
271 const uint8_t length[2] = {};
272 const uint8_t empty[UserConfiguration_size] = {};
273 status = fram_write(USER_CONFIG_LEN_ADDR, length, sizeof(length));
274 if (status != FRAM_OK) {
276 }
277 status = fram_write(USER_CONFIG_START_ADDRESS, empty, sizeof(empty));
278 if (status != FRAM_OK) {
280 }
281 return USERCONFIG_OK;
282}
const char data[]
uint8_t buffer[32]
uint8_t i
Storage interface for onboard FRAM chips.
fram_status fram_write(fram_addr addr, const uint8_t *data, size_t len)
Write bytes at the given address.
Definition fram.c:20
fram_status
Definition fram.h:32
fram_status fram_read(fram_addr addr, size_t len, uint8_t *data)
Read bytes at the given address.
Definition fram.c:28
@ FRAM_OK
Definition fram.h:33
int DecodeUserConfiguration(const uint8_t *data, const size_t len, UserConfiguration *config)
Decodes user configuration data.
Definition transcoder.c:376
size_t EncodeUserConfiguration(const UserConfiguration *config, uint8_t *buffer)
Encodes user configuration data.
Definition transcoder.c:361
#define USER_CONFIG_START_ADDRESS
Definition user_config.h:47
UserConfigStatus UserConfigBytes(uint8_t *buffer, uint16_t *length)
Gets encoded bytes from userconfig.
Definition user_config.c:79
UserConfigStatus UserConfigClear(void)
Clears the user configuration stored in FRAM.
void UserConfigPrintAny(const UserConfiguration *config)
Prints arbitrary user config over serial.
UserConfigStatus UserConfigSave(const UserConfiguration *config)
Saves the configuration to FRAM.
void UserConfigPrint(void)
Prints the current user configuration over serial.
UserConfigStatus UserConfigLoad(void)
Loads user configuration data from FRAM to RAM.
#define USER_CONFIG_LEN_ADDR
Definition user_config.h:49
const UserConfiguration * UserConfigGet(void)
Gets a reference to the loaded user configuration data in RAM.
UserConfigStatus UserConfigLoadBytes(uint8_t *buffer, uint16_t length)
Load user config from bytes.
UserConfigStatus
Definition user_config.h:51
@ USERCONFIG_ENCODE_ERROR
Definition user_config.h:60
@ USERCONFIG_OK
Definition user_config.h:52
@ USERCONFIG_DECODE_ERROR
Definition user_config.h:54
@ USERCONFIG_EMPTY_CONFIG
Definition user_config.h:59
@ USERCONFIG_NULL_CONFIG
Definition user_config.h:61
@ USERCONFIG_FRAM_ERROR
Definition user_config.h:53
Controls the user config webserver.
Library for encoding/decoding protobuf messages.
UserConfigStatus UserConfig_ReadFromFRAM(uint16_t addr, uint16_t length, uint8_t *data)
Reads user configuration data from FRAM.
Definition user_config.c:69
UserConfigStatus UserConfig_WriteToFRAM(uint16_t addr, uint8_t *data, uint16_t length)
Writes user configuration data to FRAM.
Definition user_config.c:59