static int TlsBindSocketToInterface( int sock ) { int retFnVal= FALSE; if( TlsValidateNetworkInterface(sock) ) { int retVal =-1; DEBUG_LOG_PRINT(("TlsBindSocketToInterface : Binding socket to %s" , vg_NetworkInterface )); retVal = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, vg_NetworkInterface, strlen(vg_NetworkInterface)+1 ); if( retVal < 0 ) { DEBUG_LOG_PRINT(("ERROR : setsockopt(%d,SO_BINDTODEVICE) Failed <%d> <%s>" ,sock, errno , strerror(errno))); } else { DEBUG_LOG_PRINT(("TlsBindSocketToInterface : Success")); retFnVal= TRUE; } } else { DEBUG_LOG_PRINT(("TlsBindSocketToInterface : No Valid Network i/f found")); } return retFnVal; }
void TlsSetNetworkInterface( char *ifname ) { int Max_size; Max_size = strlen(ifname)+1; vg_NetworkInterface = (char *) malloc (Max_size); if( NULL != ifname ) { memcpy(vg_NetworkInterface,ifname,strlen(ifname)+1); DEBUG_LOG_PRINT(("TlsSetNetworkInterface : Set %s",vg_NetworkInterface)); } else { memset(vg_NetworkInterface,0,Max_size ); DEBUG_LOG_PRINT(("TlsSetNetworkInterface : Clear Network Interface")); } }
void powsw_softintr(void *arg) { struct powsw_softc *sc = arg; int sw; int s; s = spl6(); if (sc->sc_tick++ >= POWSW_MAX_TICK) { /* tick is over, broken switch? */ printf("%s: unstable power switch?, ignored\n", device_xname(sc->sc_dev)); powsw_reset_counter(sc); mfp_bit_set_ierb(sc->sc_mask); splx(s); return; } sw = (mfp_get_gpip() & sc->sc_mask) ? 1 : 0; DEBUG_LOG_ADD('0' + sw); if (sw == sc->sc_last_sw) { sc->sc_count++; } else { sc->sc_last_sw = sw; sc->sc_count = 1; } if (sc->sc_count < POWSW_THRESHOLD) { callout_schedule(&sc->sc_callout, 1); } else { /* switch seems stable */ DEBUG_LOG_PRINT(); if (sc->sc_last_sw == sc->sc_prev) { /* switch state is not changed, it was a noise */ DPRINTF(" ignore(sw=%d,prev=%d)\n", sc->sc_last_sw, sc->sc_prev); } else { /* switch state has been changed */ sc->sc_prev = sc->sc_last_sw; powsw_set_aer(sc, 1 - sc->sc_prev); sysmon_task_queue_sched(0, powsw_pswitch_event, sc); } powsw_reset_counter(sc); mfp_bit_set_ierb(sc->sc_mask); // enable interrupt } splx(s); }
static void clientUtils_GpsStateThread( void* arg ) { int started = 0; int highestFD = 0; fd_set rset,wset,eset; struct timeval tv; int cnt; t_clientUtils_GpsState *gpsState = (t_clientUtils_GpsState *)arg; DEBUG_LOG_PRINT_LEV2(("%s: gps thread running, value of gpsState.ExitGpsThread : %d\n",__FUNCTION__, gpsState->exitThread)); // now loop for (;gpsState->exitThread == 0;) { RECONNECT_SUCCESS: FD_ZERO(&rset); FD_ZERO(&wset); FD_ZERO(&eset); FD_SET( gpsState->fd, &rset ); highestFD = gpsState->fd; if( NULL != gpsState->callbackFdUpdate ) { int vl_Index, maxFd; maxFd = gpsState->callbackFdUpdate( &rset , &wset , &eset ); DEBUG_LOG_PRINT_LEV2(("%s: gpsState->callbackFdUpdate returned maxFD %d, highestFD %d\n",__FUNCTION__,maxFd,highestFD)); if( maxFd > highestFD ) highestFD = maxFd; } tv.tv_sec = MAX_SECONDS_TO_POLL; tv.tv_usec = 0; cnt = select( highestFD + 1, &rset, &wset, NULL /*&eset*/, &tv ); if( cnt ) { DEBUG_LOG_PRINT_LEV2(("%s: Select returned with %d events\n",__FUNCTION__,cnt)); } else { DEBUG_LOG_PRINT_LEV2(("%s: Select returned with no events\n",__FUNCTION__)); } while( cnt >= 1 ) { if( FD_ISSET( gpsState->fd, &rset) ) { char *p_Buffer; int v_DataLen; int done = FALSE; p_Buffer = (char *) malloc( BUFFER_SIZE ); v_DataLen = 0; do { int32_t bytesRead = 0; bytesRead = recv(gpsState->fd, p_Buffer + v_DataLen , BUFFER_SIZE, MSG_DONTWAIT ); if( bytesRead > 0 ) { if( bytesRead < BUFFER_SIZE ) { /* No further data to be read. All done */ done = TRUE; v_DataLen += bytesRead; } else { /* Re allocate memory and copy it over */ char *p_Tmp; /* +LMSqc54521 */ /* Update current data size */ v_DataLen += bytesRead; /* -LMSqc54521 */ p_Tmp = realloc( p_Buffer , v_DataLen + BUFFER_SIZE ); if( p_Tmp != NULL ) { p_Buffer = p_Tmp; /* +LMSqc54521 */ //v_DataLen += BUFFER_SIZE; /* -LMSqc54521 */ done = FALSE; } else { done = TRUE; DEBUG_LOG_PRINT_LEV2(("%s: Realloc Failed!!!!",__FUNCTION__)); } } } else if( (bytesRead == 0) || ((bytesRead < 0) && (!((errno == EAGAIN) || (errno == EWOULDBLOCK)))) ) { bool reconnect = FALSE; if(!((errno == EAGAIN) || (errno == EWOULDBLOCK))) { DEBUG_LOG_PRINT_LEV2(("%s : Error occured : %s hence attempting to reconnect\n",__FUNCTION__, strerror(errno))); } if(p_Buffer != NULL) { free( p_Buffer ); p_Buffer = NULL; } /* Attempt to reconnect agpsd->lbsd */ reconnect = clientUtils_Reconnect(gpsState); if (FALSE == reconnect) { DEBUG_LOG_PRINT(("%s: Error in reconnection to LBSD\n",__FUNCTION__)); DEBUG_LOG_PRINT_LEV2(("Socket closed at lbsd")); /* if this connection is closed, terminate all existing connections and close the thread */ gpsState->exitThread = 1; gpsState->init = 0; close( gpsState->fd ); return; } else { /* Go back to the beginning of this thread as we may have lost context with lbsd */ DEBUG_LOG_PRINT(("%s: SUCCESS: Connection to LBSD re-established.\n",__FUNCTION__)); goto RECONNECT_SUCCESS; } } else { DEBUG_LOG_PRINT_LEV2(("Error occured, errno: %d\n", errno)); /* FIDO 370798 : Dont free memory when len size is exactly 1024 bytes*/ if (v_DataLen != BUFFER_SIZE) { if(p_Buffer != NULL) { free( p_Buffer ); p_Buffer = NULL; } v_DataLen = 0; } done = TRUE; /* FIDO 370798 Dont free memory when len size is exactly 1024 bytes*/ } } while( !done ); if( v_DataLen > 0) { uint32_t bytesProcessed = 0; t_agps_Command command; DEBUG_LOG_PRINT_LEV2(("Data received, No. of bytes received is %d\n", v_DataLen)); // @todo : Does not correctly handle case where incorrect data is received! while( bytesProcessed < v_DataLen ) { int bytesDecoded; t_Buffer v_ReadBuffer; v_ReadBuffer.p_Buffer = p_Buffer + bytesProcessed; v_ReadBuffer.v_Length = v_DataLen - bytesProcessed; bytesDecoded = agpscodec_DecodeCommand(&command, v_ReadBuffer ); bytesProcessed += bytesDecoded; DEBUG_LOG_PRINT_LEV2(("Received data : %d bytes decoded to command\n" ,bytesProcessed )); /* +LMSqc22572 -Anil */ if( command.type != SOCKET_AGPS_STATUS ) receivedCommandType = command.type; /* +LMSqc22572 -Anil */ /* Now send the command for further processing */ /* +ER:270717 */ if( bytesDecoded ) { gpsState->callback( command ); if(command.data != NULL) { if(command.data->pData != NULL) { free(command.data->pData); command.data->pData = NULL; } free(command.data); command.data = NULL; } } /* -ER:270717 */ else { /* CDA: Needs review */ #define AGPS_CODEC_HEADER 0xA53C7E5A uint32_t index = bytesProcessed; uint32_t endOfBuffer = v_DataLen; uint32_t HeaderCheck = 0; unsigned char isHeaderFound = 0; if(v_DataLen >= sizeof(HeaderCheck) ) { for( ; index < endOfBuffer - (int)(sizeof(HeaderCheck)-1) && isHeaderFound == 0; index++ ) { memcpy(&HeaderCheck, p_Buffer+index, sizeof(HeaderCheck)); if( HeaderCheck == AGPS_CODEC_HEADER ) { DEBUG_LOG_PRINT_LEV2(("Header found")); bytesProcessed = index; isHeaderFound = 1; } } if( isHeaderFound == 0 ) { break; } } else { DEBUG_LOG_PRINT_LEV2(("Data size is less than header size")); break; } } /* -ER:270717 */ } } /* +LMSqc54521 */ if(p_Buffer != NULL) { free(p_Buffer); p_Buffer = NULL; } /* -LMSqc54521 */ } if( NULL != gpsState->callbackEventUpdate ) { cnt -= gpsState->callbackEventUpdate(&rset , &wset , &eset ); } --cnt; } } }
/***************************************************************************** * clientUtils_Reconnect *****************************************************************************/ bool clientUtils_Reconnect(t_clientUtils_GpsState *gpsState) { int conStatus = -1; int retryCount = 0; bool done = FALSE; DEBUG_LOG_PRINT(("%s: Enter\n",__FUNCTION__)); shutdown( gpsState->fd , SHUT_WR ); close( gpsState->fd ); usleep(RECONNECT_WAIT_DURATION_IN_MUSEC); gpsState->fd = socket(AF_INET, SOCK_STREAM, 0); /* init socket descriptor */ if ( gpsState->fd < 0 ) { DEBUG_LOG_PRINT(("%s Error: Socket creation: %s\n",__FUNCTION__, strerror(errno))); return FALSE; } /*** PLACE DATA IN sockaddr_in struct ***/ bzero(&gpsState->s_in, sizeof(gpsState->s_in)); gpsState->s_in.sin_family = AF_INET; gpsState->s_in.sin_addr.s_addr = htonl(INADDR_SERVER_HOST); gpsState->s_in.sin_port = htons(PORT); while ( (retryCount < RECONNECT_MAX_RETRIES) ) { conStatus = connect(gpsState->fd, (struct sockaddr *)&gpsState->s_in, sizeof(gpsState->s_in)); if (conStatus != 0 ) { DEBUG_LOG_PRINT_LEV2((" %s ERROR: Connect Error. Return %d: %s\n", __FUNCTION__,conStatus,strerror(errno))); retryCount++; DEBUG_LOG_PRINT((" %s: Retrying: %d time\n", __FUNCTION__,retryCount)); usleep(RECONNECT_WAIT_DURATION_IN_MUSEC); } else { DEBUG_LOG_PRINT(("%s: SUCCESS: Connect call Successful. Return %d\n",__FUNCTION__,conStatus)); break; } } if (conStatus != 0) { DEBUG_LOG_PRINT(("%s: Error: Connection failed %d times. Application needs to restart\n",__FUNCTION__,retryCount)); return FALSE; } if( gpsState->init == 1 ) { clientUtils_FdSetNonBlock(gpsState->fd); gpsState->statusValue = SOCKET_AGPS_NO_STATUS_AVAILABLE; gpsState->reconnectReq = TRUE; gpsState->callbackReconnect(); } DEBUG_LOG_PRINT_LEV2(("clientUtils_Reconnect: SUCCESS: Reconnected to LBSD")); return TRUE; }
/***************************************************************************** * clientUtils_InitGpsState : *****************************************************************************/ bool clientUtils_InitGpsState(t_clientUtils_GpsState *gpsState) { bool reconnect = FALSE; if( gpsState->init != 1 ) { int conStatus; gpsState->fd = socket(AF_INET, SOCK_STREAM, 0); /* init socket descriptor */ if ( gpsState->fd < 0 ) { DEBUG_LOG_PRINT_LEV2(("Error: socketOpen\n")); return FALSE; } /*** PLACE DATA IN sockaddr_in struct ***/ bzero(&gpsState->s_in, sizeof(gpsState->s_in)); gpsState->s_in.sin_family = AF_INET; gpsState->s_in.sin_addr.s_addr = htonl(INADDR_SERVER_HOST); gpsState->s_in.sin_port = htons(PORT); gpsState->exitThread = 0; conStatus = connect(gpsState->fd, (struct sockaddr *)&gpsState->s_in, sizeof(gpsState->s_in)); DEBUG_LOG_PRINT_LEV2((" %s Connect Error : %d\n", __FUNCTION__,errno)); if (conStatus < 0) { DEBUG_LOG_PRINT_LEV2(("Error: connecting\n")); DEBUG_LOG_PRINT_LEV2(("Attempting Reconnection")); /* Attempt to reconnect agpsd->lbsd */ reconnect = clientUtils_Reconnect(gpsState); if (FALSE == reconnect) { DEBUG_LOG_PRINT(("%s: Error in reconnection to LBSD\n",__FUNCTION__)); DEBUG_LOG_PRINT_LEV2(("Socket closed at lbsd")); /* if this connection is closed, terminate all existing connections and close the thread */ gpsState->exitThread = 1; gpsState->init = 0; close( gpsState->fd ); return FALSE; } else { DEBUG_LOG_PRINT(("%s: SUCCESS: Connection to LBSD re-established.\n",__FUNCTION__)); } } gpsState->init = 1; clientUtils_FdSetNonBlock(gpsState->fd); if ( pthread_create( &(gpsState->thread), NULL, (void *)&clientUtils_GpsStateThread, (void *)gpsState ) != 0 ) { DEBUG_LOG_PRINT_LEV2(("could not create gps thread: %s", strerror(errno))); return FALSE; } gpsState->statusValue = SOCKET_AGPS_NO_STATUS_AVAILABLE; DEBUG_LOG_PRINT_LEV2(("gps state initialized")); } return TRUE; }
static int TlsValidateNetworkInterface(int sock) { struct ifconf iflist; struct ifreq *ifr; int numIf,i,done; int num_size = 1; int no_more_buffsize = 0; if((vg_NetworkInterface == NULL) || (strlen(vg_NetworkInterface) == 0)) { DEBUG_LOG_PRINT(("TlsValidateNetworkInterface : No Network i/f set")); return FALSE; } /* Retrieve the interfaces */ iflist.ifc_len = TLS_NETWORK_LIST_BUFF_SIZE; iflist.ifc_buf = (char *) malloc (iflist.ifc_len); do{ DEBUG_LOG_PRINT(("TlsValidateNetworkInterface : ifc_len assigned %d bytes",iflist.ifc_len)); if(ioctl(sock, SIOCGIFCONF, &iflist) < 0) { DEBUG_LOG_PRINT(("ERROR : ioctl(%d,SIOCGIFCONF) Failed" , sock)); free (iflist.ifc_buf); iflist.ifc_buf = NULL; return FALSE; } else { if (iflist.ifc_len == TLS_NETWORK_LIST_BUFF_SIZE * num_size) { DEBUG_LOG_PRINT(("TlsValidateNetworkInterface : More Network list expected, allocate another 512 bytes")); num_size++; iflist.ifc_len = TLS_NETWORK_LIST_BUFF_SIZE * num_size; iflist.ifc_buf = (char *) realloc (iflist.ifc_buf,iflist.ifc_len); } else { no_more_buffsize = 1; } } } while (!no_more_buffsize); ifr = iflist.ifc_req; numIf = iflist.ifc_len / sizeof(struct ifreq); /* Iterate through the list of interfaces. */ for(i = 0,done=FALSE; i < numIf && !done; i++) { struct ifreq *item = &ifr[i]; /* Show the device name and IP address */ DEBUG_LOG_PRINT(("%s: IP %s\n", \ item->ifr_name,\ inet_ntoa(((struct sockaddr_in *)&item->ifr_addr)->sin_addr))); if( strcmp(vg_NetworkInterface,item->ifr_name) == 0 ) { done = TRUE; } else if (strcmp("pdp0",item->ifr_name) == 0) { DEBUG_LOG_PRINT(("TlsValidateNetworkInterface:found default PDP0")); //todo // set the local flags to manage pdp0 by default. // But now , just consider number of ifs > 0 , to have pdp0 from GpsLocationProvider. } } if (done == FALSE && numIf > 0) { strcpy(vg_NetworkInterface, "pdp0"); DEBUG_LOG_PRINT(("TlsValidateNetworkInterface:Set default :%s,numberIFs:%d ",vg_NetworkInterface,numIf )); done = TRUE; } free(iflist.ifc_buf); iflist.ifc_buf = NULL; return done; }