1
0

small refactor

- put unions on main for usage on several file
- add DATA_IND on sender
This commit is contained in:
Rémi Heredero 2024-04-21 09:19:11 +02:00
parent b93c51f369
commit bbec23752f
Signed by: Klagarge
GPG Key ID: 3CBAC2C6CD1E8807
3 changed files with 89 additions and 21 deletions

View File

@ -4,25 +4,6 @@
#include <cstdint> #include <cstdint>
#include <stdint.h> #include <stdint.h>
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) { void send_DATA_IND(Adresse source, Adresse destination, uint8_t* dataFramePtr) {
struct queueMsg_t queueMsg; // queue message struct queueMsg_t queueMsg; // queue message
osStatus_t retCode; // return error code osStatus_t retCode; // return error code

View File

@ -10,7 +10,12 @@ const osMessageQueueAttr_t queue_macData_attr = {
void MacSender(void *argument) { void MacSender(void *argument) {
struct queueMsg_t queueMsg; // queue message struct queueMsg_t queueMsg; // queue message
uint8_t* msg; uint8_t* msg;
Adresse src;
Adresse dst;
uint8_t length;
Status status;
osStatus_t retCode; // return error code osStatus_t retCode; // return error code
char* strPtr;
lastToken = osMemoryPoolAlloc(memPool, osWaitForever); lastToken = osMemoryPoolAlloc(memPool, osWaitForever);
queue_macData_id = osMessageQueueNew(4, sizeof(struct queueMsg_t), &queue_macData_attr); queue_macData_id = osMessageQueueNew(4, sizeof(struct queueMsg_t), &queue_macData_attr);
@ -54,8 +59,8 @@ void MacSender(void *argument) {
osWaitForever); osWaitForever);
CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE);
// Send msg from internal queue if exist // Send one msg from internal queue if exist
while (osMemoryPoolGetCount(queue_macData_id) != 0) { // Message in Queue if (osMemoryPoolGetCount(queue_macData_id) != 0) { // Message in Queue
retCode = osMessageQueueGet( retCode = osMessageQueueGet(
queue_macData_id, queue_macData_id,
&queueMsg, &queueMsg,
@ -87,7 +92,35 @@ void MacSender(void *argument) {
// DATABACK MESSAGE // DATABACK MESSAGE
//---------------------------------------------------------------------- //----------------------------------------------------------------------
case DATABACK: { 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; break;
} }
@ -135,7 +168,40 @@ void MacSender(void *argument) {
// DATA MESSAGE // DATA MESSAGE
//---------------------------------------------------------------------- //----------------------------------------------------------------------
case DATA_IND: { 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; break;
} }

21
main.h
View File

@ -118,3 +118,24 @@ struct queueMsg_t
uint8_t addr; ///< the source or destination address uint8_t addr; ///< the source or destination address
uint8_t sapi; ///< the source or destination SAPI 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;