1 |
mocchiut |
1.1 |
//where I am |
2 |
|
|
#include "R3_FAFEDE_State.h" |
3 |
|
|
//manager |
4 |
|
|
#include "StateManager.h" |
5 |
|
|
//home |
6 |
|
|
#include "R0_Init_State.h" |
7 |
|
|
//a step after |
8 |
|
|
#include "R4_HEADER_State.h" |
9 |
|
|
|
10 |
|
|
//to compute CRC |
11 |
|
|
#include "CRC.h" |
12 |
|
|
|
13 |
|
|
//to log something |
14 |
|
|
//#include "Logger.h" |
15 |
|
|
|
16 |
|
|
namespace PamOffLineSW |
17 |
|
|
{ |
18 |
|
|
extern LogUtil* mainLogUtil; |
19 |
|
|
extern long int iNumCadres; //cadre's number |
20 |
|
|
extern unsigned long long int iByte_tot;// how many bytes I have read till now |
21 |
|
|
|
22 |
|
|
R3_FAFEDE_State R3_FAFEDE_State::instance; |
23 |
|
|
int R3_FAFEDE_State::posHeaderPKT=2; |
24 |
|
|
|
25 |
|
|
R3_FAFEDE_State::R3_FAFEDE_State() |
26 |
|
|
{ |
27 |
|
|
initHeaderPKT(); |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
R3_FAFEDE_State::~R3_FAFEDE_State() |
31 |
|
|
{ |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
void R3_FAFEDE_State::initHeaderPKT() |
35 |
|
|
{ |
36 |
|
|
for(int j=0; j<LENGTH_HEADER_PKT; j++) |
37 |
|
|
{ |
38 |
|
|
headerPKT[j]=0; |
39 |
|
|
} |
40 |
|
|
headerPKT[0]=CODE_FA; |
41 |
|
|
headerPKT[1]=CODE_FE; |
42 |
|
|
headerPKT[2]=CODE_DE; |
43 |
|
|
|
44 |
|
|
posHeaderPKT=2; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
void R3_FAFEDE_State::resetHeaderPKT() |
48 |
|
|
{ |
49 |
|
|
posHeaderPKT=2; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
void R3_FAFEDE_State::readInput(char inputbyte) |
53 |
|
|
{ |
54 |
|
|
//the three bytes before this one are 0xFA 0xFE 0xDE, we want to check if it is a Pamela Packet |
55 |
|
|
//to do this we read 16 bytes starting from 0xFA and compute the CRC |
56 |
|
|
posHeaderPKT++; |
57 |
|
|
headerPKT[posHeaderPKT]=inputbyte; |
58 |
|
|
|
59 |
|
|
//if we have read all the header |
60 |
|
|
if(posHeaderPKT==(LENGTH_HEADER_PKT-1)) |
61 |
|
|
{ |
62 |
|
|
//TODO: maybe if ex_pkt_type_1 != ex_pkt_type_2 the pkt is not good? maybe it shuold be discarded? |
63 |
|
|
// put this check here and if the pkt must can't be recognised |
64 |
|
|
|
65 |
|
|
//here we check the CRC |
66 |
|
|
short int CalcCheckSum=(short int)(unsigned char)CM_Compute_CRC16(0, (unsigned char*)&headerPKT, LENGTH_HEADER_PKT-1); |
67 |
|
|
|
68 |
|
|
//bytes when the PKT starts |
69 |
|
|
unsigned long long int wherePKT_start=iByte_tot-posHeaderPKT+1; |
70 |
|
|
|
71 |
|
|
//I check also that pkt_type is correct? headerPKT[3]=header[4] ?? |
72 |
|
|
if ((headerPKT[3]==headerPKT[4])&&(CalcCheckSum==(short int)(unsigned char)(headerPKT[LENGTH_HEADER_PKT-1]))) |
73 |
|
|
{ |
74 |
|
|
//I have found 16 bytes that are a Pamela Packet Header |
75 |
|
|
// I will pass it to state R4 and there I will add data to the Packet |
76 |
|
|
R4_HEADER_State::getInstance().initBufferPKT(headerPKT); |
77 |
|
|
StateManager::getInstance().changeState(&R4_HEADER_State::getInstance()); |
78 |
|
|
//just to be sure that always is clean |
79 |
|
|
initHeaderPKT(); |
80 |
|
|
} |
81 |
|
|
else |
82 |
|
|
{ |
83 |
|
|
stringstream oss; |
84 |
|
|
oss.str()="Packet Header skipped: "; |
85 |
|
|
if (headerPKT[3]!=headerPKT[4]) |
86 |
|
|
{ |
87 |
|
|
oss<<"Packet Type mismatch "; |
88 |
|
|
} |
89 |
|
|
else |
90 |
|
|
{ |
91 |
|
|
oss<<"Packet Header CRC check failed "; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
oss<<"The packet header starts at byte: "<<wherePKT_start<<" in cadre "<<iNumCadres<<" restart the search"; |
95 |
|
|
string msg = oss.str(); |
96 |
|
|
mainLogUtil->logWarning(msg); |
97 |
|
|
|
98 |
|
|
//the 16 bytes analized cannot be considered a correct Header for a Pamela Packet. |
99 |
|
|
//I want to check if in this 16 bytes I can find a new candidate for a Pam Pkt Header, i.e. a new set of FA FE DE. |
100 |
|
|
//to do this I go back to the R0 state, and start look for 0xFA |
101 |
|
|
resetHeaderPKT();//here I don't clean the buffer with the header |
102 |
|
|
//change to state R0, it means searching 0xFA |
103 |
|
|
StateManager::getInstance().changeState(&R0_Init_State::getInstance()); |
104 |
|
|
for(int i=3;i<LENGTH_HEADER_PKT;i++) |
105 |
|
|
{ |
106 |
|
|
//I am searching for byte 0xFA, if found I will check if it is the beginning of a new header |
107 |
|
|
StateManager::getInstance().getCurrentState()->readInput(headerPKT[i]); |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
} |