-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxbee_utilities.c
More file actions
483 lines (367 loc) · 11.6 KB
/
Copy pathxbee_utilities.c
File metadata and controls
483 lines (367 loc) · 11.6 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// xbee_utilities.c - Copyright 2016, HZB, ILL/SANE & ISIS
#define RELEASE_XBEE_UTILS 1.00
// HISTORY -------------------------------------------------------------
// 1.00 - First release on June 2016
#include <avr/io.h>
#include <string.h>
#include "../config.h"
#include "xbee.h"
#include "xbee_utilities.h"
#include "xbee_AT_comm.h"
#include "module_globals.h"
// Global Variables
volatile BuffType *setPtr;
volatile BuffType frameBuffer[BUFFER_LENGTH];
volatile uint8_t bufferSize;
//
void buffer_init(void)
{
setPtr = (BuffType*)&frameBuffer; // Set up the IN pointer to the start of the buffer
bufferSize = 0; // Reset the buffer elements counter
}
void buffer_storeData(BuffType data)
{
if(bufferSize < BUFFER_LENGTH)
bufferSize++;
*setPtr = data; // store data
setPtr++; // increment the IN pointer to the next element
if (setPtr == (BuffType*)&frameBuffer[BUFFER_LENGTH])
setPtr = (BuffType*)&frameBuffer; // Wrap pointer if end of array reached
}
// Clear buffer status
void buffer_removeData(uint8_t index)
{
frameBuffer[index].status = 0xFF; //bad frame
}
// Return the index of reply corresponding to the command type
uint8_t xbee_hasReply(uint8_t cmd_type, uint8_t range)
{
switch(range)
{
case EQUAL:
for(uint8_t i = 0; i < bufferSize; ++i)
{
if((frameBuffer[i].status == 0x00) && (frameBuffer[i].type == cmd_type))
{
return i;
}
}
break;
// case LESS_THAN:
// for(uint8_t i = 0; i < bufferSize; ++i)
// {
// if((frameBuffer[i].status == 0x00) && (frameBuffer[i].type < cmd_type))
// {
// return i;
// }
// }
// break;
case GREATER_THAN:
for(uint8_t i = 0; i < bufferSize; ++i)
{
if((frameBuffer[i].status == 0x00) && (frameBuffer[i].type > cmd_type))
{
return i;
}
}
break;
}
return 0xFF;
}
// ...
inline uint8_t get_at_frame_type(char *id, uint8_t len)
{
//if(len < 2) return UNDEFINED;
if((id[0] == 'D') && (id[1] == 'A')) return DA_MSG_TYPE;
if((id[0] == 'D') && (id[1] == 'L')) return DL_MSG_TYPE;
if((id[0] == 'D') && (id[1] == 'H')) return DH_MSG_TYPE;
if((id[0] == 'A') && (id[1] == 'I')) return AI_MSG_TYPE;
if((id[0] == 'H') && (id[1] == 'V')) return HV_MSG_TYPE;
if((id[0] == 'S') && (id[1] == 'L')) return SL_MSG_TYPE;
if((id[0] == 'A') && (id[1] == 'S')) return AS_MSG_TYPE;
if((id[0] == 'S') && (id[1] == 'C')) return SC_MSG_TYPE;
if((id[0] == 'J') && (id[1] == 'V')) return JV_MSG_TYPE;
if((id[0] == 'S') && (id[1] == 'D')) return SD_MSG_TYPE;
if((id[0] == 'V') && (id[1] == 'R')) return VR_MSG_TYPE;
if((id[0] == 'W') && (id[1] == 'R')) return WR_MSG_TYPE;
if((id[0] == 'N') && (id[1] == 'I')) return NI_MSG_TYPE;
if((id[0] == 'C') && (id[1] == 'E')) return CE_MSG_TYPE;
if((id[0] == 'S') && (id[1] == 'M')) return SM_MSG_TYPE;
if((id[0] == 'C') && (id[1] == 'H')) return CH_MSG_TYPE;
if((id[0] == 'Z') && (id[1] == 'S')) return ZS_MSG_TYPE;
if((id[0] == 'N') && (id[1] == 'J')) return NJ_MSG_TYPE;
if((id[0] == 'A') && (id[1] == '1')) return A1_MSG_TYPE;
if((id[0] == 'A') && (id[1] == '2')) return A2_MSG_TYPE;
if((id[0] == 'D') && (id[1] == 'B')) return DB_MSG_TYPE;
return 0;
}
// Build a frame for XBee communication
inline void xbee_build_frame(uint8_t *buffer, uint8_t length)
{
if(length < 5) return;
if(!xbee_chkChecksum(buffer)) return;
BuffType newFrame;
uint8_t data_counter = 0;
newFrame.length = (buffer[1]<<8) + buffer[2];
newFrame.api_id = buffer[3];
newFrame.data_len = 0; //init
newFrame.type = 0; //init
#ifdef USE_LAN
switch (newFrame.api_id)
{
case API_TX_64: //0x00
newFrame.type = buffer[14];
newFrame.status = STATUS_BYTE;// buffer[15];
if (length < (DATA_INDEX+1)) return;
for (uint8_t i = DATA_INDEX; i < length; ++i)
newFrame.data[data_counter++] = buffer[i];
newFrame.data_len = --data_counter;
if (newFrame.type == (SET_OPTIONS_CMD||LOGIN_MSG))
{
GCM_IP.IP_oct_1 = buffer[5];
GCM_IP.IP_oct_2= buffer[7];
GCM_IP.IP_oct_3 = buffer[9];
GCM_IP.IP_oct_4 = buffer[11];
}
break;
case API_AT_CMD: //0x08
newFrame.type = get_at_frame_type((char*) &buffer[5], 2);
newFrame.status = 0;
// no payload data
if (length < (7+1)) return;
for (uint8_t i = 7; i < length; ++i){
newFrame.data[data_counter++] = buffer[i];
}
newFrame.data_len = --data_counter;
break;
default:
return;
break;
}
#endif
#ifdef USE_XBEE
switch(newFrame.api_id)
{
case AT_ID:
newFrame.status = buffer[7];
newFrame.type = get_at_frame_type((char*) &buffer[5], 2);
//write data
if (length < (8+1)) return;
for (uint8_t i = 8; i < length; ++i)
newFrame.data[data_counter++] = buffer[i];
newFrame.data_len = --data_counter;
break;
case RX_ID:
newFrame.type = buffer[OPCODE_INDEX];
newFrame.status = STATUS_BYTE;
if (length < (DATA_INDEX+1)) return;
for (uint8_t i = DATA_INDEX; i < length; ++i)
newFrame.data[data_counter++] = buffer[i];
newFrame.data_len = --data_counter;
break;
case STA_ID:
if (buffer[4]==2)
{
xbee.associated = 1;
}else if(buffer[4]==3){
xbee.associated = 0;
}
newFrame.status = 0xFF;
newFrame.type = STATUS_MSG_TYPE;
newFrame.data[0]= buffer[4];
newFrame.data_len =1;
break;
case AT_REMOTE_ID:
newFrame.status = buffer[17];
newFrame.type = get_at_frame_type((char*) &buffer[15],2);
//write data
if (length < (18+1)) return;
for (uint8_t i = 18; i < length; ++i)
newFrame.data[data_counter++] = buffer[i];
newFrame.data_len = --data_counter;
break;
default:
newFrame.status = 0xFF;
}
#endif
buffer_storeData(newFrame);
}
// Store device position (instrument id or labs) into the buffer
inline uint8_t devicePos_to_buffer(char *pos, uint8_t offset, uint8_t *buffer)
{
uint8_t index = 0;
while (pos[index]!='\0')
{
buffer[index+offset] = pos[index];
index++;
}
//fill spaces if pos is less then 4 letters
for(uint8_t i=0; i<(4 - index); i++) buffer[i+index+offset] = 0x20;
//return index, add spaces
return (index + offset) + (4 - index);
}
// Pack data (string + int parameters) in a XBee-frame (AT) ready to send from frame_buffer
// Only used with xbee_reset_connection & xbee_getAddress functions
uint8_t xbee_pack_tx_frame(uint8_t *params, uint8_t paramsNumber)
{
uint8_t temp_buffer[SINGLE_FRAME_LENGTH];
uint8_t index = 5;
uint8_t *temp = params;
temp_buffer[0] = 0x7E; // Start delimiter
// Let index 1 & 2 free for storing frame length
temp_buffer[3] = 0x08; // API Identifier Value for AT Command type allows for module parameters to be queried or set.
temp_buffer[4] = 0x42; // Constant Frame ID arbitrarily selected / Different of 0 to get an answer
while (paramsNumber)
{
temp_buffer[index++] = *params++;
paramsNumber--;
}
temp_buffer[index] = xbee_getChecksum(temp_buffer,3,index); // Calculate checksum
temp_buffer[1] = ((index-3) >> 8);
temp_buffer[2] = (index-3);
memcpy(temp, temp_buffer, index+1); // Store frame in the params array
return index+1; // Return number of bytes packed
}
// Function provided for convenience.
// When using this function you will able to send the data properly only once.
// Pure data passed in frame_buffer will be packed in frame_buffer.
// Pack data (string + int parameters) in a XBee-frame (TX 64-bit address) ready to send
// Disable acknowledgment and response frame
// Returns number of bytes packed
uint8_t xbee_pack_tx64_frame(uint8_t db_cmd_type, uint8_t *params, uint8_t paramsNumber, uint32_t dest_high, uint32_t dest_low)
{
uint8_t temp_buffer[SINGLE_FRAME_LENGTH];
uint8_t index = 0;
uint8_t *temp = params;
#ifdef USE_XBEE
temp_buffer[0] = 0x7E; // Start delimiter
// Let index 1 & 2 free for storing frame length
temp_buffer[3] = 0x00; // API Identifier Value for 64-bit TX Request message will cause the module to send RF Data as an RF Packet.
temp_buffer[4] = 0x00; // Constant Frame ID arbitrarily selected
// Identifies the UART data frame for the host to correlate with a subsequent ACK (acknowledgment).
// Setting Frame ID to ‘0' will disable response frame.
// 32-bit destination address high
temp_buffer[5] = (uint8_t)(dest_high >> 24);
temp_buffer[6] = (uint8_t)(dest_high >> 16);
temp_buffer[7] = (uint8_t)(dest_high >> 8);
temp_buffer[8] = (uint8_t) dest_high;
// 32-bit destination address low
temp_buffer[9] = (uint8_t)(dest_low >> 24);
temp_buffer[10] = (uint8_t)(dest_low >> 16);
temp_buffer[11] = (uint8_t)(dest_low >> 8);
temp_buffer[12] = (uint8_t) dest_low;
temp_buffer[13] = 0x01; // Disable acknowledgment
temp_buffer[14] = db_cmd_type; // Database command type
#endif
#ifdef USE_LAN
temp_buffer[0] = 0x7E;
//len
temp_buffer[3] = 0x80; //API ID 64 bit RX // must be together 0x80 to be compatible with the xBee frame structure for the server
temp_buffer[4] = GCM_IP.IP_oct_1;
temp_buffer[5] = 46;
temp_buffer[6] = GCM_IP.IP_oct_2;
temp_buffer[7] = 46;
temp_buffer[8] = GCM_IP.IP_oct_3;
temp_buffer[9] = 46;
temp_buffer[10] = GCM_IP.IP_oct_4;
temp_buffer[11] = 0;
temp_buffer[12] = 0x00;
temp_buffer[13] = 0x03;
temp_buffer[14] = db_cmd_type;
#endif
temp_buffer[15] = MESSAGEFOPRMAT_IDENTIFIER;
temp_buffer[16] = version.Branch_id;
temp_buffer[17] = (uint8_t)( version.Fw_version>> 8); //MSB
temp_buffer[18] = (uint8_t) (version.Fw_version ); //LSB
#ifdef LEVELMETER
// Pack entered device number in buffer
for(uint8_t i = 0; i < xbee.dev_id_str_len; i++){
temp_buffer[i+19] = xbee.dev_id_str[i];
}
index = 19 + xbee.dev_id_str_len;
#else
index = 19;
#endif
// Parameter
while (paramsNumber)
{
temp_buffer[index] = *params++;
index++;
paramsNumber--;
}
// Calculate checksum
temp_buffer[index] = xbee_getChecksum(temp_buffer,3,index);
// Add frame length - excludes Start delimiter, Length and checksum
temp_buffer[1] = ((index-3) >> 8);
temp_buffer[2] = (index-3);
memcpy(temp, temp_buffer, index+1); // Frame is in the params array
return index+1;
}
// Calculate checksum of the packed frame
uint8_t xbee_getChecksum(uint8_t *buffer, uint8_t start, uint8_t stop)
{
if (start > stop) return 0;
uint8_t summe = 0;
uint8_t index = start;
while(index < stop)
{//add all bytes from start to stop
summe += buffer[index++];
}
return 0xFF-summe;//result;
}
// Return true if checksum is correct, false otherwise
_Bool xbee_chkChecksum(uint8_t *buffer)
{
uint8_t index = 0;
uint8_t summe = 0;
uint16_t bytes_number = xbee_get_packet_len(buffer)+1; //data + checksum
while(bytes_number)
{//add all bytes after frame length (3.byte)
summe += buffer[index+3];
bytes_number--;
index++;
}
return (summe == 0xFF);
}
// Return the length of the passed XBee-frame
uint8_t xbee_get_packet_len(uint8_t *buffer)
{
return (buffer[1]<<8) + buffer[2];
}
/**
* @brief Used for the LAN-Variant of the Gascounters. Necessary for Xbee simulation.
*
* @param CMD_NAME_FC
* @param CMD_NAME_SC
* @param status
* @param values
* @param value_length
*
* @return void
*/
void xbee_pseudo_send_AT_response( char CMD_NAME_FC, char CMD_NAME_SC, uint8_t status, uint8_t *values, uint8_t value_length)
{
uint8_t temp_buffer[50];
uint16_t i;
uint8_t *temp = values;
temp_buffer[0] = 0x7E;
//length
temp_buffer[3] = API_AT_CMD_RESPONSE;
temp_buffer[4] = 1;// FRAME_ID;
temp_buffer[5] = (uint8_t) CMD_NAME_FC;
temp_buffer[6] = (uint8_t) CMD_NAME_SC;
temp_buffer[7] = status;
i = 8;
while (value_length)
{
temp_buffer[i++] = *values++;
value_length--;
}
//checksum
temp_buffer[i] = xbee_getChecksum(temp_buffer,3,i);
//add length
temp_buffer[1] = ((i-3) >> 8);
temp_buffer[2] = (i-3);
memcpy(temp, temp_buffer, i+1);
xbee_send_msg(temp,i+1);
}