diff --git a/mac_receiver.c b/mac_receiver.c index 6b7ffde..8dc3fe9 100644 --- a/mac_receiver.c +++ b/mac_receiver.c @@ -4,25 +4,6 @@ #include #include - -typedef union { - struct { - uint8_t sapi: 3; // MSB - uint8_t addr: 4; - uint8_t nothing: 1; // LSB - }; - uint8_t raw; -} Adresse; - -typedef union { - struct { - uint8_t ack: 1; // MSB - uint8_t read: 1; - uint8_t checksum: 6; // LSB - }; - uint8_t raw; -} Status; - void send_DATA_IND(Adresse source, Adresse destination, uint8_t* dataFramePtr) { struct queueMsg_t queueMsg; // queue message osStatus_t retCode; // return error code diff --git a/mac_sender.c b/mac_sender.c index 4f669d4..b5303c1 100644 --- a/mac_sender.c +++ b/mac_sender.c @@ -10,7 +10,12 @@ const osMessageQueueAttr_t queue_macData_attr = { void MacSender(void *argument) { struct queueMsg_t queueMsg; // queue message uint8_t* msg; + Adresse src; + Adresse dst; + uint8_t length; + Status status; osStatus_t retCode; // return error code + char* strPtr; lastToken = osMemoryPoolAlloc(memPool, osWaitForever); queue_macData_id = osMessageQueueNew(4, sizeof(struct queueMsg_t), &queue_macData_attr); @@ -54,8 +59,8 @@ void MacSender(void *argument) { osWaitForever); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); - // Send msg from internal queue if exist - while (osMemoryPoolGetCount(queue_macData_id) != 0) { // Message in Queue + // Send one msg from internal queue if exist + if (osMemoryPoolGetCount(queue_macData_id) != 0) { // Message in Queue retCode = osMessageQueueGet( queue_macData_id, &queueMsg, @@ -87,7 +92,35 @@ void MacSender(void *argument) { // DATABACK MESSAGE //---------------------------------------------------------------------- case DATABACK: { + src.raw = msg[0]; + dst.raw = msg[1]; + length = msg[2]; + status.raw = msg[3+length]; + if(status.read == 0) { + if(status.ack == 0) { + msg = osMemoryPoolAlloc(memPool, osWaitForever); + queueMsg.type = TO_PHY; + queueMsg.anyPtr = + + } else { + + } + + } else { + strPtr = osMemoryPoolAlloc(memPool, osWaitForever); + sprintf(strPtr, "Dest. %d couldn't read message from %d\0", dst.add+1, src.addr+1); + queueMsg.type = MAC_ERROR; + queueMsg.addr = src.addr; + queueMsg.sapi = src.sapi; + queueMsg.anyPtr = strPtr; + retCode = osMessageQueuePut( + queue_lcd_id, + &queueMsg, + osPriorityNormal, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + } break; } @@ -135,7 +168,40 @@ void MacSender(void *argument) { // DATA MESSAGE //---------------------------------------------------------------------- case DATA_IND: { + dst.addr = queueMsg.addr; + dst.sapi = queueMsg.sapi; + dst.nothing = 0; + src.addr = gTokenInterface.myAddress; + src.sapi = queueMsg.sapi; + src.nothing = 0; + length = strlen(queueMsg.anyPtr); + if(dst.addr == BROADCAST_ADDRESS) { + status.read = 1; + status.ack = 1; + } else { + status.read = 0; + status.ack = 0; + } + + msg = osMemoryPoolAlloc(memPool, osWaitForever); + msg[0] = src.raw; + msg[1] = dst.raw; + msg[2] = length; + memcpy(&msg[3], queueMsg.anyPtr, length); + status.checksum = Checksum(msg); + msg[3+length] = status.raw; + + retCode = osMemoryPoolFree(memPool, queueMsg.anyPtr); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + queueMsg.anyPtr = msg; + queueMsg.type = TO_PHY; + retCode = osMessageQueuePut( + queue_macData_id, + &queueMsg, + osPriorityNormal, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); break; } diff --git a/main.h b/main.h index d2711e3..7375580 100644 --- a/main.h +++ b/main.h @@ -118,3 +118,24 @@ struct queueMsg_t uint8_t addr; ///< the source or destination address uint8_t sapi; ///< the source or destination SAPI }; + +//-------------------------------------------------------------------------------- +// The mac control union +//-------------------------------------------------------------------------------- +typedef union { + struct { + uint8_t sapi: 3; // MSB + uint8_t addr: 4; + uint8_t nothing: 1; // LSB + }; + uint8_t raw; +} Adresse; + +typedef union { + struct { + uint8_t ack: 1; // MSB + uint8_t read: 1; + uint8_t checksum: 6; // LSB + }; + uint8_t raw; +} Status; \ No newline at end of file