1
0
This repository has been archived on 2024-09-17. You can view files and clone it, but cannot push or open issues or pull requests.
tor-heredero-tokenring/mac_receiver.c

219 lines
6.3 KiB
C
Raw Permalink Normal View History

2024-04-10 17:17:57 +00:00
2024-04-20 16:44:37 +00:00
#include "main.h"
#include <cassert>
#include <cstdint>
#include <stdint.h>
2024-05-01 14:09:03 +00:00
/**
* @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
*/
2024-04-20 16:44:37 +00:00
void send_DATA_IND(Adresse source, Adresse destination, uint8_t* dataFramePtr) {
struct queueMsg_t queueMsg; // queue message
osStatus_t retCode; // return error code
2024-05-01 14:09:03 +00:00
char* strPtr; // string pointer of the message
2024-04-20 16:44:37 +00:00
2024-05-01 14:09:03 +00:00
// Get memmory for new message and test if succed
2024-04-21 19:23:15 +00:00
strPtr = osMemoryPoolAlloc(memPool, 0);
if(strPtr == NULL) {
assert(false);
}
2024-04-20 16:44:37 +00:00
2024-05-01 14:09:03 +00:00
// Copy data from dataFramePtr to strPtrt8_t* dataFramePtr) {
2024-04-20 16:44:37 +00:00
for(uint8_t i = 0; i < dataFramePtr[2]; i++) {
strPtr[i] = (char)dataFramePtr[3+i];
}
2024-05-01 14:09:03 +00:00
// 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;
2024-04-20 16:44:37 +00:00
queueMsg.anyPtr = strPtr;
2024-05-01 14:09:03 +00:00
// Test distination
2024-04-20 16:44:37 +00:00
switch (destination.sapi) {
2024-05-01 14:09:03 +00:00
// Send to right application queue
2024-04-20 16:44:37 +00:00
case TIME_SAPI:
retCode = osMessageQueuePut(
queue_timeR_id,
&queueMsg,
osPriorityNormal,
2024-04-21 19:23:15 +00:00
0);
2024-04-20 16:44:37 +00:00
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
break;
case CHAT_SAPI:
retCode = osMessageQueuePut(
queue_chatR_id,
&queueMsg,
osPriorityNormal,
2024-04-21 19:23:15 +00:00
0);
2024-04-20 16:44:37 +00:00
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
break;
default:
assert(false);
}
}
2024-05-01 14:09:03 +00:00
/**
* @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
*/
2024-04-20 16:44:37 +00:00
void send_DATABACK(Adresse source, Adresse destination, uint8_t* dataFramePtr) {
struct queueMsg_t queueMsg; // queue message
osStatus_t retCode; // return error code
2024-05-01 14:09:03 +00:00
// Define data in queueMsg struct
2024-04-20 16:44:37 +00:00
queueMsg.type = DATABACK;
queueMsg.anyPtr = dataFramePtr;
queueMsg.addr = source.addr;
queueMsg.sapi = source.sapi;
2024-05-01 14:09:03 +00:00
// Put on MAC sender queue
2024-04-20 16:44:37 +00:00
retCode = osMessageQueuePut(
queue_macS_id,
&queueMsg,
osPriorityNormal,
2024-04-21 19:23:15 +00:00
0);
2024-04-20 16:44:37 +00:00
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
}
void MacReceiver(void *argument) {
struct queueMsg_t queueMsg; // queue message
2024-05-01 14:09:03 +00:00
Adresse src; // source Address (addr, sapi)
Adresse dst; // destination Address (addr, sapi)
2024-04-20 16:44:37 +00:00
uint8_t length;
Status status;
uint8_t* msg;
osStatus_t retCode; // return error code
for(;;) {
//--------------------------------------------------------------------------
// QUEUE READ
//--------------------------------------------------------------------------
{
2024-05-01 14:09:03 +00:00
// Get message from queue, test retCode and get msg
2024-04-20 16:44:37 +00:00
retCode = osMessageQueueGet(
queue_macR_id,
&queueMsg,
NULL,
osWaitForever);
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
msg = queueMsg.anyPtr;
}
2024-05-01 14:09:03 +00:00
//--------------------------------------------------------------------------
// SWITCH ON MESSAGE TYPE
//--------------------------------------------------------------------------
2024-04-20 16:44:37 +00:00
switch (queueMsg.type) {
//----------------------------------------------------------------------
// MESSAGE FROM PHY
//----------------------------------------------------------------------
case FROM_PHY:
if(msg[0] == TOKEN_TAG) {
//--------------------------------------------------------------
// TOKEN
//--------------------------------------------------------------
2024-05-01 14:09:03 +00:00
// Send token to MAC sender
2024-04-20 16:44:37 +00:00
queueMsg.type = TOKEN;
retCode = osMessageQueuePut(
queue_macS_id,
&queueMsg,
osPriorityNormal,
osWaitForever);
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
} else {
//--------------------------------------------------------------
// MESSAGE
//--------------------------------------------------------------
2024-05-01 14:09:03 +00:00
// Get source Addresse, destination Addresse, length and status
2024-04-20 16:44:37 +00:00
src.raw = msg[0];
dst.raw = msg[1];
length = msg[2];
status.raw = msg[3+length];
//--------------------------------------------------------------
// MESSAGE FOR ME (or broadcast)
//--------------------------------------------------------------
if( dst.addr == gTokenInterface.myAddress ||
dst.addr == BROADCAST_ADDRESS ) {
if((Checksum(msg) & 0x3F) == status.checksum) {
2024-05-01 14:09:03 +00:00
// Checksum OK -----------------------------------------
2024-04-20 16:44:37 +00:00
status.ack = 1;
if(dst.sapi == CHAT_SAPI && gTokenInterface.connected ||
dst.sapi == TIME_SAPI) {
2024-04-20 16:44:37 +00:00
// Send to Time or Chat ----------------------------
send_DATA_IND(src, dst, queueMsg.anyPtr);
status.read = 1;
} else {
status.read = 0;
}
2024-05-01 14:09:03 +00:00
msg[3+length] = status.raw; // Add status to message
2024-04-20 16:44:37 +00:00
if(src.addr == gTokenInterface.myAddress) { // For me, from me
// Send DATABACK -----------------------------------
send_DATABACK(src, dst, queueMsg.anyPtr);
} else {
// Send to PHY -------------------------------------
queueMsg.type = TO_PHY;
retCode = osMessageQueuePut(
queue_phyS_id,
&queueMsg,
osPriorityNormal,
2024-04-21 19:23:15 +00:00
0);
2024-04-20 16:44:37 +00:00
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
}
} else { // for me but bad checksum
status.ack = 0;
status.read = gTokenInterface.connected; // Maybe it's 1
2024-04-20 16:44:37 +00:00
msg[3+length] = status.raw;
send_DATABACK(src, dst, queueMsg.anyPtr);
}
//--------------------------------------------------------------
// MESSAGE FOR SOMEONE ELSE
//--------------------------------------------------------------
} else if(src.addr == gTokenInterface.myAddress) {
// MESSAGE FROM ME -----------------------------------------
send_DATABACK(src, dst, queueMsg.anyPtr);
} else {
// MESSAGE FROM SOMEONE ELSE -------------------------------
queueMsg.type = TO_PHY;
retCode = osMessageQueuePut(
queue_phyS_id,
&queueMsg,
osPriorityNormal,
2024-04-21 19:23:15 +00:00
0);
2024-04-20 16:44:37 +00:00
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
}
}
break;
//----------------------------------------------------------------------
// DEFAULT - TBD
//----------------------------------------------------------------------
default:
break;
}
}
2024-04-10 17:17:57 +00:00
}