SingingCat 0
application
esp8266_cloud.c
1#include "main-header.h"
2#include "loader-api.h"
3#include "addressing.h"
4#include "routing.h"
5#include "hijack.h"
6#include "command-handler.h"
7#include "espressif/esp8266_cloud.h"
8#include "wireless_state_machine.h"
9#include "sc_time.h"
10// #include "bare-metal.h"
11
16extern long wifi_state_since;
17static byte cloud_activated;
21static int last_cloud_event_loop;
29static byte cloud_connect_state;
30
34static long last_cloud_probe;
35/*
36 * \brief set to NOW() each time we receive a probe reply from the server
37 */
38static long last_cloud_probe_answer;
44#define SECS_BETWEEN_CLOUD_PROBES 300
48#define SECS_UNTIL_CONNECTION_DEAD (SECS_BETWEEN_CLOUD_PROBES * 4)
49
50//ic void esp_cloud_answered(struct command *com, struct command *reply);
51static void esp_cloud_initiate_connection();
53
58 last_cloud_probe_answer = 0;
59 cloud_activated = 0;
60 last_cloud_event_loop = 0;
61 cloud_connect_state = 0;
62 last_cloud_probe = 0;
63}
64
69 const char *c;
70
71 c = config_get_cloud_server();
72 if ((c == NULL) || (c[0] == 0)) {
73 return "module-in.singingcat.net";
74 } else {
75 return c;
76 }
77}
82void esp_cloud_state_update(int code, int data) {
83 printf("SUBSYSTEM: Cloud Code: ");
84 if (code == STATE_CLOUD_RECONNECT) {
85 printf("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) {
93 printf("Connected");
94 } else if (code == STATE_CLOUD_CONNECTFAILURE) {
95 printf("Connect Failure: %i", data);
96 } else {
97 printf("Weird code: cloud update %i", code);
98 }
99 printf("\r\n");
100}
101
102static void check_announce() {
103 if (is_hijacked_esp32()) {
104 return;
105 }
106 long now = mculib_get_seconds_since_boot();
107
108 if ((now - last_cloud_probe_answer) > SECS_UNTIL_CONNECTION_DEAD) {
110 }
111 if (last_cloud_probe > now) {
112 last_cloud_probe = now;
113 }
114 if ((now - last_cloud_probe) < SECS_BETWEEN_CLOUD_PROBES) {
115 return;
116 }
117 esp_cloud_initiate_connection();
118 last_cloud_probe = now;
119}
124 if (is_hijacked_esp32()) {
125 return;
126 }
127 check_announce();
128}
129
138 if (is_hijacked_esp32()) {
139 return;
140 }
141 if (cloud_activated) {
142 return;
143 }
144 cloud_activated = 1;
145 esp_cloud_initiate_connection();
146 long now = mculib_get_seconds_since_boot();
147
148 last_cloud_probe = now;
149 last_cloud_probe_answer = now;
150 printf("Cloud connection activated\r\n");
151}
152void esp_cloud_deactivate() {
153 if (is_hijacked_esp32()) {
154 return;
155 }
156 if (!cloud_activated) {
157 return;
158 }
159 cloud_activated = 0;
160 cloud_connect_state = 0;
161 printf("Cloud connection deactivated\r\n");
162 send_routing_update_now();
163}
168 if (is_hijacked_esp32()) {
169 return 0;
170 }
171 return cloud_activated ? 1 : 0;
172}
173int esp_cloud_is_connected() {
174 if (is_hijacked_esp32()) {
175 return 0;
176 }
177 return cloud_connect_state == 2 ? 1 : 0;
178}
179
180
181static void esp_cloud_initiate_connection() {
182 if (is_hijacked_esp32()) {
183 return;
184 }
185 struct command *com;
186
187 com = alloc_command();
188 if (com == NULL) {
189 printf("no command available on cloud connected.\r\n");
190 return;
191 }
192 if (cloud_connect_state == 0) {
193 cloud_connect_state = 1;
194 }
195 com->sourcedev = SOURCE_WIFI;
196 com->com = 3;
197 com->recipient = CLOUD_SERVER;
198 com->target = CLOUD_SERVER;
199 com->local_flags = (1 << COM_LOCAL_FLAG_FORCE_DEVICE); // wether or not state of esp8266 is ready, this command MUST be sent out via wifi only
200 // deliverying to cloud via wifi only, special case, add local time information
201 time_add_debug_to_command(com);
203}
207void esp_cloud_answered(struct command *com, struct command *reply) {
208 if (reply == NULL) {
209 printf("No answer from cloud\r\n");
210 } else {
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;
215 if (lc != 2) {
216 // first reply from cloud -> announce new routes
217 send_routing_update_now();
218 }
219 }
220}
221
222const char *esp_cloud_connect_state_string() {
223 if (cloud_connect_state == 0) {
224 return "initializing";
225 } else if (cloud_connect_state == 1) {
226 return "connecting";
227 } else if (cloud_connect_state == 2) {
228 return "connected";
229 } else {
230 return "?";
231 }
232}
233
234void esp_info() {
235 long now = mculib_get_seconds_since_boot();
236
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);
240 printf("cloudhost : %s\r\n", esp_cloud_get_desired_server());
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();
252 //esp8266_decoder_print_info();
253 printf("====== end esp8266 info ========\r\n");
254}
void esp8266_cloud_loop()
called from esp8266 event loop if we are connected
void esp_cloud_init()
this resets the cloud state
Definition: esp8266_cloud.c:57
int deliver_command(struct command *com, pkt_callback)
deliver a command to a module
Definition: queue.c:651
#define SECS_UNTIL_CONNECTION_DEAD
if we don't receive an answer for this long (in seconds) assume connection is dead
Definition: esp8266_cloud.c:48
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 ...
Definition: esp8266_cloud.c:82
const char * esp_cloud_get_desired_server()
returns server we want to connect to
Definition: esp8266_cloud.c:68
#define SECS_BETWEEN_CLOUD_PROBES
how often to probe the cloud connection
Definition: esp8266_cloud.c:44
struct command * alloc_command()
allocate a free command
Definition: queue.c:173
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
Definition: esp8266.c:1661
definitions of routing table structures
int com
Definition: command.h:22