ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
power.c
Go to the documentation of this file.
1#include "power.h"
2
3#include "../../proto/transcoder.h"
4#include "../communication.h"
5
6ControllerStatus PowerCommandTransaction(const PowerCommand* input,
7 PowerCommand* output);
8
9ControllerStatus PowerCommandTransaction(const PowerCommand* input,
10 PowerCommand* output) {
11 // get reference to tx and rx buffers
12 Buffer* tx = ControllerTx();
13 Buffer* rx = ControllerRx();
14
15 // encode command
16 tx->len = EncodePowerCommand(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(PowerCommand));
36
37 return CONTROLLER_SUCCESS;
38}
39
41 // format input command
42 PowerCommand power_cmd = PowerCommand_init_zero;
43 power_cmd.type = PowerCommand_Type_SLEEP;
44
45 PowerCommand resp = PowerCommand_init_zero;
46
47 if (PowerCommandTransaction(&power_cmd, &resp) != CONTROLLER_SUCCESS) {
48 return false;
49 }
50
51 return true;
52}
53
55 PowerCommand power_cmd = PowerCommand_init_zero;
56 power_cmd.type = PowerCommand_Type_WAKEUP;
57
58 PowerCommand resp = PowerCommand_init_zero;
59
60 if (PowerCommandTransaction(&power_cmd, &resp) != CONTROLLER_SUCCESS) {
61 return false;
62 }
63
64 // NOTE can handle boot number/reason from resp if needed
65
66 return true;
67}
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_ERROR
@ CONTROLLER_SUCCESS
size_t EncodePowerCommand(const PowerCommand *power_cmd, uint8_t *buffer, size_t size)
Encodes a PowerCommand.
Definition transcoder.c:335
Esp32Command DecodeEsp32Command(const uint8_t *data, const size_t len)
Decodes an Esp32Command message.
Definition transcoder.c:246
ControllerStatus PowerCommandTransaction(const PowerCommand *input, PowerCommand *output)
Definition power.c:9
bool ControllerPowerWakeup(void)
Definition power.c:54
bool ControllerPowerSleep(void)
Definition power.c:40
Power interface for the esp32.
size_t len
size_t size
uint8_t * data