void appli_entry_point() { wip_channel_t socket; wip_debug( "[SAMPLE]: connecting to client %s:%i...\n", PEER_STRADDR, PEER_PORT); socket = wip_TCPClientCreate( PEER_STRADDR, PEER_PORT, evh, NULL); if( ! socket) { wip_debug( "[SAMPLE] Can't connect\n"); return; } }
static void evh( wip_event_t *ev, void *ctx) { switch( ev->kind) { case WIP_CEV_OPEN: { wip_debug ("[SAMPLE] Connection established successfully\n"); break; } case WIP_CEV_READ: { int nread; wip_debug ("[SAMPLE] Some data arrived\n"); nread = wip_read( ev->channel, rcv_buffer + rcv_offset, sizeof( rcv_buffer) - rcv_offset); if( nread < 0) { wip_debug( "[SAMPLE] read error %i\n", nread); return; } rcv_offset += nread; if( rcv_offset == sizeof( rcv_buffer)) { wip_debug( "[SAMPLE] Reception capacity exceeded, won't read more\n"); } else { wip_debug( "[SAMPLE] Wrote %i bytes of data from network to rcv_buffer. " "%i bytes remain available in rcv_buffer\n", nread, sizeof( rcv_buffer) - rcv_offset); } break; } case WIP_CEV_WRITE: { int nwrite; wip_debug ("[SAMPLE] Can send more data\n"); nwrite = wip_write( ev->channel, snd_buffer + snd_offset, sizeof( snd_buffer) - snd_offset); if( nwrite < 0) { wip_debug( "[SAMPLE] write error %i\n", nwrite); return; } snd_offset += nwrite; if( snd_offset == sizeof( snd_buffer)) { wip_debug( "[SAMPLE] Everything has been sent, won't send more.\n"); } else { wip_debug( "[SAMPLE] Wrote %i bytes. " "%i bytes left to send in snd_buffer\n", nwrite, sizeof( snd_buffer) - snd_offset); } break; } case WIP_CEV_ERROR: { wip_debug( "[SAMPLE] Error %i on socket. Closing.\n", ev->content.error.errnum); wip_close( ev->channel); break; } case WIP_CEV_PEER_CLOSE: { wip_debug( "[SAMPLE] Connection closed by peer\n"); wip_close( ev->channel); break; } } }
void NextFixHandler(u8 timerid, void *context) { (void)timerid; (void)context; s32 sReturn = -1; char prevGPSstat; adl_rtcTimeStamp_t CurrentTimeStamp; adl_rtcTimeStamp_t DeltaTimeStamp; TCP_STATUS status; static BOOL AGPSRequested=0; static u8 Count=0; //Count of how many times this function was entered. int ret_val; prevGPSstat = g_status.GPSStatus; TRACE((1, "Next GPS Fix timer triggered")); // control the tracking mode state. Process the tracking mode // requests from the rest of the system. switch (TrackingMode) { case TRACKING_MODE_OFF: if (GetDogParkMode() == DOGPARK_ENABLED) { TrackingMode = DOG_PARK_TRACKING_MODE; } else if (AccelTrackingReq == TRACKING_MODE_REQ_ACTIVE) { TrackingMode = ACCEL_TRIG_TRACKING_MODE; } else if (TimedWakeupTrackingReq == TRACKING_MODE_REQ_ACTIVE) { TrackingMode = TIMED_WAKE_TRACKING_MODE; } else { TrackingMode = TRACKING_MODE_OFF; } break; case ACCEL_TRIG_TRACKING_MODE: if (GetDogParkMode() == DOGPARK_ENABLED) { TrackingMode = DOG_PARK_TRACKING_MODE; } else if (AccelTrackingReq == TRACKING_MODE_REQ_RELEASE) { TrackingMode = TRACKING_MODE_OFF; } else { TrackingMode = ACCEL_TRIG_TRACKING_MODE; } break; case DOG_PARK_TRACKING_MODE: if (GetDogParkMode() == DOGPARK_DISABLED) { TrackingMode = TRACKING_MODE_OFF; } else { TrackingMode = DOG_PARK_TRACKING_MODE; } break; case TIMED_WAKE_TRACKING_MODE: if (TimedWakeupTrackingReq == TRACKING_MODE_REQ_RELEASE) { TrackingMode = TRACKING_MODE_OFF; } else { TrackingMode = TIMED_WAKE_TRACKING_MODE; } break; default: DumpMessage("Undefined tracking mode\r\n"); TrackingMode = TRACKING_MODE_OFF; break; } switch (gps_state) { // the GPS should be in OFF mode while in this state. case GPS_OFF: if ((TrackingMode == ACCEL_TRIG_TRACKING_MODE) || (TrackingMode == DOG_PARK_TRACKING_MODE) || (TrackingMode == TIMED_WAKE_TRACKING_MODE)) { GetConvertTime(&TrackTimeStamp); gps_state = GPS_START; DumpMessage("GPS START!\r\n"); //Reset the flag AGPSRequested = 0; } else { gps_state = GPS_OFF; } break; // attempt to start up the GPS. case GPS_START: gps_Start(); if (!HadFix && !AttemptedAGPSNoFix) { DumpMessage("Never had fix. Going to use AGPS!\r\n"); //performAGPS(0.0, 0.0, 0.0, 0.0); AttemptedAGPSNoFix = 1; } else if (HadFix) { GetConvertTime(&CurrentTimeStamp); //Request AGPS is last fix is more than 2 hrs old. if((CurrentTimeStamp.TimeStamp - g_AGPSData.LastReqTimeStamp) >= 2*60*60) { AGPSRequested = 0; wm_sprintf(g_traceBuf,"g_AGPSData.LastReqTimeStamp : 0x%x, CurrentTimeStamp: 0x%x\r\n",(unsigned int)g_AGPSData.LastReqTimeStamp,(unsigned int)CurrentTimeStamp.TimeStamp); DumpMessage(g_traceBuf); DumpMessageUSB(g_traceBuf,1); DumpMessage("AGPS data is old. Request fresh data\r\n"); } // if (adl_rtcDiffTime(&CurrentTimeStamp, &FixTimeStamp, &DeltaTimeStamp) < 0) // { // DisplayErrorCode("adl_rtcConvertTime", __FILE__, __LINE__, sReturn); // } // else // { // wm_sprintf(g_traceBuf,"FixTimeStamp : 0x%x, CurrentTimeStamp: 0x%x\r\n",(unsigned int)FixTimeStamp.TimeStamp,(unsigned int)CurrentTimeStamp.TimeStamp); // DumpMessage(g_traceBuf); // DumpMessageUSB(g_traceBuf,1); //if (DeltaTimeStamp.TimeStamp >= 60 * 60 * 2) //{ // DumpMessage("Over two hours since last fix, going to use AGPS!\r\n"); // performAGPS(0.0, 0.0, 0.0, 0.0); //} //} } gps_state = GPS_GET_FIX; DumpMessage("GET FIX!\r\n"); //} break; // keep on the GPS until we get a fix. probably add a timeout to this... case GPS_GET_FIX: //Increment the count to keep a track of how many times this state was entered. Count++; // if tracking mode has been disabled... if (TrackingMode == TRACKING_MODE_OFF) { gps_state = GPS_OFF; break; } GetGGAInfo(&g_GGAInfo); GetVGTInfo(&g_VTGInfo); //Request AGPS GetTCPStatus(&status); if(!AGPSRequested && status == TCP_CONNECT) { //Get the current time GetConvertTime(&CurrentTimeStamp); //Compare the times. if((CurrentTimeStamp.TimeStamp - g_AGPSData.LastReqTimeStamp) < 2*60*60) //check for < 2 hours { wm_sprintf(g_traceBuf,"CurrentTimeStamp.TimeStamp: 0x%x, g_AGPSData.LastReqTimeStamp: 0x%x\r\n", (unsigned int)CurrentTimeStamp.TimeStamp,(unsigned int)g_AGPSData.LastReqTimeStamp); DumpMessage(g_traceBuf); DumpMessageUSB(g_traceBuf,1); //Send the last stored AGPS data to ublox directly. if ((ret_val = adl_fcmSendData(ubloxDataFcmHandle, (unsigned char *)g_AGPSData.AGPSData, g_AGPSData.datalen)) != OK) { wip_debug("ERROR: Could not send data to ublox device: %d\r\n", ret_val); wm_sprintf(g_traceBuf,"ERROR: Could not send data to ublox device: %d\r\n", ret_val); DumpMessageUSB(g_traceBuf,1); set_agps_status(AGPS_NOT_USED); } else { AGPSRequested = 1; wm_sprintf(g_traceBuf,"AGPS data sent to ublox (<2 hrs case): %ld bytes\r\n", g_AGPSData.datalen); DumpMessage(g_traceBuf); DumpMessageUSB(g_traceBuf,1); } } } // Check if we got a fix. if (g_GGAInfo.FixQuality == FIX_3D) { SetFixStatus(HAD_3D_FIX); alarmGenerated = 0; GetConvertTime(&FixTimeStamp); gps_state = GPS_PROC_FIX; if (g_config.WaypointInterval >= GPS_WAYPOINT_SHUTDOWN_INTERVAL) { gps_Stop(); } // GPS working //g_status.GPSStatus = 'W'; // clear alarm if (alarm_suppress_status(GPS_ALARM_SUP) == ALARM_EXPIRED) { g_status.GPSAlarm = 'N'; GPSAlarmGenerated = 0; } gps_state = GPS_PROC_FIX; DumpMessage("GPS_PROC_FIX\r\n"); } else { if (g_GGAInfo.FixQuality == FIX_2D) { DumpMessage("GOT 2D FIX -- will not use\r\n"); if(status == TCP_CONNECT && !AGPSRequested) { //Request AGPS with available 2D fix performAGPS((g_GGAInfo.Latitude/1000000.0), (g_GGAInfo.Longitude/1000000.0), 0.0, 0.0); AGPSRequested = 1; } } else //this fucntion is called once every 925ms. so 16*925 = 15 secs { //Check for the validity of WaitTimeout if(g_AGPSData.WaitTimeout == 0 || g_AGPSData.WaitTimeout > 99) g_AGPSData.WaitTimeout = 15; //Default to 15 if found out of range //If 2D fix not received in 15 secs then request AGPS with last valid 3D fix. //Request AGPS with last GPS fix if(Count > g_AGPSData.WaitTimeout && status == TCP_CONNECT && !AGPSRequested ) { DumpMessage("Request fresh AGPS data\r\n"); performAGPS(0.0, 0.0, 0.0, 0.0); AGPSRequested = 1; Count=0; } } g_status.GPSStatus = 'N'; if (prevGPSstat == 'W') { TRACE((1, "Lost GPS Signal")); } gps_state = GPS_GET_FIX; } break; case GPS_PROC_FIX: ProcFix(FIX_3D); // GPS working g_status.GPSStatus = 'W'; DumpMessage("Got a fix1\r\n"); adl_atSendResponsePort(ADL_AT_RSP, ADL_PORT_USB, "***GOT_FIX***\r\n"); gps_state = GPS_WAIT_NEXT_FIX; HadFix = 1; break; case GPS_WAIT_NEXT_FIX: DumpMessage("GPS_WAIT_NEXT_FIX\r\n"); GetConvertTime(&CurrentTimeStamp); // time difference between last fix and current time. if (adl_rtcDiffTime(&CurrentTimeStamp, &FixTimeStamp, &DeltaTimeStamp) < 0) { DisplayErrorCode("adl_rtcConvertTime", __FILE__, __LINE__, sReturn); } // If the tracking interval time has elapsed, // go and get another fix or if the trackind mode contoller has disabled tracking, // go into the off state. if (DeltaTimeStamp.TimeStamp >= g_config.WaypointInterval) { wm_sprintf(g_traceBuf, "currenttimestamp = 0x%x fix time stamp = 0x%x delta time stamp = 0x%x waypoint interval = %d\r\n", (unsigned int )CurrentTimeStamp.TimeStamp, (unsigned int )FixTimeStamp.TimeStamp, (unsigned int )DeltaTimeStamp.TimeStamp, (int )g_config.WaypointInterval); DumpMessage(g_traceBuf); gps_state = GPS_START; } else { if (TrackingMode == TRACKING_MODE_OFF) { gps_Stop(); gps_state = GPS_OFF; FirstDogParkFix = 1; } else { gps_state = GPS_WAIT_NEXT_FIX; } } break; default: DumpMessage("Error: Unknown GPS state\r\n"); break; } }