diff --git a/mac_receiver.c b/mac_receiver.c index ca2d104..b69a186 100644 --- a/mac_receiver.c +++ b/mac_receiver.c @@ -4,29 +4,43 @@ #include #include +/** + * @brief Senda copy of DATA_IND to the right application queue + * + * @param source source Address (addr, sapi) + * @param destination destination Address (addr, sapi) + * @param dataFramePtr pointer to the data frame + +*/ void send_DATA_IND(Adresse source, Adresse destination, uint8_t* dataFramePtr) { struct queueMsg_t queueMsg; // queue message osStatus_t retCode; // return error code - char* strPtr; - - queueMsg.type = DATA_IND; - queueMsg.addr = source.addr; - queueMsg.sapi = source.sapi; + char* strPtr; // string pointer of the message + // Get memmory for new message and test if succed strPtr = osMemoryPoolAlloc(memPool, 0); if(strPtr == NULL) { assert(false); } + // Copy data from dataFramePtr to strPtrt8_t* dataFramePtr) { for(uint8_t i = 0; i < dataFramePtr[2]; i++) { strPtr[i] = (char)dataFramePtr[3+i]; } - strPtr[dataFramePtr[2]] = '\0'; // null-terminate string - queueMsg.anyPtr = strPtr; - //retCode = osMemoryPoolFree(memPool, dataFramePtr); - //CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + // add null-terminate string + strPtr[dataFramePtr[2]] = '\0'; + + // Define data in queueMsg struct + queueMsg.type = DATA_IND; + queueMsg.addr = source.addr; + queueMsg.sapi = source.sapi; + queueMsg.anyPtr = strPtr; + + // Test distination switch (destination.sapi) { + + // Send to right application queue case TIME_SAPI: retCode = osMessageQueuePut( queue_timeR_id, @@ -48,30 +62,37 @@ void send_DATA_IND(Adresse source, Adresse destination, uint8_t* dataFramePtr) { } } +/** + * @brief Send DATABACK to MAC sender + * + * @param source source Address (addr, sapi) + * @param destination destination Address (addr, sapi) + * @param dataFramePtr pointer to the data frame + */ void send_DATABACK(Adresse source, Adresse destination, uint8_t* dataFramePtr) { struct queueMsg_t queueMsg; // queue message osStatus_t retCode; // return error code + // Define data in queueMsg struct queueMsg.type = DATABACK; queueMsg.anyPtr = dataFramePtr; queueMsg.addr = source.addr; queueMsg.sapi = source.sapi; - + // Put on MAC sender queue retCode = osMessageQueuePut( queue_macS_id, &queueMsg, osPriorityNormal, 0); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); - } void MacReceiver(void *argument) { struct queueMsg_t queueMsg; // queue message - Adresse src; - Adresse dst; + Adresse src; // source Address (addr, sapi) + Adresse dst; // destination Address (addr, sapi) uint8_t length; Status status; uint8_t* msg; @@ -82,6 +103,7 @@ void MacReceiver(void *argument) { // QUEUE READ //-------------------------------------------------------------------------- { + // Get message from queue, test retCode and get msg retCode = osMessageQueueGet( queue_macR_id, &queueMsg, @@ -91,6 +113,10 @@ void MacReceiver(void *argument) { msg = queueMsg.anyPtr; } + + //-------------------------------------------------------------------------- + // SWITCH ON MESSAGE TYPE + //-------------------------------------------------------------------------- switch (queueMsg.type) { //---------------------------------------------------------------------- @@ -101,6 +127,7 @@ void MacReceiver(void *argument) { //-------------------------------------------------------------- // TOKEN //-------------------------------------------------------------- + // Send token to MAC sender queueMsg.type = TOKEN; retCode = osMessageQueuePut( queue_macS_id, @@ -113,6 +140,7 @@ void MacReceiver(void *argument) { //-------------------------------------------------------------- // MESSAGE //-------------------------------------------------------------- + // Get source Addresse, destination Addresse, length and status src.raw = msg[0]; dst.raw = msg[1]; length = msg[2]; @@ -125,6 +153,7 @@ void MacReceiver(void *argument) { dst.addr == BROADCAST_ADDRESS ) { if((Checksum(msg) & 0x3F) == status.checksum) { + // Checksum OK ----------------------------------------- status.ack = 1; if(dst.sapi == CHAT_SAPI && gTokenInterface.connected || @@ -135,7 +164,7 @@ void MacReceiver(void *argument) { } else { status.read = 0; } - msg[3+length] = status.raw; + msg[3+length] = status.raw; // Add status to message if(src.addr == gTokenInterface.myAddress) { // For me, from me // Send DATABACK ----------------------------------- diff --git a/mac_sender.c b/mac_sender.c index f16ca14..ee20842 100644 --- a/mac_sender.c +++ b/mac_sender.c @@ -5,19 +5,28 @@ #include #include -uint8_t* lastToken; -uint8_t* lastSentMsgPtr; +uint8_t* lastToken; // Pointer to last token +uint8_t* lastSentMsgPtr; // Pointer to last sent message (for retransmission) +// Queue for messages to be sent when token is received osMessageQueueId_t queue_macData_id; const osMessageQueueAttr_t queue_macData_attr = { .name = "MAC_DATA" }; +/** + * @brief Send token to the next station + */ void sendToken() { + // Create queueMsg struct with new memory struct queueMsg_t queueMsg; queueMsg.anyPtr = osMemoryPoolAlloc(memPool,osWaitForever); - memcpy(queueMsg.anyPtr, lastToken, TOKENSIZE-2); queueMsg.type = TO_PHY; + + // Copy token + memcpy(queueMsg.anyPtr, lastToken, TOKENSIZE-2); + + // Send token to PHY osStatus_t retCode = osMessageQueuePut( queue_phyS_id, &queueMsg, @@ -26,18 +35,24 @@ void sendToken() { CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); } +/** + * @brief MAC sender task + */ 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; - SapiToken stationStatus; + uint8_t* msg; // pointer to message + Adresse src; // source Address + Adresse dst; // destination Address + uint8_t length; // length of message + Status status; // status of message + osStatus_t retCode; // return error code + char* strPtr; // pointer to string message + SapiToken stationStatus; // Status of one station + // Allocate memory for last token lastToken = osMemoryPoolAlloc(memPool, osWaitForever); + + // Create queue for messages to be sent when token is received queue_macData_id = osMessageQueueNew(4, sizeof(struct queueMsg_t), &queue_macData_attr); @@ -45,6 +60,8 @@ void MacSender(void *argument) { //-------------------------------------------------------------------------- // QUEUE READ //-------------------------------------------------------------------------- + { + // Get message from queue, test retCode and get msg retCode = osMessageQueueGet( queue_macS_id, &queueMsg, @@ -53,6 +70,7 @@ void MacSender(void *argument) { CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); msg = queueMsg.anyPtr; + } switch(queueMsg.type) { @@ -61,8 +79,6 @@ void MacSender(void *argument) { //---------------------------------------------------------------------- case TOKEN: { // Get token and save it -// osDelay(300); - //lastToken = osMemoryPoolAlloc(memPool,osWaitForever); memcpy(lastToken, msg, TOKENSIZE-2); // update token @@ -81,13 +97,12 @@ void MacSender(void *argument) { osWaitForever); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + // Free memory retCode = osMemoryPoolFree(memPool, queueMsg.anyPtr); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); // Send one msg from internal queue if exist - //if (osMemoryPoolGetCount(queue_macData_id) != 0) { // Message in Queue retCode = osMessageQueueGet(queue_macData_id, &queueMsg, NULL, 0); - //CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); if(retCode == 0){ queueMsg.type = TO_PHY; retCode = osMessageQueuePut( @@ -106,17 +121,22 @@ void MacSender(void *argument) { // DATABACK MESSAGE //---------------------------------------------------------------------- case DATABACK: { + // Get source Addresse, destination Addresse, length and status src.raw = msg[0]; dst.raw = msg[1]; length = msg[2]; status.raw = msg[3+length]; if (dst.addr == BROADCAST_ADDRESS) { + // Broadcast message -> free memory retCode = osMemoryPoolFree(memPool, queueMsg.anyPtr); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + + // Send token sendToken(); + } else if(src.addr != gTokenInterface.myAddress) { - + // Not from me -> to PHY queueMsg.type = TO_PHY; retCode = osMessageQueuePut( queue_phyS_id, @@ -127,7 +147,7 @@ void MacSender(void *argument) { } else if(status.read == 1) { if(status.ack == 1) { - // Everything is fine, free memory + // Read + ack => Everything is fine -> free memory and send token retCode = osMemoryPoolFree(memPool, queueMsg.anyPtr); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); retCode = osMemoryPoolFree(memPool, lastSentMsgPtr); @@ -135,11 +155,8 @@ void MacSender(void *argument) { sendToken(); } else { - // Checksum error, send original message again + // Read but checksum error, send original message again if(lastSentMsgPtr != NULL) { - //retCode = osMemoryPoolFree(memPool, queueMsg.anyPtr); - //CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); - memcpy(queueMsg.anyPtr, lastSentMsgPtr, lastSentMsgPtr[2]+4); queueMsg.type = TO_PHY; //queueMsg.anyPtr = lastSentMsgPtr; @@ -168,6 +185,7 @@ void MacSender(void *argument) { } } else { + // Not read => Station not connected -> free backup message retCode = osMemoryPoolFree(memPool, queueMsg.anyPtr); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); @@ -185,6 +203,7 @@ void MacSender(void *argument) { 0); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + // Send token sendToken(); } @@ -195,25 +214,18 @@ void MacSender(void *argument) { // NEW TOKEN MESSAGE //---------------------------------------------------------------------- case NEW_TOKEN: { + // Create new token lastToken[0] = TOKEN_TAG; - for(uint8_t i = 1; i < sizeof(TOKENSIZE-2); i++) { - lastToken[i] = 0; - } + // Set all station status to 0 + memset(lastToken, 0, sizeof(TOKENSIZE-2)); + + // Set my station status on station list and lastToken gTokenInterface.station_list[gTokenInterface.myAddress] = (0x1 << TIME_SAPI) + (gTokenInterface.connected << CHAT_SAPI); lastToken[gTokenInterface.myAddress+1] = gTokenInterface.station_list[gTokenInterface.myAddress]; + + // Send token sendToken(); - /* - queueMsg.type = TO_PHY; - queueMsg.anyPtr = lastToken; - - retCode = osMessageQueuePut( - queue_phyS_id, - &queueMsg, - osPriorityNormal, - osWaitForever); - CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); - */ break; } @@ -237,6 +249,7 @@ void MacSender(void *argument) { // DATA MESSAGE //---------------------------------------------------------------------- case DATA_IND: { + // Set source Addresse, destination Addresse and length dst.addr = queueMsg.addr; dst.sapi = queueMsg.sapi; dst.nothing = 0; @@ -245,6 +258,7 @@ void MacSender(void *argument) { src.nothing = 0; length = strlen(queueMsg.anyPtr); + // Set station status if(dst.addr == BROADCAST_ADDRESS) { status.read = 1; status.ack = 1; @@ -255,24 +269,33 @@ void MacSender(void *argument) { stationStatus.raw = gTokenInterface.station_list[dst.addr]; } + // Check if destination is online if( (dst.addr == BROADCAST_ADDRESS) || (stationStatus.chat == 1)) { + // Allocate memory for message and check if allocation was successful msg = osMemoryPoolAlloc(memPool, 0); if(msg == NULL) { printf("Memory allocation failed #1\r\n"); assert(false); } + + // Set message msg[0] = src.raw; msg[1] = dst.raw; msg[2] = length; + + // Copy message to memory memcpy(&msg[3], queueMsg.anyPtr, length); + + // Set status status.checksum = Checksum(msg); msg[3+length] = status.raw; + // Free memory retCode = osMemoryPoolFree(memPool, queueMsg.anyPtr); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); - + // Backup message if destination is chat station and isn't a broadcast message if( (dst.addr != BROADCAST_ADDRESS) && (dst.sapi == CHAT_SAPI) ) { lastSentMsgPtr = osMemoryPoolAlloc(memPool, 0); if(lastSentMsgPtr == NULL) { @@ -282,6 +305,7 @@ void MacSender(void *argument) { memcpy(lastSentMsgPtr, msg, length+4); } + // Send message to PHY queueMsg.anyPtr = msg; queueMsg.type = TO_PHY; retCode = osMessageQueuePut( @@ -292,6 +316,7 @@ void MacSender(void *argument) { CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); } else { + // Destination is not online strPtr = queueMsg.anyPtr; sprintf(strPtr, "%d is not online\0", dst.addr+1); queueMsg.type = MAC_ERROR; diff --git a/main.c b/main.c index 817b00e..a36757b 100644 --- a/main.c +++ b/main.c @@ -404,7 +404,7 @@ int main(void) { //------------------------------------------------------------------------------ // Create Threads //------------------------------------------------------------------------------ - // audio_id = osThreadNew(AudioPlayer, NULL, &audio_attr); + audio_id = osThreadNew(AudioPlayer, NULL, &audio_attr); debug_id = osThreadNew(DebugStation, NULL, &debug_attr); phy_rec_id = osThreadNew(PhReceiver, NULL, &phy_rec_attr); phy_snd_id = osThreadNew(PhSender, NULL, &phy_snd_attr); diff --git a/main.h b/main.h index 3c2ba57..6363ae6 100644 --- a/main.h +++ b/main.h @@ -16,9 +16,9 @@ //-------------------------------------------------------------------------------- // Constants to change the system behavior //-------------------------------------------------------------------------------- -#define DEBUG_MODE 0 // mode is physical line (0) or debug (1) +#define DEBUG_MODE 1 // mode is physical line (0) or debug (1) #define MYADDRESS 3 // your address choice (table number) -#define MAX_BLOCK_SIZE 100 // size max for a frame +#define MAX_BLOCK_SIZE 250 // size max for a frame //-------------------------------------------------------------------------------- // Constants to NOT change for the system working diff --git a/tokenring_project.uvoptx b/tokenring_project.uvoptx index e156d63..75d8f6c 100644 --- a/tokenring_project.uvoptx +++ b/tokenring_project.uvoptx @@ -145,7 +145,7 @@ 0 ST-LINKIII-KEIL_SWO - -U066DFF485153826687131237 -O8398 -SF1800 -C0 -A0 -I2 -HNlocalhost -HP7184 -P2 -N00("ARM CoreSight SW-DP (ARM Core") -D00(5BA02477) -L00(0) -TO131075 -TC216000000 -TT216000000 -TP21 -TDS806B -TDT0 -TDC1F -TIE1 -TIP1 -FO7 -FD20010000 -FC1000 -FN1 -FF0STM32F7x_1024.FLM -FS08000000 -FL0100000 -FP0($$Device:STM32F746NGHx$CMSIS\Flash\STM32F7x_1024.FLM) -WA0 -WE0 -WVCE4 -WS2710 -WM0 -WP2 + -U066EFF3731324B4D43133455 -O8398 -SF1800 -C0 -A0 -I2 -HNlocalhost -HP7184 -P2 -N00("ARM CoreSight SW-DP (ARM Core") -D00(5BA02477) -L00(0) -TO131075 -TC216000000 -TT216000000 -TP21 -TDS806B -TDT0 -TDC1F -TIE1 -TIP1 -FO7 -FD20010000 -FC1000 -FN1 -FF0STM32F7x_1024.FLM -FS08000000 -FL0100000 -FP0($$Device:STM32F746NGHx$CMSIS\Flash\STM32F7x_1024.FLM) -WA0 -WE0 -WVCE4 -WS2710 -WM0 -WP2 0 @@ -157,41 +157,9 @@ 0 0 - 108 - 1 -
134255386
- 0 - 0 - 0 - 0 - 0 - 1 - .\mac_sender.c - - \\tokenring_project\mac_sender.c\108 -
- - 1 - 0 - 110 - 1 -
134255394
- 0 - 0 - 0 - 0 - 0 - 1 - .\mac_sender.c - - \\tokenring_project\mac_sender.c\110 -
- - 2 - 0 - 116 - 1 -
134254758
+ 18 + 0 +
134314022
0 0 0 @@ -200,29 +168,61 @@ 1 .\mac_receiver.c - \\tokenring_project\mac_receiver.c\116 + \\tokenring_project\mac_receiver.c\18
- 3 + 1 0 - 89 - 1 -
134255344
+ 106 + 0 +
0
0 0 0 0 0 - 1 + 0 + .\mac_receiver.c + + +
+ + 2 + 0 + 107 + 0 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + .\mac_receiver.c + + +
+ + 3 + 0 + 264 + 1 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 .\mac_sender.c - \\tokenring_project\mac_sender.c\89 +
4 0 - 92 - 1 + 86 + 0
0
0 0 @@ -237,8 +237,8 @@ 5 0 - 109 - 1 + 106 + 0
0
0 0 @@ -253,8 +253,8 @@ 6 0 - 111 - 1 + 107 + 0
0
0 0 @@ -278,46 +278,164 @@ 0 0 0 + .\mac_sender.c + + +
+ + 8 + 0 + 106 + 0 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 .\main.c
+ + 9 + 0 + 107 + 0 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + .\main.c + + +
+ + 10 + 0 + 106 + 0 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + .\main.h + + +
+ + 11 + 0 + 107 + 0 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + .\main.h + + +
+ + 12 + 0 + 106 + 0 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + .\phy_receiver.c + + +
+ + 13 + 0 + 107 + 0 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + .\phy_receiver.c + + +
+ + 14 + 0 + 106 + 0 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + .\phy_sender.c + + +
+ + 15 + 0 + 107 + 0 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + .\phy_sender.c + + +
0 1 - gTokenInterface + sai 1 1 - t + gTokenInterface 2 1 - lastToken,0x10 + t 3 1 - memPool + lastToken 4 1 - qPtr[0] - - - 5 - 1 - qPtr[1] - - - 6 - 1 - qPtr[2] + memPool diff --git a/tokenring_project.uvprojx b/tokenring_project.uvprojx index 6099233..0ad19f1 100644 --- a/tokenring_project.uvprojx +++ b/tokenring_project.uvprojx @@ -10,7 +10,7 @@ Target 1 0x4 ARM-ADS - 5060960::V5.06 update 7 (build 960)::C:\Program Files (x86)\ARM_Compiler_5.06u7 + 5060960::V5.06 update 7 (build 960)::..\..\Program Files (x86)\ARM_Compiler_5.06u7 0