RobOmega - PSL RoboCup / Mbed 2 deprecated nRF24L01P_L475VG_CarteBleu

Dependencies:   mbed nRF24L01P

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers protocol.cpp Source File

protocol.cpp

00001 #include "protocol.h"
00002 #include "bufferCirculaire.h"
00003 
00004 //------------------------------------------------Encodage/Decodage des trames------------------------------------------------//
00005 //!!! La taille de "msg[]" doit être [msgPayloadLength + 6]
00006 int encodeMessage(int msgFunction, int msgPayloadLength, char* msgPayload, char* msg)
00007 {
00008     int msgLength = 0;
00009     msg[msgLength++] = 0xFE;                                                                //Start of Frame
00010     msg[msgLength++] = (char)(msgFunction >> 8);                                            //Message Function MSB
00011     msg[msgLength++] = (char)(msgFunction);                                                 //Message Function LSB
00012     msg[msgLength++] = (char)(msgPayloadLength >> 8);                                       //Message Payload Length MSB
00013     msg[msgLength++] = (char)(msgPayloadLength);                                            //Message Payload Length LSB
00014     int j;
00015     for (j = 0 ; j < msgPayloadLength; j++)
00016         msg[msgLength++] = msgPayload[j];                                                   //MessagePayload
00017     msg[msgLength++] = calculateChecksum(msgFunction, msgPayloadLength, msgPayload);        //Checksum
00018     
00019     return msgLength;
00020 }
00021 
00022 char calculateChecksum(int msgFunction, int msgPayloadLength, char* msgPayload)
00023 {
00024     char checksum = 0xFE;
00025     checksum ^= (char)(msgFunction >> 8);
00026     checksum ^= (char)(msgFunction);
00027     checksum ^= (char)(msgPayloadLength >> 8);
00028     checksum ^= (char)(msgPayloadLength);
00029     int i;
00030     for(i = 0; i < msgPayloadLength; i++)
00031         checksum ^= msgPayload[i];
00032     
00033     return checksum;
00034 }
00035 
00036 unsigned char rcvState = WAITING;
00037 int decodedMsgFunction;
00038 int decodedPayloadLength;
00039 char decodedPayload[BUFFER_SIZE];
00040 int decodedPayloadIndex = 0;
00041 
00042 void decodeMessage(char receivedByte)
00043 {
00044     switch(rcvState)
00045     {
00046         case WAITING:
00047             if (receivedByte == 0xFE)
00048                 rcvState = FUNCTION_MSB;
00049             break;
00050             
00051         case FUNCTION_MSB:
00052             decodedMsgFunction = receivedByte << 8;
00053             rcvState = FUNCTION_LSB;
00054             break;
00055             
00056         case FUNCTION_LSB:
00057             decodedMsgFunction += receivedByte;
00058             rcvState = LENGTH_MSB;
00059             break;
00060             
00061         case LENGTH_MSB:
00062             decodedPayloadLength = receivedByte << 8;
00063             rcvState = LENGTH_LSB;
00064             break;
00065             
00066         case LENGTH_LSB:
00067             decodedPayloadLength += receivedByte;
00068             rcvState = PAYLOAD;
00069             break;
00070             
00071         case PAYLOAD:
00072             if(decodedPayloadLength >= BUFFER_SIZE)
00073             {
00074                 rcvState = WAITING;
00075                 decodedPayloadLength = 0;
00076                 decodedMsgFunction = 0;
00077             }
00078             else if (decodedPayloadLength > 0)
00079             {
00080                 decodedPayload[decodedPayloadIndex++] = receivedByte;
00081                 if (decodedPayloadIndex >= decodedPayloadLength)
00082                 {
00083                     rcvState = CHECKSUM;
00084                     decodedPayloadIndex = 0;
00085                 }
00086             }
00087             else
00088             {
00089                 rcvState = CHECKSUM;
00090             }
00091             break;
00092             
00093         case CHECKSUM:
00094         {    
00095             char calculatedChecksum = calculateChecksum(decodedMsgFunction, decodedPayloadLength, decodedPayload);
00096             if (calculatedChecksum == receivedByte)
00097             {
00098                 //Victoire, la trame recu correspond à la trame envoyé
00099                 //TODO: Faire une fonction qui traite la charge utile apres décodage.
00100             }
00101             
00102             rcvState = WAITING;
00103             break;
00104         }
00105     }
00106 }