ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
wifi_userconfig.cpp
Go to the documentation of this file.
2
3#include <nanopb/pb_decode.h>
4#include <nanopb/pb_encode.h>
5#include <string.h>
6
7#include "config_server.hpp"
8#include "configuration.hpp"
9
10namespace ModuleHandler {
11
13 this->type = Esp32Command_user_config_command_tag;
14 this->state = 0;
15
16 UserConfiguration default_config = UserConfiguration_init_default;
17 default_config.Upload_interval = 10;
18 strncpy(default_config.API_Endpoint_URL,
19 "http://dirtviz.jlab.ucsc.edu/api/sensor/",
20 sizeof(default_config.API_Endpoint_URL));
21 setConfig(default_config);
22}
23
24void ModuleUserConfig::OnReceive(const Esp32Command& cmd) {
25 Log.traceln("ModuleUserConfig::OnReceive");
26
27 // Check which command
28 if (cmd.which_command != Esp32Command_user_config_command_tag) {
29 Log.errorln("Wrong command type received");
30 return;
31 }
32
33 switch (cmd.command.user_config_command.type) {
34 case UserConfigCommand_RequestType_REQUEST_CONFIG:
35 Log.traceln("Received config request");
36 requestConfig(cmd.command.user_config_command);
37 break;
38
39 case UserConfigCommand_RequestType_RESPONSE_CONFIG:
40 Log.traceln("Received config response");
41 responseConfig(cmd.command.user_config_command);
42 break;
43
44 case UserConfigCommand_RequestType_START:
45 Log.traceln("Received start command");
46 start();
47 break;
48
49 default:
50 Log.errorln("Unknown user config command type: %d",
51 cmd.command.user_config_command.type);
52 return;
53 }
54}
55
57 Log.traceln("ModuleUserConfig::OnRequest");
58 memcpy(buffer, this->buffer, buffer_len);
59 return buffer_len;
60}
61
62void ModuleUserConfig::requestConfig(const UserConfigCommand& cmd) {
63 Log.noticeln(" ============ Received Config Request ============");
64 Log.noticeln(" STM32 is requesting current configuration");
65 // Log.noticeln("============ Sending Configuration ============");
66 // Log.noticeln(" Preparing to send current config to STM32");
67
68 Esp32Command response = {0};
69 response.which_command = Esp32Command_user_config_command_tag;
70 response.command.user_config_command.type =
71 UserConfigCommand_RequestType_RESPONSE_CONFIG;
72 response.command.user_config_command.has_config_data = true;
73
74 const UserConfiguration config = getConfig();
75
76 memcpy(&response.command.user_config_command.config_data, &config,
77 sizeof(UserConfiguration));
78
79 pb_ostream_t ostream = pb_ostream_from_buffer(buffer, Esp32Command_size);
80 if (!pb_encode(&ostream, Esp32Command_fields, &response)) {
81 Log.errorln("Failed to encode response");
82 buffer_len = 0;
83 }
84
85 buffer_len = ostream.bytes_written;
86
87 Log.noticeln("Successfully encoded configuration (%d bytes)", buffer_len);
88}
89
90void ModuleUserConfig::responseConfig(const UserConfigCommand& cmd) {
91 Log.noticeln(" ============ Received New Configuration ============");
92
93 setConfig(cmd.config_data);
94 // updateWebConfig(&current_config_);
96}
97
98void ModuleUserConfig::start() {
99 Log.noticeln("Starting WiFi User Config module");
100
101 setupServer();
102 Log.noticeln("HTTP server started");
103
104 Esp32Command response = Esp32Command_init_zero;
105 response.which_command = Esp32Command_user_config_command_tag;
106 response.command.user_config_command.type =
107 UserConfigCommand_RequestType_START;
108
109 pb_ostream_t ostream = pb_ostream_from_buffer(buffer, Esp32Command_size);
110 if (!pb_encode(&ostream, Esp32Command_fields, &response)) {
111 Log.errorln("Failed to encode response");
112 buffer_len = 0;
113 ;
114 }
115
116 buffer_len = ostream.bytes_written;
117}
118} // namespace ModuleHandler
void OnReceive(const Esp32Command &cmd) override
Arduino I2C onRecieve.
size_t OnRequest(uint8_t *buffer) override
Arduino I2C onRequest.
Class prototype for modules.
void setupServer()
Setup webserver.
void printReceivedConfig()
Print the current user configuration to the log.
void setConfig(const UserConfiguration &new_config)
Set the user configuration.
const UserConfiguration & getConfig()
Get a copy of the current user configuration.
uint8_t buffer[32]
Power module for the esp32.