/** 
 * 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()*/
Exemplo n.º 2
0
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 */