main.c
8.04 KB
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
#include <string.h>
#include "libwismart.h"
#include "libwismart_irqs.h" /* implement irq handlers */
#include "lwip/inet.h"
#include "globals.h"
#include "httpClient.h"
#include "callbacks.h"
#include "buffer.h"
#include "i2c.h"
#define WIFI_MODE WIFI_MODE_CLIENT
#define NETWORK_SSID "linksys"
#define NETWORK_KEY ""
#define WPA_USER "smartcities"
#define WPA_PASS "superpass"
#define ENCRYPT_MODE "WPA Enterprise"
#define NETWORK_SSID_AP "linksysAP"
#define NETWORK_KEY_AP NULL
#define NETWORK_CHANNEL_AP 1
void initLibwismart(void)
{
wismart_hwif_t hwif = libwismart_GetDefaultHWIF();
libwismart_Init(hwif);
}
uint8_t connected=0;
uint8_t timeout=0;
char ssid[32]; // ssid clave red usuario contrasenya tipicript
char net_key[13];
char user[64];
char password[64];
char encrypt_type[64];
//Declare the registers
wismart_registryKey_t key_1;
wismart_registryKey_t key_2;
wismart_registryKey_t key_3;
wismart_registryKey_t key_4;
wismart_registryKey_t key_5;
void init_registry(void){
uint32_t retval;
/*
Inform registry module about the size of the variables we are going to work with.
NOTE: A flash erase should be perfomed every time:
1. The order with which these variables are created is changed
2. The size of one or more varaibles is changed
*/
retval = libwismart_RegistryCreateKey(&key_1, 1, sizeof(ssid));
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryCreateKey(1) Failed!!\r\n");
while(1);
}
retval = libwismart_RegistryCreateKey(&key_2, 1, sizeof(net_key));
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryCreateKey(2) Failed!!\r\n");
while(1);
}
retval = libwismart_RegistryCreateKey(&key_3, 1, sizeof(user));
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryCreateKey(3) Failed!!\r\n");
while(1);
}
retval = libwismart_RegistryCreateKey(&key_4, 1, sizeof(password));
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryCreateKey(4) Failed!!\r\n");
while(1);
}
retval = libwismart_RegistryCreateKey(&key_5, 1, sizeof(encrypt_type));
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryCreateKey(5) Failed!!\r\n");
while(1);
}
/*
Open the registry file. Now we can perform get/set operations
*/
retval = libwismart_RegistryOpen(1);
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryOpen() Failed!!\r\n");
while(1);
}
if(libwismart_RegistryIsValueEmpty(&key_1)){
/*
Init and save registry variable.1
*/
strcpy(ssid,NETWORK_SSID);
libwismart_RegistrySet(&key_1,&ssid);
/*
Init and save registry variable.2
*/
strcpy(user,WPA_USER);
libwismart_RegistrySet(&key_3, &user);
/*
Init and save registry variable.3
*/
strcpy(password,WPA_PASS);
libwismart_RegistrySet(&key_4, &password);
/*
Init and save registry variable.4
*/
strcpy(net_key,NETWORK_KEY);
libwismart_RegistrySet(&key_2, &net_key);
/*
Init and save registry variable.4
*/
strcpy(encrypt_type,ENCRYPT_MODE);
libwismart_RegistrySet(&key_5, &encrypt_type);
}
else{
printf("------------------------------------------\r\n");
printf("Registry values...\r\n");
printf("------------------------------------------\r\n");
/*
Restore registry variable.1
*/
retval = libwismart_RegistryGet(&key_1,&ssid);
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryGet(1) Failed!!\r\n");
while(1);
}
/*
Restore registry variable.2
*/
retval = libwismart_RegistryGet(&key_2,&net_key);
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryGet(2) Failed!!\r\n");
while(1);
}
/*
Restore registry variable.3
*/
retval = libwismart_RegistryGet(&key_3,&user);
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryGet(3) Failed!!\r\n");
while(1);
}
retval = libwismart_RegistryGet(&key_4,&password);
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryGet(4) Failed!!\r\n");
while(1);
}
retval = libwismart_RegistryGet(&key_5,&encrypt_type);
if(retval != WISMART_SUCCESS){
printf("libwismart_RegistryGet(5) Failed!!\r\n");
while(1);
}
/*
Print restored values:
*/
printf("SSID = %s\r\n",ssid);
printf("Net key = %s\r\n",net_key);
printf("User = %s\r\n",user);
printf("Password = %s\r\n",password);
printf("Encryption type = %s\r\n",encrypt_type);
}
}
int main(void)
{
initLibwismart();
uint32_t ind[4]={0};
char** buffers[4];
uint32_t sizes[4]={0};
int i;
int num_sensors;
struct wpa_param wpa;
wpa.eap_method = WISMART_EAP_METHOD_TTLS;
wpa.u.ttls.identity=WPA_USER;
wpa.u.ttls.password=WPA_PASS;
wpa.u.ttls.ca_cert=NULL;
struct httpHeaders head200 = { get, "/", 1, "http://www.w3.org/", 19 };
struct httpHeaders head301 = { get, "/", 1, "w3.org", 6 };
struct httpHeaders head404 = { get, "/errorerrorerror", 15, "www.w3.org" };
// init_registry();
libwismart_EnableBsdSocketAPI();
libwismart_PowerSave_Enable();
libwismart_PowerSave_HigherProfile(TRUE);
libwismart_RegisterDhcpCB(dhcp_connect_result_cb);
libwismart_WiFiInit();
libwismart_SetScanRunsForConnTimeout(4);
libwismart_WiFiConnectEnterprise(NETWORK_SSID, &wpa, wifi_connect_result_cb);
if(timeout==1){
printf("esta a timeout\r\n");
//corroborar los parametros del AP
libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,(unsigned char*)NETWORK_KEY_AP,wifi_connect_ap_result_cb,NULL);
}
//int httpRequest(struct httpHeaders head, char* content, int content_size)
//chThdSleepMilliseconds(5000);
//httpRequest(head200, NULL, 0);
/*httpRequest(head301, NULL, 0);
httpRequest(head404, NULL, 0);*/
// i2c scans the sensors active and returns how many thay are
// num_sensors value
printf("waiting\r\n");
chThdSleepMilliseconds(10000);
for(;;)
{
// i2c gets the data and combines it with the time stamp
char* data="message,0";
char* data2="message,1";
char* data3="message,2";
for(i=0;i<4;i++){
printf("------------------BUFFER %d ----------------------\r\n",i);
buffers[i]=put_message(data, buffers[i] ,&ind[i],&sizes[i]);
buffers[i]=put_message(data2, buffers[i] ,&ind[i],&sizes[i]);
buffers[i]=put_message(data3, buffers[i] ,&ind[i],&sizes[i]);
printf("mirant memoria\r\n");
int res=check_memory();
if(res==SOFT_REACHED){
printf("--------------soft limit-------------\r\n");
int ok=send(buffers[i],&ind[i],&sizes[i], "", "");
if(ok==JSON_COMM_ERROR)
{
printf("wismart is not connected\r\n");
}
else if( ok==JSON_OTHER_ERROR){
printf("some error ocurred\r\n");
}
else if(ok ==JSON_POST_OK){
printf(" send OK \r\n");
}
}
else if(res==HARD_REACHED){
destroy(buffers);
}
}
chThdSleepMilliseconds(500);
}
}