forked from Geotab/IOX-raspberryPi-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOControl.c
More file actions
313 lines (245 loc) · 8.64 KB
/
IOControl.c
File metadata and controls
313 lines (245 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//-----------------------------------------------------------------------------
//By using and accessing this material you agree to the terms of the Sample Code
//License Agreement found here. If you do not agree you may not access
//or otherwise use this information.
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "Types.h"
#include "string.h"
#include "IOExpander.h"
#include "statemachine.h"
#include "startup.h"
#include "NVM.h"
#include "CANTxManager.h"
#include "ticktimer.h"
#include "PRNG.h"
#include "MessageBuilder.h"
#include "IOControl.h"
#include "InfoTable.h"
#include "can.h"
#include <stdio.h>
#include "MimePassthrough.h"
//-----------------------------------------------------------------------------
// Macros
//-----------------------------------------------------------------------------
#define SHA_VERSION 0x12345678
//-----------------------------------------------------------------------------
// User defined types
//-----------------------------------------------------------------------------
typedef union
{
uint8_t bAll;
struct
{
uint8_t fGoingToSleep : 1;
uint8_t fJustWokeUp : 1;
uint8_t fCanActivity : 1;
uint8_t : 5;
} Single;
} IOControlFlags_t;
typedef union
{
uint16_t iAll;
struct
{
uint16_t fSendAdditionalInfo : 1;
uint16_t fSleep : 1;
uint16_t fWakeup : 1;
uint16_t fSendPollResponse : 1;
uint16_t : 10;
} Single;
} IOControlCommands_t;
typedef struct
{
Timer_t Timer;
Timer_t SleepTimer;
uint32_t iRandomDelay;
StateMachineFunction_t pfReturnState;
IOControlFlags_t Flags;
IOControlCommands_t Commands;
} IOControl_t;
//-----------------------------------------------------------------------------
// Module variables
//-----------------------------------------------------------------------------
uint8_t gfGoHandshakeAcked;
static IOControl_t mIOControl;
//-----------------------------------------------------------------------------
// Module function declarations
//-----------------------------------------------------------------------------
static void IOControl_SMWaitForPoll(bool fIsInit);
static void IOControl_SMConfigure(bool fIsInit);
static void IOControl_SMIdle(bool fIsInit);
static void IOControl_SMSendPollResponse(bool fIsInit);
static void IOControl_SMSendAdditionalInfo(bool fIsInit);
//-----------------------------------------------------------------------------
// Module constants
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Function definitions
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Function : IOControl_Init
// Purpose : Initialise the IOControl module
// Parameters : None
// Return : None
// Notes : None
//-----------------------------------------------------------------------------
void IOControl_Init(void)
{
SM_Add(IOControl_SMWaitForPoll);
}
// ****************************************************************
// *********************** STATIC METHODS *************************
// ****************************************************************
// ****************************************************************
// ******************** START of IO Control SM ********************
// ****************************************************************
static void IOControl_SMWaitForPoll(bool fIsInit)
{
if (fIsInit)
{
mIOControl.pfReturnState = IOControl_SMWaitForPoll;
}
if (mIOControl.Commands.Single.fSendPollResponse)
{
SM_SetNextState(IOControl_SMSendPollResponse);
return;
}
if (gfGoHandshakeAcked)
{
SM_SetNextState(IOControl_SMConfigure);
return;
}
}
static void IOControl_SMConfigure(bool fIsInit)
{
mIOControl.pfReturnState = IOControl_SMIdle;
mIOControl.Commands.iAll = 0; // reset commands and buffers again because of messages that could have been received (not flags)
mIOControl.Commands.Single.fSendAdditionalInfo = true;
CANTxManager_Reset();
SM_SetNextState(IOControl_SMIdle);
}
static void IOControl_SMIdle(bool fIsInit)
{
if (mIOControl.Commands.Single.fSendAdditionalInfo)
{
mIOControl.Commands.Single.fSendAdditionalInfo = 0;
SM_SetNextState(IOControl_SMSendAdditionalInfo);
return;
}
if (mIOControl.Commands.Single.fSleep)
{
mIOControl.Commands.Single.fSleep = 0;
Timer_Reset(&mIOControl.Timer);
mIOControl.Flags.Single.fGoingToSleep = 1;
}
if ((mIOControl.Flags.Single.fGoingToSleep) && (Timer_HasExpired(mIOControl.Timer, DELAY_BEFORE_SLEEP_MS)))
{
mIOControl.Flags.Single.fGoingToSleep = 0;
printf("Received message_id = 0x%x: Causes all IOXs to go into sleep mode\n", MSG_ID_SLEEP);
return;
}
if (mIOControl.Commands.Single.fSendPollResponse)
{
SM_SetNextState(IOControl_SMSendPollResponse);
return;
}
}
static void IOControl_SMSendAdditionalInfo(bool fIsInit)
{
uint8_t bErrorCondition = 0;
uint8_t bHardwareVersion = gIox.bHardwareVersion;
IOExpanderMsg_t Msg;
MB_AdditionalInfoMsg(&Msg, SHA_VERSION, PROD_VERSION, bErrorCondition, bHardwareVersion, true);
CANTxManager_AddMessage(&Msg);
SM_SetNextState(mIOControl.pfReturnState);
}
static void IOControl_SMSendPollResponse(bool fIsInit)
{
if (fIsInit)
{
mIOControl.Commands.Single.fSendPollResponse = 0;
Timer_Reset(&mIOControl.Timer);
mIOControl.iRandomDelay = PRNG_GetNext(POLL_RESPONSE_DELAY_MIN_MS, POLL_RESPONSE_DELAY_MAX_MS);
}
if (Timer_HasExpired(mIOControl.Timer, mIOControl.iRandomDelay))
{
IOExpanderMsg_t Msg;
uint8_t bResetReason = 0;
MB_PollResponse(&Msg, !gfGoHandshakeAcked
, mIOControl.Flags.Single.fGoingToSleep
, mIOControl.Flags.Single.fJustWokeUp
, bResetReason
, true);
if (mIOControl.Flags.Single.fJustWokeUp)
{
mIOControl.Flags.Single.fJustWokeUp = 0;
}
CANTxManager_AddMessage(&Msg);
SM_SetNextState(mIOControl.pfReturnState);
}
}
// ****************************************************************
// ********************* END of IO Control SM *********************
// ****************************************************************
// ****************************************************************
// ************* START of IO Control Message Handlers *************
// ****************************************************************
void IOControl_ResetHandler(IOExpanderMsg_t * Msg)
{
printf("Received message_id = 0x%x: Reset IOX\n", MSG_ID_RESET);
}
void IOControl_WakeupHandler(IOExpanderMsg_t * Msg)
{
printf("Received message_id = 0x%x: Wakes up all the IOXs from sleep mode\n", MSG_ID_WAKEUP);
mIOControl.Flags.Single.fJustWokeUp = 1;
mIOControl.Commands.Single.fWakeup = 1;
}
void IOControl_SleepHandler(IOExpanderMsg_t * Msg)
{
mIOControl.Commands.Single.fSleep = 1;
}
void IOControl_PollHandler(IOExpanderMsg_t * Msg)
{
printf("Received message_id = 0x%x: Poll request\n", MSG_ID_POLL);
mIOControl.Commands.Single.fSendPollResponse = 1;
}
void IOControl_TxHandler(IOExpanderMsg_t * Msg)
{
int i;
printf("Received message_id = 0x%x: TX Data from GO - ", MSG_ID_TX_DATA);
for(i = 0; i < Msg->bLength; i++)
{
printf("0x%x ", Msg->abData[i]);
}
printf("\n");
}
/**
* IOControl_ApplicationSpecificHandler:
* To process Application-Specific Data (0x1C) message coming from GO.
* @param IOExpanderMsg : CAN message received from GO.
* Refer to IO Expander Messages document - Application Specific Data (0x1C)
*/
void IOControl_ApplicationSpecificHandler(IOExpanderMsg_t * IOExpanderMsg)
{
uint8_t bType = IOExpanderMsg->abData[0];
printf("Received message_id = 0x%x: Application Specific Data from GO\n", MSG_ID_APPLICATION_SPECIFIC_DATA);
if (bType == MODEM_TRANSMISSION_RESULT)
{
ThirdParty_SetTransmissionStatus(IOExpanderMsg->abData[1]);
//Print modem transmission result
if(IOExpanderMsg->abData[1] == 0x01)
{
printf("Modem transmission result - Accepted\n");
}
else if(IOExpanderMsg->abData[1] == 0x00)
{
printf("Modem transmission result - Rejected\n");
}
}
}
// ****************************************************************
// ************** END of IO Control Message Handlers **************
// ****************************************************************
// End of file