-
Notifications
You must be signed in to change notification settings - Fork 1
/
application_manager.c
327 lines (279 loc) · 10.8 KB
/
application_manager.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
\file application_manager.c
\brief Application Manager source file.
(c) 2018 Microchip Technology Inc. and its subsidiaries.
Subject to your compliance with these terms, you may use Microchip software and any
derivatives exclusively with Microchip products. It is your responsibility to comply with third party
license terms applicable to your use of third party software (including open source software) that
may accompany Microchip software.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
FOR A PARTICULAR PURPOSE.
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
SOFTWARE.
*/
#include <string.h>
#include <time.h>
#include <stdio.h>
#include "utils/atomic.h"
#include <avr/wdt.h>
#include<avr/interrupt.h>
#include<avr/io.h>
#include "include/pin_manager.h"
#include "application_manager.h"
#include "mcc.h"
#include "config/IoT_Sensor_Node_config.h"
#include "config/conf_winc.h"
#include "config/mqtt_config.h"
#include "config/cloud_config.h"
#include "cloud/cloud_service.h"
#include "cloud/mqtt_service.h"
#include "cloud/crypto_client/crypto_client.h"
#include "cloud/wifi_service.h"
#include "CryptoAuth_init.h"
#include "../mcc_generated_files/sensors_handling.h"
#include "credentials_storage/credentials_storage.h"
#include "led.h"
#include "debug_print.h"
#include "time_service.h"
#if CFG_ENABLE_CLI
#include "cli/cli.h"
#endif
#define MAIN_DATATASK_INTERVAL 1000L
// The debounce time is currently close to 2 Seconds.
#define AWS_MCHP_SANDBOX_URL "xxxxxxxxx.iot.xxxxxxxxxxx.amazonaws.com"
// This will contain the device ID, before we have it this dummy value is the init value which is non-0
char attDeviceID[20] = "BAAAAADD1DBAAADD1D";
char mqttSubscribeTopic[SUBSCRIBE_TOPIC_SIZE];
ATCA_STATUS retValCryptoClientSerialNumber;
static uint8_t holdCount = 0;
#define TOGGLE_ON 1
#define TOGGLE_OFF 0
uint32_t MAIN_dataTask(void *payload);
timerStruct_t MAIN_dataTasksTimer = {MAIN_dataTask};
int adc_result = 0;
int array[42];
int adc_counter = 0;
char buf[128];
#define ADC_CHANNEL 7
static void wifiConnectionStateChanged(uint8_t status);
static void sendToCloud(void);
static void subscribeToCloud(void);
static void receivedFromCloud(uint8_t *topic, uint8_t *payload);
uint32_t initDeviceShadow(void *payload);
timerStruct_t initDeviceShadowTimer = {initDeviceShadow};
void loadDefaultAWSEndpoint(void);
#define PERIOD_EXAMPLE_VALUE (300) // 150 HZ min sample rate AHA https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjs1K-4iM3tAhUQXKwKHcglAncQFjABegQIBBAC&url=https%3A%2F%2Fwww.ahajournals.org%2Fdoi%2Fpdf%2F10.1161%2Fhc5001.101063%23%3A~%3Atext%3DThe%2520American%2520Heart%2520Association%2520(AHA%2Cthe%2520limitations%2520of%2520previous%2520studies.&usg=AOvVaw38JqixTbKeuD7eR6_EVrIB
void TCA0_init(void);
void free_running() {
while (1) {
if (ADC0_IsConversionDone()) {
if (adc_counter < sizeof array / sizeof array[0]) {
array[adc_counter] = ADC0.RES;
adc_counter++;
}
break;
}
}
}
ISR(TCA0_OVF_vect) {
free_running();
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm;
}
// This will get called every 1 second only while we have a valid Cloud connection
static void sendToCloud(void) {
static char json[PAYLOAD_SIZE];
static char publishMqttTopic[PUBLISH_TOPIC_SIZE];
int len = 0;
memset((void*) publishMqttTopic, 0, sizeof (publishMqttTopic));
sprintf(publishMqttTopic, "%s/sensors", cid);
// This part runs every CFG_SEND_INTERVAL seconds
if (shared_networking_params.haveAPConnection) {
int size = sizeof array / sizeof array[0];
char *pos = buf;
for (int i = 0; i != adc_counter; i++) {
if (i) {
pos += sprintf(pos, ",");
}
pos += sprintf(pos, "%d", array[i]);
}
len = sprintf(json, "{\"Data\":%s}",buf);
adc_counter = 0;
}
if (len > 0) {
CLOUD_publishData((uint8_t*) publishMqttTopic, (uint8_t*) json, len);
if (holdCount) {
holdCount--;
} else {
ledParameterYellow.onTime = LED_BLIP;
ledParameterYellow.offTime = LED_BLIP;
LED_control(&ledParameterYellow);
}
}
}
//This handles messages published from the MQTT server when subscribed
static void receivedFromCloud(uint8_t *topic, uint8_t *payload) {
char *toggleToken = "\"toggle\":";
char *subString;
sprintf(mqttSubscribeTopic, "$aws/things/%s/shadow/update/delta", cid);
if (strncmp((void*) mqttSubscribeTopic, (void*) topic, strlen(mqttSubscribeTopic)) == 0) {
if ((subString = strstr((char*) payload, toggleToken))) {
if (subString[strlen(toggleToken)] == '1') {
ledParameterYellow.onTime = SOLID_ON;
ledParameterYellow.offTime = SOLID_OFF;
LED_control(&ledParameterYellow);
} else {
ledParameterYellow.onTime = SOLID_OFF;
ledParameterYellow.offTime = SOLID_ON;
LED_control(&ledParameterYellow);
}
holdCount = 2;
}
}
debug_printIoTAppMsg("topic: %s", topic);
debug_printIoTAppMsg("payload: %s", payload);
}
void application_init(void) {
uint8_t mode = WIFI_DEFAULT;
// Initialization of modules before interrupts are enabled
SYSTEM_Initialize();
LED_test();
#if CFG_ENABLE_CLI
CLI_init();
CLI_setdeviceId(attDeviceID);
#endif
debug_init(attDeviceID);
// Initialization of modules where the init needs interrupts to be enabled
if (!CryptoAuth_Initialize()) {
debug_printError("APP: CryptoAuthInit failed");
shared_networking_params.haveError = 1;
}
// Get serial number from the ECC608 chip
retValCryptoClientSerialNumber = CRYPTO_CLIENT_printSerialNumber(attDeviceID);
if (retValCryptoClientSerialNumber != ATCA_SUCCESS) {
shared_networking_params.haveError = 1;
switch (retValCryptoClientSerialNumber) {
case ATCA_GEN_FAIL:
debug_printError("APP: DeviceID generation failed, unspecified error");
break;
case ATCA_BAD_PARAM:
debug_printError("APP: DeviceID generation failed, bad argument");
default:
debug_printError("APP: DeviceID generation failed");
break;
}
}
#if CFG_ENABLE_CLI
CLI_setdeviceId(attDeviceID);
#endif
debug_setPrefix(attDeviceID);
loadDefaultAWSEndpoint();
wifi_readThingNameFromWinc();
wifi_init(wifiConnectionStateChanged, mode);
if (mode == WIFI_DEFAULT) {
CLOUD_setupTask(attDeviceID);
timeout_create(&MAIN_dataTasksTimer, MAIN_DATATASK_INTERVAL);
}
LED_test();
subscribeToCloud();
ADC0.CTRLA |= 1 << ADC_FREERUN_bp;
ADC0_StartConversion(ADC_CHANNEL);
TCA0_init();
sei(); // Enable global interrupts by setting global interrupt enable bit in SREG
}
static void subscribeToCloud(void) {
sprintf(mqttSubscribeTopic, "$aws/things/%s/shadow/update/delta", cid);
CLOUD_registerSubscription((uint8_t*) mqttSubscribeTopic, receivedFromCloud);
}
#if USE_CUSTOM_ENDPOINT_URL
void loadCustomAWSEndpoint(void) {
memset(awsEndpoint, '\0', AWS_ENDPOINT_LEN);
sprintf(awsEndpoint, "%s", CFG_MQTT_HOSTURL);
debug_printIoTAppMsg("Custom AWS Endpoint is used : %s", awsEndpoint);
}
#else
void loadDefaultAWSEndpoint(void) {
memset(awsEndpoint, '\0', AWS_ENDPOINT_LEN);
wifi_readAWSEndpointFromWinc();
if (awsEndpoint[0] == 0xFF) {
sprintf(awsEndpoint, "%s", AWS_MCHP_SANDBOX_URL);
debug_printIoTAppMsg("Using the AWS Sandbox endpoint : %s", awsEndpoint);
}
}
#endif
// This scheduler will check all tasks and timers that are due and service them
void runScheduler(void) {
timeout_callNextCallback();
}
// This gets called by the scheduler approximately every 100ms
uint32_t MAIN_dataTask(void *payload) {
if (CLOUD_checkIsConnected()) {
sendToCloud();
} else {
ledParameterYellow.onTime = SOLID_OFF;
ledParameterYellow.offTime = SOLID_ON;
LED_control(&ledParameterYellow);
}
if (!shared_networking_params.amConnectingAP) {
if (shared_networking_params.haveAPConnection) {
ledParameterBlue.onTime = SOLID_ON;
ledParameterBlue.offTime = SOLID_OFF;
LED_control(&ledParameterBlue);
}
// Green LED if we are in Access Point
if (!shared_networking_params.amConnectingSocket) {
if (CLOUD_checkIsConnected()) {
ledParameterGreen.onTime = SOLID_ON;
ledParameterGreen.offTime = SOLID_OFF;
LED_control(&ledParameterGreen);
} else if (shared_networking_params.haveDataConnection == 1) {
ledParameterGreen.onTime = LED_BLINK;
ledParameterGreen.offTime = LED_BLINK;
LED_control(&ledParameterGreen);
}
}
}
// RED LED
if (shared_networking_params.haveError) {
ledParameterRed.onTime = SOLID_ON;
ledParameterRed.offTime = SOLID_OFF;
LED_control(&ledParameterRed);
} else {
ledParameterRed.onTime = SOLID_OFF;
ledParameterRed.offTime = SOLID_ON;
LED_control(&ledParameterRed);
}
// This is milliseconds managed by the RTC and the scheduler, this return
// makes the timer run another time, returning 0 will make it stop
return MAIN_DATATASK_INTERVAL;
}
void application_post_provisioning(void) {
CLOUD_setupTask(attDeviceID);
timeout_create(&MAIN_dataTasksTimer, MAIN_DATATASK_INTERVAL);
}
// React to the WIFI state change here. Status of 1 means connected, Status of 0 means disconnected
static void wifiConnectionStateChanged(uint8_t status) {
// If we have no AP access we want to retry
if (status != 1) {
CLOUD_reset();
}
}
void TCA0_init(void) {
/* enable overflow interrupt */
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm;
/* set Normal mode */
TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc;
/* disable event counting */
TCA0.SINGLE.EVCTRL &= ~(TCA_SINGLE_CNTEI_bm);
/* set the period */
TCA0.SINGLE.PER = PERIOD_EXAMPLE_VALUE;
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1024_gc /* set clock source (sys_clk/1024) */
| TCA_SINGLE_ENABLE_bm; /* start timer */
}