Commit c1dff015805a8c476d1573a2c653ef7a5c8fd1e9

Authored by Maria Jorba Brosa
1 parent 49514ec4

--no commit message

Project/applications/smartcities/callbacks.c
@@ -22,5 +22,38 @@ void dhcp_connect_result_cb(int result) @@ -22,5 +22,38 @@ void dhcp_connect_result_cb(int result)
22 22
23 void wifi_connect_result_cb(int result) 23 void wifi_connect_result_cb(int result)
24 { 24 {
  25 + if(result==WISMART_WIFI_CONN_TIMEOUT){
  26 + timeout=1;
  27 + }
25 printf("WiFi Connect indication: %s\r\n", (result == WISMART_WIFI_CONNECTED) ? "Connected": "Failed\r\n"); 28 printf("WiFi Connect indication: %s\r\n", (result == WISMART_WIFI_CONNECTED) ? "Connected": "Failed\r\n");
26 -}  
27 \ No newline at end of file 29 \ No newline at end of file
  30 +}
  31 +
  32 +void wismart_softap_clients_cb(wismart_softap_cb_t reason, const uint8_t *mac, const libwismart_ip_addr_t *ip)
  33 +{
  34 + #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
  35 +#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
  36 +
  37 + switch(reason){
  38 + case WISMART_WIFI_AP_CLIENT_CONNECTED:
  39 + printf("Client: "MACSTR" connected\r\n",MAC2STR(mac));
  40 + break;
  41 + case WISMART_WIFI_AP_CLIENT_DISCONNECTED:
  42 + printf("Client: "MACSTR" disconnected\r\n",MAC2STR(mac));
  43 + break;
  44 + case WISMART_WIFI_AP_CLIENT_EXPIRED:
  45 + printf("Client: "MACSTR" connection have expired\r\n",MAC2STR(mac));
  46 + break;
  47 + case WISMART_WIFI_AP_CLIENT_GET_IP:
  48 + printf("Client: "MACSTR" got ip: %s\r\n", MAC2STR(mac), inet_ntoa(*ip));
  49 + break;
  50 + }
  51 +
  52 +#undef MAC2STR
  53 +#undef MACSTR
  54 +}
  55 +
  56 +void wifi_connect_ap_result_cb(int result)
  57 +{
  58 +
  59 + printf("New WiFi Network state: %s\r\n", (result == WISMART_WIFI_CONNECTED) ? "Created": "Failed\r\n");
  60 +}
Project/applications/smartcities/main.c
@@ -11,7 +11,11 @@ @@ -11,7 +11,11 @@
11 #define NETWORK_SSID "linksys" 11 #define NETWORK_SSID "linksys"
12 #define NETWORK_KEY "" 12 #define NETWORK_KEY ""
13 #define WPA_USER "smartcities" 13 #define WPA_USER "smartcities"
14 -#define WPA_PASS "superpass" 14 +#define WPA_PASS "superpass"
  15 +#define ENCRYPT_MODE "WPA Enterprise"
  16 +#define NETWORK_SSID_AP "linksysAP"
  17 +#define NETWORK_KEY_AP NULL
  18 +#define NETWORK_CHANNEL_AP 1
15 19
16 void initLibwismart(void) 20 void initLibwismart(void)
17 { 21 {
@@ -19,12 +23,168 @@ void initLibwismart(void) @@ -19,12 +23,168 @@ void initLibwismart(void)
19 libwismart_Init(hwif); 23 libwismart_Init(hwif);
20 } 24 }
21 25
  26 +uint8_t connected=0;
  27 +uint8_t timeout=0;
  28 +char ssid[32]; // ssid clave red usuario contrasenya tipicript
  29 +char net_key[13];
  30 +char user[64];
  31 +char password[64];
  32 +char encrypt_type[64];
  33 +//Declare the registers
  34 +
  35 + wismart_registryKey_t key_1;
  36 + wismart_registryKey_t key_2;
  37 + wismart_registryKey_t key_3;
  38 + wismart_registryKey_t key_4;
  39 + wismart_registryKey_t key_5;
  40 +
  41 +void init_registry(void){
  42 +
  43 + uint32_t retval;
  44 +
  45 + /*
  46 + Inform registry module about the size of the variables we are going to work with.
  47 +
  48 + NOTE: A flash erase should be perfomed every time:
  49 + 1. The order with which these variables are created is changed
  50 + 2. The size of one or more varaibles is changed
  51 + */
  52 + retval = libwismart_RegistryCreateKey(&key_1, 1, sizeof(ssid));
  53 + if(retval != WISMART_SUCCESS){
  54 + APP_DBG("libwismart_RegistryCreateKey(1) Failed!!\r\n");
  55 + while(1);
  56 + }
  57 +
  58 + retval = libwismart_RegistryCreateKey(&key_2, 1, sizeof(net_key));
  59 + if(retval != WISMART_SUCCESS){
  60 + APP_DBG("libwismart_RegistryCreateKey(2) Failed!!\r\n");
  61 + while(1);
  62 + }
  63 +
  64 + retval = libwismart_RegistryCreateKey(&key_3, 1, sizeof(user));
  65 + if(retval != WISMART_SUCCESS){
  66 + APP_DBG("libwismart_RegistryCreateKey(3) Failed!!\r\n");
  67 + while(1);
  68 + }
  69 +
  70 + retval = libwismart_RegistryCreateKey(&key_4, 1, sizeof(password));
  71 + if(retval != WISMART_SUCCESS){
  72 + APP_DBG("libwismart_RegistryCreateKey(4) Failed!!\r\n");
  73 + while(1);
  74 + }
  75 +
  76 + retval = libwismart_RegistryCreateKey(&key_5, 1, sizeof(encrypt_type));
  77 + if(retval != WISMART_SUCCESS){
  78 + APP_DBG("libwismart_RegistryCreateKey(5) Failed!!\r\n");
  79 + while(1);
  80 + }
  81 +
  82 + /*
  83 + Open the registry file. Now we can perform get/set operations
  84 + */
  85 + retval = libwismart_RegistryOpen(1);
  86 + if(retval != WISMART_SUCCESS){
  87 + APP_DBG("libwismart_RegistryOpen() Failed!!\r\n");
  88 + while(1);
  89 + }
  90 +
  91 +
  92 + if(libwismart_RegistryIsValueEmpty(&regVar1Key)){
  93 +
  94 + /*
  95 + Init and save registry variable.1
  96 + */
  97 + ssid=NETWORK_SSID;
  98 + libwismart_RegistrySet(&key_1,&ssid);
  99 +
  100 + /*
  101 + Init and save registry variable.2
  102 + */
  103 + user=WPA_USER;
  104 + libwismart_RegistrySet(&key_3, &user);
  105 +
  106 + /*
  107 + Init and save registry variable.3
  108 + */
  109 + password=WPA_PASS;
  110 + libwismart_RegistrySet(&key_4, &password);
  111 +
  112 + /*
  113 + Init and save registry variable.4
  114 + */
  115 + net_key=NETWORK_KEY;
  116 + libwismart_RegistrySet(&key_2, &net_key);
  117 +
  118 + /*
  119 + Init and save registry variable.4
  120 + */
  121 + encrypt_type=ENCRYPT_MODE;
  122 + libwismart_RegistrySet(&key_5, &encrypt_type);
  123 + }
  124 + else{
  125 +
  126 + APP_DBG("------------------------------------------\r\n");
  127 + APP_DBG("Registry values...\r\n");
  128 + APP_DBG("------------------------------------------\r\n");
  129 +
  130 + /*
  131 + Restore registry variable.1
  132 + */
  133 + retval = libwismart_RegistryGet(&key_1,&ssid);
  134 + if(retval != WISMART_SUCCESS){
  135 + APP_DBG("libwismart_RegistryGet(1) Failed!!\r\n");
  136 + while(1);
  137 + }
  138 +
  139 + /*
  140 + Restore registry variable.2
  141 + */
  142 + retval = libwismart_RegistryGet(&key_2,&net_key);
  143 + if(retval != WISMART_SUCCESS){
  144 + APP_DBG("libwismart_RegistryGet(2) Failed!!\r\n");
  145 + while(1);
  146 + }
  147 +
  148 + /*
  149 + Restore registry variable.3
  150 + */
  151 + retval = libwismart_RegistryGet(&key_3,&user);
  152 + if(retval != WISMART_SUCCESS){
  153 + APP_DBG("libwismart_RegistryGet(3) Failed!!\r\n");
  154 + while(1);
  155 + }
  156 +
  157 + retval = libwismart_RegistryGet(&key_4,&password);
  158 + if(retval != WISMART_SUCCESS){
  159 + APP_DBG("libwismart_RegistryGet(4) Failed!!\r\n");
  160 + while(1);
  161 + }
  162 +
  163 + retval = libwismart_RegistryGet(&key_5,&encrypt_type);
  164 + if(retval != WISMART_SUCCESS){
  165 + APP_DBG("libwismart_RegistryGet(5) Failed!!\r\n");
  166 + while(1);
  167 + }
  168 +
  169 +
  170 + /*
  171 + Print restored values:
  172 + */
  173 + APP_DBG("SSID = %s\r\n",ssid);
  174 + APP_DBG("Net key = %s\r\n",net_key);
  175 + APP_DBG("User = %s\r\n",user);
  176 + APP_DBG("Password = %s\r\n",password);
  177 + APP_DBG("Encryption type = %s\r\n",encrypt_type);
  178 + }
  179 +
  180 +
  181 +}
  182 +
  183 +
  184 +
22 int main(void) 185 int main(void)
23 { 186 {
24 - //if(libwismart_RegistryIsValueEmpty(wismart_registryKey_t* key)){  
25 -  
26 - //}  
27 - //else { 187 +
28 uint32_t ind[4]={0}; 188 uint32_t ind[4]={0};
29 char** buffers[4]; 189 char** buffers[4];
30 int sizes[4]={0}; 190 int sizes[4]={0};
@@ -33,6 +193,7 @@ int main(void) @@ -33,6 +193,7 @@ int main(void)
33 193
34 buffers[i]=NULL; 194 buffers[i]=NULL;
35 } 195 }
  196 +
36 struct wpa_param wpa; 197 struct wpa_param wpa;
37 wpa.eap_method = WISMART_EAP_METHOD_TTLS; 198 wpa.eap_method = WISMART_EAP_METHOD_TTLS;
38 wpa.u.ttls.identity=WPA_USER; 199 wpa.u.ttls.identity=WPA_USER;
@@ -43,16 +204,22 @@ int main(void) @@ -43,16 +204,22 @@ int main(void)
43 struct httpHeaders head301 = { get, "/", 1, "w3.org", 6 }; 204 struct httpHeaders head301 = { get, "/", 1, "w3.org", 6 };
44 struct httpHeaders head404 = { get, "/errorerrorerror", 15, "www.w3.org" }; 205 struct httpHeaders head404 = { get, "/errorerrorerror", 15, "www.w3.org" };
45 206
  207 + init_registry();
46 initLibwismart(); 208 initLibwismart();
47 libwismart_PowerSave_Enable(); 209 libwismart_PowerSave_Enable();
48 libwismart_PowerSave_HigherProfile(TRUE); 210 libwismart_PowerSave_HigherProfile(TRUE);
49 -  
50 libwismart_RegisterDhcpCB(dhcp_connect_result_cb); 211 libwismart_RegisterDhcpCB(dhcp_connect_result_cb);
51 -  
52 libwismart_WiFiInit(); 212 libwismart_WiFiInit();
53 - //falta definir les variables de la xarxa 213 + libwismart_SetScanRunsForConnTimeout(4);
54 libwismart_WiFiConnectEnterprise(NETWORK_SSID, &wpa, wifi_connect_result_cb); 214 libwismart_WiFiConnectEnterprise(NETWORK_SSID, &wpa, wifi_connect_result_cb);
55 215
  216 +
  217 + if(timout==1){
  218 +
  219 + //corroborar los parametros del AP
  220 + libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,(unsigned char*)NETWORK_KEY_AP,wifi_connect_ap_result_cb, wismart_softap_clients_cb);
  221 + }
  222 +
56 //int httpRequest(struct httpHeaders head, char* content, int content_size) 223 //int httpRequest(struct httpHeaders head, char* content, int content_size)
57 //chThdSleepMilliseconds(5000); 224 //chThdSleepMilliseconds(5000);
58 225
@@ -91,5 +258,5 @@ int main(void) @@ -91,5 +258,5 @@ int main(void)
91 258
92 chThdSleepMilliseconds(500); 259 chThdSleepMilliseconds(500);
93 } 260 }
94 - //} 261 +
95 } 262 }