main.c 6.16 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"
#include "configServer.h"

#define WIFI_MODE               WIFI_MODE_CLIENT
#define NETWORK_SSID_AP         "modularsens"
#define NETWORK_KEY_AP          NULL
#define NETWORK_CHANNEL_AP      1

#define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000
#define NUMSENSORS 4

wismart_registryKey_t geo;


void initLibwismart(void)
{
    wismart_hwif_t hwif = libwismart_GetDefaultHWIF();
    libwismart_Init(hwif);
}

uint8_t connected=0;
uint8_t timeout=0;
uint8_t retries=0;

void init_registry(void)
{
    char ssid[33];  // ssid clave red usuario contrasenya tipicript
    char wepkey[14];
    char passphrase[64];
    config_params_t config;
    uint16_t security;
    libwismart_ProfileGet_Int("security", &security);
    libwismart_ProfileGet_Str("ssid", ssid);
    libwismart_ProfileGet_Str("wepkey", wepkey);
    libwismart_ProfileGet_Str("passphrase", passphrase);

    strcpy(config.localization,"none");
    libwismart_RegistryCreateKey(&geo, 1, sizeof(config));
    libwismart_RegistryOpen(1);
    if(!libwismart_RegistryIsValueEmpty(&geo))
    {
        libwismart_RegistryGet(&geo,&config);
    }

    printf("SSID = %s\r\n",ssid);
    printf("Wep key = %s\r\n",wepkey);
    printf("Passphrase = %s\r\n", passphrase);
    printf("User = %s\r\n",config.user);
    printf("Password = %s\r\n",config.password);
    printf("Encryption type = %d\r\n",security);
    printf("Geo Localization = %s\r\n", config.localization);

    strcpy(mod.geoloc,config.localization);

    if(security == PROFILE_SECURITY_OPEN)
    {
        printf("open detected\r\n");
        libwismart_WiFiConnect(ssid,NULL,wifi_connect_result_cb);
    }
    else if(security == PROFILE_SECURITY_WPA_WPA2)
    {
        if(!strcmp(config.user,""))
        {
            printf("wpa detected\r\n");
            libwismart_WiFiConnect(ssid,passphrase,wifi_connect_result_cb);
        }
        else
        {
            printf("wpa Enterprise detected\r\n");
            struct wpa_param wpa;
            wpa.eap_method = WISMART_EAP_METHOD_TTLS;
            wpa.u.ttls.identity = config.user;
            wpa.u.ttls.password = config.password;
            wpa.u.ttls.ca_cert=NULL;
            libwismart_WiFiConnectEnterprise(ssid, &wpa, wifi_connect_result_cb);
        }
    }
    else
    {
        printf("wep detected\r\n");
        //Is WEP
        libwismart_WiFiConnect(ssid,wepkey,wifi_connect_result_cb);
    }

}
int main(void)
{
    initLibwismart();
    libwismart_PowerSave_Enable();
    libwismart_PowerSave_HigherProfile(TRUE);
    libwismart_RegisterDhcpCB(dhcp_connect_result_cb);
    libwismart_WiFiInit();
    libwismart_SetScanRunsForConnTimeout(4); //Edit a 4
	libwismart_EnableBsdSocketAPI();

    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" };

    uint32_t ind[4]={0};
    char** buffers[4];
    uint32_t sizes[4]={0};
	char* sensor_id[4];
    int i;
    int num_sensors;
    
   init_registry();
    while(connected == 0 && timeout != 1  )
    {chThdSleepMilliseconds(500);}
    if(timeout==1)
    {
        printf("Creating AP\r\n");
        //corroborar los parametros del AP
        configServer_start(1);
        libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,NULL,softapMode_apStartedCb, softapMode_clientIndicationCb);
        for(;;)
        {
            chThdSleepMilliseconds(1000);
        }
    }
    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<NUMSENSORS;i++){

                printf("------------------BUFFER %d ----------------------\r\n",i);
                printf("index=%d\r\n",ind[i]);
				
				// 3 missatges de prova a cada buffer
                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 j;
					for(j=0;j<NUMSENSORS;j++)
					{
						printf(" enviant buffer %d\r\n",j);
						
						// fem servir 085 de prova, però haurem de fer servir sensor_id[j]
						int ok=send(buffers[j],&ind[j],&sizes[j], "bmp", "085");
						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){
					printf("--------------hard limit-------------\r\n");
                    
					// fem servir 085 de prova, però haurem de fer servir sensor_id[0]
					while( send(buffers[0],&ind[0],&sizes[0], "bmp", "085") != JSON_POST_OK )
					{
						printf("hard_reached and unable to sentd.\r\nLa biblia en vers: i Jesús digué: Aixeca't, Llàtzer!!\r\n");
						chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME);
					}
					int j;
					for(j=1;j<NUMSENSORS;j++)
					{
						printf(" enviant buffer %d\r\n",j);
						
						// fem servir 085 de prova, però haurem de fer servir sensor_id[j]
						int ok=send(buffers[j],&ind[j],&sizes[j], "bmp", "085");
						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");
						}
					}
                }
            }
        //chThdSleepMilliseconds(100);
    }
}