-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbh_xbdcomm.c
683 lines (569 loc) · 23.2 KB
/
xbh_xbdcomm.c
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#include <string.h>
#include "hal/i2c_comm.h"
#include "hal/crc.h"
#include "xbh.h"
#include "util.h"
#include "xbh_xbdcomm.h"
#include "xbd_multipacket.h"
//I2C Slave address
#define SLAVE_ADDR (0x75)
unsigned char xbd_comm=COMM_I2C;
#if 0/*{{{*/
#define CE_IDLE 0
#define CE_ACK_WAIT 1
#define CE_FAILURE 2
#define CE_ANSWER_WAIT 3
#define CE_ANSWER_RECD 4
#define CE_MAGIC_SEQNUM 0
#define CE_TIMEOUT_SECS 2
#define CE_MAX_TIMEOUTS 10
#define UART_COMM_BAUDRATE 115200
#define UART_OVERDRIVE_BAUDRATE 250000
#define UART_DEBUG_BAUDRATE 115200
#define UDP_CONN_WRITEOP 'W'
#define UDP_CONN_READOP 'R'
#define UDP_CONN_NOOP '0'
//#define XBD_UDP_DEBUG
//const char XBHcrcFail[] PROGMEM = "XBHcrcFL";
u32 xbd_ip=DEFAULT_XBD_IP;
unsigned char * xbd_ip_bytes=(unsigned char *)&xbd_ip;
#define XBD_PORT 22596
unsigned char ce_state=CE_IDLE;
unsigned char ce_seqnum=0;
unsigned char ce_last_ackd=0;
unsigned char ce_timeouts=0;
unsigned long ce_timeout_at=0;
extern uint8_t I2cReceiveData[I2C_RECEIVE_DATA_BUFFER_SIZE];
#endif /*}}}*/
// TODO: Implement other transports/*{{{*/
#if 0
void inc_ce_seqnum()
{
++ce_seqnum;
if(0 == ce_seqnum) ce_seqnum=1;
}
uint8_t usart_read_char(uint8_t i)
{
int receive_char;
loop_until_bit_is_set(UCSR0A ,RXC0);
receive_char = (UDR0);
return receive_char;
}
void xbdCommUdpGet(unsigned char index)
{
unsigned int ip_byte[4];
struct UDP_Header *udp;
struct IP_Header *ip;
unsigned int src_port;
uint16_t *p_crc;
uint16_t udp_data_len = (UDP_DATA_END_VAR)-(UDP_DATA_START);
udp = (struct UDP_Header *)ð_buffer[UDP_OFFSET];
ip = (struct IP_Header *)ð_buffer[IP_OFFSET];
src_port = NTOHS((udp->udp_SrcPort));
if((ip->IP_Srcaddr != xbd_ip) ||(src_port != XBD_PORT))
{
ip_byte[0]=(ip->IP_Srcaddr)&0xff;
ip_byte[1]=((ip->IP_Srcaddr)>>8)&0xff;
ip_byte[2]=((ip->IP_Srcaddr)>>16)&0xff;
ip_byte[3]=((ip->IP_Srcaddr)>>24)&0xff;
XBH_ERROR("xbdCommUdpGet srcPort: %i *\n",src_port);
XBH_ERROR("xbdCommUdpGet SrcIP: %i.%i.%i.%i *\n",ip_byte[0],ip_byte[1],ip_byte[2],ip_byte[3]);
return;
}
#ifdef XBD_UDP_DEBUG
XBH_DEBUG("xbdCommUdpGet Bytes: %\n",udp_data_len);
XBD_debugOutBuffer("xbdCommUdpGet data", ð_buffer[UDP_DATA_START],udp_data_len);
#endif
switch(udp_conn_lastop)
{
case UDP_CONN_WRITEOP:
#ifdef XBD_UDP_DEBUG
XBH_DEBUG("UDP_CONN_WRITEOP, answer len=%d\n",udp_data_len);
#endif
switch(ce_state)
{
case CE_ACK_WAIT:
//XBH_DEBUG("WR len=\n");
if( //UDP ACK Rec'd
(0xAC == eth_buffer[UDP_DATA_START]) &&
(ce_seqnum == eth_buffer[UDP_DATA_START+1]) &&
(2==udp_data_len)
)
{
//XBH_DEBUG("WR ce_state=CE_ANSWER_WAIT\n");
inc_ce_seqnum();
ce_timeouts=0;
ce_timeout_at=time+CE_TIMEOUT_SECS;
ce_state=CE_ANSWER_WAIT;
}
else
{
XBH_WARN("WR case CE_ACK_WAIT els\n");
XBH_WARN("Bytes: %\n",udp_data_len);
XBD_debugOutBuffer("WR case CE_ACK_WAIT else data", ð_buffer[UDP_DATA_START],udp_data_len);
//In case the XBD has not seen my last UDP ack:
if(ce_last_ackd == eth_buffer[UDP_DATA_START+1])
{
XBH_WARN("WR AW ACK re-tx\n");
// Send the UDP ACK
eth_buffer[UDP_DATA_START]=0xAC;
create_new_udp_packet( (unsigned int) 2,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
}
else
{
// this may happen after an reset if xbd did not reset and has data waiting
XBH_WARN("WR AW ACK unknown seq nu\n");
// Send the UDP ACK anyway
eth_buffer[UDP_DATA_START]=0xAC;
create_new_udp_packet( (unsigned int) 2,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
}
}
break;
case CE_ANSWER_WAIT:
if( //StreamComm 'A'CK Rec'd
('D' == eth_buffer[UDP_DATA_START]) &&
('A' == eth_buffer[UDP_DATA_START+2]) &&
(3==udp_data_len)
)
{
//XBH_DEBUG("WR AnswW 'A' ACKe\n");
if((ce_last_ackd==eth_buffer[UDP_DATA_START+1]) && (0 !=ce_last_ackd) )
XBH_WARN(" but it was already ACKe\n");
// Send the UDP ACK and set CE state machine to idle
eth_buffer[UDP_DATA_START]=0xAC;
ce_last_ackd=eth_buffer[UDP_DATA_START+1];
create_new_udp_packet( (unsigned int) 2,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
ce_state=CE_IDLE;
udp_conn_lastop = UDP_CONN_NOOP;
}
else
{
XBH_ERROR("WR case CE_ANSWER_WAIT els\n");
XBH_ERROR("Bytes: %\n",udp_data_len);
XBD_debugOutBuffer("WR case CE_ANSWER_WAIT else data", ð_buffer[UDP_DATA_START],udp_data_len);
}
break;
case CE_IDLE:
#ifdef XBD_UDP_DEBUG
XBH_DEBUG("WR case CE_IDLE: Bytes: %\n",udp_data_len);
XBD_debugOutBuffer("xbdCommUdpGet data", ð_buffer[UDP_DATA_START],udp_data_len);
#endif
// TODO: Remove this block, it may not be necessary anymore
XBH_WARN("WR IDLE ACK re-tx\n");
// Send the UDP ACK
eth_buffer[UDP_DATA_START]=0xAC;
ce_last_ackd=eth_buffer[UDP_DATA_START+1];
create_new_udp_packet( (unsigned int) 2,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
break;
default:
XBH_ERROR("\nBad ce_state [%d] at xbdCommUdpGet WO!\n",ce_state);
ce_state=CE_FAILURE;
}//switch(ce_state)
break;//case UDP_CONN_WRITEOP:
case UDP_CONN_READOP:
#ifdef XBD_UDP_DEBUG
XBH_DEBUG("UDP_CONN_READOP, len=%d\n",udp_data_len);
#endif
if((udp_data_len-2) > I2C_RECEIVE_DATA_BUFFER_SIZE) {
XBH_ERROR("Received packet too large for buffer! (%d bytes\n",udp_data_len);
XBD_debugOutBuffer("too large packet", ð_buffer[UDP_DATA_START],udp_data_len);
}
else
{
switch(ce_state)
{
case CE_ACK_WAIT:
if( //UDP ACK Rec'd
(0xAC == eth_buffer[UDP_DATA_START]) &&
(ce_seqnum == eth_buffer[UDP_DATA_START+1]) &&
(2==udp_data_len)
)
{
inc_ce_seqnum();
ce_timeouts=0;
ce_timeout_at=time+CE_TIMEOUT_SECS;
ce_state=CE_ANSWER_WAIT;
//XBH_DEBUG("RE ce_state=CE_ANSWER_WAIT\n");
}
else
{
XBH_WARN("RE case CE_ACK_WAIT else ");
XBH_WARN("Bytes: %\n",udp_data_len);
XBD_debugOutBuffer("RE case CE_ACK_WAIT else data", ð_buffer[UDP_DATA_START],udp_data_len);
XBH_WARN("RE AW ACK re-tx\n");
//In case the XBD has not seen my last UDP ack:
if(ce_last_ackd == eth_buffer[UDP_DATA_START+1])
{
// Re-Send the UDP ACK
eth_buffer[UDP_DATA_START]=0xAC;
create_new_udp_packet( (unsigned int) 2,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
}
else
{
XBH_ERROR("RD AW ACK Arsc\n");
// Re-Send the UDP ACK anyway
eth_buffer[UDP_DATA_START]=0xAC;
create_new_udp_packet( (unsigned int) 2,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
}
}
break;
case CE_ANSWER_WAIT:
if( //StreamComm 'A'CK Rec'd
('D' == eth_buffer[UDP_DATA_START]) &&
(3<=udp_data_len)
)
{
memcpy(I2cReceiveData,ð_buffer[UDP_DATA_START+2],udp_data_len-2);
//Check CRC
p_crc=(uint16_t *)ð_buffer[UDP_DATA_START+udp_data_len-CRC16SIZE];
if( ! (crc16check(ð_buffer[UDP_DATA_START+2], udp_data_len-2-CRC16SIZE, p_crc)) )
{
XBD_debugOutBuffer("RE CE_ANSWER_WAIT wrong CRC at ", ð_buffer[UDP_DATA_START], udp_data_len);
break;
}
// Send the UDP ACK and set CE state machine to idle
eth_buffer[UDP_DATA_START]=0xAC;
ce_last_ackd=eth_buffer[UDP_DATA_START+1];
create_new_udp_packet( (unsigned int) 2,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
ce_state=CE_IDLE;
udp_conn_lastop = UDP_CONN_NOOP;
//XBH_DEBUG("RE ce_state=CE_IDLE\n");
}
else
{
XBH_WARN("RE case CE_ANSWER_WAIT els\n");
XBH_WARN("Bytes: %\n",udp_data_len);
XBD_debugOutBuffer("RE case CE_ANSWER_WAIT else data", ð_buffer[UDP_DATA_START],udp_data_len);
XBH_ERROR("RD AnswW ACK Arsc\n");
}
break;
case CE_IDLE:
#ifdef XBD_UDP_DEBUG
XBH_DEBUG("RE case CE_IDLE: Bytes: %\n",udp_data_len);
XBD_debugOutBuffer("xbdCommUdpGet data", ð_buffer[UDP_DATA_START],udp_data_len);
#endif
XBH_WARN("WR IDLE ACK re-tx\n");
// Send the UDP ACK
eth_buffer[UDP_DATA_START]=0xAC;
ce_last_ackd=eth_buffer[UDP_DATA_START+1];
create_new_udp_packet( (unsigned int) 2,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
break;
default:
XBH_ERROR("\nBad ce_state [%d] at xbdCommUdpGet RO!\n",ce_state);
ce_state=CE_FAILURE;
}//switch(ce_state)
}//else
break;//case UDP_CONN_READOP:
case UDP_CONN_NOOP:
XBH_WARN("UDP_CONN_NOOP, len=%d\n",udp_data_len);
//In case the XBD has not seen my last UDP ack:
if( (ce_last_ackd == eth_buffer[UDP_DATA_START+1]) &&
('D' == eth_buffer[UDP_DATA_START]))
{
XBH_WARN("NOOP ACK re-tx\n");
// Send the UDP ACK
eth_buffer[UDP_DATA_START]=0xAC;
create_new_udp_packet( (unsigned int) 2,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
}
break;
}//switch(udp_conn_lastop)
}
void xbdCommUdpInit()
{
static uint8_t app_added=0;
//XBH_DEBUG("\nce_state=CE_IDLE @ xbdCommUdpInit(\n");
ce_state=CE_IDLE;
if(!app_added)
{
app_added=1;
add_udp_app (XBD_PORT, (void(*)(unsigned char))xbdCommUdpGet);
(*((unsigned long*)&xbd_ip_bytes[0])) = get_eeprom_value(XBDIP_EEPROM_STORE,DEFAULT_XBD_IP);
}
}
#endif/*}}}*/
void xbdCommExit()
{
switch(xbd_comm) {
case COMM_I2C:
break;
case COMM_UART:
case COMM_UART_OVERDRIVE:
case COMM_ETHERNET:
break;
}
}
void xbdCommInit(uint8_t commMode)
{
xbd_comm=commMode;
switch(xbd_comm) {
case COMM_I2C:
XBH_DEBUG("I2C\n");
break;
//TODO: Implement other modes/*{{{*/
#if 0
case COMM_UART:
usart_init(UART_COMM_BAUDRATE);
usart_status.usart_ignore = 1;
break;
case COMM_UART_OVERDRIVE:
usart_init(UART_OVERDRIVE_BAUDRATE);
usart_status.usart_ignore = 1;
break;
case COMM_ETHERNET:
usart_init(UART_DEBUG_BAUDRATE);
usart_status.usart_ignore = 0;
XBH_DEBUG("\nEthernet XB\n");
xbdCommUdpInit();
break;
#endif
/*}}}*/
default:
uart_printf("UNSUPPORTED XBD communication mode!\n");
}
}
/**
* Sends buf to XBD
* @param buf Buffer to send to XBD
* @param length Length of buffer
*/
void xbdSend(const void *buf, size_t length) {
uint16_t crc;
//generate CRC16
crc = crc16_create(buf, length);
// We can't write directly to end of buffer because of possible alignment issues
// Manually split crc
((uint8_t *)buf)[length] = crc >> 8;
((uint8_t *)buf)[length+1] = crc & 0xFF;
length+=CRC16SIZE;
switch(xbd_comm) {
case COMM_I2C:
i2c_comm_write(SLAVE_ADDR, buf, length, true);
break;
//TODO: Implement other modes/*{{{*/
#if 0
case COMM_UART:
case COMM_UART_OVERDRIVE:
usart_write_char(0);
usart_write_char(0);
usart_write_char(0);
usart_write_char(0);
usart_write_char('W');
usart_write_char(length);
for(i=0;i<length;i++) usart_write_char(buf[i]);
ch=usart_read_char(0);
if('A' != ch) XBH_ERROR("ACK was not an 'A\n");
break;
case COMM_ETHERNET:
//XBH_DEBUG("\nce_state=CE_ACK_WAI\n");
ce_state=CE_ACK_WAIT;
ce_timeouts=0;
SWITCH_TO_SMALL_BUFFER;
eth_buffer[UDP_DATA_START]='D';
eth_buffer[UDP_DATA_START+1]=ce_seqnum;
eth_buffer[UDP_DATA_START+2]='W';
memcpy(ð_buffer[UDP_DATA_START+3],buf,length);
#ifdef XBD_UDP_DEBUG
XBD_debugOutBuffer("udp_data", ð_buffer[UDP_DATA_START],length+3);
XBH_DEBUG("----------UDP send-----------\n");
#endif
create_new_udp_packet( (unsigned int) length+3,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
ce_timeout_at=time+CE_TIMEOUT_SECS;
udp_conn_lastop = UDP_CONN_WRITEOP;
do
{
eth_get_data(); //Hast Du keinen, pump Dir einen!
switch(ce_state)
{
case CE_ACK_WAIT:
if(ce_timeout_at<=time)
{
eth_buffer[UDP_DATA_START]='D';
eth_buffer[UDP_DATA_START+1]=ce_seqnum;
eth_buffer[UDP_DATA_START+2]='W';
memcpy(ð_buffer[UDP_DATA_START+3],buf,length);
//XBH_DEBUG("\n*** ce_timeout **\n");
#ifdef XBD_UDP_DEBUG
XBD_debugOutBuffer("CE_ACK_WAIT udp_data", ð_buffer[UDP_DATA_START],length+3);
XBH_DEBUG("----------UDP retransmit-----------\n");
#endif
create_new_udp_packet( (unsigned int) length+3,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
++ce_timeouts;
ce_timeout_at=time+CE_TIMEOUT_SECS;
}
if(ce_timeouts >= CE_MAX_TIMEOUTS)
{
XBH_WARN("ce_state=CE_FAILURE, timeout limi\n");
ce_state=CE_FAILURE;
udp_conn_lastop = UDP_CONN_NOOP;
}
break;
case CE_ANSWER_WAIT:
break;
case CE_IDLE:
udp_conn_lastop = UDP_CONN_NOOP;
break;
default:
XBH_ERROR("\nBad ce_state [%d] at xbdSend!\n",ce_state);
ce_state=CE_FAILURE;
udp_conn_lastop = UDP_CONN_NOOP;
}//end of switch(ce_state)
}// end of do
while(udp_conn_lastop != UDP_CONN_NOOP);
SWITCH_TO_LARGE_BUFFER;
break;
#endif/*}}}*/
}
}
/**
* Receives buf from XBD
* @param buf Buffer to receive from XBD
* @param length Length of buffer
* @return -1 if fail, 0 if success
*/
int xbdReceive(void *buf, size_t length) {
int i;
uint16_t crc;
//XBH_DEBUG("XBD: Waiting for %d byte\n",length);
length+=CRC16SIZE;
switch(xbd_comm) {
case COMM_I2C:
i = i2c_comm_read(SLAVE_ADDR, buf, length, true);
if( i != 0 ) {
XBH_ERROR("I2C Receive error: %d\n",i);
}
break;
// TODO: Implement UART and ethernet transports/*{{{*/
#if 0
case COMM_UART:
case COMM_UART_OVERDRIVE:
usart_write_char(0);
usart_write_char(0);
usart_write_char(0);
usart_write_char(0);
usart_write_char('R');
usart_write_char(length);
for(i=0;i<length;i++) {
buf[i]=usart_read_char(i);
}
break;
case COMM_ETHERNET:
//XBH_DEBUG("\nce_state=CE_ACK_WAI\n");
ce_state=CE_ACK_WAIT;
ce_timeouts=0;
SWITCH_TO_SMALL_BUFFER;
eth_buffer[UDP_DATA_START]='D';
eth_buffer[UDP_DATA_START+1]=ce_seqnum;
eth_buffer[UDP_DATA_START+2]='R';
eth_buffer[UDP_DATA_START+3]=length;
#ifdef XBD_UDP_DEBUG
XBD_debugOutBuffer("COMM_ETHERNET udp_data", ð_buffer[UDP_DATA_START],4);
XBH_DEBUG("----------UDP receive-----------\n");
#endif
create_new_udp_packet( (unsigned int) 4,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
/*
udp_conn_lastop = UDP_CONN_READOP;
do
{
eth_get_data(); //Hast Du keinen, pump Dir einen!
}
while(udp_conn_lastop != UDP_CONN_NOOP);
*/
ce_timeout_at=time+CE_TIMEOUT_SECS;
udp_conn_lastop = UDP_CONN_READOP;
do
{
eth_get_data(); //Hast Du keinen, pump Dir einen!
switch(ce_state)
{
case CE_ACK_WAIT:
if(ce_timeout_at<=time)
{
eth_buffer[UDP_DATA_START]='D';
eth_buffer[UDP_DATA_START+1]=ce_seqnum;
eth_buffer[UDP_DATA_START+2]='R';
eth_buffer[UDP_DATA_START+3]=length;
//XBH_DEBUG("\n*** ce_timeout **\n");
#ifdef XBD_UDP_DEBUG
#endif
XBD_debugOutBuffer("CE_ACK_WAIT udp_data", ð_buffer[UDP_DATA_START],4);
XBH_DEBUG("----------UDP retransmit-----------\n");
create_new_udp_packet( (unsigned int) 4,
(unsigned int) XBD_PORT,
(unsigned int) XBD_PORT,
(unsigned long) xbd_ip);
++ce_timeouts;
ce_timeout_at=time+CE_TIMEOUT_SECS;
}
if(ce_timeouts >= CE_MAX_TIMEOUTS)
{
XBH_ERROR("\nce_state=CE_FAILURE, timeout limi\n");
ce_state=CE_FAILURE;
udp_conn_lastop = UDP_CONN_NOOP;
}
break;
case CE_ANSWER_WAIT:
break;
case CE_IDLE:
break;
default:
XBH_ERROR("\nBad ce_state [%d] at xbdSend!\n",ce_state);
ce_state=CE_FAILURE;
udp_conn_lastop = UDP_CONN_NOOP;
}//end of switch(ce_state)
}// end of do
while(udp_conn_lastop != UDP_CONN_NOOP);
#ifdef XBD_UDP_DEBUG
XBD_debugOutBuffer("udp_data from XBD", I2cReceiveData, length);
#endif
memcpy(buf,I2cReceiveData,length);
SWITCH_TO_LARGE_BUFFER;
break;
#endif/*}}}*/
}
//Check CRC
crc=((uint8_t *)buf)[length-CRC16SIZE] << 8;
crc|=((uint8_t *)buf)[length-(CRC16SIZE-1)];
if(crc != crc16_create(buf, length-CRC16SIZE) ) {
//XBD_debugOutBuffer("wrong CRC", buf, length);
XBH_ERROR("wrong CRC\n");
memcpy(buf, XBD_CMD[XBD_CMD_ccf], XBD_COMMAND_LEN);
return -1;
}
return 0;
}