diff --git a/Project/applications/smartcities/callbacks.c b/Project/applications/smartcities/callbacks.c index 9716f58..a3d349e 100644 --- a/Project/applications/smartcities/callbacks.c +++ b/Project/applications/smartcities/callbacks.c @@ -22,5 +22,38 @@ void dhcp_connect_result_cb(int result) void wifi_connect_result_cb(int result) { + if(result==WISMART_WIFI_CONN_TIMEOUT){ + timeout=1; + } printf("WiFi Connect indication: %s\r\n", (result == WISMART_WIFI_CONNECTED) ? "Connected": "Failed\r\n"); -} \ No newline at end of file +} + +void wismart_softap_clients_cb(wismart_softap_cb_t reason, const uint8_t *mac, const libwismart_ip_addr_t *ip) +{ + #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] +#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" + + switch(reason){ + case WISMART_WIFI_AP_CLIENT_CONNECTED: + printf("Client: "MACSTR" connected\r\n",MAC2STR(mac)); + break; + case WISMART_WIFI_AP_CLIENT_DISCONNECTED: + printf("Client: "MACSTR" disconnected\r\n",MAC2STR(mac)); + break; + case WISMART_WIFI_AP_CLIENT_EXPIRED: + printf("Client: "MACSTR" connection have expired\r\n",MAC2STR(mac)); + break; + case WISMART_WIFI_AP_CLIENT_GET_IP: + printf("Client: "MACSTR" got ip: %s\r\n", MAC2STR(mac), inet_ntoa(*ip)); + break; + } + +#undef MAC2STR +#undef MACSTR +} + +void wifi_connect_ap_result_cb(int result) +{ + + printf("New WiFi Network state: %s\r\n", (result == WISMART_WIFI_CONNECTED) ? "Created": "Failed\r\n"); +} diff --git a/Project/applications/smartcities/main.c b/Project/applications/smartcities/main.c index 57c6d5e..f471874 100644 --- a/Project/applications/smartcities/main.c +++ b/Project/applications/smartcities/main.c @@ -11,7 +11,11 @@ #define NETWORK_SSID "linksys" #define NETWORK_KEY "" #define WPA_USER "smartcities" -#define WPA_PASS "superpass" +#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) { @@ -19,12 +23,168 @@ void initLibwismart(void) 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){ + APP_DBG("libwismart_RegistryCreateKey(1) Failed!!\r\n"); + while(1); + } + + retval = libwismart_RegistryCreateKey(&key_2, 1, sizeof(net_key)); + if(retval != WISMART_SUCCESS){ + APP_DBG("libwismart_RegistryCreateKey(2) Failed!!\r\n"); + while(1); + } + + retval = libwismart_RegistryCreateKey(&key_3, 1, sizeof(user)); + if(retval != WISMART_SUCCESS){ + APP_DBG("libwismart_RegistryCreateKey(3) Failed!!\r\n"); + while(1); + } + + retval = libwismart_RegistryCreateKey(&key_4, 1, sizeof(password)); + if(retval != WISMART_SUCCESS){ + APP_DBG("libwismart_RegistryCreateKey(4) Failed!!\r\n"); + while(1); + } + + retval = libwismart_RegistryCreateKey(&key_5, 1, sizeof(encrypt_type)); + if(retval != WISMART_SUCCESS){ + APP_DBG("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){ + APP_DBG("libwismart_RegistryOpen() Failed!!\r\n"); + while(1); + } + + + if(libwismart_RegistryIsValueEmpty(®Var1Key)){ + + /* + Init and save registry variable.1 + */ + ssid=NETWORK_SSID; + libwismart_RegistrySet(&key_1,&ssid); + + /* + Init and save registry variable.2 + */ + user=WPA_USER; + libwismart_RegistrySet(&key_3, &user); + + /* + Init and save registry variable.3 + */ + password=WPA_PASS; + libwismart_RegistrySet(&key_4, &password); + + /* + Init and save registry variable.4 + */ + net_key=NETWORK_KEY; + libwismart_RegistrySet(&key_2, &net_key); + + /* + Init and save registry variable.4 + */ + encrypt_type=ENCRYPT_MODE; + libwismart_RegistrySet(&key_5, &encrypt_type); + } + else{ + + APP_DBG("------------------------------------------\r\n"); + APP_DBG("Registry values...\r\n"); + APP_DBG("------------------------------------------\r\n"); + + /* + Restore registry variable.1 + */ + retval = libwismart_RegistryGet(&key_1,&ssid); + if(retval != WISMART_SUCCESS){ + APP_DBG("libwismart_RegistryGet(1) Failed!!\r\n"); + while(1); + } + + /* + Restore registry variable.2 + */ + retval = libwismart_RegistryGet(&key_2,&net_key); + if(retval != WISMART_SUCCESS){ + APP_DBG("libwismart_RegistryGet(2) Failed!!\r\n"); + while(1); + } + + /* + Restore registry variable.3 + */ + retval = libwismart_RegistryGet(&key_3,&user); + if(retval != WISMART_SUCCESS){ + APP_DBG("libwismart_RegistryGet(3) Failed!!\r\n"); + while(1); + } + + retval = libwismart_RegistryGet(&key_4,&password); + if(retval != WISMART_SUCCESS){ + APP_DBG("libwismart_RegistryGet(4) Failed!!\r\n"); + while(1); + } + + retval = libwismart_RegistryGet(&key_5,&encrypt_type); + if(retval != WISMART_SUCCESS){ + APP_DBG("libwismart_RegistryGet(5) Failed!!\r\n"); + while(1); + } + + + /* + Print restored values: + */ + APP_DBG("SSID = %s\r\n",ssid); + APP_DBG("Net key = %s\r\n",net_key); + APP_DBG("User = %s\r\n",user); + APP_DBG("Password = %s\r\n",password); + APP_DBG("Encryption type = %s\r\n",encrypt_type); + } + + +} + + + int main(void) { - //if(libwismart_RegistryIsValueEmpty(wismart_registryKey_t* key)){ - - //} - //else { + uint32_t ind[4]={0}; char** buffers[4]; int sizes[4]={0}; @@ -33,6 +193,7 @@ int main(void) buffers[i]=NULL; } + struct wpa_param wpa; wpa.eap_method = WISMART_EAP_METHOD_TTLS; wpa.u.ttls.identity=WPA_USER; @@ -43,16 +204,22 @@ int main(void) struct httpHeaders head301 = { get, "/", 1, "w3.org", 6 }; struct httpHeaders head404 = { get, "/errorerrorerror", 15, "www.w3.org" }; + init_registry(); initLibwismart(); libwismart_PowerSave_Enable(); libwismart_PowerSave_HigherProfile(TRUE); - libwismart_RegisterDhcpCB(dhcp_connect_result_cb); - libwismart_WiFiInit(); - //falta definir les variables de la xarxa + libwismart_SetScanRunsForConnTimeout(4); libwismart_WiFiConnectEnterprise(NETWORK_SSID, &wpa, wifi_connect_result_cb); + + if(timout==1){ + + //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, wismart_softap_clients_cb); + } + //int httpRequest(struct httpHeaders head, char* content, int content_size) //chThdSleepMilliseconds(5000); @@ -91,5 +258,5 @@ int main(void) chThdSleepMilliseconds(500); } - //} + }