2024-04-15 14:26:40 +00:00
|
|
|
#include "main.h"
|
2024-04-21 19:23:15 +00:00
|
|
|
#include <cstdio>
|
2024-04-20 10:40:44 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
uint8_t* lastToken;
|
|
|
|
osMessageQueueId_t queue_macData_id;
|
|
|
|
const osMessageQueueAttr_t queue_macData_attr = {
|
|
|
|
.name = "MAC_DATA"
|
|
|
|
};
|
|
|
|
|
|
|
|
void MacSender(void *argument) {
|
2024-04-15 14:26:40 +00:00
|
|
|
struct queueMsg_t queueMsg; // queue message
|
2024-04-17 15:09:48 +00:00
|
|
|
uint8_t* msg;
|
2024-04-21 07:19:11 +00:00
|
|
|
Adresse src;
|
|
|
|
Adresse dst;
|
|
|
|
uint8_t length;
|
|
|
|
Status status;
|
2024-04-15 14:26:40 +00:00
|
|
|
osStatus_t retCode; // return error code
|
2024-04-21 07:19:11 +00:00
|
|
|
char* strPtr;
|
2024-04-20 10:40:44 +00:00
|
|
|
|
|
|
|
lastToken = osMemoryPoolAlloc(memPool, osWaitForever);
|
|
|
|
queue_macData_id = osMessageQueueNew(4, sizeof(struct queueMsg_t), &queue_macData_attr);
|
|
|
|
|
2024-04-15 14:26:40 +00:00
|
|
|
|
|
|
|
for(;;) {
|
2024-04-20 16:44:37 +00:00
|
|
|
//--------------------------------------------------------------------------
|
2024-04-15 14:26:40 +00:00
|
|
|
// QUEUE READ
|
2024-04-20 16:44:37 +00:00
|
|
|
//--------------------------------------------------------------------------
|
2024-04-15 14:26:40 +00:00
|
|
|
retCode = osMessageQueueGet(
|
|
|
|
queue_macS_id,
|
|
|
|
&queueMsg,
|
|
|
|
NULL,
|
|
|
|
osWaitForever);
|
|
|
|
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
|
2024-04-20 10:40:44 +00:00
|
|
|
|
2024-04-17 15:09:48 +00:00
|
|
|
msg = queueMsg.anyPtr;
|
2024-04-15 14:26:40 +00:00
|
|
|
|
|
|
|
switch(queueMsg.type) {
|
|
|
|
|
2024-04-20 16:44:37 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// TOKEN MESSAGE
|
|
|
|
//----------------------------------------------------------------------
|
2024-04-20 10:40:44 +00:00
|
|
|
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);
|
2024-04-21 19:23:15 +00:00
|
|
|
printf("Token sended\r\n");
|
2024-04-20 10:40:44 +00:00
|
|
|
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
|
|
|
|
|
2024-04-21 07:19:11 +00:00
|
|
|
// Send one msg from internal queue if exist
|
2024-04-21 19:23:15 +00:00
|
|
|
//if (osMemoryPoolGetCount(queue_macData_id) != 0) { // Message in Queue
|
|
|
|
if(osMessageQueueGet(queue_macData_id, &queueMsg, NULL, 0) == 0){
|
2024-04-20 10:40:44 +00:00
|
|
|
queueMsg.type = TO_PHY;
|
|
|
|
retCode = osMessageQueuePut(
|
|
|
|
queue_phyS_id,
|
|
|
|
&queueMsg,
|
|
|
|
osPriorityNormal,
|
2024-04-21 19:23:15 +00:00
|
|
|
0);
|
2024-04-20 10:40:44 +00:00
|
|
|
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
|
2024-04-17 15:09:48 +00:00
|
|
|
}
|
2024-04-20 10:40:44 +00:00
|
|
|
|
|
|
|
// Send token
|
|
|
|
queueMsg.anyPtr = lastToken;
|
2024-04-17 15:09:48 +00:00
|
|
|
queueMsg.type = TO_PHY;
|
|
|
|
retCode = osMessageQueuePut(
|
|
|
|
queue_phyS_id,
|
|
|
|
&queueMsg,
|
|
|
|
osPriorityNormal,
|
2024-04-21 19:23:15 +00:00
|
|
|
0);
|
2024-04-17 15:09:48 +00:00
|
|
|
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
|
2024-04-15 14:26:40 +00:00
|
|
|
break;
|
2024-04-20 10:40:44 +00:00
|
|
|
}
|
2024-04-15 14:26:40 +00:00
|
|
|
|
2024-04-20 16:44:37 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DATABACK MESSAGE
|
|
|
|
//----------------------------------------------------------------------
|
2024-04-20 10:40:44 +00:00
|
|
|
case DATABACK: {
|
2024-04-21 07:19:11 +00:00
|
|
|
src.raw = msg[0];
|
|
|
|
dst.raw = msg[1];
|
|
|
|
length = msg[2];
|
|
|
|
status.raw = msg[3+length];
|
2024-04-20 10:40:44 +00:00
|
|
|
|
2024-04-21 19:23:15 +00:00
|
|
|
if(src.addr == BROADCAST_ADDRESS) {
|
|
|
|
retCode = osMemoryPoolFree(memPool, queueMsg.anyPtr);
|
|
|
|
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(status.read == 1) {
|
2024-04-21 07:19:11 +00:00
|
|
|
if(status.ack == 0) {
|
|
|
|
msg = osMemoryPoolAlloc(memPool, osWaitForever);
|
|
|
|
queueMsg.type = TO_PHY;
|
2024-04-21 19:23:15 +00:00
|
|
|
//queueMsg.anyPtr =
|
2024-04-21 07:19:11 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2024-04-21 19:23:15 +00:00
|
|
|
// Send original message to PHY
|
|
|
|
queueMsg.type = TO_PHY;
|
|
|
|
retCode = osMessageQueuePut(
|
|
|
|
queue_macData_id,
|
|
|
|
&queueMsg,
|
|
|
|
osPriorityNormal,
|
|
|
|
0);
|
|
|
|
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
|
|
|
|
|
|
|
|
// Send error message to LCD
|
2024-04-21 07:19:11 +00:00
|
|
|
strPtr = osMemoryPoolAlloc(memPool, osWaitForever);
|
2024-04-21 19:23:15 +00:00
|
|
|
sprintf(strPtr, "Dest. %d couldn't read message from %d\0", dst.addr+1, src.addr+1);
|
2024-04-21 07:19:11 +00:00
|
|
|
queueMsg.type = MAC_ERROR;
|
|
|
|
queueMsg.addr = src.addr;
|
|
|
|
queueMsg.sapi = src.sapi;
|
|
|
|
queueMsg.anyPtr = strPtr;
|
|
|
|
retCode = osMessageQueuePut(
|
|
|
|
queue_lcd_id,
|
|
|
|
&queueMsg,
|
|
|
|
osPriorityNormal,
|
2024-04-21 19:23:15 +00:00
|
|
|
0);
|
2024-04-21 07:19:11 +00:00
|
|
|
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
|
|
|
|
}
|
2024-04-15 14:26:40 +00:00
|
|
|
break;
|
2024-04-20 10:40:44 +00:00
|
|
|
}
|
2024-04-15 14:26:40 +00:00
|
|
|
|
2024-04-20 16:44:37 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// NEW TOKEN MESSAGE
|
|
|
|
//----------------------------------------------------------------------
|
2024-04-20 10:40:44 +00:00
|
|
|
case NEW_TOKEN: {
|
|
|
|
lastToken[0] = TOKEN_TAG;
|
2024-04-17 15:09:48 +00:00
|
|
|
|
|
|
|
for(uint8_t i = 1; i < sizeof(TOKENSIZE-2); i++) {
|
2024-04-20 10:40:44 +00:00
|
|
|
lastToken[i] = 0;
|
2024-04-15 14:26:40 +00:00
|
|
|
}
|
2024-04-17 15:09:48 +00:00
|
|
|
gTokenInterface.station_list[gTokenInterface.myAddress] = (0x1 << TIME_SAPI) + (gTokenInterface.connected << CHAT_SAPI);
|
2024-04-20 10:40:44 +00:00
|
|
|
lastToken[gTokenInterface.myAddress+1] = gTokenInterface.station_list[gTokenInterface.myAddress];
|
2024-04-17 15:09:48 +00:00
|
|
|
|
2024-04-15 14:26:40 +00:00
|
|
|
queueMsg.type = TO_PHY;
|
2024-04-20 10:40:44 +00:00
|
|
|
queueMsg.anyPtr = lastToken;
|
2024-04-17 15:09:48 +00:00
|
|
|
|
|
|
|
retCode = osMessageQueuePut(
|
|
|
|
queue_phyS_id,
|
|
|
|
&queueMsg,
|
|
|
|
osPriorityNormal,
|
|
|
|
osWaitForever);
|
|
|
|
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
|
2024-04-15 14:26:40 +00:00
|
|
|
break;
|
2024-04-20 10:40:44 +00:00
|
|
|
}
|
2024-04-15 14:26:40 +00:00
|
|
|
|
2024-04-20 16:44:37 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// START MESSAGE
|
|
|
|
//----------------------------------------------------------------------
|
2024-04-20 10:40:44 +00:00
|
|
|
case START: {
|
2024-04-20 16:44:37 +00:00
|
|
|
gTokenInterface.connected = true;
|
2024-04-15 14:26:40 +00:00
|
|
|
break;
|
2024-04-20 10:40:44 +00:00
|
|
|
}
|
2024-04-15 14:26:40 +00:00
|
|
|
|
2024-04-20 16:44:37 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// STOP MESSAGE
|
|
|
|
//----------------------------------------------------------------------
|
2024-04-20 10:40:44 +00:00
|
|
|
case STOP: {
|
2024-04-20 16:44:37 +00:00
|
|
|
gTokenInterface.connected = false;
|
2024-04-15 14:26:40 +00:00
|
|
|
break;
|
2024-04-20 10:40:44 +00:00
|
|
|
}
|
2024-04-15 14:26:40 +00:00
|
|
|
|
2024-04-20 16:44:37 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DATA MESSAGE
|
|
|
|
//----------------------------------------------------------------------
|
2024-04-20 10:40:44 +00:00
|
|
|
case DATA_IND: {
|
2024-04-21 07:19:11 +00:00
|
|
|
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);
|
2024-04-20 10:40:44 +00:00
|
|
|
|
2024-04-21 07:19:11 +00:00
|
|
|
if(dst.addr == BROADCAST_ADDRESS) {
|
|
|
|
status.read = 1;
|
|
|
|
status.ack = 1;
|
|
|
|
} else {
|
|
|
|
status.read = 0;
|
|
|
|
status.ack = 0;
|
|
|
|
}
|
|
|
|
|
2024-04-21 19:23:15 +00:00
|
|
|
msg = osMemoryPoolAlloc(memPool, osWaitForever); // TODO - Leak of memory
|
2024-04-21 07:19:11 +00:00
|
|
|
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,
|
2024-04-21 19:23:15 +00:00
|
|
|
0);
|
2024-04-21 07:19:11 +00:00
|
|
|
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
|
2024-04-15 14:26:40 +00:00
|
|
|
break;
|
2024-04-20 10:40:44 +00:00
|
|
|
}
|
|
|
|
|
2024-04-20 16:44:37 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DEFAULT - TBD
|
|
|
|
//----------------------------------------------------------------------
|
2024-04-20 10:40:44 +00:00
|
|
|
default: {
|
2024-04-15 14:26:40 +00:00
|
|
|
break;
|
2024-04-20 10:40:44 +00:00
|
|
|
}
|
2024-04-15 14:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-04-10 17:17:57 +00:00
|
|
|
}
|