1#include "main-header.h"
6#include "command-handler.h"
7#include "espressif/esp8266_cloud.h"
8#include "wireless_state_machine.h"
16extern long wifi_state_since;
17static byte cloud_activated;
21static int last_cloud_event_loop;
29static byte cloud_connect_state;
34static long last_cloud_probe;
38static long last_cloud_probe_answer;
44#define SECS_BETWEEN_CLOUD_PROBES 300
48#define SECS_UNTIL_CONNECTION_DEAD (SECS_BETWEEN_CLOUD_PROBES * 4)
51static void esp_cloud_initiate_connection();
58 last_cloud_probe_answer = 0;
60 last_cloud_event_loop = 0;
61 cloud_connect_state = 0;
71 c = config_get_cloud_server();
72 if ((c == NULL) || (c[0] == 0)) {
73 return "module-in.singingcat.net";
83 printf(
"SUBSYSTEM: Cloud Code: ");
84 if (code == STATE_CLOUD_RECONNECT) {
86 }
else if (code == STATE_CLOUD_AUTORECONNECT) {
87 printf(
"Autoreconnect");
88 }
else if (code == STATE_CLOUD_RECONNECTED) {
89 printf(
"Reconnected");
90 }
else if (code == STATE_CLOUD_DISCONNECTED) {
91 printf(
"Disconnected");
92 }
else if (code == STATE_CLOUD_CONNECTED) {
94 }
else if (code == STATE_CLOUD_CONNECTFAILURE) {
95 printf(
"Connect Failure: %i", data);
97 printf(
"Weird code: cloud update %i", code);
102static void check_announce() {
103 if (is_hijacked_esp32()) {
106 long now = mculib_get_seconds_since_boot();
111 if (last_cloud_probe > now) {
112 last_cloud_probe = now;
117 esp_cloud_initiate_connection();
118 last_cloud_probe = now;
124 if (is_hijacked_esp32()) {
138 if (is_hijacked_esp32()) {
141 if (cloud_activated) {
145 esp_cloud_initiate_connection();
146 long now = mculib_get_seconds_since_boot();
148 last_cloud_probe = now;
149 last_cloud_probe_answer = now;
150 printf(
"Cloud connection activated\r\n");
152void esp_cloud_deactivate() {
153 if (is_hijacked_esp32()) {
156 if (!cloud_activated) {
160 cloud_connect_state = 0;
161 printf(
"Cloud connection deactivated\r\n");
162 send_routing_update_now();
168 if (is_hijacked_esp32()) {
171 return cloud_activated ? 1 : 0;
173int esp_cloud_is_connected() {
174 if (is_hijacked_esp32()) {
177 return cloud_connect_state == 2 ? 1 : 0;
181static void esp_cloud_initiate_connection() {
182 if (is_hijacked_esp32()) {
189 printf(
"no command available on cloud connected.\r\n");
192 if (cloud_connect_state == 0) {
193 cloud_connect_state = 1;
195 com->sourcedev = SOURCE_WIFI;
197 com->recipient = CLOUD_SERVER;
198 com->target = CLOUD_SERVER;
199 com->local_flags = (1 << COM_LOCAL_FLAG_FORCE_DEVICE);
201 time_add_debug_to_command(
com);
209 printf(
"No answer from cloud\r\n");
211 last_cloud_probe_answer = mculib_get_seconds_since_boot();
212 printf(
"Cloud answered ;-) yeah.\r\n");
213 int lc = cloud_connect_state;
214 cloud_connect_state = 2;
217 send_routing_update_now();
222const char *esp_cloud_connect_state_string() {
223 if (cloud_connect_state == 0) {
224 return "initializing";
225 }
else if (cloud_connect_state == 1) {
227 }
else if (cloud_connect_state == 2) {
235 long now = mculib_get_seconds_since_boot();
237 printf(
"====== esp8266 info ========\r\n");
238 printf(
"Last ssid : %s\r\n", esp_get_last_ssid());
239 printf(
"Time since boot : %D\r\n", now);
241 printf(
"wifi state : %s\r\n", wireless_state_to_string(wireless_get_current_state()));
242 printf(
"wifi state timeout : %i (%i,%i)\r\n", wireless_get_current_timeout(), wifi_state_since, (now - wifi_state_since));
243 printf(
"cloud_connect_state : %x (%s)\r\n", cloud_connect_state, esp_cloud_connect_state_string());
244 printf(
"cloud_activated : %x\r\n", cloud_activated);
245 printf(
"cloud token : %s\r\n", config_get_cloud_token());
246 printf(
"ESP-serial IRQ Mode : %i\r\n", get_esp8266_usart_mode());
247 printf(
"last cloud ping sent : %D (%Ds ago)\r\n", last_cloud_probe, (now - last_cloud_probe));
248 printf(
"last cloud ping received : %D (%Ds ago)\r\n", last_cloud_probe_answer, (now - last_cloud_probe_answer));
249 printf(
"# of valid commands received : %D\r\n", esp8266_get_valid_commands());
250 printf(
"# of invalid commands recv'd : %D\r\n", esp8266_get_invalid_commands());
251 print_esp_core_info();
253 printf(
"====== end esp8266 info ========\r\n");
void esp8266_cloud_loop()
called from esp8266 event loop if we are connected
void esp_cloud_init()
this resets the cloud state
int deliver_command(struct command *com, pkt_callback)
deliver a command to a module
#define SECS_UNTIL_CONNECTION_DEAD
if we don't receive an answer for this long (in seconds) assume connection is dead
void esp_cloud_activate()
activate the cloud.
byte esp_cloud_is_activated()
return 1 if cloud connection is activated
void esp_cloud_state_update(int code, int data)
whenever the esp chip sends us an update about the cloud state this gets called by the event handler ...
const char * esp_cloud_get_desired_server()
returns server we want to connect to
#define SECS_BETWEEN_CLOUD_PROBES
how often to probe the cloud connection
struct command * alloc_command()
allocate a free command
void esp_cloud_answered(struct command *com, struct command *reply)
called on command timeout or reply
void esp8266_cloud_connection_failed()
called if the cloud connection failed
definitions of routing table structures