ser_phy_hci.c 51.6 KB
Newer Older
X
xieyangrun 已提交
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 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
/**
 * Copyright (c) 2014 - 2017, Nordic Semiconductor ASA
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 * 
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 * 
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 * 
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
#include <stddef.h>
#include <string.h>

#include "app_error.h"
#include "app_util.h"
#include "app_util_platform.h"
#include "app_timer.h"
#include "nrf_queue.h"
#include "ser_phy.h"
#include "ser_phy_hci.h"
#include "crc16.h"
#include "nrf_soc.h"
#include "ser_config.h"
#include "ser_phy_debug_comm.h"
#define NRF_LOG_MODULE_NAME "SPHY_HCI"
#include "nrf_log.h"
// hide globals for release version, expose for debug version
#if defined(SER_PHY_HCI_DEBUG_ENABLE)
#define _static
#else
#define _static static
#endif

#define PKT_HDR_SIZE            4 /**< Packet header size in number of bytes. */
#define PKT_CRC_SIZE            2 /**< Packet CRC size in number of bytes. */
#define MAX_PACKET_SIZE_IN_BITS (11uL * \
                                 (SER_HAL_TRANSPORT_MAX_PKT_SIZE + PKT_HDR_SIZE + PKT_CRC_SIZE))
#define BAUD_TIME_us            (1000000uL / SER_PHY_UART_BAUDRATE_VAL)

#define TX_EVT_QUEUE_SIZE            16
#define RX_EVT_QUEUE_SIZE            16
#define PKT_TYPE_VENDOR_SPECIFIC     14                                                /**< Packet type vendor specific. */
#define PKT_TYPE_ACK                 0                                                 /**< Packet type acknowledgement. */
#define PKT_TYPE_LINK_CONTROL        15                                                /**< Packet type link control. */
#define DATA_INTEGRITY_MASK          (1 << 6)                                          /**< Mask for data integrity bit in the packet header. */
#define RELIABLE_PKT_MASK            (1 << 7)                                          /**< Mask for reliable packet bit in the packet header. */
#define INITIAL_ACK_NUMBER_EXPECTED  0                                                 /**< Initial acknowledge number expected. */
#define INITIAL_SEQ_NUMBER           INITIAL_ACK_NUMBER_EXPECTED                       /**< Initial acknowledge number transmitted. */
#define INVALID_PKT_TYPE             0xFFFFFFFFu                                       /**< Internal invalid packet type value. */
#define MAX_TRANSMISSION_TIME_ms     (MAX_PACKET_SIZE_IN_BITS * BAUD_TIME_us / 1000uL) /**< Max transmission time of a single application packet over UART in units of mseconds. */
#define RETRANSMISSION_TIMEOUT_IN_ms (10uL * MAX_TRANSMISSION_TIME_ms)                 /**< Retransmission timeout for application packet in units of mseconds. */

#ifdef  HCI_LINK_CONTROL
#define HCI_PKT_SYNC        0x7E01u                                                    /**< Link Control Packet: type SYNC */
#define HCI_PKT_SYNC_RSP    0x7D02u                                                    /**< Link Control Packet: type SYNC RESPONSE */
#define HCI_PKT_CONFIG      0xFC03u                                                    /**< Link Control Packet: type CONFIG */
#define HCI_PKT_CONFIG_RSP  0x7B04u                                                    /**< Link Control Packet: type CONFIG RESPONSE */
#define HCI_CONFIG_FIELD    0x11u                                                      /**< Configuration field of CONFIG and CONFIG_RSP packet */
#define HCI_PKT_SYNC_SIZE   6u                                                         /**< Size of SYNC and SYNC_RSP packet */
#define HCI_PKT_CONFIG_SIZE 7u                                                         /**< Size of CONFIG and CONFIG_RSP packet */
#define HCI_LINK_CONTROL_PKT_INVALID 0xFFFFu                                           /**< Size of CONFIG and CONFIG_RSP packet */
#define HCI_LINK_CONTROL_TIMEOUT     1u                                                /**< Default link control timeout. */
#endif  /* HCI_LINK_CONTROL */


#define RETRANSMISSION_TIMEOUT_IN_TICKS (APP_TIMER_TICKS(RETRANSMISSION_TIMEOUT_IN_ms)) /**< Retransmission timeout for application packet in units of timer ticks. */
#define MAX_RETRY_COUNT                 5                                      /**< Max retransmission retry count for application packets. */

#if   (defined(HCI_TIMER0))
#define HCI_TIMER            NRF_TIMER0
#define HCI_TIMER_IRQn       TIMER0_IRQn
#define HCI_TIMER_IRQHandler TIMER0_IRQHandler
#elif (defined(HCI_TIMER1))
#define HCI_TIMER            NRF_TIMER1
#define HCI_TIMER_IRQn       TIMER1_IRQn
#define HCI_TIMER_IRQHandler TIMER1_IRQHandler
#elif (defined(HCI_TIMER2))
#define HCI_TIMER            NRF_TIMER2
#define HCI_TIMER_IRQn       TIMER2_IRQn
#define HCI_TIMER_IRQHandler TIMER2_IRQHandler
#else
#define HCI_APP_TIMER
#endif


/**@brief States of the hci event driven state machine. */
typedef enum
{
    HCI_TX_STATE_DISABLE,
    HCI_TX_STATE_SEND,
    HCI_TX_STATE_WAIT_FOR_FIRST_TX_END,
    HCI_TX_STATE_WAIT_FOR_ACK_OR_TX_END,
    HCI_TX_STATE_WAIT_FOR_ACK,
    HCI_TX_STATE_WAIT_FOR_TX_END
} hci_tx_fsm_state_t;

typedef enum
{
    HCI_RX_STATE_DISABLE,
    HCI_RX_STATE_RECEIVE,
    HCI_RX_STATE_WAIT_FOR_MEM,
    HCI_RX_STATE_WAIT_FOR_SLIP_ACK_END,
    HCI_RX_STATE_WAIT_FOR_SLIP_NACK_END,
} hci_rx_fsm_state_t;

typedef enum
{
    HCI_EVT_TIMEOUT,
} hci_timer_evt_type_t;

typedef enum
{
    HCI_SER_PHY_TX_REQUEST,
    HCI_SER_PHY_RX_BUF_GRANTED,
    HCI_SER_PHY_EVT_GEN_ENABLE,
    HCI_SER_PHY_EVT_GEN_DISABLE
} ser_phy_int_evt_type_t;

typedef enum
{
    HCI_SER_PHY_EVT,
    HCI_SLIP_EVT,
    HCI_TIMER_EVT,
} hci_evt_source_t;

#ifdef HCI_LINK_CONTROL
typedef enum
{
    HCI_MODE_DISABLE,
    HCI_MODE_UNINITIALIZED,
    HCI_MODE_INITIALIZED,
    HCI_MODE_ACTIVE,
} hci_mode_t;
#endif /*HCI_LINK_CONTROL */

typedef struct
{
    hci_timer_evt_type_t evt_type; /**< Type of an event. */
} hci_timer_evt_t;

typedef struct
{
    ser_phy_int_evt_type_t evt_type; /**< Type of an event. */
} ser_phy_int_evt_t;

typedef struct
{
    hci_evt_source_t evt_source; /**< source of an event. */
    union
    {
        ser_phy_int_evt_t      ser_phy_evt;      /**< ser_phy event. */
        ser_phy_hci_slip_evt_t ser_phy_slip_evt; /**< ser_phy_hci event. */
        hci_timer_evt_t        timer_evt;        /**< timer event. */
    } evt;
} hci_evt_t;

_static uint8_t m_tx_packet_header[PKT_HDR_SIZE];
_static uint8_t m_tx_packet_crc[PKT_CRC_SIZE];
_static uint8_t m_tx_ack_packet[PKT_HDR_SIZE];
#ifdef HCI_LINK_CONTROL
_static uint8_t m_tx_link_control_header[PKT_HDR_SIZE];
_static uint8_t m_tx_link_control_payload[HCI_PKT_CONFIG_SIZE - PKT_HDR_SIZE];
#endif /* HCI_LINK_CONTROL */

_static uint32_t m_packet_ack_number; // Sequence number counter of the packet expected to be received
_static uint32_t m_packet_seq_number; // Sequence number counter of the transmitted packet for which acknowledgement packet is waited for


_static uint32_t m_tx_retry_count;


// _static uint32_t m_tx_retx_counter = 0;
// _static uint32_t m_rx_drop_counter = 0;

NRF_QUEUE_DEF(hci_evt_t,
              m_tx_evt_queue,
              TX_EVT_QUEUE_SIZE,
              NRF_QUEUE_MODE_NO_OVERFLOW);

NRF_QUEUE_DEF(hci_evt_t,
              m_rx_evt_queue,
              RX_EVT_QUEUE_SIZE,
              NRF_QUEUE_MODE_NO_OVERFLOW);

_static hci_tx_fsm_state_t m_hci_tx_fsm_state = HCI_TX_STATE_DISABLE;
_static hci_rx_fsm_state_t m_hci_rx_fsm_state = HCI_RX_STATE_DISABLE;

#ifdef HCI_LINK_CONTROL
_static hci_mode_t m_hci_mode                  = HCI_MODE_DISABLE;
_static uint16_t   m_hci_link_control_next_pkt = HCI_PKT_SYNC;
_static bool       m_hci_other_side_active     = false;
#endif /* HCI_LINK_CONTROL */

#ifdef HCI_APP_TIMER
APP_TIMER_DEF(m_app_timer_id);
#endif

_static bool m_tx_fsm_idle_flag = true;
_static bool m_rx_fsm_idle_flag = true;

_static bool m_buffer_reqested_flag = false;

_static uint8_t * m_p_rx_buffer = NULL;
_static uint16_t  m_rx_packet_length;
_static uint8_t * m_p_rx_packet;
_static uint8_t * m_p_tx_payload = NULL;
_static uint16_t  m_tx_payload_length;

_static ser_phy_events_handler_t m_ser_phy_callback = NULL;

static void hci_tx_event_handler(hci_evt_t * p_event);
static void hci_rx_event_handler(hci_evt_t * p_event);
#ifdef HCI_LINK_CONTROL
static void hci_link_control_event_handler(hci_evt_t * p_event);
#endif /* HCI_LINK_CONTROL */

_static bool m_hci_timer_enabled_flag  = true;
_static bool m_hci_timout_pending_flag = false;
_static bool m_hci_global_enable_flag  = true;

#define ser_phy_hci_assert(cond) APP_ERROR_CHECK_BOOL(cond)

static void hci_signal_timeout_event(void)
{
    hci_evt_t event;

    event.evt_source             = HCI_TIMER_EVT;
    event.evt.timer_evt.evt_type = HCI_EVT_TIMEOUT;
    DEBUG_EVT_TIMEOUT(0);

#ifndef HCI_LINK_CONTROL
    hci_tx_event_handler(&event);
#else
    hci_link_control_event_handler(&event);
    if ((m_hci_mode == HCI_MODE_ACTIVE) && m_hci_other_side_active)
    {
        hci_tx_event_handler(&event);
    }
#endif /* HCI_LINK_CONTROL */
}


#ifndef HCI_APP_TIMER

void HCI_TIMER_IRQHandler(void)
{

    if ((HCI_TIMER->EVENTS_COMPARE[1] == 1) && (HCI_TIMER->INTENSET & TIMER_INTENSET_COMPARE1_Msk))
    {
        HCI_TIMER->EVENTS_COMPARE[1] = 0;
        HCI_TIMER->TASKS_CLEAR       = 1;

        if (m_hci_timer_enabled_flag)
        {
            hci_signal_timeout_event();
        }
        else
        {
            m_hci_timout_pending_flag = true;
        }
    }
}


static void hci_timeout_setup(uint32_t count)
{

    uint32_t time_msec;

    if (count)
    {
        HCI_TIMER->INTENCLR          = TIMER_INTENCLR_COMPARE1_Msk;
        time_msec                    = count * RETRANSMISSION_TIMEOUT_IN_ms;
        HCI_TIMER->CC[1]             = time_msec * 31;
        HCI_TIMER->CC[1]            += time_msec / 4;
        HCI_TIMER->TASKS_CLEAR       = 1; // < Clear TIMER
        HCI_TIMER->EVENTS_COMPARE[1] = 0;
        HCI_TIMER->TASKS_START       = 1; // < Start TIMER
        HCI_TIMER->INTENSET          = TIMER_INTENSET_COMPARE1_Msk;
    }
    else
    {
        HCI_TIMER->INTENCLR   = TIMER_INTENCLR_COMPARE1_Msk;
        HCI_TIMER->TASKS_STOP = 1; // < Start TIMER
    }
}


#else

_static bool     m_hci_timer_setup_flag = false;
_static uint32_t m_hci_timer_counter    = 0;
_static uint32_t m_hci_timer_setup;

static void hci_timeout_setup(uint32_t count)
{
    m_hci_timer_setup      = count;
    m_hci_timer_setup_flag = true;
}


static void hci_timeout_handler(void * p_context)
{

    if (m_hci_timer_setup_flag)
    {
        m_hci_timer_setup_flag = false;
        m_hci_timer_counter    = m_hci_timer_setup; /* for 1 it will be always more than 1 tick - jitter is up to 1 tick */
    }
    else if ( m_hci_timer_counter )
    {
        m_hci_timer_counter--;

        if (m_hci_timer_counter == 0)
        {
            if (m_hci_timer_enabled_flag)
            {
                hci_signal_timeout_event();
            }
            else
            {
                m_hci_timout_pending_flag = true;
            }
        }
    }
    return;
}


#endif


/**@brief Function for validating a received packet.
 *
 * @param[in] p_buffer Pointer to the packet data.
 * @param[in] length   Length of packet data in bytes.
 *
 * @return true if received packet is valid, false in other case.
 */
static bool is_rx_pkt_valid(const uint8_t * p_buffer, uint32_t length)
{
    // Executed packet filtering algorithm order:
    // - verify packet overall length
    // - verify data integrity bit set
    // - verify reliable packet bit set
    // - verify supported packet type
    // - verify header checksum
    // - verify payload length field
    // - verify CRC
    if (length <= PKT_HDR_SIZE)
    {
        return false;
    }

    if (!(p_buffer[0] & DATA_INTEGRITY_MASK))
    {
        return false;
    }

    if (!(p_buffer[0] & RELIABLE_PKT_MASK))
    {
        return false;
    }

    if ((p_buffer[1] & 0x0Fu) != PKT_TYPE_VENDOR_SPECIFIC)
    {
        return false;
    }

    const uint32_t expected_checksum =
        ((p_buffer[0] + p_buffer[1] + p_buffer[2] + p_buffer[3])) & 0xFFu;

    if (expected_checksum != 0)
    {
        return false;
    }

    const uint16_t crc_calculated = crc16_compute(p_buffer, (length - PKT_CRC_SIZE), NULL);
    const uint16_t crc_received   = uint16_decode(&p_buffer[length - PKT_CRC_SIZE]);

    if (crc_calculated != crc_received)
    {
        return false;
    }

    return true;
}


/**@brief Function for getting the sequence number of the next reliable packet expected.
 *
 * @return sequence number of the next reliable packet expected.
 */
static __INLINE uint8_t packet_ack_get(void)
{
    return (uint8_t) m_packet_ack_number;
}


/**@brief Function for getting the sequence number of a reliable TX packet for which peer protocol
 * entity acknowledgment is pending.
 *
 * @return sequence number of a reliable TX packet for which peer protocol entity acknowledgement
 * is pending.
 */
static __INLINE uint8_t packet_seq_get(void)
{
    return m_packet_seq_number;
}


static __INLINE uint8_t packet_seq_nmbr_extract(const uint8_t * p_buffer)
{
    return (p_buffer[0] & 0x07u);
}


/**@brief Function for constructing 1st byte of the packet header of the packet to be transmitted.
 *
 * @return 1st byte of the packet header of the packet to be transmitted
 */
static __INLINE uint8_t tx_packet_byte_zero_construct(void)
{
    const uint32_t value = DATA_INTEGRITY_MASK | RELIABLE_PKT_MASK |
                           (packet_ack_get() << 3u) | packet_seq_get();

    return (uint8_t) value;
}


/**@brief Function for calculating a packet header checksum.
 *
 * @param[in] p_hdr Pointer to the packet header.
 *
 * @return          Calculated checksum.
 */
static __INLINE uint8_t header_checksum_calculate(const uint8_t * p_hdr)
{
    // @note: no pointer validation check needed as already checked by calling function.
    uint32_t checksum;

    checksum  = p_hdr[0];
    checksum += p_hdr[1];
    checksum += p_hdr[2];
    checksum &= 0xFFu;
    checksum  = (~checksum + 1u);

    return (uint8_t)checksum;
}


/**@brief Function for getting the expected ACK number.
 *
 * @return expected ACK number.
 */
static __INLINE uint8_t expected_ack_number_get(void)
{
    uint8_t seq_nmbr = packet_seq_get();

    ++seq_nmbr;
    seq_nmbr &= 0x07u;

    return seq_nmbr;
}


/**@brief Function for getting the expected ACK number.
 *
 * @return next expected ACK number.
 */

static __INLINE uint8_t next_expected_ack_number_get(void)
{
    uint8_t seq_nmbr = expected_ack_number_get();

    ++seq_nmbr;
    seq_nmbr &= 0x07u;

    return seq_nmbr;
}


/**@brief Function for processing a received acknowledgement packet.
 *
 * Verifies does the received acknowledgement packet has the expected acknowledgement number and
 * that the header checksum is correct.
 *
 * @param[in] p_buffer Pointer to the packet data.
 *
 * @return true if valid acknowledgement packet received.
 */

static bool rx_ack_pkt_valid(const uint8_t * p_buffer)
{
    // @note: no pointer validation check needed as allready checked by calling function.

    // Verify header checksum.
    const uint32_t expected_checksum =
        ((p_buffer[0] + p_buffer[1] + p_buffer[2] + p_buffer[3])) & 0xFFu;

    if (expected_checksum != 0)
    {
        return false;
    }

    const uint8_t ack_number = (p_buffer[0] >> 3u) & 0x07u;

    // Verify expected acknowledgment number.
    return ( (ack_number == expected_ack_number_get()) ||
             (ack_number == next_expected_ack_number_get()) );
}


/**@brief Function for decoding a packet type field.
 *
 * @param[in] p_buffer Pointer to the packet data.
 * @param[in] length   Length of packet data in bytes.
 *
 * @return Packet type field or INVALID_PKT_TYPE in case of decode error.
 */

static uint32_t packet_type_decode(const uint8_t * p_buffer, uint32_t length)
{
    // @note: no pointer validation check needed as allready checked by calling function.
    uint32_t return_value;

    if (length >= PKT_HDR_SIZE)
    {
        return_value = (p_buffer[1] & 0x0Fu);
    }
    else
    {
        return_value = INVALID_PKT_TYPE;
    }

    return return_value;
}

#ifdef HCI_LINK_CONTROL
/**@brief Function for decoding a link control packet.
 *
 * @param[in] p_buffer    Pointer to the packet data.
 * @param[in] length      Length of packet data in bytes.
 *
 * @return Link Control Packet Type if decoding successful, HCI_LINK_CONTROL_PKT_INVALID otherwise.
 */
static uint16_t link_control_packet_decode(const uint8_t * p_buffer, uint32_t length)
{
    // @note: no pointer validation check needed as allready checked by calling function.
    uint16_t packet_type = HCI_LINK_CONTROL_PKT_INVALID;

 // Executed link control packet filtering algorithm order:
    // - verify packet overall length
    // - verify data integrity bit cleared
    // - verify reliable packet bit cleared
    // - verify header checksum
    // - verify payload: length and value

    if (length < HCI_PKT_SYNC_SIZE)
    {
        packet_type = HCI_LINK_CONTROL_PKT_INVALID;
    }

    packet_type = p_buffer[PKT_HDR_SIZE] | (p_buffer[PKT_HDR_SIZE + 1] << 8u);

    if ((p_buffer[0] & DATA_INTEGRITY_MASK) || (p_buffer[0] & RELIABLE_PKT_MASK))
    {
        packet_type = HCI_LINK_CONTROL_PKT_INVALID;
    }

    const uint32_t expected_checksum =
        ((p_buffer[0] + p_buffer[1] + p_buffer[2] + p_buffer[3])) & 0xFFu;

    if (expected_checksum != 0)
    {
        packet_type = HCI_LINK_CONTROL_PKT_INVALID;
    }

    // This is a CONFIG or CONFIG_RSP packet
    if ((packet_type == HCI_PKT_CONFIG) || (packet_type == HCI_PKT_CONFIG_RSP))
    {
        if (length != HCI_PKT_CONFIG_SIZE)
        {
            packet_type = HCI_LINK_CONTROL_PKT_INVALID;
        }
        // Verify configuration field (0x11):
        // - Sliding Window Size       == 1,
        // - OOF Flow Control          == 0,
        // - Data Integrity Check Type == 1,
        // - Version Number            == 0
        if (p_buffer[HCI_PKT_CONFIG_SIZE - 1] != HCI_CONFIG_FIELD)
        {
            packet_type = HCI_LINK_CONTROL_PKT_INVALID;
        }
    }
    // This is a SYNC or SYNC_RSP packet
    else if ((packet_type == HCI_PKT_SYNC) || (packet_type == HCI_PKT_SYNC_RSP))
    {
        if (length != HCI_PKT_SYNC_SIZE)
        {
            packet_type = HCI_LINK_CONTROL_PKT_INVALID;
        }
    }
    else
    {
        packet_type = HCI_LINK_CONTROL_PKT_INVALID;
    }

    return packet_type;
}
#endif /* HCI_LINK_CONTROL */

/**@brief Function for writing an acknowledgment packet for transmission.
 */

static void ack_transmit(void)
{
    uint32_t err_code;
    // TX ACK packet format:
    // - Unreliable Packet type
    // - Payload Length set to 0
    // - Sequence Number set to 0
    // - Header checksum calculated
    // - Acknowledge Number set correctly
    m_tx_ack_packet[0] = (packet_ack_get() << 3u);
    m_tx_ack_packet[1] = 0;
    m_tx_ack_packet[2] = 0;
    m_tx_ack_packet[3] = header_checksum_calculate(m_tx_ack_packet);

    ser_phy_hci_pkt_params_t pkt_header;

    pkt_header.p_buffer     = m_tx_ack_packet;
    pkt_header.num_of_bytes = PKT_HDR_SIZE;
    DEBUG_EVT_SLIP_ACK_TX(0);
    err_code = ser_phy_hci_slip_tx_pkt_send(&pkt_header, NULL, NULL);
    ser_phy_hci_assert(err_code == NRF_SUCCESS);

    return;
}


static void ser_phy_event_callback(ser_phy_evt_t event)
{
    if (m_ser_phy_callback)
    {
        m_ser_phy_callback(event);
    }

    return;
}


static void memory_request_callback(uint16_t size)
{
    ser_phy_evt_t event;

    DEBUG_EVT_HCI_PHY_EVT_BUF_REQUEST(0);

    event.evt_type                               = SER_PHY_EVT_RX_BUF_REQUEST;
    event.evt_params.rx_buf_request.num_of_bytes = size;
    ser_phy_event_callback(event);
}


static void packet_received_callback(uint8_t * pBuffer, uint16_t size)
{
    ser_phy_evt_t event;

    DEBUG_EVT_HCI_PHY_EVT_RX_PKT_RECEIVED(0);

    event.evt_type = SER_PHY_EVT_RX_PKT_RECEIVED;
    event.evt_params.rx_pkt_received.num_of_bytes = size;
    event.evt_params.rx_pkt_received.p_buffer     = pBuffer;
    ser_phy_event_callback(event);
}


static void packet_dropped_callback(void)
{
    ser_phy_evt_t event;

    DEBUG_EVT_HCI_PHY_EVT_RX_PKT_DROPPED(0);

    event.evt_type = SER_PHY_EVT_RX_PKT_DROPPED;
    ser_phy_event_callback(event);
}


static void packet_transmitted_callback(void)
{
    ser_phy_evt_t event;

    DEBUG_EVT_HCI_PHY_EVT_TX_PKT_SENT(0);

    event.evt_type = SER_PHY_EVT_TX_PKT_SENT;
    ser_phy_event_callback(event);
}


static void error_callback(void)
{
    ser_phy_evt_t event;

    DEBUG_EVT_HCI_PHY_EVT_TX_ERROR(0);

    event.evt_type = SER_PHY_EVT_HW_ERROR;
    event.evt_params.hw_error.p_buffer = m_p_tx_payload;
    ser_phy_event_callback(event);
}


static void hci_slip_event_handler(ser_phy_hci_slip_evt_t * p_event)
{
    hci_evt_t event;
    uint32_t  packet_type;
    uint32_t  err_code;

    if ( p_event->evt_type == SER_PHY_HCI_SLIP_EVT_PKT_SENT )
    {
        NRF_LOG_DEBUG("EVT_PKT_SENT\r\n");

        DEBUG_EVT_SLIP_PACKET_TXED(0);
        event.evt_source                    = HCI_SLIP_EVT;
        event.evt.ser_phy_slip_evt.evt_type = p_event->evt_type;
#ifndef HCI_LINK_CONTROL
        hci_tx_event_handler(&event);
#else
        if ((m_hci_mode == HCI_MODE_ACTIVE) && m_hci_other_side_active)
        {
            hci_tx_event_handler(&event);
        }
#endif /*HCI_LINK_CONTROL*/
    }
    else if ( p_event->evt_type == SER_PHY_HCI_SLIP_EVT_ACK_SENT )
    {
        NRF_LOG_DEBUG("EVT_ACK_SENT\r\n");

        DEBUG_EVT_SLIP_ACK_TXED(0);
        event.evt_source                    = HCI_SLIP_EVT;
        event.evt.ser_phy_slip_evt.evt_type = p_event->evt_type;
#ifndef HCI_LINK_CONTROL
        hci_rx_event_handler(&event);
#else
        if ((m_hci_mode == HCI_MODE_ACTIVE) && m_hci_other_side_active)
        {
            hci_rx_event_handler(&event);
        }
#endif /* HCI_LINK_CONTROL */
    }

    else if ( p_event->evt_type == SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED )
    {
        event.evt_source = HCI_SLIP_EVT;
        event.evt.ser_phy_slip_evt.evt_type                         = p_event->evt_type;
        event.evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer =
            p_event->evt_params.received_pkt.p_buffer;
        event.evt.ser_phy_slip_evt.evt_params.received_pkt.num_of_bytes =
            p_event->evt_params.received_pkt.num_of_bytes;
        ser_phy_hci_assert(event.evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer != NULL);
        ser_phy_hci_assert(event.evt.ser_phy_slip_evt.evt_params.received_pkt.num_of_bytes != 0);
        packet_type = packet_type_decode(
            event.evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer,
            event.evt.ser_phy_slip_evt.evt_params.received_pkt.num_of_bytes);

        NRF_LOG_DEBUG("EVT_PKT_RECEIVED 0x%X/%u\r\n", packet_type,
            p_event->evt_params.received_pkt.num_of_bytes);

        if (packet_type == PKT_TYPE_ACK )
        {
            DEBUG_EVT_SLIP_ACK_RXED(0);
#ifndef HCI_LINK_CONTROL
            hci_tx_event_handler(&event);
#else
            if ((m_hci_mode == HCI_MODE_ACTIVE) && m_hci_other_side_active)
            {
                hci_tx_event_handler(&event);
            }
            else
            {
                err_code = ser_phy_hci_slip_rx_buf_free(
                      event.evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer);
            }
#endif /* HCI_LINK_CONTROL */
        }
        else if ( packet_type == PKT_TYPE_VENDOR_SPECIFIC )
        {
            if (is_rx_pkt_valid(event.evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer,
                                event.evt.ser_phy_slip_evt.evt_params.received_pkt.num_of_bytes))
            {
                DEBUG_EVT_SLIP_PACKET_RXED(0);
#ifndef HCI_LINK_CONTROL
                hci_rx_event_handler(&event);
#else
                if ((m_hci_mode == HCI_MODE_ACTIVE) && m_hci_other_side_active)
                {
                    hci_rx_event_handler(&event);
                }
                else
                {
                    err_code = ser_phy_hci_slip_rx_buf_free(
                                    event.evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer);
                }
#endif /* HCI_LINK_CONTROL */
            }
            else
            {
                err_code = ser_phy_hci_slip_rx_buf_free(
                    event.evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer);
                ser_phy_hci_assert(err_code == NRF_SUCCESS);
                /* throw assert when in debug mode*/
                DEBUG_EVT_SLIP_ERR_RXED(0);
            }
        }
#ifdef HCI_LINK_CONTROL
        else if (packet_type == PKT_TYPE_LINK_CONTROL)
        {
            hci_link_control_event_handler(&event);
        }
#endif /* HCI_LINK_CONTROL */
        else
        {
            err_code = ser_phy_hci_slip_rx_buf_free(
                event.evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer);
            ser_phy_hci_assert(err_code == NRF_SUCCESS);
            /* throw assert when in debug mode*/
            DEBUG_EVT_SLIP_ERR_RXED(0);
        }
    }
    else
    {
        NRF_LOG_DEBUG("EVT_HW_ERROR\r\n");
    }
}


static void hci_pkt_send(void)
{
    uint32_t err_code;

    m_tx_packet_header[0] = tx_packet_byte_zero_construct();
    uint16_t type_and_length_fields = ((m_tx_payload_length << 4u) | PKT_TYPE_VENDOR_SPECIFIC);
    (void)uint16_encode(type_and_length_fields, &(m_tx_packet_header[1]));
    m_tx_packet_header[3] = header_checksum_calculate(m_tx_packet_header);
    uint16_t crc = crc16_compute(m_tx_packet_header, PKT_HDR_SIZE, NULL);
    crc = crc16_compute(m_p_tx_payload, m_tx_payload_length, &crc);
    (void)uint16_encode(crc, m_tx_packet_crc);

    ser_phy_hci_pkt_params_t pkt_header;
    ser_phy_hci_pkt_params_t pkt_payload;
    ser_phy_hci_pkt_params_t pkt_crc;

    pkt_header.p_buffer      = m_tx_packet_header;
    pkt_header.num_of_bytes  = PKT_HDR_SIZE;
    pkt_payload.p_buffer     = m_p_tx_payload;
    pkt_payload.num_of_bytes = m_tx_payload_length;
    pkt_crc.p_buffer         = m_tx_packet_crc;
    pkt_crc.num_of_bytes     = PKT_CRC_SIZE;
    DEBUG_EVT_SLIP_PACKET_TX(0);
    err_code = ser_phy_hci_slip_tx_pkt_send(&pkt_header, &pkt_payload, &pkt_crc);
    ser_phy_hci_assert(err_code == NRF_SUCCESS);

    return;
}

#ifdef HCI_LINK_CONTROL
static void hci_link_control_pkt_send(void)
{
    uint32_t err_code;
    uint16_t link_control_payload_len = 0;

    m_tx_link_control_header[0] = 0x00u;       // SEQ, ACK, DI and RP are set to 0 for link control
    if (m_hci_link_control_next_pkt == HCI_PKT_SYNC)
    {
        link_control_payload_len = HCI_PKT_SYNC_SIZE - PKT_HDR_SIZE;
        (void)uint16_encode(HCI_PKT_SYNC, m_tx_link_control_payload);
    }
    else if (m_hci_link_control_next_pkt == HCI_PKT_SYNC_RSP)
    {
        link_control_payload_len = HCI_PKT_SYNC_SIZE - PKT_HDR_SIZE;
        (void)uint16_encode(HCI_PKT_SYNC_RSP, m_tx_link_control_payload);
    }
    else if (m_hci_link_control_next_pkt == HCI_PKT_CONFIG)
    {
        link_control_payload_len = HCI_PKT_CONFIG_SIZE - PKT_HDR_SIZE;
        (void)uint16_encode(HCI_PKT_CONFIG, m_tx_link_control_payload);
        m_tx_link_control_payload[2] = HCI_CONFIG_FIELD;
    }
    else if (m_hci_link_control_next_pkt == HCI_PKT_CONFIG_RSP)
    {
        link_control_payload_len = HCI_PKT_CONFIG_SIZE - PKT_HDR_SIZE;
        (void)uint16_encode(HCI_PKT_CONFIG_RSP, m_tx_link_control_payload);
        m_tx_link_control_payload[2] = HCI_CONFIG_FIELD;
    }
    uint16_t type_and_length_fields = ((link_control_payload_len << 4u) | PKT_TYPE_LINK_CONTROL);
    (void)uint16_encode(type_and_length_fields, &(m_tx_link_control_header[1]));
    m_tx_link_control_header[3] = header_checksum_calculate(m_tx_link_control_header);

    ser_phy_hci_pkt_params_t pkt_header;
    ser_phy_hci_pkt_params_t pkt_payload;
    ser_phy_hci_pkt_params_t pkt_crc;

    pkt_header.p_buffer      = m_tx_link_control_header;
    pkt_header.num_of_bytes  = PKT_HDR_SIZE;
    pkt_payload.p_buffer     = m_tx_link_control_payload;
    pkt_payload.num_of_bytes = link_control_payload_len;
    pkt_crc.p_buffer         = NULL;
    pkt_crc.num_of_bytes     = 0;
    DEBUG_EVT_SLIP_PACKET_TX(0);
    err_code = ser_phy_hci_slip_tx_pkt_send(&pkt_header, &pkt_payload, &pkt_crc);
    ser_phy_hci_assert(err_code == NRF_SUCCESS);

    return;
}
#endif /* HCI_LINK_CONTROL */

static void hci_pkt_sent_upcall(void)
{
    m_packet_seq_number++; // incoming ACK is valid, increment SEQ
    m_packet_seq_number &= 0x07u;
    m_p_tx_payload       = NULL;
    packet_transmitted_callback();

    return;
}


static void hci_release_ack_buffer(hci_evt_t * p_event)
{
    uint32_t err_code;

    err_code = ser_phy_hci_slip_rx_buf_free(
        p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer);
    ser_phy_hci_assert(err_code == NRF_SUCCESS);

    return;
}


static void hci_process_orphaned_ack(hci_evt_t * p_event)
{
    hci_release_ack_buffer(p_event);
    return;
}

/* main tx fsm   */
static void hci_tx_fsm_event_process(hci_evt_t * p_event)
{

    switch (m_hci_tx_fsm_state)
    {
        case HCI_TX_STATE_SEND:

            if ((p_event->evt_source == HCI_SER_PHY_EVT) &&
                (p_event->evt.ser_phy_evt.evt_type == HCI_SER_PHY_TX_REQUEST))
            {
                hci_pkt_send();
                hci_timeout_setup(0);
                m_tx_retry_count   = MAX_RETRY_COUNT;
                m_hci_tx_fsm_state = HCI_TX_STATE_WAIT_FOR_FIRST_TX_END;
            }
            else if ((p_event->evt_source == HCI_SLIP_EVT) &&
                     (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED))
            {
                hci_process_orphaned_ack(p_event);
            }

            break;

        case HCI_TX_STATE_WAIT_FOR_FIRST_TX_END:

            if ((p_event->evt_source == HCI_SLIP_EVT) &&
                (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_PKT_SENT))
            {
                hci_timeout_setup(1);
                m_hci_tx_fsm_state = HCI_TX_STATE_WAIT_FOR_ACK;
            }
            else if ((p_event->evt_source == HCI_SLIP_EVT) &&
                     (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED))
            {
                hci_process_orphaned_ack(p_event);
            }
            break;

        case HCI_TX_STATE_WAIT_FOR_ACK_OR_TX_END:

            if ((p_event->evt_source == HCI_SLIP_EVT) &&
                (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_PKT_SENT))
            {
                hci_timeout_setup(1);
                m_hci_tx_fsm_state = HCI_TX_STATE_WAIT_FOR_ACK;
            }
            else if ((p_event->evt_source == HCI_SLIP_EVT) &&
                     (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED))
            {
                if (rx_ack_pkt_valid(p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer))
                {
                    hci_timeout_setup(0);
                    m_hci_tx_fsm_state = HCI_TX_STATE_WAIT_FOR_TX_END;
                }
                hci_release_ack_buffer(p_event);
            }
            break;

        case HCI_TX_STATE_WAIT_FOR_ACK:

            if ((p_event->evt_source == HCI_SLIP_EVT) &&
                (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED))
            {
                if (rx_ack_pkt_valid(p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer))
                {
                    hci_timeout_setup(0);
                    hci_pkt_sent_upcall();
                    m_hci_tx_fsm_state = HCI_TX_STATE_SEND;
                }
                hci_release_ack_buffer(p_event);
            }
            else if (p_event->evt_source == HCI_TIMER_EVT)
            {
                m_tx_retry_count--;
                // m_tx_retx_counter++; // global retransmissions counter
                if (m_tx_retry_count)
                {
                    hci_pkt_send();
                    DEBUG_HCI_RETX(0);
                    m_hci_tx_fsm_state = HCI_TX_STATE_WAIT_FOR_ACK_OR_TX_END;
                }
                else
                {
                    error_callback();
                    m_hci_tx_fsm_state = HCI_TX_STATE_SEND;
                }
            }
            break;

        case HCI_TX_STATE_WAIT_FOR_TX_END:

            if ((p_event->evt_source == HCI_SLIP_EVT) &&
                (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_PKT_SENT))
            {
                hci_pkt_sent_upcall();
                m_hci_tx_fsm_state = HCI_TX_STATE_SEND;
            }
            else if ((p_event->evt_source == HCI_SLIP_EVT) &&
                     (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED))
            {
                hci_process_orphaned_ack(p_event);
            }

            break;

#ifdef HCI_LINK_CONTROL
        case HCI_TX_STATE_DISABLE:
            /* This case should not happen if HCI is in ACTIVE mode */
            if (m_hci_mode == HCI_MODE_ACTIVE)
            {
                ser_phy_hci_assert(false);
            }
            break;
#endif /* HCI_LINK_CONTROL */

        default:
            ser_phy_hci_assert(false);
            break;
    }
}


static void hci_mem_request(hci_evt_t * p_event)
{
    m_buffer_reqested_flag = true;
    m_p_rx_packet          = p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer;
    m_rx_packet_length     = p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.num_of_bytes;
    ser_phy_hci_assert(m_rx_packet_length > PKT_HDR_SIZE + PKT_CRC_SIZE);
    memory_request_callback(m_rx_packet_length - PKT_HDR_SIZE - PKT_CRC_SIZE);
    return;
}


static void hci_inc_ack()
{
    m_packet_ack_number++;
    m_packet_ack_number &= 0x07u;
}


static void hci_rx_fsm_event_process(hci_evt_t * p_event)
{
    switch (m_hci_rx_fsm_state)
    {
        case HCI_RX_STATE_RECEIVE:

            if ((p_event->evt_source == HCI_SLIP_EVT) &&
                (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED))
            {
                /* type and crc and check sum are validated by slip handler */
                uint8_t rx_seq_number = packet_seq_nmbr_extract(
                    p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer);

                if (packet_ack_get() == rx_seq_number)
                {
                    hci_mem_request(p_event);
                    m_hci_rx_fsm_state = HCI_RX_STATE_WAIT_FOR_MEM;
                }
                else
                {
                    // m_rx_drop_counter++;
                    m_hci_rx_fsm_state = HCI_RX_STATE_WAIT_FOR_SLIP_NACK_END;
                    (void) ser_phy_hci_slip_rx_buf_free(m_p_rx_packet); // and drop a packet
                    ack_transmit();                                     // send NACK with valid ACK
                }
            }
            break;

        case HCI_RX_STATE_WAIT_FOR_MEM:

            if ((p_event->evt_source == HCI_SER_PHY_EVT) &&
                (p_event->evt.ser_phy_evt.evt_type == HCI_SER_PHY_RX_BUF_GRANTED))
            {
                if (m_p_rx_buffer)
                {
                    memcpy(m_p_rx_buffer,
                           m_p_rx_packet + PKT_HDR_SIZE,
                           m_rx_packet_length - PKT_HDR_SIZE - PKT_CRC_SIZE);
                    (void) ser_phy_hci_slip_rx_buf_free(m_p_rx_packet);
                }
                m_hci_rx_fsm_state = HCI_RX_STATE_WAIT_FOR_SLIP_ACK_END;
                hci_inc_ack(); // SEQ was valid for good packet, we will send incremented SEQ as ACK
                ack_transmit();
            }

            break;

        case HCI_RX_STATE_WAIT_FOR_SLIP_ACK_END:

            if ((p_event->evt_source == HCI_SLIP_EVT) &&
                (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_ACK_SENT))
            {

                if (m_p_rx_buffer)
                {
                    packet_received_callback(m_p_rx_buffer,
                                             m_rx_packet_length - PKT_HDR_SIZE - PKT_CRC_SIZE);
                }
                else
                {
                    packet_dropped_callback();
                }
                m_hci_rx_fsm_state = HCI_RX_STATE_RECEIVE;
            }
            else if ((p_event->evt_source == HCI_SLIP_EVT) &&
                    (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED))
            {
                (void) ser_phy_hci_slip_rx_buf_free(p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer);
            }
            break;

        case HCI_RX_STATE_WAIT_FOR_SLIP_NACK_END:
            if ((p_event->evt_source == HCI_SLIP_EVT) &&
               (p_event->evt.ser_phy_slip_evt.evt_type == SER_PHY_HCI_SLIP_EVT_ACK_SENT))
            {
               m_hci_rx_fsm_state = HCI_RX_STATE_RECEIVE;
            }
            else
            {
               (void) ser_phy_hci_slip_rx_buf_free(p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer);
            }
            break;


#ifdef HCI_LINK_CONTROL
        case HCI_RX_STATE_DISABLE:
            if (m_hci_mode == HCI_MODE_ACTIVE)
            {
                ser_phy_hci_assert(false);
            }
            break;
#endif /* HCI_LINK_CONTROL */

        default:
            ser_phy_hci_assert(false);
            break;
    }
}


/* this function might be entered only via hci_tx_event_handler */
static void hci_tx_fsm(void)
{
    hci_evt_t event;
    uint32_t  err_code = NRF_SUCCESS;

    while (err_code == NRF_SUCCESS)
    {

        CRITICAL_REGION_ENTER();
        err_code = nrf_queue_pop(&m_tx_evt_queue, &event);

        if (err_code != NRF_SUCCESS)
        {
            m_tx_fsm_idle_flag = true;
        }
        CRITICAL_REGION_EXIT();

        if (err_code == NRF_SUCCESS)
        {
            hci_tx_fsm_event_process(&event); /* this is the only entry to the TX_FSM */
        }
    }
    return;
}


/* this function might be entered only via hci_rx_event_handler */
static void hci_rx_fsm(void)
{
    hci_evt_t event;
    uint32_t  err_code = NRF_SUCCESS;

    while (err_code == NRF_SUCCESS)
    {
        CRITICAL_REGION_ENTER();
        err_code = nrf_queue_pop(&m_rx_evt_queue, &event);

        if (err_code != NRF_SUCCESS)
        {
            m_rx_fsm_idle_flag = true;
        }
        CRITICAL_REGION_EXIT();

        if (err_code == NRF_SUCCESS)
        {
            hci_rx_fsm_event_process(&event); /* this is the only entry to the RX_FSM */
        }
    }
    return;
}


/* something might have been queued by API with disabled 'PHY-interrupts' */
static void hci_tx_reschedule()
{
    bool     tx_exec_flag = false;
    uint32_t tx_queue_length;

    CRITICAL_REGION_ENTER();
    tx_queue_length = nrf_queue_utilization_get(&m_tx_evt_queue);

#ifndef HCI_LINK_CONTROL
    if (m_tx_fsm_idle_flag && m_hci_global_enable_flag && tx_queue_length)
#else
    if (m_tx_fsm_idle_flag && m_hci_global_enable_flag && tx_queue_length && (m_hci_mode == HCI_MODE_ACTIVE))
#endif /* HCI_LINK_CONTROL */
    {
        tx_exec_flag       = true;  // FSM should be activated
        m_tx_fsm_idle_flag = false; // FSM will be busy from now on till the queue is exhausted
    }
    CRITICAL_REGION_EXIT();

    if (tx_exec_flag)
    {
        hci_tx_fsm();
    }
    return;
}


/* entry to TX state machine, might be called asynchronously from different contexts */
/* Puts event into the TX event queue and execute if FSM was idle */
static void hci_tx_event_handler(hci_evt_t * p_event)
{
    bool     tx_exec_flag = false;
    uint32_t err_code;

    CRITICAL_REGION_ENTER();
    err_code = nrf_queue_push(&m_tx_evt_queue, p_event);
    ser_phy_hci_assert(err_code == NRF_SUCCESS);

    // CRITICAL_REGION_ENTER();
    /* only one process can acquire tx_exec_flag */
    if (m_tx_fsm_idle_flag && m_hci_global_enable_flag)
    {
        tx_exec_flag       = true;  // FSM should be activated
        m_tx_fsm_idle_flag = false; // FSM will be busy from now on till the queue is exhausted
    }
    CRITICAL_REGION_EXIT();

    if (tx_exec_flag)
    {
        hci_tx_fsm();
    }
    return;
}


/* Something might have been queued by API with disabled 'PHY-interrupts' */
static void hci_rx_reschedule()
{
    bool     rx_exec_flag = false;
    uint32_t rx_queue_length;

    CRITICAL_REGION_ENTER();
    rx_queue_length = nrf_queue_utilization_get(&m_rx_evt_queue);

#ifndef HCI_LINK_CONTROL
    if (m_rx_fsm_idle_flag && m_hci_global_enable_flag && rx_queue_length)
#else
    if (m_rx_fsm_idle_flag && m_hci_global_enable_flag && rx_queue_length && (m_hci_mode == HCI_MODE_ACTIVE))
#endif /* HCI_LINK_CONTROL */
    {
        rx_exec_flag       = true;  // FSM should be activated
        m_rx_fsm_idle_flag = false; // FSM will be busy from now on till the queue is exhausted
    }
    CRITICAL_REGION_EXIT();

    if (rx_exec_flag)
    {
        hci_rx_fsm();
    }

}


/* Entry to RX state machine, might be called asynchronously from different contexts */
/* Puts event into the RX event queue and execute if FSM was idle */
static void hci_rx_event_handler(hci_evt_t * p_event)
{
    bool     rx_exec_flag = false;
    uint32_t err_code;

    CRITICAL_REGION_ENTER();
    err_code = nrf_queue_push(&m_rx_evt_queue, p_event);
    ser_phy_hci_assert(err_code == NRF_SUCCESS);

    /* only one process can acquire rx_exec_flag */
    // CRITICAL_REGION_ENTER();
    if (m_rx_fsm_idle_flag && m_hci_global_enable_flag)
    {
        rx_exec_flag       = true;  // FSM should be activated
        m_rx_fsm_idle_flag = false; // FSM will be busy from now on till the queue is exhausted
    }
    CRITICAL_REGION_EXIT();

    if (rx_exec_flag)
    {
        hci_rx_fsm();
    }

    return;
}

#ifdef HCI_LINK_CONTROL
/* Link control event handler - used only for Link Control packets */
/* This handler will be called only in 2 cases:
   - when SER_PHY_HCI_SLIP_EVT_PKT_RECEIVED event is received
   - when HCI_TIMER_EVT event is reveived */
static void hci_link_control_event_handler(hci_evt_t * p_event)
{
    uint16_t pkt_type = HCI_LINK_CONTROL_PKT_INVALID;

    switch (p_event->evt_source)
    {
        case HCI_SLIP_EVT:
            pkt_type = link_control_packet_decode(
                            p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer,
                            p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.num_of_bytes);
            /* Perform HCI mode transition if needed */
            CRITICAL_REGION_ENTER();
            switch (pkt_type)
            {
                case HCI_PKT_SYNC:
                    m_hci_link_control_next_pkt = HCI_PKT_SYNC_RSP;
                    /* Restart HCI communication if it was in ACTIVE mode */
                    if (m_hci_mode == HCI_MODE_ACTIVE)
                    {
                        m_hci_mode          = HCI_MODE_UNINITIALIZED;
                        m_packet_ack_number = INITIAL_ACK_NUMBER_EXPECTED;
                        m_packet_seq_number = INITIAL_SEQ_NUMBER;
                        m_hci_tx_fsm_state  = HCI_TX_STATE_DISABLE;
                        m_hci_rx_fsm_state  = HCI_RX_STATE_DISABLE;
                        m_hci_other_side_active = false;
                    }
                    hci_link_control_pkt_send();
                    hci_timeout_setup(HCI_LINK_CONTROL_TIMEOUT); // Need to trigger transmitting SYNC messages
                    break;
                case HCI_PKT_SYNC_RSP:
                    if (m_hci_mode == HCI_MODE_UNINITIALIZED)
                    {
                        m_hci_mode                  = HCI_MODE_INITIALIZED;
                        m_hci_link_control_next_pkt = HCI_PKT_CONFIG;
                    }
                    break;
                case HCI_PKT_CONFIG:
                    if (m_hci_mode != HCI_MODE_UNINITIALIZED)
                    {
                        m_hci_link_control_next_pkt = HCI_PKT_CONFIG_RSP;
                        hci_link_control_pkt_send();
                        m_hci_other_side_active = true;
                    }
                    break;
                case HCI_PKT_CONFIG_RSP:
                    if (m_hci_mode == HCI_MODE_INITIALIZED)
                    {
                        m_hci_mode          = HCI_MODE_ACTIVE;
                        m_hci_tx_fsm_state  = HCI_TX_STATE_SEND;
                        m_hci_rx_fsm_state  = HCI_RX_STATE_RECEIVE;
                    }
                    break;
            }
            CRITICAL_REGION_EXIT();
            (void) ser_phy_hci_slip_rx_buf_free(
                p_event->evt.ser_phy_slip_evt.evt_params.received_pkt.p_buffer);
            /* Kick the state machine so it can start process BLE packets */
            if ((m_hci_mode == HCI_MODE_ACTIVE) && m_hci_other_side_active)
            {
                hci_tx_reschedule();
                hci_rx_reschedule();
            }
            break;

        case HCI_TIMER_EVT:
            /* Send one of the Link Control packets if in Unintialized or Initialized state */
            CRITICAL_REGION_ENTER();
            switch (m_hci_mode)
            {
                case HCI_MODE_UNINITIALIZED:
                    //send packet
                    m_hci_link_control_next_pkt = HCI_PKT_SYNC;
                    hci_link_control_pkt_send();
                    hci_timeout_setup(HCI_LINK_CONTROL_TIMEOUT);
                    break;
                case HCI_MODE_INITIALIZED:
                    m_hci_link_control_next_pkt = HCI_PKT_CONFIG;
                    hci_link_control_pkt_send();
                    hci_timeout_setup(HCI_LINK_CONTROL_TIMEOUT);
                    break;
                case HCI_MODE_ACTIVE:
                case HCI_MODE_DISABLE:
                default:
                    // No implementation needed
                    break;
            }
            CRITICAL_REGION_EXIT();
            break;
        case HCI_SER_PHY_EVT:
        default:
            // No implementation needed
            break;
    }
}
#endif /* HCI_LINK_CONTROL */

/* ser_phy API function */
void ser_phy_interrupts_enable(void)
{
    bool pending_timer_callback_flag = false;

    CRITICAL_REGION_ENTER();
    m_hci_timer_enabled_flag = true;

    if (m_hci_timout_pending_flag)
    {
        m_hci_timout_pending_flag   = false;
        pending_timer_callback_flag = true;
    }
    CRITICAL_REGION_EXIT();
    // this is a workaround - scheduled SER_PHY EVENTS
    m_hci_global_enable_flag = true;
    hci_tx_reschedule();
    hci_rx_reschedule();

    if (pending_timer_callback_flag)
    {
        hci_signal_timeout_event();
    }

    return;
}


/* ser_phy API function */
void ser_phy_interrupts_disable(void)
{
    CRITICAL_REGION_ENTER();
    m_hci_timer_enabled_flag = false;
    // transport calls PHY API with ser_phy_interrupts_disabled
    m_hci_global_enable_flag = false;
    CRITICAL_REGION_EXIT();
}


/* ser_phy API function */
uint32_t ser_phy_rx_buf_set(uint8_t * p_buffer)
{
    uint32_t  status = NRF_SUCCESS;
    hci_evt_t event;

    if (m_buffer_reqested_flag)
    {
        m_buffer_reqested_flag         = false;
        m_p_rx_buffer                  = p_buffer;
        event.evt_source               = HCI_SER_PHY_EVT;
        event.evt.ser_phy_evt.evt_type = HCI_SER_PHY_RX_BUF_GRANTED;
        hci_rx_event_handler(&event);
    }
    else
    {
        status = NRF_ERROR_BUSY;
    }
    return status;
}


/* ser_phy API function */
uint32_t ser_phy_tx_pkt_send(const uint8_t * p_buffer, uint16_t num_of_bytes)
{
    uint32_t  status = NRF_SUCCESS;
    hci_evt_t event;

    if ( p_buffer == NULL || num_of_bytes == 0)
    {
        return NRF_ERROR_NULL;
    }

    if ( m_p_tx_payload == NULL)
    {
        m_tx_payload_length = num_of_bytes;
        m_p_tx_payload      = (uint8_t *)p_buffer;
        DEBUG_EVT_TX_REQ(0);
        event.evt_source               = HCI_SER_PHY_EVT;
        event.evt.ser_phy_evt.evt_type = HCI_SER_PHY_TX_REQUEST;
        hci_tx_event_handler(&event);
    }
    else
    {
        status = NRF_ERROR_BUSY;
    }

    return status;
}


static uint32_t  hci_timer_init(void)
{
    uint32_t err_code = NRF_SUCCESS;

#ifdef HCI_APP_TIMER

    err_code = app_timer_create(&m_app_timer_id, APP_TIMER_MODE_REPEATED, hci_timeout_handler);

    if (err_code != NRF_SUCCESS)
    {
        return NRF_ERROR_INTERNAL;
    }

    err_code = app_timer_start(m_app_timer_id, RETRANSMISSION_TIMEOUT_IN_TICKS, NULL);

    if (err_code != NRF_SUCCESS)
    {
        return NRF_ERROR_INTERNAL;
    }

#else

    // Configure TIMER for compare[1] event
    HCI_TIMER->PRESCALER = 9;
    HCI_TIMER->MODE      = TIMER_MODE_MODE_Timer;
    HCI_TIMER->BITMODE   = TIMER_BITMODE_BITMODE_16Bit;

    // Clear TIMER
    HCI_TIMER->TASKS_CLEAR = 1;

    // Enable interrupt
    HCI_TIMER->INTENCLR = 0xFFFFFFFF;
    HCI_TIMER->INTENSET = TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos;

    NVIC_ClearPendingIRQ(HCI_TIMER_IRQn);
    NVIC_SetPriority(HCI_TIMER_IRQn, APP_IRQ_PRIORITY_HIGH);
    NVIC_EnableIRQ(HCI_TIMER_IRQn);

#endif

    return err_code;

}


/* ser_phy API function */
uint32_t ser_phy_open(ser_phy_events_handler_t events_handler)
{
    uint32_t err_code;

    if ((m_hci_tx_fsm_state != HCI_TX_STATE_DISABLE) || (m_hci_rx_fsm_state != HCI_RX_STATE_DISABLE))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    if (events_handler == NULL)
    {
        return NRF_ERROR_NULL;
    }

    err_code = hci_timer_init();

    if (err_code != NRF_SUCCESS)
    {
        return NRF_ERROR_INTERNAL;
    }

    nrf_queue_reset(&m_tx_evt_queue);
    nrf_queue_reset(&m_rx_evt_queue);

    err_code = ser_phy_hci_slip_open(hci_slip_event_handler);

    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    if (err_code == NRF_SUCCESS)
    {
        m_packet_ack_number = INITIAL_ACK_NUMBER_EXPECTED;
        m_packet_seq_number = INITIAL_SEQ_NUMBER;
        m_ser_phy_callback  = events_handler;

#ifndef HCI_LINK_CONTROL
        m_hci_tx_fsm_state  = HCI_TX_STATE_SEND;
        m_hci_rx_fsm_state  = HCI_RX_STATE_RECEIVE;
#else
        hci_timeout_setup(HCI_LINK_CONTROL_TIMEOUT);// Trigger sending SYNC messages
        m_hci_mode              = HCI_MODE_UNINITIALIZED;
        m_hci_other_side_active = false;
#endif /*HCI_LINK_CONTROL*/
    }
    return err_code;
}

static uint32_t hci_timer_close(void)
{
    uint32_t err_code = NRF_SUCCESS;

#ifdef HCI_APP_TIMER
    err_code = app_timer_stop(m_app_timer_id);

    if (err_code != NRF_SUCCESS)
    {
        return NRF_ERROR_INTERNAL;
    }
#endif

    return err_code;
}

/* ser_phy API function */
void ser_phy_close(void)
{
    m_ser_phy_callback = NULL;
    ser_phy_hci_slip_close();
    m_hci_tx_fsm_state = HCI_TX_STATE_DISABLE;
    m_hci_rx_fsm_state = HCI_RX_STATE_DISABLE;

#ifdef HCI_LINK_CONTROL
    m_hci_mode         = HCI_MODE_DISABLE;
#endif /* HCI_LINK_CONTROL */

    uint32_t err_code = hci_timer_close();
    ser_phy_hci_assert(err_code == NRF_SUCCESS);
}