main.c 8.04 KB
#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);
		}
}