SingingCat 0
application
powerup.c
1#include "main-header.h"
2#include "boot_info.h"
3#include "powerup.h"
4#include "user_app_exe.h"
5
6static byte power_up_msg;
7static byte msg_active;
8static long power_up_sent;
9static byte delctr;
10void powerup_init() {
11 delctr = 0;
12 power_up_msg = 0;
13 power_up_sent = 0;
14 msg_active = 0;
15}
16int powerup_delivered() {
17 return power_up_msg;
18}
19int powerup_status() {
20 return msg_active << 1 | power_up_msg;
21}
22
23
24static void power_callback(struct command *com, struct command *reply) {
25 msg_active = 0;
26 if (reply == NULL) {
27 return;
28 }
29 printf("Powerup message delivered.\r\n");
30 power_up_msg = 1;
31
32 // trigger some other stuff that needs to wait for server to be available
33 send_user_app_status();
34}
35void check_power_on_loop() {
36 if (power_up_msg) {
37 return;
38 }
39 if (!(mculib_has_time_passed(10, &power_up_sent))) {
40 return;
41 }
42 delctr++;
43 if ((delctr > 6) && (msg_active)) {
44 msg_active = 0;
45 printf("powerup - cleared msgactive for retry\r\n");
46 }
47 send_power_up(POWERUP_LOOP);//feels wrong
48}
49// send powerup message now, no matter if we sent it before
50void send_power_up_now(int reason) {
51 delctr = 0;
52 struct command *com;
53
55 if (com == NULL) {
56 printf("No command space for powerup!!\r\n");
57 return;
58 }
59 com->com = 37; //powerup
60 com->target = CLOUD_SERVER;
61 int rc = loader_get_last_reset_cause();
62
63 if (rc == 0) {
64 command_add_arg(com, "rc=0"); // pointer rendered to (nil)
65 } else {
66 command_add_arg(com, "rc=%p", rc);
67 }
68 if (config_get_flag(CONFIG_FLAGS_POWER_SAVE)) {
69 command_add_arg(com, "ps=1");
70 } else {
71 command_add_arg(com, "ps=0");
72 }
73 command_add_arg(com, "pur=%i", reason);
74 msg_active = 1;
75 rc = deliver_command(com, &power_callback);
76 if (rc != 0) {
77 msg_active = 0;
78 printf("powerup msg failed to queue: %i\r\n", rc);
79 }
80}
81
82// send power up message if necessary
83void send_power_up(int reason) {
84 if (msg_active) {
85 // we still got one in the send queue
86 return;
87 }
88 send_power_up_now(reason);
89}
int deliver_command(struct command *com, pkt_callback)
deliver a command to a module
Definition: queue.c:651
void command_add_arg(struct command *com, const char *format,...)
adds an arg to a partially initialised command structure
struct command * alloc_command()
allocate a free command
Definition: queue.c:173
int com
Definition: command.h:22