uint8_t os_msg_post( taskproctype taskproc, Msg_t *msg, uint16_t delay, uint16_t period ) { #if( N_QUEUES > 0 ) OSMsgQ_t *temp; MsgQ_t queue; uint8_t result; uint8_t *data; uint8_t i; result = TaskMsgQGet( taskproc, &queue ); if ( result == MSG_QUEUE_UNDEF ) { return result; } temp = &msgQList[ queue ]; if ( MsgQFull( temp ) == 1 ) { return MSG_QUEUE_FULL; } data = (uint8_t*)( (uint16_t)temp->msgPool + (temp->head) * (temp->messageSize) ); msg->delay = delay; msg->reload = period; for ( i = 0; i < temp->messageSize; ++i ) { data[ i ] = ((uint8_t*)msg)[i]; } /* Look for buffer wrap around */ if ( temp->head == 0 ) { temp->head = temp->size - 1; } else { temp->head--; } /* Signal the change event */ os_signal_event(temp->change); os_event_set_signaling_tid( temp->change, running_tid ); return MSG_QUEUE_POSTED; #else return 0; #endif }
uint8_t os_msg_receive( Msg_t *msg, MsgQ_t queue ) { #if( N_QUEUES > 0 ) OSMsgQ_t *temp; uint8_t *data; uint8_t i; uint8_t found; temp = &msgQList[ queue ]; if ( MsgQEmpty( temp ) == 1 ) { return MSG_QUEUE_EMPTY; } /* If all messages have a delay > 0 we consider the queue as empty */ if ( MsgQAllDelayed( temp ) == 1 ) { return MSG_QUEUE_EMPTY; } /* At least one message is ready to be delivered. Find it! */ found = 0; while ( found == 0 ) { data = (uint8_t*)( (Mem_t)temp->msgPool + (temp->tail) * (temp->messageSize) ); for ( i = 0; i < temp->messageSize; ++i ) { ((uint8_t*)msg)[ i ] = data[ i ]; } /* Look for buffer wrap around */ if ( temp->tail == 0 ) { temp->tail = temp->size - 1; } else { temp->tail--; } uint8_t messagePeriodic = ( msg->reload > 0 ); uint8_t messageTimedOut = ( msg->delay == 0 ); if ( messageTimedOut ){ found = 1; if ( messagePeriodic ) { msg->delay = msg->reload; } } /* Put the message back at head position if delay > 0, or if it is a periodic message that timed out */ if (( !messageTimedOut ) || ( messagePeriodic && messageTimedOut )) { data = (uint8_t*)( (Mem_t)temp->msgPool + (temp->head) * (temp->messageSize) ); for ( i = 0; i < temp->messageSize; ++i ) { data[ i ] = ((uint8_t*)msg)[i]; } /* Look for buffer wrap around */ if ( temp->head == 0 ) { temp->head = temp->size - 1; } else { temp->head--; } } } /* Signal the change event */ os_signal_event(temp->change); os_event_set_signaling_tid( temp->change, running_tid ); return MSG_QUEUE_RECEIVED; #else return 0; #endif }