-
Notifications
You must be signed in to change notification settings - Fork 1
/
hdlc.cpp
696 lines (631 loc) · 23.4 KB
/
hdlc.cpp
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
684
685
686
687
688
689
690
691
692
693
694
695
696
/**
* Copyright (c) 2016, Autonomous Networks Research Group. All rights reserved.
* Developed by:
* Autonomous Networks Research Group (ANRG)
* University of Southern California
* http://anrg.usc.edu/
*
* Contributors:
* Jason A. Tran
* Pradipta Ghosh
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* with the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
* - Neither the names of Autonomous Networks Research Group, nor University of
* Southern California, nor the names of its contributors may be used to
* endorse or promote products derived from this Software without specific
* prior written permission.
* - A citation to the Autonomous Networks Research Group must be included in
* any publications benefiting from the use of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
* THE SOFTWARE.
*/
/**
* @file hdlc.cpp
* @brief Full duplex hdlc implementation for mbed-os.
*
* This implementation leverages yahdlc, an open source library. The current
* implementation is stop & wait.
*
* @author Pradipta Ghosh <[email protected]>
* @author Jason A. Tran <[email protected]>
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include "mbed.h"
#include "platform/CircularBuffer.h"
#include "hdlc.h"
#include "rtos.h"
#include "uart_pkt.h"
#include "utlist.h"
#define DEBUG 0
#if (DEBUG)
#define PRINTF(...) pc.printf(__VA_ARGS__)
extern Serial pc;
#else
#define PRINTF(...)
#endif /* (DEBUG) */
#define UART_BUFSIZE (512U)
DigitalOut led2(LED2);
static osThreadId sender_pid;
static unsigned char HDLC_STACK[DEFAULT_STACK_SIZE];
Thread hdlc(osPriorityNormal, (uint32_t) DEFAULT_STACK_SIZE,
(unsigned char *)HDLC_STACK);
Serial uart2(p28,p27, HDLC_BAUDRATE);
static hdlc_entry_t *hdlc_reg;
/**
* @brief adds a thread to port mapping
* @param entry The entry
*/
void hdlc_register(hdlc_entry_t *entry)
{
LL_PREPEND(hdlc_reg, entry);
}
/**
* @brief deletes a thread to port mapping
* @param entry The entry
*/
void hdlc_unregister(hdlc_entry_t *entry)
{
LL_DELETE(hdlc_reg, entry);
}
static Mail<msg_t, HDLC_MAILBOX_SIZE> *sender_mailbox_ptr;
Mail<msg_t, HDLC_MAILBOX_SIZE> hdlc_mailbox;
Semaphore recv_buf_mutex(1);
Semaphore recv_buf_cpy_mutex(1);
// Mutex recv_buf_mutex;
Timer global_time;
Timer uart_lock_time;
CircularBuffer<char, UART_BUFSIZE> circ_buf;
void write_hdlc(uint8_t *,int);
static char hdlc_recv_data[HDLC_MAX_PKT_SIZE];
static char hdlc_recv_data_cpy[HDLC_MAX_PKT_SIZE];
static char hdlc_send_frame[2 * (HDLC_MAX_PKT_SIZE + 2 + 2 + 2)];
static char hdlc_ack_frame[2 + 2 + 2 + 2];
static hdlc_buf_t recv_buf; // the initialization is done in the hdlc init function
static hdlc_buf_t recv_buf_cpy; // the initialization is done in the hdlc init function
static hdlc_buf_t send_buf;
static hdlc_buf_t ack_buf;
/* uart access control lock */
static bool uart_lock = 0;
/**
* @brief Interrupt handler to processing incoming hdlc bytes
*/
static void rx_cb(void)//(void *arg, uint8_t data)
{
unsigned char data;
while (uart2.readable()) {
data = uart2.getc(); // Get an character from the Serial
circ_buf.push(data); // Put to the ring/circular buffer
if (data == YAHDLC_FLAG_SEQUENCE) {
// wakeup hdlc thread
msg_t *msg = hdlc_mailbox.alloc();
if(msg == NULL)
{
PRINTF("hdlc: rx_cb no more space available on mailbox\n");
return;
}
msg->sender_pid = osThreadGetId();
msg->type = HDLC_MSG_RECV;
msg->source_mailbox = &hdlc_mailbox;
hdlc_mailbox.put(msg);
}
}
}
/**
* @brief This function is used by hdlc thread to check the validity of the incoming packets
*
* @param recv_seq_no The receive sequence no
* @param send_seq_no The send sequence no
*/
static void _hdlc_receive(unsigned int *recv_seq_no, unsigned int *send_seq_no)
{
msg_t *msg, *ack_msg;
int ret;
char c;
uart_pkt_hdr_t hdr;
hdlc_entry_t *entry;
while(1) {
if (!circ_buf.pop(c)) {
return;
}
recv_buf_mutex.wait();
ret = yahdlc_get_data(&recv_buf.control, &c, 1, recv_buf.data,
&recv_buf.length);
recv_buf_mutex.release();
if (ret == -ENOMSG) {
continue; //full packet not yet parsed
}
if (ret == -EIO) {
PRINTF("FCS ERROR OR INVALID FRAME!\n");
recv_buf.control.frame = (yahdlc_frame_t)0;
recv_buf.control.seq_no = 0;
return;
}
if (recv_buf.length > 0 &&
(recv_buf.control.seq_no == *recv_seq_no % 8 ||
recv_buf.control.seq_no == (*recv_seq_no - 1) % 8)) {
/* valid data frame received */
PRINTF("hdlc: received data frame w/ seq_no: %d\n", recv_buf.control.seq_no);
/* always send ack. This maybe bogging down the mailbox */
ack_msg = hdlc_mailbox.alloc();
if (ack_msg == NULL)
{
PRINTF("hdlc: ACK no more space available on mailbox\n");
return;
}
ack_msg->sender_pid = osThreadGetId();
ack_msg->type = HDLC_MSG_SND_ACK;
ack_msg->content.value = recv_buf.control.seq_no;
ack_msg->source_mailbox = &hdlc_mailbox;
hdlc_mailbox.put(ack_msg);
PRINTF("hdlc: received data frame w/ seq_no: %d\n", recv_buf.control.seq_no);
/* pass on packet to thread */
if (recv_buf.control.seq_no == (*recv_seq_no % 8)){
/* lock pkt until thread makes a copy and unlocks */
PRINTF("hdlc: received data frame w/ seq_no: %d\n", recv_buf.control.seq_no);
fflush(stdout);
recv_buf_cpy_mutex.wait();
recv_buf_mutex.wait();
buffer_cpy(&recv_buf_cpy,&recv_buf);
recv_buf_mutex.release();
uart_pkt_parse_hdr(&hdr, recv_buf_cpy.data, recv_buf_cpy.length);
LL_SEARCH_SCALAR(hdlc_reg, entry, port, hdr.dst_port);
PRINTF("hdlc: received packet for port %d\n", hdr.dst_port);
(*recv_seq_no)++;
if (entry) {
msg = entry->mailbox->alloc();
if( msg == NULL)
break;
msg->sender_pid = osThreadGetId();
msg->type = HDLC_PKT_RDY;
msg->content.ptr = &recv_buf_cpy;
msg->source_mailbox = &hdlc_mailbox;
entry->mailbox->put(msg);
} else {
PRINTF("hdlc: no thread subscribed to port!\n");
hdlc_pkt_release(&recv_buf_cpy);
}
}
recv_buf.control.frame = (yahdlc_frame_t)0;
recv_buf.control.seq_no = 0;
return;
} else if (recv_buf.length == 0 &&
(recv_buf.control.frame == YAHDLC_FRAME_ACK ||
recv_buf.control.frame == YAHDLC_FRAME_NACK)) {
PRINTF("hdlc: received ACK/NACK w/ seq_no: %d\n", recv_buf.control.seq_no);
if(recv_buf.control.seq_no == (*send_seq_no % 8)) {
msg=sender_mailbox_ptr->alloc();
if (msg == NULL)
return;
uart_lock = 0;
(*send_seq_no)++;
msg->sender_pid = osThreadGetId();
msg->type = HDLC_RESP_SND_SUCC;
msg->content.value = (uint32_t) 0;
msg->source_mailbox = &hdlc_mailbox;
sender_mailbox_ptr->put(msg);
PRINTF("hdlc: sender_pid is %d\n", sender_pid);
}
recv_buf.control.frame = (yahdlc_frame_t)0;
recv_buf.control.seq_no = 0;
return;
}
}
}
/**
* @brief This is the hdlc thread
*/
static void _hdlc()
{
msg_t *msg, *reply;
unsigned int recv_seq_no = 0;
unsigned int send_seq_no = 0;
osEvent evt;
while(1) {
led2=!led2;
// hdlc_ready=1;
if(uart_lock) {
// int uart_ll=(int)uart_lock_time.read_us();
// if(uart_ll>10*HDLC_RETRANS_TIMEO_USEC)
// {
// PRINTF("hdlc: UART is locked for %d us_seconds\n",uart_ll);
// uart_lock=0;
// goto getmail;
// }
int timeout = (int)HDLC_RETRANS_TIMEO_USEC - (int) global_time.read_us();
if(timeout < 0) {
// PRINTF("hdlc: inside timeout negative\n");
/* send message to self to resend msg */
msg = hdlc_mailbox.alloc();
if(msg == NULL) {
PRINTF("hdlc: no more space available on mailbox");
/* service mailbox and try again */
evt = hdlc_mailbox.get();
}
else {
msg->sender_pid = osThreadGetId();
msg->type = HDLC_MSG_RESEND;
msg->source_mailbox = &hdlc_mailbox;
hdlc_mailbox.put(msg);
evt = hdlc_mailbox.get();
}
} else {
evt = hdlc_mailbox.get(timeout / 1000);
if(evt.status == osEventTimeout){
continue;
}
}
} else {
// PRINTF("hdlc: waiting for mail\n");
getmail: evt = hdlc_mailbox.get();
}
if (evt.status == osEventMail)
{
msg = (msg_t*)evt.value.p;
switch (msg->type) {
case HDLC_MSG_RECV:
PRINTF("hdlc: receiving msg...\n");
_hdlc_receive(&recv_seq_no, &send_seq_no);
hdlc_mailbox.free(msg);
break;
case HDLC_MSG_SND:
PRINTF("hdlc: request to send received from pid %d\n", msg->sender_pid);
if (uart_lock) {
/* ask thread to try again in x usec */
PRINTF("hdlc: uart locked, telling thr to retry\n");
reply=((Mail<msg_t, HDLC_MAILBOX_SIZE>*)msg->source_mailbox)->alloc();
if(reply == NULL) {
PRINTF("hdlc: no space in thread mailbox. ERROR!!\n");
}
else {
reply->type = HDLC_RESP_RETRY_W_TIMEO;
reply->content.value = (uint32_t) HDLC_RTRY_TIMEO_USEC;
reply->sender_pid = osThreadGetId();
((Mail<msg_t, HDLC_MAILBOX_SIZE>*)msg->source_mailbox)->put(reply);
}
} else {
uart_lock = 1;
sender_pid = msg->sender_pid;
PRINTF("hdlc: sender_pid set to %d\n", sender_pid);
send_buf.control.frame = YAHDLC_FRAME_DATA;
send_buf.control.seq_no = send_seq_no % 8;
hdlc_pkt_t *pkt = (hdlc_pkt_t*)msg->content.ptr;
yahdlc_frame_data(&(send_buf.control), pkt->data,
pkt->length, send_buf.data, &send_buf.length);
sender_mailbox_ptr=(Mail<msg_t, HDLC_MAILBOX_SIZE>*)msg->source_mailbox;
PRINTF("hdlc: sending frame seq no %d, len %d\n",
send_buf.control.seq_no,send_buf.length);
write_hdlc((uint8_t *)send_buf.data, send_buf.length);
global_time.reset();
uart_lock_time.reset();
}
hdlc_mailbox.free(msg);
break;
case HDLC_MSG_SND_ACK:
/* send ACK */
ack_buf.control.frame = YAHDLC_FRAME_ACK;
ack_buf.control.seq_no = msg->content.value;
yahdlc_frame_data(&(ack_buf.control), NULL, 0, ack_buf.data,
&(ack_buf.length));
PRINTF("hdlc: sending ack w/ seq no %d, len %d\n",
ack_buf.control.seq_no,ack_buf.length);
write_hdlc((uint8_t *)ack_buf.data, ack_buf.length);
// uart2.write((uint8_t *)ack_buf.data, ack_buf.length,0,0);
hdlc_mailbox.free(msg);
break;
case HDLC_MSG_RESEND:
PRINTF("hdlc: Resending frame w/ seq no %d (on send_seq_no %d)\n",
send_buf.control.seq_no, send_seq_no);
write_hdlc((uint8_t *)send_buf.data, send_buf.length);
// uart2.write((uint8_t *)send_buf.data, send_buf.length,0,0);
global_time.reset();
hdlc_mailbox.free(msg);
break;
default:
PRINTF("INVALID HDLC MSG\n");
//LED3_ON;
hdlc_mailbox.free(msg);
break;
}
}
}
/* this should never be reached */
}
/**
* @brief Send @p pkt as an hdlc command packet over serial. This function blocks.
* @param pkt Packet to be sent.
* @param sender_mailbox Pointer to sender's mailbox.
* @return [description]
*/
int hdlc_send_command(hdlc_pkt_t *pkt, Mail<msg_t, HDLC_MAILBOX_SIZE> *sender_mailbox,
riot_to_mbed_t reply)
{
// /* TODO: should the second input not be a void pointer? */
// /* TODO: limit the number of retries */
msg_t *msg, *msg2;
hdlc_buf_t *buf;
uart_pkt_hdr_t hdr;
// Timer command_send_time;
/* send pkt */
msg = hdlc_mailbox.alloc();
msg->type = HDLC_MSG_SND;
msg->content.ptr = pkt;
msg->sender_pid = osThreadGetId();
msg->source_mailbox = sender_mailbox;
hdlc_mailbox.put(msg);
PRINTF("hdlc: in hdlc send command\n");
while(1)
{
osEvent evt = sender_mailbox->get(2000);
if(evt.status == osEventTimeout){
return 0;
}
// if(uart_ll>10*HDLC_RETRANS_TIMEO_USEC)
if (evt.status == osEventMail)
{
msg = (msg_t*)evt.value.p;
switch (msg->type)
{
case HDLC_RESP_SND_SUCC:
PRINTF("sent frame_no!\n");
sender_mailbox->free(msg);
break;
case HDLC_RESP_RETRY_W_TIMEO:
Thread::wait(msg->content.value/1000);
msg2 = hdlc_mailbox.alloc();
if (msg2 == NULL) {
while(msg2 == NULL)
{
msg2 = sender_mailbox->alloc();
Thread::wait(10);
}
/* TODO: this doesn't seem right... */
msg2->type = HDLC_RESP_RETRY_W_TIMEO;
msg2->content.value = (uint32_t) HDLC_RTRY_TIMEO_USEC;
msg2->sender_pid = osThreadGetId();
msg2->source_mailbox = sender_mailbox;
sender_mailbox->put(msg2);
sender_mailbox->free(msg);
break;
}
msg2->type = HDLC_MSG_SND;
msg2->content.ptr = pkt;
msg2->sender_pid = osThreadGetId();
msg2->source_mailbox = sender_mailbox;
hdlc_mailbox.put(msg2);
sender_mailbox->free(msg);
break;
case HDLC_PKT_RDY:
buf = (hdlc_buf_t *)msg->content.ptr;
uart_pkt_parse_hdr(&hdr, (void *)buf->data, (size_t) (buf->length));
hdlc_pkt_release(buf);
sender_mailbox->free(msg);
PRINTF("hdlc_send_command: received pkt\n");
if (hdr.pkt_type == reply)
return 1;
else
return 0;
break;
default:
sender_mailbox->free(msg);
/* error */
break;
}
}
}
}
/**
* @brief Send @p pkt as an hdlc command packet over serial. This function blocks.
* @param pkt Packet to be sent.
* @param sender_mailbox Pointer to sender's mailbox.
* @return [description]
*/
int hdlc_send_command_wo_resp(hdlc_pkt_t *pkt, Mail<msg_t, HDLC_MAILBOX_SIZE> *sender_mailbox)
{
// /* TODO: should the second input not be a void pointer? */
// /* TODO: limit the number of retries */
msg_t *msg, *msg2;
hdlc_buf_t *buf;
char recv_data_local[HDLC_MAX_PKT_SIZE];
uart_pkt_hdr_t hdr;
// Timer command_send_time;
/* send pkt */
msg = hdlc_mailbox.alloc();
msg->type = HDLC_MSG_SND;
msg->content.ptr = pkt;
msg->sender_pid = osThreadGetId();
msg->source_mailbox = sender_mailbox;
hdlc_mailbox.put(msg);
PRINTF("hdlc: in hdlc send command\n");
while(1)
{
osEvent evt = sender_mailbox->get(2000);
if(evt.status == osEventTimeout){
return 0;
}
// if(uart_ll>10*RETRANSMIT_TIMEO_USEC)
if (evt.status == osEventMail)
{
msg = (msg_t*)evt.value.p;
switch (msg->type)
{
case HDLC_RESP_SND_SUCC:
PRINTF("sent frame_no!\n");
sender_mailbox->free(msg);
return 1;
break;
case HDLC_RESP_RETRY_W_TIMEO:
Thread::wait(msg->content.value/1000);
msg2 = hdlc_mailbox.alloc();
if (msg2 == NULL) {
while(msg2 == NULL)
{
msg2 = sender_mailbox->alloc();
Thread::wait(10);
}
/* TODO: this doesn't seem right... */
msg2->type = HDLC_RESP_RETRY_W_TIMEO;
msg2->content.value = (uint32_t) HDLC_RTRY_TIMEO_USEC;
msg2->sender_pid = osThreadGetId();
msg2->source_mailbox = sender_mailbox;
sender_mailbox->put(msg2);
sender_mailbox->free(msg);
break;
}
msg2->type = HDLC_MSG_SND;
msg2->content.ptr = pkt;
msg2->sender_pid = osThreadGetId();
msg2->source_mailbox = sender_mailbox;
hdlc_mailbox.put(msg2);
sender_mailbox->free(msg);
break;
default:
sender_mailbox->free(msg);
/* error */
break;
}
}
}
}
/**
* @brief Releases the buffer mutex so that the next hdlc packet can be processed
* @param buf The buffer pointer
* @return status
*
*/
int hdlc_pkt_release(hdlc_buf_t *buf)
{
if(recv_buf_cpy_mutex.wait(0))
{
PRINTF("hdlc: Packet not locked. Might be empty!\n");
recv_buf_cpy_mutex.release();
return -1;
}
else
{
buf->control.frame = (yahdlc_frame_t)0;
buf->control.seq_no = 0;
PRINTF("hdlc: relesed lock!\n");
recv_buf_cpy_mutex.release();
return 0;
}
}
/**
* @brief Returns a pointer to the hdlc mailbox.
*
* @return The hdlc mailbox.
*/
Mail<msg_t, HDLC_MAILBOX_SIZE> *get_hdlc_mailbox()
{
return &hdlc_mailbox;
}
/**
* @brief Writes the data to uart
*
* @param ptr The pointer to the hdlc packet
* @param[in] len The length
*/
void write_hdlc(uint8_t *ptr,int len)
{
int count = 0;
while ( count < len ) {
if (uart2.writeable()) {
uart2.putc(ptr[count]);
count++;
}
}
}
/**
* @brief replicates the entire hdlc_buf_t
*
* @param dst The destination
* @param src The source
*/
void buffer_cpy(hdlc_buf_t* dst, hdlc_buf_t* src)
{
memcpy(dst->data,src->data,HDLC_MAX_PKT_SIZE);
memcpy(&dst->control,&src->control,sizeof(yahdlc_control_t));
dst->length=src->length;
}
/**
* @brief Intialized the HDLC Thread
*
* @param[in] priority The priority of the hdlc thread
*
* @return pointer to the hdlc mailbox
*/
Mail<msg_t, HDLC_MAILBOX_SIZE> *hdlc_init(osPriority priority)
{
led2 = 1;
recv_buf.data = hdlc_recv_data;
recv_buf_cpy.data= hdlc_recv_data_cpy;
send_buf.data = hdlc_send_frame;
ack_buf.data = hdlc_ack_frame;
global_time.start();
uart_lock_time.start();
uart2.attach(&rx_cb,Serial::RxIrq);
hdlc.set_priority(priority);
hdlc.start(_hdlc);
PRINTF("hdlc: thread id %d\n",hdlc.gettid());
return &hdlc_mailbox;
}
/**
* @brief Sends a hdlc packet.
*
* @param pkt The return packet
* @param[in] type The hdlc msg type
* @param sender_mailbox The sender mailbox
* @param ptr The pointer to the actual data
*
* @return status
*/
int send_hdlc_mail(msg_t *msg, uint8_t type,
Mail<msg_t, HDLC_MAILBOX_SIZE> *sender_mailbox, void *ptr)
{
msg = hdlc_mailbox.alloc();
if (msg == NULL)
return -1;
msg->type = type;
msg->content.ptr = ptr;
msg->sender_pid = osThreadGetId();
msg->source_mailbox = sender_mailbox;
hdlc_mailbox.put(msg);
return 1;
}
/**
* @brief Sends a hdlc retry message.
*
* @param msg The hdlc message
* @param sender_mailbox The sender mailbox
*
* @return status
*/
int send_hdlc_retry_mail(msg_t *msg, Mail<msg_t, HDLC_MAILBOX_SIZE> *sender_mailbox)
{
msg = sender_mailbox->alloc();
if (msg == NULL)
return -1;
msg->type = HDLC_RESP_RETRY_W_TIMEO;
msg->content.value = (uint32_t) HDLC_RTRY_TIMEO_USEC;
msg->sender_pid = osThreadGetId();
msg->source_mailbox = sender_mailbox;
sender_mailbox->put(msg);
return 1;
}