#include #include #include #include #include //EEPROM #include #include #include #include #include #include "http-client_curl.h" #include "legato.h" //ADC #include "le_adc_interface.h" #include "le_adc_common.h" //GPS #include "le_gnss_common.h" #include "le_gnss_interface.h" //RTC #include "le_rtc_common.h" #include "le_rtc_interface.h" //WiFi #include "le_wifiClient_common.h" #include "le_wifiClient_interface.h" #include "le_wifiDefs_interface.h" //SMS #include "sms.h" //------------------------------------------------------------------------------------------------- /** * Define value. */ //------------------------------------------------------------------------------------------------- //RTC //different time between GPStime UTCtime (10years & 5 days) #define CM_DELTA_POSIX_TIME_EPOCH_GPS_TIME_EPOCH_IN_SEC 315964800 //EEPROM #define EEPMAX 4096 #define RDMAX 4096 #define I2C_M_WR 0 unsigned char eepromd[16] = { 0x30,0x2d,0x30,0x36, 0x2d,0x30,0x39,0x2d, 0x30,0x38,0x3a,0x32, 0x36,0x0a,0x4d,0x66 }; // SSID DEFAULT SETTING #define SSID "953_B0024777232A" #define SSID_NBR_BYTES (sizeof(SSID)-1) #define PASSPHRASE "1004f4d1e1" //#define TEMP_STRING_MAX_BYTES 512 #define NBR_OF_SCAN_LOOPS 2 //-------------------------------------------------------------------------------------------------- /** * Access point reference. */ //-------------------------------------------------------------------------------------------------- static le_wifiClient_AccessPointRef_t AccessPointRefToConnectTo = NULL; //-------------------------------------------------------------------------------------------------- /** * Event handler reference. */ //-------------------------------------------------------------------------------------------------- static le_wifiClient_NewEventHandlerRef_t HdlrRef = NULL; //-------------------------------------------------------------------------------------------------- /** * WiFi scans counter */ //-------------------------------------------------------------------------------------------------- static uint32_t ScanDoneEventCounter = 0; //------------------------------------------------------------------------------------------------- /** * Get ADC. */ //------------------------------------------------------------------------------------------------- void get_adc() { le_result_t result = LE_FAULT; int32_t value; const char* channelName = "EXT_ADC0"; le_adc_ConnectService(); result = le_adc_ReadValue(channelName, &value); printf("result : %d \n",result); printf("value : %d \n",value); if (result == LE_OK) { printf("%s:%d\n", channelName, value); } else { printf("Read %s failed.\n", channelName); exit(EXIT_FAILURE); } } //------------------------------------------------------------------------------------------------- /** * Set RTC. */ //------------------------------------------------------------------------------------------------- void setRtc(le_gnss_SampleRef_t SampRef) { uint64_t epochTime; uint64_t time; le_result_t res = LE_FAULT; printf("==== SET RTC ==== \n"); res = le_gnss_GetEpochTime(SampRef,&epochTime); if(res == LE_OK){ printf("(gnss) Epoch Time %llu ms\n", (unsigned long long int) epochTime); le_rtc_ConnectService(); time = ((uint64_t)(epochTime/1000) - CM_DELTA_POSIX_TIME_EPOCH_GPS_TIME_EPOCH_IN_SEC) * 1000; printf("(rtc)Epoch Time %llu ms\n", (unsigned long long int) time); res = le_rtc_SetUserTime(time); if(res != LE_OK){ printf("Set time fault\n"); } else{ printf("successed\n"); } } } //------------------------------------------------------------------------------------------------- /** * Connect & Get GPS Data. */ //------------------------------------------------------------------------------------------------- void get_gnss() { //input le_gnss_SampleRef_t sampRef; // output int32_t latitude; int32_t longitude; int32_t hAccu; le_gnss_FixState_t state; le_result_t res = LE_FAULT; printf("====== START Program =====\n"); le_gnss_ConnectService(); printf("======= GNSS ENABLE ======\n"); res = le_gnss_Enable(); printf("Enable result: %d\n",res); if(res == LE_FAULT){ //-6 printf("Enable failed\n"); exit(EXIT_FAILURE); } else { if(LE_OK == res){ //0 printf("Successed\n"); } else if(LE_NOT_PERMITTED == res){ //-5 printf("not initialzed\n"); } else if(LE_DUPLICATE == res){ //-14 printf("already enabled\n"); } } res = LE_FAULT; printf("======= GNSS START ======\n"); res = le_gnss_Start(); printf("Start result: %d\n",res); if(res == LE_FAULT){ //-6 printf("GNSS start failed\n"); exit(EXIT_FAILURE); } else { if(LE_OK == res){ //0 printf("Successed\n"); } else if(LE_NOT_PERMITTED == res){ //-5 printf("not initialzed or disable\n"); } else if(LE_DUPLICATE == res){ //-14 printf("already started\n"); } } res = LE_FAULT; //get state do{ printf("==== GET state ====\n"); res = le_gnss_GetState(); printf("get state result: %d \n",res); //if(LE_GNSS_STATE_UNINITIALIZED == res){ //0 if(res == 0){ printf("State : not initialized\n"); } //else if(LE_GNSS_STATE_READY == res){ else if(res == 1){ //1 printf("State : Ready\n"); } //else if(LE_GNSS_STATE_ACTIVE == res){ else if(res == 2){ //2 printf("State : Active\n"); } // else if(LE_GNSS_STATE_DISABLED == res){ else if(res == 3){ //3 printf("State : Disable\n"); } //else if(LE_GNSS_STATE_MAX == res){ else if(res == 4){ //4 printf("State : do not use\n"); } }while(res != 2); //control ext_gps_LNA_EN signal printf("======= EnableExternalLna ======\n"); res = le_gnss_EnableExternalLna(); printf("enable externalLna result: %d \n",res); if(LE_OK == res){ //0 printf("Successed\n"); } else if(LE_NOT_PERMITTED == res){ //-5 printf(" Not permitted \n"); } else if(LE_UNSUPPORTED == res){ //-18 printf("not supported on this platform\n"); } res = LE_FAULT; //get position state printf("==== GET position state ====\n"); sampRef = le_gnss_GetLastSampleRef(); if (NULL == sampRef) { printf("New Position sample is NULL!\n"); } else { printf("New Position sample %p\n", sampRef); } res = LE_FAULT; res = le_gnss_GetPositionState(sampRef,&state); printf("get position result: %d \n",res); if(LE_OK != res){ //-14 printf("failed \n"); exit(EXIT_FAILURE); }else{ //0 printf("SUCCESS \n"); printf("get position state result: %d \n",state); if(state == LE_GNSS_STATE_FIX_NO_POS){ //0 le_gnss_ReleaseSampleRef(sampRef); printf("NO fix\n"); }else if(state == LE_GNSS_STATE_FIX_2D){ //1 printf("2D position fix\n"); }else if(state == LE_GNSS_STATE_FIX_3D){ //2 printf("3D position fix\n"); }else if(state == LE_GNSS_STATE_FIX_ESTIMATED){ //3 printf("estimated\n"); } } //get location printf("==== GET Location ==== \n"); res = LE_FAULT; res = le_gnss_GetLocation(sampRef,&latitude,&longitude,&hAccu); printf("get location result: %d\n",res); if(LE_FAULT == res){ //-6 printf("failed\n"); } else if(LE_OUT_OF_RANGE == res){ //-3 printf("OUT OF RANGE\n"); printf("Location invalid [%d, %d, %d]\n", latitude, longitude, hAccu); } else if (LE_OK == res){ //0 printf("SUCCESS \n"); printf("Position lat.%.6f \nlong.%.6f \nhAccuracy.%.2fm \n", (float)latitude/1000000.0,(float)longitude/1000000.0, (float)hAccu/100.0); } else{ printf("error"); } //Set UTC time setRtc(sampRef); // Release provided Position sample reference le_gnss_ReleaseSampleRef(sampRef); printf("======= END ======\n"); } //------------------------------------------------------------------------------------------------- /** * EEPROM. */ //------------------------------------------------------------------------------------------------- int eeprom() { const unsigned char i2caddress = 0x50; int i; int page; int fd1; //unsigned short eepdata; unsigned short eepadr = 0x0000; unsigned short eepindex = 0; unsigned char wbuf[64]; unsigned char rdbuf[RDMAX]; char str[RDMAX]; char str2[RDMAX]; struct i2c_msg msgs[104]; struct i2c_rdwr_ioctl_data msgset[1]; fd1 = open("/dev/i2c-4",O_RDWR | O_NONBLOCK); if(fd1 < 0) { printf("i2c Open Error\n"); return -1; } printf("i2c Open\n"); eepadr = 0x0000; eepindex = 0; for(page = 0;page < 4096/32;page++){ wbuf[0] = eepadr >> 8; wbuf[1] = eepadr & 0xFF; //transmit msg msgs[0].addr = i2caddress; msgs[0].flags = I2C_M_WR; msgs[0].len = 2; msgs[0].buf = wbuf; //receive msg msgs[1].addr = i2caddress; msgs[1].flags = I2C_M_RD | I2C_M_NOSTART; msgs[1].len = 32; msgs[1].buf = &rdbuf[eepindex << 1]; msgset[0].msgs = msgs; msgset[0].nmsgs = 2; if(ioctl(fd1,I2C_RDWR,&msgset) < 0) { printf("unable to get read data: %s\n", strerror(errno)); return -1; } printf("get read data %x\n",page); int i = 0; str[0] = 0; for(i=0;i<32;i++){ sprintf(str2,"%02x",rdbuf[i]); strcat(str,str2); } printf("0x%s\n",str); eepadr += 32; } /* eeprom write eepadr = 0x20 */ eepadr = 0x20 << 5; wbuf[0] = eepadr >> 8; wbuf[1] = eepadr & 0xFF; for(i = 0;i < 16;i++){ wbuf[2 + i] = eepromd[i]; } //transmit msg msgs[0].addr = i2caddress; msgs[0].flags = I2C_M_WR; msgs[0].len = 2 + i; msgs[0].buf = wbuf; msgset[0].msgs = msgs; msgset[0].nmsgs = 1; if(ioctl(fd1,I2C_RDWR,&msgset) < 0) { printf("unable to put write data: %s\n", strerror(errno)); return -1; } sleep(10); eepadr = 0x0000; eepindex = 0; for(page = 0;page < 4096/32;page++){ wbuf[0] = eepadr >> 8; wbuf[1] = eepadr & 0xFF; //transmit msg msgs[0].addr = i2caddress; msgs[0].flags = I2C_M_WR; msgs[0].len = 2; msgs[0].buf = wbuf; //receive msg msgs[1].addr = i2caddress; msgs[1].flags = I2C_M_RD | I2C_M_NOSTART; msgs[1].len = 32; msgs[1].buf = &rdbuf[eepindex << 1]; msgset[0].msgs = msgs; msgset[0].nmsgs = 2; if(ioctl(fd1,I2C_RDWR,&msgset) < 0) { printf("unable to get read data: %s\n", strerror(errno)); return -1; } printf("get read data %x\n",page); int i = 0; str[0] = 0; for(i=0;i<32;i++){ sprintf(str2,"%02x",rdbuf[i]); strcat(str,str2); } printf("0x%s\n",str); eepadr += 32; } close(fd1); return 0; } //------------------------------------------------------------------------------------------------- /** * Shutdown */ //------------------------------------------------------------------------------------------------- int shutdown_com() { system( "sys_shutdown" ); return 0; } //------------------------------------------------------------------------------------------------- /** * wifi. */ //------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- /** * IP Handling must be done by the application once the WiFi link is established */ //-------------------------------------------------------------------------------------------------- static void AskForIpAddress ( void ) { int systemResult; char tmpString[512]; printf("=== ASK FOR IP ADDRESS === \n"); // DHCP client snprintf(tmpString, sizeof(tmpString), "PATH=/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin;" "/sbin/udhcpc -R -b -i wlan0" ); systemResult = system(tmpString); // Return value of -1 means that the fork() has failed (see man system). if (0 == WEXITSTATUS(systemResult)) { printf("DHCP client: %s \n", tmpString); } else { printf("DHCP client %s Failed: (%d) \n", tmpString, systemResult); } } //-------------------------------------------------------------------------------------------------- /** * Starts a connection to the AccessPointRefToConnectTo previously found in * TestReadScanResults() */ //-------------------------------------------------------------------------------------------------- static void Connect() { le_result_t result; char flg_conn = 0; // the AccessPointRefToConnectTo should still be valid after a Scan (if this will fail) if (NULL != AccessPointRefToConnectTo) { printf("=== Setting & Connecting === \n"); result = le_wifiClient_SetSecurityProtocol(AccessPointRefToConnectTo,LE_WIFICLIENT_SECURITY_WPA2_PSK_PERSONAL); if(LE_OK != result){ printf("ERROR:le_wifiClient_SetSecurityProtocol returns %d\n",result); } else{ printf("Successfully set security protocol\n"); } result = le_wifiClient_SetPassphrase(AccessPointRefToConnectTo,PASSPHRASE); if(LE_OK != result){ printf("ERROR:le_wifiClient_SetPassphrase returns %d\n",result); } else{ printf("Successfully set PASSPHRASE \n"); } printf("Connect to %p \n", AccessPointRefToConnectTo); while(flg_conn == 0){ result = le_wifiClient_Connect(AccessPointRefToConnectTo); printf("le_wifiClient_Connect return %d \n",result); if (LE_OK == result) { printf("Connect OK\n"); AskForIpAddress(); flg_conn = 1; } else if (LE_TIMEOUT == result) { printf("Connect Timeout \n"); } else if (LE_DUPLICATE == result) { printf("Connect already \n"); AskForIpAddress(); flg_conn = 1; } else { printf("Connect ERROR \n"); flg_conn = 1; } } } else { printf("ERROR: AccessPointRefToConnectTo not found.\n"); } } //-------------------------------------------------------------------------------------------------- /** * Handler for WiFi client changes */ //-------------------------------------------------------------------------------------------------- static void EventHandler ( const le_wifiClient_Event_t wifiEventPtr_event, void *contextPtr ) { printf("event"); switch( wifiEventPtr_event ) { case LE_WIFICLIENT_EVENT_CONNECTED: { ///< WiFi Client Connected printf("WiFi Client Connected."); /*printf("Interface: %s, bssid: %s", &wifiEventPtr->ifName[0], &wifiEventPtr->apBssid[0]);*/ AskForIpAddress(); } break; case LE_WIFICLIENT_EVENT_DISCONNECTED: { printf("WiFi Client Disconnected."); /*printf("Interface: %s, disconnectCause: %d", &wifiEventPtr->ifName[0], wifiEventPtr->cause);*/ } break; case LE_WIFICLIENT_EVENT_SCAN_DONE: { ScanDoneEventCounter++; printf("WiFi Client Scan is done."); //TestReadScanResults(); if (ScanDoneEventCounter < NBR_OF_SCAN_LOOPS) { sleep(2); printf("LE_WIFICLIENT_EVENT_SCAN_DONE: Start New Scan %d)", ScanDoneEventCounter); le_wifiClient_Scan(); } else { printf("LE_WIFICLIENT_EVENT_SCAN_DONE: try connect. %d)", ScanDoneEventCounter); Connect(); } } break; default: printf("ERROR Unknown event %d", wifiEventPtr_event); break; } } le_wifiClient_NewEventHandlerRef_t WifiEventHandlerRef = NULL; //-------------------------------------------------------------------------------------------------- /** * WiFi client start */ //-------------------------------------------------------------------------------------------------- void wifiClient_Start ( void ) { le_result_t result; //le_wifiDefs_ConnectService(); le_wifiClient_ConnectService(); printf("=== Start wifi client === \n"); HdlrRef = le_wifiClient_AddNewEventHandler(EventHandler, NULL); result = le_wifiClient_Start(); //printf("le_wifiClient_Start return %d \n",result); if (LE_OK == result) { printf("Start OK\n"); } else if (LE_BUSY == result) //else if (LE_DUPLICATE == result) { printf("Already started\n"); } else { printf("Start ERROR\n"); //break; } printf("SSID %s\n",SSID); AccessPointRefToConnectTo = le_wifiClient_Create((const uint8_t *)SSID,SSID_NBR_BYTES); printf("test called le_wifiClient_Create. returned %p \n", AccessPointRefToConnectTo); printf("test Scan started. Waiting X seconds for it to finish\n"); if (LE_OK == le_wifiClient_Scan()) { printf("Scan OK\n"); //TestReadScanResults(); } else { printf("Scan ERROR\n"); } } //-------------------------------------------------------------------------------------------------- /** * SMS */ //-------------------------------------------------------------------------------------------------- void sms() { smsmt_Receiver(); smsmt_MonitorStorage(); } //-------------------------------------------------------------------------------------------------- /** * main */ //-------------------------------------------------------------------------------------------------- COMPONENT_INIT { get_adc(); get_gnss(); /*le_timer_Ref_t ledtimerRef = le_timer_Create("ledTimer"); //set timer le_timer_SetMsInterval(ledtimerRef,100); //set repeat le_timer_SetRepeat(ledtimerRef,0); //set callback function to handle timeer expiration event le_timer_SetHandler(ledtimerRef,sms); //start timer le_timer_Start(ledtimerRef);*/ }