/** * @brief Read data from a socket. * @param socket_handle[in] Handle to the socket to read from. * @param buffer[out] Buffer to read data into. * @param size[in] Size of the data buffer. * @param bytes_read[out] Number of bytes read. * @return Result code. */ tSIRF_RESULT SIRF_PAL_NET_Read( tSIRF_SOCKET socket_handle, tSIRF_UINT8 *buffer, tSIRF_UINT32 size, tSIRF_UINT32 *bytes_read ) { int result = 0; DEBUGCHK(SIRF_PAL_NET_INVALID_SOCKET != socket_handle); DEBUGCHK(bytes_read); *bytes_read = 0; do { result = recv(socket_handle, (char*)(buffer+*bytes_read), size-*bytes_read, 0); } while ( 0 > result && EINTR == errno ); if ( 0 > result ) { DEBUGMSG(1,(DEBUGTXT("recv() failed, errno=%d, size=%d, *bytes_read=%d\n"), errno, size, *bytes_read)); SIRF_PAL_OS_THREAD_Sleep(100); return SIRF_PAL_NET_ERROR; } *bytes_read = result; return 0 < result ? SIRF_SUCCESS : SIRF_PAL_NET_CONNECTION_CLOSED; } /* SIRF_PAL_NET_Read() */
/** * Read thread to process messages from or to the GPRS modem */ static SIRF_PAL_OS_THREAD_DECL SIRF_GPRS_AT_COMMAND_ReadThread( SIRF_PAL_OS_THREAD_PARAMS ) { tSIRF_UINT32 wait_start; tSIRF_UINT32 wait_end; tSIRF_UINT32 wait_elapsed; tSIRF_UINT32 timeout; tSIRF_UINT32 result; tSIRF_UINT32 semaphore_result; SIRF_PAL_OS_THREAD_UNUSED; SIRF_PAL_OS_THREAD_START(); gprs_at_command.semaphore_timeout = SIRF_TIMEOUT_INFINITE; /*-------------------------------------------------------------------------*/ /* Main loop */ /*-------------------------------------------------------------------------*/ while ( gprs_at_command.thread_running ) { wait_start = SIRF_PAL_OS_TIME_SystemTime(); semaphore_result = SIRF_PAL_OS_SEMAPHORE_Wait(gprs_at_command.semaphore, gprs_at_command.semaphore_timeout); /* We just woke up. Check to see if we've been asked to exit */ if ( !gprs_at_command.thread_running ) { break; } /* Record how long we were waiting, just in case what we were looking * for did not arrive */ wait_end = SIRF_PAL_OS_TIME_SystemTime(); /* 32 bits of timer will roll over every ~50 days, but we need to be * safe */ if (wait_end >= wait_start) { wait_elapsed = wait_end - wait_start; } else { /* Roll over case */ tSIRF_UINT64 temp; temp = wait_end + ULONG_MAX + 1 - wait_start; /* This should now be less than 32 bits */ wait_elapsed = (tSIRF_UINT32) temp; } /* If we are in TCP Data mode process all of these messages */ if (GPRS_SERVER_STATE_TCP_IP_DATA == gprs_at_command.state) { result = process_next_msg( &gprs_at_command.listener[(tSIRF_UINT32)gprs_at_command.data_handle].msg_queue_mas, handle_mas_msg, &timeout); if (SIRF_SUCCESS != result) { /* really bad error occured here, send it to the listeners */ handle_unexpected_result(result); SIRF_PAL_OS_THREAD_Sleep(10); } /* For TCP Data the timeout values are either 0 or GPRS_DATA_TIMEOUT * as the data connection should be shut down if idle for the * GPRS_DATA_TIMEOUT amount. If a message was processed the timeout * value is set to 0 so that the next one can be processed */ else if (SIRF_TIMEOUT_INFINITE == timeout) { result = process_next_msg( &gprs_at_command.listener[(tSIRF_UINT32)gprs_at_command.data_handle].msg_queue_mas_input, handle_mas_input_msg, &timeout); if (SIRF_SUCCESS != result) { /* really bad error occured here, send it to the listeners */ handle_unexpected_result(result); SIRF_PAL_OS_THREAD_Sleep(10); } /* At this point check to see if the timeout has elapsed and we need * to transition from data to at command mode */ if (SIRF_SUCCESS != semaphore_result && wait_elapsed >= GPRS_DATA_TIMEOUT) { gprs_switch_from_data_mode(); gprs_at_command.semaphore_timeout = SIRF_TIMEOUT_INFINITE; } else { gprs_at_command.semaphore_timeout = GPRS_DATA_TIMEOUT; } } continue; } /* If we make it to this line of code the state is either * GPRS_SERVER_STATE_AT_COMMAND * GPRS_SERVER_STATE_AT_RESPONSE * * Handle responses and unsolicited messages next */ result = process_next_msg( &gprs_at_command.msg_queue_gprs, handle_gprs_msg, &timeout); if (SIRF_SUCCESS != result) { /* really bad error occured here, send it to the listeners */ handle_unexpected_result(result); SIRF_PAL_OS_THREAD_Sleep(10); continue; } /* SIRF_TIMEOUT_INFINITE means nothing was processed. * Other values indicate that a message was processed */ if (SIRF_TIMEOUT_INFINITE != timeout) { /* Loop around, don't wait and look for another message */ gprs_at_command.semaphore_timeout = 0; gprs_at_command.command_timeout_left = timeout; continue; } /* Now check the state to see if we are still waiting for a response. If * we are then we can't process any input messages yet and must wait until * the timout before proceding. Check to see if the timeout has occured * and if not update the timeout values */ else if (GPRS_SERVER_STATE_AT_RESPONSE == gprs_at_command.state ) { if ((SIRF_SUCCESS != semaphore_result) && (wait_elapsed >= gprs_at_command.command_timeout_left)) { /* This is an error case. We timed out waiting for the expected * response, Send up an error message and continue */ handle_response_timeout(); gprs_at_command.state = GPRS_SERVER_STATE_AT_COMMAND; } else { /* Adjust the timeout for the time elapsed waiting */ gprs_at_command.command_timeout_left -= wait_elapsed; gprs_at_command.semaphore_timeout = gprs_at_command.command_timeout_left; } /* In either case, continue */ continue; } result = process_next_msg( &gprs_at_command.msg_queue_gprs_input, handle_input_msg, &gprs_at_command.semaphore_timeout); if (SIRF_SUCCESS != result) { /* really bad error occured here, send it to the listeners */ handle_unexpected_result(result); SIRF_PAL_OS_THREAD_Sleep(10); } else { gprs_at_command.command_timeout_left = gprs_at_command.semaphore_timeout; } } /* while()*/ SIRF_PAL_OS_THREAD_RETURN(); } /* SIRF_EXT_UART_ReadThread()*/
tSIRF_RESULT SIRF_EXT_AUX_Create(tSIRF_UINT32 port, tSIRF_UINT32 baud_rate) { tSIRF_RESULT tRet; /* Create write mutex */ tRet = SIRF_PAL_OS_MUTEX_Create(&c_AUX_COM_WRITE_Critical); if (SIRF_SUCCESS == tRet) { /* programmers note: this allocates a COM channel for each potential input port; * right now this is always a COM port, but when we go to LPL, it will become * a programmatic interface. Also, fyi, we only support one COM port here for now. */ tRet = SIRF_PAL_OS_SEMAPHORE_Create(&c_aux_open_sem, 0); if (SIRF_SUCCESS == tRet) { tRet = SIRF_PAL_COM_Create(&c_ExtAuxHandle); if (SIRF_SUCCESS == tRet) { /* rds programmers note; c_auxno_set is really a counting semaphore * that clears when the thread starts running in context */ c_auxno_set = 1; #if defined(TOOLCHAIN_VC8) #pragma warning (disable : 4054) /* cast from function to data pointer */ #endif tRet = SIRF_PAL_OS_THREAD_Create(SIRF_EXT_AUX_THREAD_1, (tSIRF_HANDLE)SIRF_EXT_AUX_CommThread, &c_hCommReaderThread); #if defined(TOOLCHAIN_VC8) #pragma warning (default : 4054) /* cast from function to data pointer */ #endif if (SIRF_SUCCESS == tRet) { SIRF_PAL_OS_THREAD_Sleep ( 10 ); tRet = SIRF_EXT_AUX_Open( 0, port, baud_rate ); while (c_auxno_set) { SIRF_PAL_OS_THREAD_Sleep(100); } } else { SIRF_PAL_OS_SEMAPHORE_Delete(c_aux_open_sem); SIRF_PAL_COM_Delete(c_ExtAuxHandle); SIRF_EXT_AUX_Close(0); } /* Thread Create */ } else { SIRF_PAL_OS_SEMAPHORE_Delete(c_aux_open_sem); } /* COM_Create */ } /* SEMAPHORE_Create */ } /* Mutex create */ return tRet; } /* SIRF_EXT_AUX_Create */
/** * @brief Reset the tracker. * @param[in] level Non-zero to put into reset. * @return Success code. */ tSIRF_RESULT SIRF_PAL_HW_WriteRESET( tSIRF_UINT32 level ) { tSIRF_RESULT retVal = SIRF_FAILURE; /* Call appropriate function based on how the ON_OFF line is configured */ switch ( g_userPalConfig.ext_sreset_n_line_usage ) { case UI_CTRL_MODE_HW_RESET_UART_DTR: if ( SIRF_PAL_HW_RESET_HIGH == level ) { retVal = SIRF_PAL_COM_UART_ClrDTR( ResetPortHandle ); } else { retVal = SIRF_PAL_COM_UART_SetDTR( ResetPortHandle ); } break; case UI_CTRL_MODE_HW_RESET_GPIO: { #ifdef NS115 int fd_gpio; char reset_buff[2] = {'1', '0'}; //char reset_on = 1; //char reset_off = 0; fd_gpio = open("sys/class/gpio/gpio60/value", O_RDWR); LOGE("returning fd from GPIO 0x%08x\n", fd_gpio); LOGE("GPS \n"); LOGE("GPS \n"); LOGE("GPS \n"); //LOGE("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA \n"); if(fd_gpio >= 0) { /*write(fd_gpio,&reset_buff[0],1); SIRF_PAL_OS_THREAD_Sleep(10); LOGE("GPS RESET IS HIGH\n");*/ if(level == SIRF_PAL_HW_ON_HIGH) { //SIRF_PAL_OS_THREAD_Sleep(10); //cmd |= 0x40; write(fd_gpio,&reset_buff[0],1); SIRF_PAL_OS_THREAD_Sleep(10); //SIRF_PAL_OS_THREAD_Sleep(10000000); //write(fd_gpio,&reset_on,1); LOGE("GPS RESET IS HIGH\n"); } else { //cmd &= ~0x40; write(fd_gpio,&reset_buff[1],1); //write(fd_gpio,&reset_off,1); LOGE("GPS RESET IS LOW\n"); } close(fd_gpio); } else { LOGE("%s: could not open gpio for GPS reset", __FUNCTION__); } #else int fd_gpio; int ret; unsigned char cmd = 0; struct RWREGS_PARAM parm; parm.start_reg = 0xFC23; parm.byte_cnt = 1; parm.buf = &cmd; fd_gpio = open("/dev/io373x", O_RDWR); LOGE("returning fd from GPIO 0x%08x\n", fd_gpio); //LOGE("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB \n"); if(fd_gpio >= 0) { cmd = 0; parm.start_reg = 0xFC23; ret = ioctl(fd_gpio, ENEEC_IOC_READ_REGS, &parm); if(ret < 0){ LOGE("Read EC 0xFC23 out state error: %d\n", ret); } //LOGE("FC25: 0x%02x\n", (0x08 & cmd)); if(level == SIRF_PAL_HW_ON_HIGH) { SIRF_PAL_OS_THREAD_Sleep(10); cmd |= 0x40; LOGE("GPS RESET ON\n"); } else { cmd &= ~0x40; LOGE("GPS RESET OFF\n"); } ret = ioctl(fd_gpio, ENEEC_IOC_WRITE_REGS, &parm); if(ret < 0){ LOGE("Write EC 0xFC23 error: %d\n", ret); } else { retVal = SIRF_SUCCESS; } close(fd_gpio); LOGE("closed GPIO for reset\n"); } else { LOGE("%s: could not open EC for GPS reset", __FUNCTION__); } #endif break; } case UI_CTRL_MODE_HW_RESET_NONE: retVal = SIRF_SUCCESS; break; default: retVal = SIRF_FAILURE; break; } return retVal; } /* SIRF_PAL_HW_WriteRESET() */
SIRF_PAL_OS_THREAD_DECL SIRF_EXT_AUX_CommThread(SIRF_PAL_OS_THREAD_PARAMS) { tSIRF_UINT32 BytesRead = 0; /* byte count in raw message buffer */ tSIRF_UINT8 Buf[MAX_BUFFER_SIZE]; /* raw message buffer */ /* Unused Parameters. */ SIRF_PAL_OS_THREAD_UNUSED SIRF_PAL_OS_THREAD_START(); b_Aux_Thread_Running = SIRF_TRUE; SIRF_PAL_OS_SEMAPHORE_Wait( c_aux_open_sem, SIRF_PAL_OS_TIMEOUT_INFINITE ); SIRF_PAL_COM_Open( c_ExtAuxHandle, c_ExtAuxPort, SIRF_PAL_COM_FC_NONE, c_ExtAuxBaudRate ); /* the caller is waiting for this to go to zero before moving on to the next port */ c_auxno_set = 0; while ( SIRF_FALSE != b_Aux_Thread_Running ) { tSIRF_RESULT wait; /* sit on this event flag until something arrives for us to do */ wait = SIRF_PAL_COM_Wait( c_ExtAuxHandle, 100 ); if (wait == SIRF_PAL_COM_TIMEOUT) { /* Timed out after 1000ms of nothing, go again. */ continue; } else if (wait != SIRF_SUCCESS) { /* The port failed (perhaps due to a close/open). Yield. */ SIRF_PAL_OS_THREAD_Sleep(100); continue; } /* we might have waited quite awhile; don't artificially delay the user */ if (SIRF_FALSE == b_Aux_Thread_Running) { /* note: this guards against a race condition where we run the rest of this pass while the rest of the code is halted */ break; } /* try to get some work; if it was a false positive, try again */ if (SIRF_PAL_COM_Read(c_ExtAuxHandle, Buf, sizeof(Buf), &BytesRead) != SIRF_SUCCESS) { continue; } if ( BytesRead ) { /* Log all of the incoming data */ #ifdef SIRF_EXT_LOG //!! SIRF_EXT_LOG_Send_Passthrough((unsigned char * ) Buf, BytesRead); /* Is this what we want? */ #endif /* Send the data to the callback function, if it is registered */ if ( f_callback ) { f_callback( (tSIRF_UINT8 *)&Buf, BytesRead ); } } } // while{} SIRF_PAL_COM_Close( c_ExtAuxHandle ); SIRF_PAL_OS_THREAD_RETURN(); } /* SIRF_EXT_AUX_CommThread */