/*
 * Fetch data from a mbox.Wait for at most timeout millisecs
 * Return -1 if timed out otherwise time spent waiting.
 */
u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **data, u32_t timeout)
{
   long addr=0L;
   int end_time = 0, start_time = 0;

   if (timeout) {
      start_time = OS_GetTime();
      if(OS_GetMailTimed (mbox, &addr,timeout)) {
         *data = NULL;
         return SYS_ARCH_TIMEOUT;
      }
      end_time = OS_GetTime();

   } else {
      OS_GetMail(mbox,&addr);
   }

   if(data) {
      if (addr == (long)&dummy_msg)
         *data = NULL;
      else
         *data=(void*)addr;

      //printf("fetch mbox:0x%x = 0x%x\r\n",mbox,*data);
   }else{
      //printf("fetch mbox:0x%x\r\n",mbox);
   }



   return (end_time - start_time);

}
Esempio n. 2
0
/*-----------------------------------------------------------------------------------
 * Blocks the thread until a message arrives in the mailbox, but does
 * not block the thread longer than "timeout" milliseconds (similar to
 * the sys_arch_sem_wait() function). The "msg" argument is a result
 * parameter that is set by the function (i.e., by doing "*msg =
 * ptr"). The "msg" parameter maybe NULL to indicate that the message
 * should be dropped.
 *
 * The return values are the same as for the sys_arch_sem_wait() function:
 * Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
 * timeout.
 *
 * Note that a function with a similar name, sys_mbox_fetch(), is
 * implemented by lwIP.
 */
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, /*@null@*/ /*@out@*/ void **msg, u32_t timeout)
{
    void *dummyptr;
    void ** tmp_ptr;
    portTickType start_time, end_time, elapsed_time;
    e_mailbox_t* embox = (e_mailbox_t*)(*mbox);

    // assuming 1 tick is 1ms which is the case for now.
    start_time = OS_GetTime32();
    /* inherited this code from freeRTOS and LWIP must be calling it in this way..*/
    if ( msg == NULL )
    {
        tmp_ptr = &dummyptr;
    }
    else
    {
        tmp_ptr = msg;
    }

    if ( timeout != 0 )
    {
        // Return values 0 means success,1 means timeout
        if (0 == OS_GetMailTimed(&embox->mbox, tmp_ptr, timeout))
        {
            end_time = OS_GetTime32();
            elapsed_time = end_time - start_time;
            if ( elapsed_time == 0 )
            {
                elapsed_time = (portTickType) 1;
            }
            return ( elapsed_time );
        }
        else /* timed out blocking for message */
        {
            if ( msg != NULL )
            {
                *msg = NULL;
            }
            return SYS_ARCH_TIMEOUT;
        }
    }
    else /* block forever for a message. */
    {
        OS_GetMail(&embox->mbox, &(*tmp_ptr));
        end_time = OS_GetTime32();
        elapsed_time = end_time - start_time;
        if ( elapsed_time == 0 )
        {
            elapsed_time = (portTickType) 1;
        }
        return ( elapsed_time ); /* return time blocked TBD test */
    }
}