Blame view

RIOT/sys/net/gnrc/link_layer/lwmac/tx_state_machine.c 31.2 KB
a752c7ab   elopes   add first test an...
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  /*
   * Copyright (C) 2015 Daniel Krebs
   *               2016 INRIA
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     net_gnrc_lwmac
   * @{
   *
   * @file
   * @brief       Implementation of TX state machine of LWMAC protocol
   *
   * @author      Daniel Krebs <github@daniel-krebs.net>
   * @author      Shuguo Zhuo  <shuguo.zhuo@inria.fr>
   * @}
   */
  
  #include "periph/rtt.h"
  #include "net/gnrc.h"
  #include "net/gnrc/lwmac/lwmac.h"
  #include "random.h"
  #include "net/gnrc/mac/internal.h"
  #include "net/gnrc/lwmac/timeout.h"
  #include "include/tx_state_machine.h"
  #include "include/lwmac_internal.h"
  
  #define ENABLE_DEBUG    (0)
  #include "debug.h"
  
  #ifndef LOG_LEVEL
  /**
   * @brief Default log level define
   */
  #define LOG_LEVEL LOG_WARNING
  #endif
  #include "log.h"
  
  /**
   * @brief   Flag to track if send packet success
   */
  #define GNRC_LWMAC_TX_SUCCESS         (0x01U)
  
  /**
   * @brief   Flag to track if send packet fail
   */
  #define GNRC_LWMAC_TX_FAIL            (0x02U)
  
  static uint8_t _send_bcast(gnrc_netdev_t *gnrc_netdev)
  {
      assert(gnrc_netdev != NULL);
  
      uint8_t tx_info = 0;
      gnrc_pktsnip_t *pkt = gnrc_netdev->tx.packet;
      bool first = false;
  
      if (gnrc_lwmac_timeout_is_running(gnrc_netdev, GNRC_LWMAC_TIMEOUT_BROADCAST_END)) {
          if (gnrc_lwmac_timeout_is_expired(gnrc_netdev, GNRC_LWMAC_TIMEOUT_BROADCAST_END)) {
              gnrc_lwmac_clear_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NEXT_BROADCAST);
              gnrc_pktbuf_release(pkt);
              gnrc_netdev->tx.packet = NULL;
              tx_info |= GNRC_LWMAC_TX_SUCCESS;
              return tx_info;
          }
      }
      else {
          LOG_INFO("[LWMAC-tx] Initialize broadcasting\n");
          gnrc_lwmac_set_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_BROADCAST_END,
                            GNRC_LWMAC_BROADCAST_DURATION_US);
  
          gnrc_pktsnip_t *pkt_payload;
  
          /* Prepare packet with LWMAC header*/
          gnrc_lwmac_frame_broadcast_t hdr = {};
          hdr.header.type = GNRC_LWMAC_FRAMETYPE_BROADCAST;
          hdr.seq_nr = gnrc_netdev->tx.bcast_seqnr++;
  
          pkt_payload = pkt->next;
          pkt->next = gnrc_pktbuf_add(pkt->next, &hdr, sizeof(hdr), GNRC_NETTYPE_LWMAC);
          if (pkt->next == NULL) {
              LOG_ERROR("ERROR: [LWMAC-tx] Cannot allocate pktbuf of type FRAMETYPE_BROADCAST\n");
              gnrc_netdev->tx.packet->next = pkt_payload;
              /* Drop the broadcast packet */
              LOG_ERROR("ERROR: [LWMAC-tx] Memory maybe full, drop the broadcast packet\n");
              gnrc_pktbuf_release(gnrc_netdev->tx.packet);
              /* clear packet point to avoid TX retry */
              gnrc_netdev->tx.packet = NULL;
              tx_info |= GNRC_LWMAC_TX_FAIL;
              return tx_info;
          }
  
          /* No Auto-ACK for broadcast packets */
          netopt_enable_t autoack = NETOPT_DISABLE;
          gnrc_netdev->dev->driver->set(gnrc_netdev->dev, NETOPT_AUTOACK, &autoack,
                                        sizeof(autoack));
          first = true;
      }
  
      if (gnrc_lwmac_timeout_is_expired(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NEXT_BROADCAST) ||
          first) {
          /* if found ongoing transmission, quit this cycle for collision avoidance.
           * Broadcast packet will be re-queued and try to send in the next cycle. */
          if (_gnrc_lwmac_get_netdev_state(gnrc_netdev) == NETOPT_STATE_RX) {
              /* save pointer to netif header */
              gnrc_pktsnip_t *netif = pkt->next->next;
  
              /* remove LWMAC header */
              pkt->next->next = NULL;
              gnrc_pktbuf_release(pkt->next);
  
              /* make append netif header after payload again */
              pkt->next = netif;
  
              if (!gnrc_mac_queue_tx_packet(&gnrc_netdev->tx, 0, gnrc_netdev->tx.packet)) {
                  gnrc_pktbuf_release(gnrc_netdev->tx.packet);
                  LOG_WARNING("WARNING: [LWMAC-tx] TX queue full, drop packet\n");
              }
              /* drop pointer so it wont be free'd */
              gnrc_netdev->tx.packet = NULL;
              tx_info |= GNRC_LWMAC_TX_FAIL;
              return tx_info;
          }
  
          /* Don't let the packet be released yet, we want to send it again */
          gnrc_pktbuf_hold(pkt, 1);
  
          int res = gnrc_netdev->send(gnrc_netdev, pkt);
          if (res < 0) {
              LOG_ERROR("ERROR: [LWMAC-tx] Send broadcast pkt failed.");
              tx_info |= GNRC_LWMAC_TX_FAIL;
              return tx_info;
          }
  
          gnrc_lwmac_set_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NEXT_BROADCAST,
                            GNRC_LWMAC_TIME_BETWEEN_BROADCAST_US);
          LOG_INFO("[LWMAC-tx] Broadcast sent\n");
      }
  
      return tx_info;
  }
  
  static uint8_t _send_wr(gnrc_netdev_t *gnrc_netdev)
  {
      assert(gnrc_netdev != NULL);
  
      uint8_t tx_info = 0;
      gnrc_pktsnip_t *pkt;
      gnrc_pktsnip_t *pkt_lwmac;
      gnrc_netif_hdr_t *nethdr;
  
      /* if found ongoing transmission, quit this cycle for collision avoidance.
       * Data packet will be re-queued and try to send in the next cycle. */
      if (_gnrc_lwmac_get_netdev_state(gnrc_netdev) == NETOPT_STATE_RX) {
          if (!gnrc_mac_queue_tx_packet(&gnrc_netdev->tx, 0, gnrc_netdev->tx.packet)) {
              gnrc_pktbuf_release(gnrc_netdev->tx.packet);
              LOG_WARNING("WARNING: [LWMAC-tx] TX queue full, drop packet\n");
          }
          /* drop pointer so it wont be free'd */
          gnrc_netdev->tx.packet = NULL;
          tx_info |= GNRC_LWMAC_TX_FAIL;
          return tx_info;
      }
  
      /* Assemble WR */
      gnrc_lwmac_frame_wr_t wr_hdr = {};
      wr_hdr.header.type = GNRC_LWMAC_FRAMETYPE_WR;
      memcpy(&(wr_hdr.dst_addr.addr), gnrc_netdev->tx.current_neighbor->l2_addr,
             gnrc_netdev->tx.current_neighbor->l2_addr_len);
      wr_hdr.dst_addr.len = gnrc_netdev->tx.current_neighbor->l2_addr_len;
  
      pkt = gnrc_pktbuf_add(NULL, &wr_hdr, sizeof(wr_hdr), GNRC_NETTYPE_LWMAC);
      if (pkt == NULL) {
          LOG_ERROR("ERROR: [LWMAC-tx] Cannot allocate pktbuf of type GNRC_NETTYPE_LWMAC\n");
          gnrc_pktbuf_release(gnrc_netdev->tx.packet);
          LOG_ERROR("ERROR: [LWMAC-tx] Memory maybe full, drop the data packet\n");
          /* clear packet point to avoid TX retry */
          gnrc_netdev->tx.packet = NULL;
          tx_info |= GNRC_LWMAC_TX_FAIL;
          return tx_info;
      }
  
      /* track the location of this lwmac_frame_wr_t header */
      pkt_lwmac = pkt;
  
      pkt = gnrc_pktbuf_add(pkt, NULL, sizeof(gnrc_netif_hdr_t), GNRC_NETTYPE_NETIF);
      if (pkt == NULL) {
          LOG_ERROR("ERROR: [LWMAC-tx] Cannot allocate pktbuf of type GNRC_NETTYPE_NETIF\n");
          gnrc_pktbuf_release(pkt_lwmac);
          LOG_ERROR("ERROR: [LWMAC-tx] Memory maybe full, drop the data packet\n");
          gnrc_pktbuf_release(gnrc_netdev->tx.packet);
          /* clear packet point to avoid TX retry */
          gnrc_netdev->tx.packet = NULL;
          tx_info |= GNRC_LWMAC_TX_FAIL;
          return tx_info;
      }
  
      /* We wouldn't get here if adding the NETIF header had failed, so no
       * sanity checks needed */
      nethdr = (gnrc_netif_hdr_t *) (gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_NETIF))->data;
  
      /* Construct NETIF header and insert address for WR packet */
      gnrc_netif_hdr_init(nethdr, 0, 0);
  
      /* Send WR as broadcast*/
      nethdr->flags |= GNRC_NETIF_HDR_FLAGS_BROADCAST;
  
      /* Disable Auto ACK */
      netopt_enable_t autoack = NETOPT_DISABLE;
      gnrc_netdev->dev->driver->set(gnrc_netdev->dev, NETOPT_AUTOACK, &autoack,
                                    sizeof(autoack));
  
      /* Prepare WR, this will discard any frame in the transceiver that has
       * possibly arrived in the meantime but we don't care at this point. */
      int res = gnrc_netdev->send(gnrc_netdev, pkt);
      if (res < 0) {
          LOG_ERROR("ERROR: [LWMAC-tx] Send WR failed.");
          if (pkt != NULL) {
              gnrc_pktbuf_release(pkt);
          }
          tx_info |= GNRC_LWMAC_TX_FAIL;
          return tx_info;
      }
  
      gnrc_priority_pktqueue_flush(&gnrc_netdev->rx.queue);
      return tx_info;
  }
  
  static uint8_t _packet_process_in_wait_for_wa(gnrc_netdev_t *gnrc_netdev)
  {
      assert(gnrc_netdev != NULL);
  
      uint8_t tx_info = 0;
      gnrc_pktsnip_t *pkt;
      bool found_wa = false;
      bool postponed = false;
      bool from_expected_destination = false;
  
      while ((pkt = gnrc_priority_pktqueue_pop(&gnrc_netdev->rx.queue)) != NULL) {
          LOG_DEBUG("[LWMAC-tx] Inspecting pkt @ %p\n", pkt);
  
          /* Parse packet */
          gnrc_lwmac_packet_info_t info;
          int ret = _gnrc_lwmac_parse_packet(pkt, &info);
  
          if (ret != 0) {
              LOG_DEBUG("[LWMAC-tx] Packet could not be parsed: %i\n", ret);
              gnrc_pktbuf_release(pkt);
              continue;
          }
  
          if (memcmp(&info.src_addr.addr, &gnrc_netdev->tx.current_neighbor->l2_addr,
                     gnrc_netdev->tx.current_neighbor->l2_addr_len) == 0) {
              from_expected_destination = true;
          }
  
          if (info.header->type == GNRC_LWMAC_FRAMETYPE_BROADCAST) {
              _gnrc_lwmac_dispatch_defer(gnrc_netdev->rx.dispatch_buffer, pkt);
              gnrc_mac_dispatch(&gnrc_netdev->rx);
              /* Drop pointer to it can't get released */
              pkt = NULL;
              continue;
          }
  
          /* Check if destination is talking to another node. It will sleep
           * after a finished transaction so there's no point in trying any
           * further now. */
          if (!(memcmp(&info.dst_addr.addr, &gnrc_netdev->l2_addr,
                       gnrc_netdev->l2_addr_len) == 0) && from_expected_destination) {
              if (!gnrc_mac_queue_tx_packet(&gnrc_netdev->tx, 0, gnrc_netdev->tx.packet)) {
                  gnrc_pktbuf_release(gnrc_netdev->tx.packet);
                  LOG_WARNING("WARNING: [LWMAC-tx] TX queue full, drop packet\n");
              }
              /* drop pointer so it wont be free'd */
              gnrc_netdev->tx.packet = NULL;
              postponed = true;
              gnrc_pktbuf_release(pkt);
              break;
          }
  
          /* if found anther node is also trying to send data,
           * quit this cycle for collision avoidance. */
          if (info.header->type == GNRC_LWMAC_FRAMETYPE_WR) {
              if (!gnrc_mac_queue_tx_packet(&gnrc_netdev->tx, 0, gnrc_netdev->tx.packet)) {
                  gnrc_pktbuf_release(gnrc_netdev->tx.packet);
                  LOG_WARNING("WARNING: [LWMAC-tx] TX queue full, drop packet\n");
              }
              /* drop pointer so it wont be free'd */
              gnrc_netdev->tx.packet = NULL;
              postponed = true;
              gnrc_pktbuf_release(pkt);
              break;
          }
  
          if (info.header->type != GNRC_LWMAC_FRAMETYPE_WA) {
              LOG_DEBUG("[LWMAC-tx] Packet is not WA: 0x%02x\n", info.header->type);
              gnrc_pktbuf_release(pkt);
              continue;
          }
  
          if (from_expected_destination) {
              /* calculate the phase of the receiver based on WA */
              gnrc_netdev->tx.timestamp = _gnrc_lwmac_phase_now();
              gnrc_lwmac_frame_wa_t *wa_hdr;
              wa_hdr = (gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_LWMAC))->data;
  
              if (gnrc_netdev->tx.timestamp >= wa_hdr->current_phase) {
                  gnrc_netdev->tx.timestamp = gnrc_netdev->tx.timestamp -
                                              wa_hdr->current_phase;
              }
              else {
                  gnrc_netdev->tx.timestamp += RTT_US_TO_TICKS(GNRC_LWMAC_WAKEUP_INTERVAL_US);
                  gnrc_netdev->tx.timestamp -= wa_hdr->current_phase;
              }
  
              uint32_t own_phase;
              own_phase = _gnrc_lwmac_ticks_to_phase(gnrc_netdev->lwmac.last_wakeup);
  
              if (own_phase >= gnrc_netdev->tx.timestamp) {
                  own_phase = own_phase - gnrc_netdev->tx.timestamp;
              }
              else {
                  own_phase = gnrc_netdev->tx.timestamp - own_phase;
              }
  
              if ((own_phase < RTT_US_TO_TICKS((3 * GNRC_LWMAC_WAKEUP_DURATION_US / 2))) ||
                  (own_phase > RTT_US_TO_TICKS(GNRC_LWMAC_WAKEUP_INTERVAL_US -
                                               (3 * GNRC_LWMAC_WAKEUP_DURATION_US / 2)))) {
                  gnrc_netdev_lwmac_set_phase_backoff(gnrc_netdev, true);
                  LOG_WARNING("WARNING: [LWMAC-tx] phase close\n");
              }
          }
  
          /* No need to keep pkt anymore */
          gnrc_pktbuf_release(pkt);
  
          if (!from_expected_destination) {
              LOG_DEBUG("[LWMAC-tx] Packet is not from expected destination\n");
              break;
          }
  
          /* All checks passed so this must be a valid WA */
          gnrc_lwmac_clear_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_WR);
  
          found_wa = true;
          break;
      }
  
      if (postponed) {
          LOG_INFO("[LWMAC-tx] Destination is talking to another node, postpone\n");
          tx_info |= GNRC_LWMAC_TX_FAIL;
          return tx_info;
      }
  
      if (!found_wa) {
          LOG_DEBUG("[LWMAC-tx] No WA yet\n");
          return tx_info;
      }
  
      /* Save newly calculated phase for destination */
      gnrc_netdev->tx.current_neighbor->phase = gnrc_netdev->tx.timestamp;
      LOG_INFO("[LWMAC-tx] New phase: %" PRIu32 "\n", gnrc_netdev->tx.timestamp);
  
      /* We've got our WA, so discard the rest, TODO: no flushing */
      gnrc_priority_pktqueue_flush(&gnrc_netdev->rx.queue);
  
      tx_info |= GNRC_LWMAC_TX_SUCCESS;
      return tx_info;
  }
  
  /* return false if send data failed, otherwise return true */
  static bool _send_data(gnrc_netdev_t *gnrc_netdev)
  {
      assert(gnrc_netdev != NULL);
  
      gnrc_pktsnip_t *pkt = gnrc_netdev->tx.packet;
      gnrc_pktsnip_t *pkt_payload;
  
      /* Enable Auto ACK again */
      netopt_enable_t autoack = NETOPT_ENABLE;
      gnrc_netdev->dev->driver->set(gnrc_netdev->dev, NETOPT_AUTOACK,
                                    &autoack, sizeof(autoack));
  
      /* It's okay to retry sending DATA. Timing doesn't matter anymore and
       * destination is waiting for a certain amount of time. */
      uint8_t csma_retries = GNRC_LWMAC_DATA_CSMA_RETRIES;
      gnrc_netdev->dev->driver->set(gnrc_netdev->dev, NETOPT_CSMA_RETRIES,
                                    &csma_retries, sizeof(csma_retries));
  
      gnrc_netdev->mac_info |= GNRC_NETDEV_MAC_INFO_CSMA_ENABLED;
      netopt_enable_t csma_enable = NETOPT_ENABLE;
      gnrc_netdev->dev->driver->set(gnrc_netdev->dev, NETOPT_CSMA,
                                    &csma_enable, sizeof(csma_enable));
  
      pkt_payload = pkt->next;
  
      /* Insert LWMAC header above NETIF header. The burst (consecutive) transmission
       * scheme works here (sender side). If the sender finds it has pending packets
       * for the receiver (and under burst limit), it sets the packet type to
       * FRAMETYPE_DATA_PENDING, to notice the receiver for next incoming packet.
       * In case the sender has no more packet for the receiver, it simply sets the
       * data type to FRAMETYPE_DATA. */
      gnrc_lwmac_hdr_t hdr;
      if ((gnrc_priority_pktqueue_length(&gnrc_netdev->tx.current_neighbor->queue) > 0) &&
          (gnrc_netdev->tx.tx_burst_count < GNRC_LWMAC_MAX_TX_BURST_PKT_NUM)) {
          hdr.type = GNRC_LWMAC_FRAMETYPE_DATA_PENDING;
          gnrc_netdev_lwmac_set_tx_continue(gnrc_netdev, true);
          gnrc_netdev->tx.tx_burst_count++;
      }
      else {
          hdr.type = GNRC_LWMAC_FRAMETYPE_DATA;
          gnrc_netdev_lwmac_set_tx_continue(gnrc_netdev, false);
      }
  
      pkt->next = gnrc_pktbuf_add(pkt->next, &hdr, sizeof(hdr), GNRC_NETTYPE_LWMAC);
      if (pkt->next == NULL) {
          LOG_ERROR("ERROR: [LWMAC-tx] Cannot allocate pktbuf of type GNRC_NETTYPE_LWMAC\n");
          LOG_ERROR("ERROR: [LWMAC-tx] Memory maybe full, drop the data packet\n");
          gnrc_netdev->tx.packet->next = pkt_payload;
          gnrc_pktbuf_release(gnrc_netdev->tx.packet);
          /* clear packet point to avoid TX retry */
          gnrc_netdev->tx.packet = NULL;
          return false;
      }
  
      /* if found ongoing transmission, quit this cycle for collision avoidance.
       * Data packet will be re-queued and try to send in the next cycle. */
      if (_gnrc_lwmac_get_netdev_state(gnrc_netdev) == NETOPT_STATE_RX) {
          /* save pointer to netif header */
          gnrc_pktsnip_t *netif = pkt->next->next;
  
          /* remove LWMAC header */
          pkt->next->next = NULL;
          gnrc_pktbuf_release(pkt->next);
  
          /* make append netif header after payload again */
          pkt->next = netif;
  
          if (!gnrc_mac_queue_tx_packet(&gnrc_netdev->tx, 0, gnrc_netdev->tx.packet)) {
              gnrc_pktbuf_release(gnrc_netdev->tx.packet);
              LOG_WARNING("WARNING: [LWMAC-tx] TX queue full, drop packet\n");
          }
          /* drop pointer so it wont be free'd */
          gnrc_netdev->tx.packet = NULL;
          return false;
      }
  
      /* Send data */
      int res = gnrc_netdev->send(gnrc_netdev, pkt);
      if (res < 0) {
          LOG_ERROR("ERROR: [LWMAC-tx] Send data failed.");
          if (pkt != NULL) {
              gnrc_pktbuf_release(pkt);
          }
          /* clear packet point to avoid TX retry */
          gnrc_netdev->tx.packet = NULL;
          return false;
      }
  
      /* Packet has been released by netdev, so drop pointer */
      gnrc_netdev->tx.packet = NULL;
  
      DEBUG("[LWMAC-tx]: spent %lu WR in TX\n", gnrc_netdev->tx.wr_sent);
  
  #if (LWMAC_ENABLE_DUTYCYLE_RECORD == 1)
      gnrc_netdev->lwmac.pkt_start_sending_time_ticks =
          rtt_get_counter() - gnrc_netdev->lwmac.pkt_start_sending_time_ticks;
      DEBUG("[LWMAC-tx]: pkt sending delay in TX: %lu us\n",
            RTT_TICKS_TO_US(gnrc_netdev->lwmac.pkt_start_sending_time_ticks));
  #endif
  
      return true;
  }
  
  void gnrc_lwmac_tx_start(gnrc_netdev_t *gnrc_netdev,
                           gnrc_pktsnip_t *pkt,
                           gnrc_mac_tx_neighbor_t *neighbor)
  {
      assert(gnrc_netdev != NULL);
      assert(pkt != NULL);
      assert(neighbor != NULL);
  
      if (gnrc_netdev->tx.packet) {
          LOG_WARNING("WARNING: [LWMAC-tx] Starting but tx.packet is still set\n");
          gnrc_pktbuf_release(gnrc_netdev->tx.packet);
      }
  
      gnrc_netdev->tx.packet = pkt;
      gnrc_netdev->tx.current_neighbor = neighbor;
      gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_INIT;
      gnrc_netdev->tx.wr_sent = 0;
  
  #if (LWMAC_ENABLE_DUTYCYLE_RECORD == 1)
      gnrc_netdev->lwmac.pkt_start_sending_time_ticks = rtt_get_counter();
  #endif
  }
  
  void gnrc_lwmac_tx_stop(gnrc_netdev_t *gnrc_netdev)
  {
      assert(gnrc_netdev != NULL);
  
      gnrc_lwmac_clear_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_WR);
      gnrc_lwmac_clear_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NO_RESPONSE);
      gnrc_lwmac_clear_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NEXT_BROADCAST);
      gnrc_lwmac_clear_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_BROADCAST_END);
      gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_STOPPED;
  
      /* Release packet in case of failure */
      if (gnrc_netdev->tx.packet) {
          if (gnrc_netdev->tx.tx_retry_count >= GNRC_LWMAC_MAX_DATA_TX_RETRIES) {
              gnrc_netdev->tx.tx_retry_count = 0;
              gnrc_pktbuf_release(gnrc_netdev->tx.packet);
              gnrc_netdev->tx.packet = NULL;
              LOG_WARNING("WARNING: [LWMAC-tx] Drop TX packet\n");
          }
          else {
              gnrc_netdev->tx.tx_retry_count++;
              return;
          }
      }
  
      if (!gnrc_netdev_lwmac_get_tx_continue(gnrc_netdev)) {
          gnrc_netdev->tx.current_neighbor = NULL;
      }
  }
  
  /* Returns whether rescheduling is needed or not */
  static bool _lwmac_tx_update(gnrc_netdev_t *gnrc_netdev)
  {
      assert(gnrc_netdev != NULL);
  
      bool reschedule = false;
  
      switch (gnrc_netdev->tx.state) {
          case GNRC_LWMAC_TX_STATE_INIT: {
              gnrc_lwmac_clear_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_WR);
              gnrc_lwmac_clear_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NO_RESPONSE);
              gnrc_lwmac_clear_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NEXT_BROADCAST);
              gnrc_lwmac_clear_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_BROADCAST_END);
  
              /* if found ongoing transmission,
               * quit this cycle for collision avoidance. */
              if (_gnrc_lwmac_get_netdev_state(gnrc_netdev) == NETOPT_STATE_RX) {
                  if (!gnrc_mac_queue_tx_packet(&gnrc_netdev->tx, 0, gnrc_netdev->tx.packet)) {
                      gnrc_pktbuf_release(gnrc_netdev->tx.packet);
                      LOG_WARNING("WARNING: [LWMAC-tx] TX queue full, drop packet\n");
                  }
                  /* drop pointer so it wont be free'd */
                  gnrc_netdev->tx.packet = NULL;
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
  
              /* check if the packet is for broadcast */
              if (gnrc_netif_hdr_get_flag(gnrc_netdev->tx.packet) &
                  (GNRC_NETIF_HDR_FLAGS_BROADCAST | GNRC_NETIF_HDR_FLAGS_MULTICAST)) {
                  /* Set CSMA retries as configured and enable */
                  uint8_t csma_retries = GNRC_LWMAC_BROADCAST_CSMA_RETRIES;
                  gnrc_netdev->dev->driver->set(gnrc_netdev->dev, NETOPT_CSMA_RETRIES,
                                                &csma_retries, sizeof(csma_retries));
                  gnrc_netdev->mac_info |= GNRC_NETDEV_MAC_INFO_CSMA_ENABLED;
                  netopt_enable_t csma_enable = NETOPT_ENABLE;
                  gnrc_netdev->dev->driver->set(gnrc_netdev->dev, NETOPT_CSMA,
                                                &csma_enable, sizeof(csma_enable));
  
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_SEND_BROADCAST;
                  reschedule = true;
                  break;
              }
              else {
                  /* Use CSMA for the first WR */
                  gnrc_netdev->mac_info |= GNRC_NETDEV_MAC_INFO_CSMA_ENABLED;
                  netopt_enable_t csma_disable = NETOPT_ENABLE;
                  gnrc_netdev->dev->driver->set(gnrc_netdev->dev, NETOPT_CSMA,
                                                &csma_disable, sizeof(csma_disable));
                  /* Set a timeout for the maximum transmission procedure */
                  gnrc_lwmac_set_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NO_RESPONSE, GNRC_LWMAC_PREAMBLE_DURATION_US);
  
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_SEND_WR;
                  reschedule = true;
                  break;
              }
          }
          case GNRC_LWMAC_TX_STATE_SEND_BROADCAST: {
              uint8_t tx_info = _send_bcast(gnrc_netdev);
  
              if (tx_info & GNRC_LWMAC_TX_SUCCESS) {
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_SUCCESSFUL;
                  reschedule = true;
                  break;
              }
  
              if (tx_info & GNRC_LWMAC_TX_FAIL) {
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
  
              break;
          }
          case GNRC_LWMAC_TX_STATE_SEND_WR: {
              /* In case of no Tx-isr error (e.g., no Tx-isr), goto TX failure. */
              if (gnrc_lwmac_timeout_is_expired(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NO_RESPONSE)) {
                  LOG_WARNING("WARNING: [LWMAC-tx] No response from destination, "
                                   "probably no TX-ISR\n");
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
              LOG_DEBUG("[LWMAC-tx] GNRC_LWMAC_TX_STATE_SEND_WR\n");
              uint8_t tx_info = _send_wr(gnrc_netdev);
  
              if (tx_info & GNRC_LWMAC_TX_FAIL) {
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
  
              gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_WAIT_WR_SENT;
              reschedule = false;
              break;
          }
          case GNRC_LWMAC_TX_STATE_WAIT_WR_SENT: {
              LOG_DEBUG("[LWMAC-tx] GNRC_LWMAC_TX_STATE_WAIT_WR_SENT\n");
  
              /* In case of no Tx-isr error (e.g., no Tx-isr), goto TX failure. */
              if (gnrc_lwmac_timeout_is_expired(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NO_RESPONSE)) {
                  LOG_WARNING("WARNING: [LWMAC-tx] No response from destination\n");
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
  
              if (gnrc_netdev_get_tx_feedback(gnrc_netdev) == TX_FEEDBACK_UNDEF) {
                  LOG_DEBUG("[LWMAC-tx] WR not yet completely sent\n");
                  break;
              }
  
              /* If found ongoing transmission, goto TX failure, i.e., postpone transmission to
               * next cycle. This is mainly for collision avoidance. */
              if (gnrc_netdev_get_tx_feedback(gnrc_netdev) == TX_FEEDBACK_BUSY) {
                  if (!gnrc_mac_queue_tx_packet(&gnrc_netdev->tx, 0, gnrc_netdev->tx.packet)) {
                      gnrc_pktbuf_release(gnrc_netdev->tx.packet);
                      LOG_WARNING("WARNING: [LWMAC-tx] TX queue full, drop packet\n");
                  }
                  /* clear packet point to avoid TX retry */
                  gnrc_netdev->tx.packet = NULL;
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
  
              if (gnrc_netdev->tx.wr_sent == 0) {
                  /* Only the first WR use CSMA */
                  gnrc_netdev->mac_info &= ~GNRC_NETDEV_MAC_INFO_CSMA_ENABLED;
                  netopt_enable_t csma_disable = NETOPT_DISABLE;
                  gnrc_netdev->dev->driver->set(gnrc_netdev->dev, NETOPT_CSMA,
                                                &csma_disable, sizeof(csma_disable));
              }
  
              gnrc_netdev->tx.wr_sent++;
  
              /* Set timeout for next WR in case no WA will be received */
              gnrc_lwmac_set_timeout(gnrc_netdev, GNRC_LWMAC_TIMEOUT_WR, GNRC_LWMAC_TIME_BETWEEN_WR_US);
  
              /* Debug WR timing */
              LOG_DEBUG("[LWMAC-tx] Destination phase was: %" PRIu32 "\n",
                             gnrc_netdev->tx.current_neighbor->phase);
              LOG_DEBUG("[LWMAC-tx] Phase when sent was:   %" PRIu32 "\n",
                             _gnrc_lwmac_ticks_to_phase(gnrc_netdev->tx.timestamp));
              LOG_DEBUG("[LWMAC-tx] Ticks when sent was:   %" PRIu32 "\n",
                             gnrc_netdev->tx.timestamp);
              _gnrc_lwmac_set_netdev_state(gnrc_netdev, NETOPT_STATE_IDLE);
              gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_WAIT_FOR_WA;
              reschedule = false;
              break;
          }
          case GNRC_LWMAC_TX_STATE_WAIT_FOR_WA: {
              LOG_DEBUG("[LWMAC-tx] GNRC_LWMAC_TX_STATE_WAIT_FOR_WA\n");
  
              if (gnrc_lwmac_timeout_is_expired(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NO_RESPONSE)) {
                  LOG_WARNING("WARNING: [LWMAC-tx] No response from destination\n");
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
  
              if (gnrc_lwmac_timeout_is_expired(gnrc_netdev, GNRC_LWMAC_TIMEOUT_WR)) {
                  /* In case the sender is in consecutive (burst) transmission to the receiver,
                   * meaning that the sender has already successfully sent at least one data to
                   * the receiver, then the sender will only spend one WR for triggering the next
                   * transmission procedure. And, if this WR doesn't work (no WA replied), the
                   * sender regards consecutive transmission failed.
                   */
                  if (gnrc_netdev_lwmac_get_tx_continue(gnrc_netdev)) {
                      LOG_DEBUG("[LWMAC-tx] Tx burst fail\n");
                      if (!gnrc_mac_queue_tx_packet(&gnrc_netdev->tx, 0, gnrc_netdev->tx.packet)) {
                          gnrc_pktbuf_release(gnrc_netdev->tx.packet);
                          LOG_WARNING("WARNING: [LWMAC-tx] TX queue full, drop packet\n");
                      }
                      /* drop pointer so it wont be free'd */
                      gnrc_netdev->tx.packet = NULL;
  
                      gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                      reschedule = true;
                      break;
                  }
                  else {
                      /* If this is the first transmission to the receiver for locating the
                       * latter's wake-up period, the sender just keep sending WRs until it
                       * finds the WA.
                       */
                      gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_SEND_WR;
                      reschedule = true;
                      break;
                  }
              }
  
              if (_gnrc_lwmac_get_netdev_state(gnrc_netdev) == NETOPT_STATE_RX) {
                  /* Wait for completion of frame reception */
                  break;
              }
  
              uint8_t tx_info = _packet_process_in_wait_for_wa(gnrc_netdev);
  
              if (tx_info & GNRC_LWMAC_TX_FAIL) {
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
  
              if (tx_info & GNRC_LWMAC_TX_SUCCESS) {
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_SEND_DATA;
                  reschedule = true;
                  break;
              }
              else {
                  /* No WA yet */
                  break;
              }
          }
          case GNRC_LWMAC_TX_STATE_SEND_DATA: {
              LOG_DEBUG("[LWMAC-tx] GNRC_LWMAC_TX_STATE_SEND_DATA\n");
  
              if (!_send_data(gnrc_netdev)) {
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
  
              gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_WAIT_FEEDBACK;
              reschedule = false;
              break;
          }
          case GNRC_LWMAC_TX_STATE_WAIT_FEEDBACK: {
              /* In case of no Tx-isr error, goto TX failure. */
              if (gnrc_lwmac_timeout_is_expired(gnrc_netdev, GNRC_LWMAC_TIMEOUT_NO_RESPONSE)) {
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
  
              LOG_DEBUG("[LWMAC-tx] GNRC_LWMAC_TX_STATE_WAIT_FEEDBACK\n");
              if (gnrc_netdev_get_tx_feedback(gnrc_netdev) == TX_FEEDBACK_UNDEF) {
                  break;
              }
              else if (gnrc_netdev_get_tx_feedback(gnrc_netdev) == TX_FEEDBACK_SUCCESS) {
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_SUCCESSFUL;
                  reschedule = true;
                  break;
              }
              else if (gnrc_netdev_get_tx_feedback(gnrc_netdev) == TX_FEEDBACK_NOACK) {
                  LOG_ERROR("ERROR: [LWMAC-tx] Not ACKED\n");
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
              else if (gnrc_netdev_get_tx_feedback(gnrc_netdev) == TX_FEEDBACK_BUSY) {
                  LOG_ERROR("ERROR: [LWMAC-tx] Channel busy \n");
                  gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
                  reschedule = true;
                  break;
              }
  
              LOG_ERROR("ERROR: [LWMAC-tx] Tx feedback unhandled: %i\n",
                             gnrc_netdev_get_tx_feedback(gnrc_netdev));
              gnrc_netdev->tx.state = GNRC_LWMAC_TX_STATE_FAILED;
              reschedule = true;
              break;
          }
          case GNRC_LWMAC_TX_STATE_SUCCESSFUL:
          case GNRC_LWMAC_TX_STATE_FAILED: {
              break;
          }
          case GNRC_LWMAC_TX_STATE_STOPPED: {
              LOG_DEBUG("[LWMAC-tx] Transmission state machine is stopped\n");
          }
      }
  
      return reschedule;
  }
  
  void gnrc_lwmac_tx_update(gnrc_netdev_t *gnrc_netdev)
  {
      /* Update until no rescheduling needed */
      while (_lwmac_tx_update(gnrc_netdev)) {}
  }