ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
microsd.c
Go to the documentation of this file.
1#include "microsd.h"
2
3#include <string.h>
4
5#include "../../proto/transcoder.h"
6#include "../../util/time.h"
7#include "../communication.h"
8
10extern unsigned int g_controller_i2c_timeout;
11
12uint32_t ControllerMicroSDSave(const uint8_t* data, const uint16_t num_bytes) {
13 // get reference to tx and rx buffers
14 Buffer* tx = ControllerTx();
15 Buffer* rx = ControllerRx();
16
17 MicroSDCommand microsd_cmd = MicroSDCommand_init_zero;
18 microsd_cmd.type = MicroSDCommand_Type_SAVE;
19
20 microsd_cmd.which_data = MicroSDCommand_meas_tag;
21 // Data is already Measurement-encoded.
22 // Therefore, decode it into microsd_cmd and re-encode altogether.
23 if (DecodeMeasurement(&microsd_cmd.data.meas, data, num_bytes) == -1) {
24 // Something might need to happen here. Previously a print statement.
25 }
26
27 // encode command
28 tx->len = EncodeMicroSDCommand(&microsd_cmd, tx->data, tx->size);
29
30 // return if communication fails
32 status = ControllerTransaction();
33 if (status != CONTROLLER_SUCCESS) {
34 return 0;
35 }
36
37 // check for errors
38 if (rx->len == 0) {
39 return 0;
40 }
41
42 // decode command
43 Esp32Command cmd = Esp32Command_init_default;
44 cmd = DecodeEsp32Command(rx->data, rx->len);
45
46 // This should be printed in the module.
47 // switch (cmd.command.microsd_command.rc) {
48 // case MicroSDCommand_ReturnCode_SUCCESS:
49 // APP_LOG(TS_OFF, VLEVEL_L,
50 // "Successfully saved measurement to microSD card.\r\n");
51 // break;
52 // case MicroSDCommand_ReturnCode_ERROR_GENERAL:
53 // APP_LOG(TS_OFF, VLEVEL_M, "Error: General error.\r\n");
54 // break;
55 // case MicroSDCommand_ReturnCode_ERROR_MICROSD_NOT_INSERTED:
56 // APP_LOG(TS_OFF, VLEVEL_M, "Error: MicroSD card not inserted.\r\n");
57 // break;
58 // case MicroSDCommand_ReturnCode_ERROR_FILE_SYSTEM_NOT_MOUNTABLE:
59 // APP_LOG(TS_OFF, VLEVEL_M,
60 // "Error: File system on microSD card not mountable.\r\n");
61 // break;
62 // case MicroSDCommand_ReturnCode_ERROR_PAYLOAD_NOT_DECODED:
63 // APP_LOG(TS_OFF, VLEVEL_M,
64 // "Error: Payload in sent message not decodable.\r\n");
65 // break;
66 // case MicroSDCommand_ReturnCode_ERROR_FILE_NOT_OPENED:
67 // APP_LOG(TS_OFF, VLEVEL_M,
68 // "Error: File(s) on microSD card not openable.\r\n");
69 // break;
70 // default:
71 // APP_LOG(TS_OFF, VLEVEL_M,
72 // "Error: Unknown MicroSDCommand_ReturnCode (%d).\r\n",
73 // cmd.command.microsd_command.rc);
74 // break;
75 //}
76
77 return cmd.command.microsd_command.rc;
78}
79
80uint32_t ControllerMicroSDUserConfig(UserConfiguration* uc,
81 const char* filename) {
82 // get reference to tx and rx buffers
83 Buffer* tx = ControllerTx();
84 Buffer* rx = ControllerRx();
85
86 MicroSDCommand microsd_cmd = MicroSDCommand_init_zero;
87 microsd_cmd.type = MicroSDCommand_Type_USERCONFIG;
88
89 // Prepend current timestamp to filename, 32-bit max is 10 digits long
90 // Ex. If input filename is "data.csv", sent filename is
91 // "/<timestamp>_data.csv" Userconfig will be written to
92 // "/<timestamp>_data.csv.userconfig"
93
94 snprintf(microsd_cmd.filename, sizeof(microsd_cmd.filename), "/%lu_%s",
95 epoch(), filename);
96
97 microsd_cmd.which_data = MicroSDCommand_uc_tag;
98 memcpy(&microsd_cmd.data.uc, uc, sizeof(UserConfiguration));
99
100 // encode command
101 tx->len = EncodeMicroSDCommand(&microsd_cmd, tx->data, tx->size);
102
103 // return if communication fails
105 status = ControllerTransaction();
106 if (status != CONTROLLER_SUCCESS) {
107 return 0;
108 }
109
110 // check for errors
111 if (rx->len == 0) {
112 return 0;
113 }
114
115 // decode command
116 Esp32Command cmd = Esp32Command_init_default;
117 cmd = DecodeEsp32Command(rx->data, rx->len);
118
119 // This should be handled in the module
120 // switch (cmd.command.microsd_command.rc) {
121 // case MicroSDCommand_ReturnCode_SUCCESS:
122 // APP_LOG(
123 // TS_OFF, VLEVEL_L,
124 // "Successfully saved userConfig and CSV headers to microSD
125 // card.\r\n");
126 // break;
127 // case MicroSDCommand_ReturnCode_ERROR_GENERAL:
128 // APP_LOG(TS_OFF, VLEVEL_M, "Error: General error.\r\n");
129 // break;
130 // case MicroSDCommand_ReturnCode_ERROR_MICROSD_NOT_INSERTED:
131 // APP_LOG(TS_OFF, VLEVEL_M, "Error: MicroSD card not inserted.\r\n");
132 // break;
133 // case MicroSDCommand_ReturnCode_ERROR_FILE_SYSTEM_NOT_MOUNTABLE:
134 // APP_LOG(TS_OFF, VLEVEL_M,
135 // "Error: File system on microSD card not mountable.\r\n");
136 // break;
137 // case MicroSDCommand_ReturnCode_ERROR_PAYLOAD_NOT_DECODED:
138 // APP_LOG(TS_OFF, VLEVEL_M,
139 // "Error: Payload in sent message not decodable.\r\n");
140 // break;
141 // case MicroSDCommand_ReturnCode_ERROR_FILE_NOT_OPENED:
142 // APP_LOG(TS_OFF, VLEVEL_M,
143 // "Error: File(s) on microSD card not openable.\r\n");
144 // break;
145 // default:
146 // APP_LOG(TS_OFF, VLEVEL_M,
147 // "Error: Unknown MicroSDCommand_ReturnCode (%d).\r\n",
148 // cmd.command.microsd_command.rc);
149 // break;
150 //}
151
152 return cmd.command.microsd_command.rc;
153}
154
155uint32_t ControllerMicroSDLog(const char* msg, const char* base) {
156 // get reference to tx and rx buffers
157 Buffer* tx = ControllerTx();
158 Buffer* rx = ControllerRx();
159
160 MicroSDCommand microsd_cmd = MicroSDCommand_init_zero;
161 microsd_cmd.type = MicroSDCommand_Type_LOG;
162
163 microsd_cmd.which_data = MicroSDCommand_log_tag;
164
165 // truncate rather than overflow, the field is a fixed size array
166 strncpy(microsd_cmd.data.log, msg, sizeof(microsd_cmd.data.log) - 1);
167 microsd_cmd.data.log[sizeof(microsd_cmd.data.log) - 1] = '\0';
168
169 // filename
170 strncpy(microsd_cmd.filename, base, sizeof(microsd_cmd.filename) - 1);
171 microsd_cmd.filename[sizeof(microsd_cmd.filename) - 1] = '\0';
172
173 // encode command
174 tx->len = EncodeMicroSDCommand(&microsd_cmd, tx->data, tx->size);
175
176 // return if communication fails
178 if (status != CONTROLLER_SUCCESS) {
179 return MicroSDCommand_ReturnCode_ERROR_GENERAL;
180 }
181
182 // check for errors
183 if (rx->len == 0) {
184 return MicroSDCommand_ReturnCode_ERROR_GENERAL;
185 }
186
187 // decode command
188 Esp32Command cmd = Esp32Command_init_default;
189 cmd = DecodeEsp32Command(rx->data, rx->len);
190
191 return cmd.command.microsd_command.rc;
192}
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 data[]
const String filename
ControllerStatus
@ CONTROLLER_SUCCESS
uint32_t ControllerMicroSDSave(const uint8_t *data, const uint16_t num_bytes)
Save data to the microSD card. Appends data to already-created files, otherwise creates a new file.
Definition microsd.c:12
uint32_t ControllerMicroSDUserConfig(UserConfiguration *uc, const char *filename)
Send the UserConfig to the ESP32 and saves a file on the microSD with the UserConfig contents.
Definition microsd.c:80
uint32_t ControllerMicroSDLog(const char *msg, const char *base)
Append a line of plain text to the log file on the microSD card.
Definition microsd.c:155
Esp32Command DecodeEsp32Command(const uint8_t *data, const size_t len)
Decodes an Esp32Command message.
Definition transcoder.c:246
size_t EncodeMicroSDCommand(const MicroSDCommand *microsd_cmd, uint8_t *buffer, size_t size)
Encodes a MicroSDCommand.
Definition transcoder.c:278
int DecodeMeasurement(Measurement *meas, const uint8_t *buffer, const size_t len)
Decodes a measurement.
Definition transcoder.c:235
uint32_t epoch(void)
Gets the current unix epoch timestamp.
Definition time.c:19
unsigned int g_controller_i2c_timeout
MicroSD interface implementation with the esp32.
size_t len
size_t size
uint8_t * data