diff --git a/mac_sender.c b/mac_sender.c index c0684d6..6c1376f 100644 --- a/mac_sender.c +++ b/mac_sender.c @@ -1,10 +1,20 @@ #include "main.h" -void MacSender(void *argument) -{ - // TODO +#include + +uint8_t* lastToken; +osMessageQueueId_t queue_macData_id; +const osMessageQueueAttr_t queue_macData_attr = { + .name = "MAC_DATA" +}; + +void MacSender(void *argument) { struct queueMsg_t queueMsg; // queue message uint8_t* msg; osStatus_t retCode; // return error code + + lastToken = osMemoryPoolAlloc(memPool, osWaitForever); + queue_macData_id = osMessageQueueNew(4, sizeof(struct queueMsg_t), &queue_macData_attr); + for(;;) { //---------------------------------------------------------------------------- @@ -16,15 +26,50 @@ void MacSender(void *argument) NULL, osWaitForever); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + msg = queueMsg.anyPtr; switch(queueMsg.type) { - case TOKEN: - msg[gTokenInterface.myAddress] = gTokenInterface.station_list[gTokenInterface.myAddress]; - for(uint8_t i = 1; i < TOKENSIZE-2; i++) { - gTokenInterface.station_list[i] = msg[i]; + case TOKEN: { + // Get token and save it + memcpy(lastToken, msg, TOKENSIZE-2); + + // update token + lastToken[gTokenInterface.myAddress+1] = (0x1 << TIME_SAPI) + (gTokenInterface.connected << CHAT_SAPI); + for(uint8_t i = 1; i < sizeof(gTokenInterface.station_list); i++) { + gTokenInterface.station_list[i-1] = lastToken[i]; } + + // send to lcd + queueMsg.type = TOKEN_LIST; + queueMsg.anyPtr = lastToken; + retCode = osMessageQueuePut( + queue_lcd_id, + &queueMsg, + osPriorityNormal, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + + // Send msg from internal queue if exist + while (osMemoryPoolGetCount(queue_macData_id) != 0) { // Message in Queue + retCode = osMessageQueueGet( + queue_macData_id, + &queueMsg, + NULL, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + queueMsg.type = TO_PHY; + retCode = osMessageQueuePut( + queue_phyS_id, + &queueMsg, + osPriorityNormal, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + } + + // Send token + queueMsg.anyPtr = lastToken; queueMsg.type = TO_PHY; retCode = osMessageQueuePut( queue_phyS_id, @@ -33,25 +78,26 @@ void MacSender(void *argument) osWaitForever); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); break; + } - case DATABACK: - + case DATABACK: { + break; + } - case NEW_TOKEN: - msg = osMemoryPoolAlloc(memPool, osWaitForever); - msg[0] = TOKEN_TAG; + case NEW_TOKEN: { + lastToken[0] = TOKEN_TAG; for(uint8_t i = 1; i < sizeof(TOKENSIZE-2); i++) { - msg[i] = 0; + lastToken[i] = 0; } gTokenInterface.station_list[gTokenInterface.myAddress] = (0x1 << TIME_SAPI) + (gTokenInterface.connected << CHAT_SAPI); - msg[gTokenInterface.myAddress+1] = gTokenInterface.station_list[gTokenInterface.myAddress]; + lastToken[gTokenInterface.myAddress+1] = gTokenInterface.station_list[gTokenInterface.myAddress]; queueMsg.type = TO_PHY; - queueMsg.anyPtr = msg; + queueMsg.anyPtr = lastToken; retCode = osMessageQueuePut( queue_phyS_id, @@ -60,26 +106,31 @@ void MacSender(void *argument) osWaitForever); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); break; + } - - case START: - + + case START: { + // Do nothing, don't care to receive start break; + } - - case STOP: - + + case STOP: { + // Do nothing, don't care to receive stop break; + } - - case DATA_IND: - + + case DATA_IND: { + break; + } + - - default: - + default: { + break; + } } } diff --git a/main.h b/main.h index b11d5df..0d4eaec 100644 --- a/main.h +++ b/main.h @@ -27,13 +27,13 @@ #define TIME_SAPI 0x03 // sapi time application number (0-7) #define BROADCAST_ADDRESS 0x0F // broadcast address #define TOKEN_TAG 0xFF // tag of tokenring frame -#define TOKENSIZE 19 // size of a token frame +#define TOKENSIZE 19 // size of a token frame (16 stations + 1 tag + start + end) #define STX 0x02 // any frame start char #define ETX 0x03 // any frame end char #define CONTINUE 0x0 // for check return code halt //-------------------------------------------------------------------------------- -// identifiers used in more the one file (thread) +// identifiers used in more the one file (thread) //-------------------------------------------------------------------------------- extern GListener gl; extern osMemoryPoolId_t memPool;