antfs.c 96.8 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 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
/**
 * This software is subject to the ANT+ Shared Source License
 * www.thisisant.com/swlicenses
 * Copyright (c) Dynastream Innovations, Inc. 2012
 * 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 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 Dynastream nor the names of its
 *    contributors may be used to endorse or promote products
 *    derived from this software without specific prior
 *    written permission.
 * 
 * The following actions are prohibited:
 * 1) Redistribution of source code containing the ANT+ Network
 *    Key. The ANT+ Network Key is available to ANT+ Adopters.
 *    Please refer to http://thisisant.com to become an ANT+
 *    Adopter and access the key.
 * 
 * 2) Reverse engineering, decompilation, and/or disassembly of
 *    software provided in binary form under this license.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE HEREBY
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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; DAMAGE TO ANY DEVICE, 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. SOME STATES DO NOT ALLOW
 * THE EXCLUSION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THE
 * ABOVE LIMITATIONS MAY NOT APPLY TO YOU.
 * 
 */
#include "sdk_common.h"
#if NRF_MODULE_ENABLED(ANTFS)

#include "antfs.h"
#include <string.h>
#include "defines.h"
#include "app_error.h"
#include "app_timer.h"
#include "ant_error.h"
#include "ant_parameters.h"
#include "ant_interface.h"
#include "ant_key_manager.h"
#include "crc.h"

#if ANTFS_CONFIG_DEBUG_LED_ENABLED
    #include "bsp.h"
#endif // ANTFS_CONFIG_DEBUG_LED_ENABLED

#define BURST_PACKET_SIZE                  8u                            /**< The burst packet size. */

#define ANTFS_CONNECTION_TYPE_OFFSET       0x00u                         /**< The connection type offset within ANT-FS message. */
#define ANTFS_COMMAND_OFFSET               0x01u                         /**< The command offset within ANT-FS message. */
#define ANTFS_RESPONSE_OFFSET              0x01u                         /**< The response offset within ANT-FS message. */

#define ANTFS_CONTROL_OFFSET               0x02u                         /**< The control offset within ANT-FS message. */
#define ANTFS_DATA_OFFSET                  0x03u                         /**< The data offset within ANT-FS message. */

#define ANTFS_BEACON_ID                    0x43u                         /**< The ANT-FS beacon ID. */
#define ANTFS_COMMAND_ID                   0x44u                         /**< The ANT-FS command ID. */

// Beacon definitions.
#define STATUS1_OFFSET                     0x01u                         /**< The beacon status1 field offset. */
#define STATUS2_OFFSET                     0x02u                         /**< The beacon status2 field offset. */
   #define DEVICE_STATE_SHIFT              0x00u                         /**< Shift value for device state bitfield. */
   #define DEVICE_STATE_MASK               (0x0Fu << DEVICE_STATE_SHIFT) /**< Device state bitmask. */
   #define DEVICE_STATE_LINK               (0x00u << DEVICE_STATE_SHIFT) /**< Device is in link state. */
   #define DEVICE_STATE_AUTHENTICATE       (0x01u << DEVICE_STATE_SHIFT) /**< Device is in authenticate state. */
   #define DEVICE_STATE_TRANSPORT          (0x02u << DEVICE_STATE_SHIFT) /**< Device is in transport state. */
   #define DEVICE_STATE_BUSY               (0x03u << DEVICE_STATE_SHIFT) /**< Device is in busy state. */

#define DEVICE_DESCRIPTOR_OFFSET_0         0x04u                         /**< Beacon ANT-FS device descriptor LSB position. */
#define DEVICE_DESCRIPTOR_OFFSET_1         0x05u                         /**< Beacon ANT-FS device descriptor LSB + 1 position. */
#define DEVICE_DESCRIPTOR_OFFSET_2         0x06u                         /**< Beacon ANT-FS device descriptor LSB + 2 position. */
#define DEVICE_DESCRIPTOR_OFFSET_3         0x07u                         /**< Beacon ANT-FS device descriptor MSB position. */

// Commands.
#define ANTFS_CMD_NONE                     0x00u                         /**< Used to identify that no ANT-FS command is in progress. */
#define ANTFS_CMD_LINK_ID                  0x02u                         /**< ANT-FS link command ID. */
#define ANTFS_CMD_DISCONNECT_ID            0x03u                         /**< ANT-FS disconnect command ID. */
#define ANTFS_CMD_AUTHENTICATE_ID          0x04u                         /**< ANT-FS authenticate command ID. */
#define ANTFS_CMD_PING_ID                  0x05u                         /**< ANT-FS ping command ID. */
#define ANTFS_CMD_DOWNLOAD_ID              0x09u                         /**< ANT-FS download request command ID. */
#define ANTFS_CMD_UPLOAD_REQUEST_ID        0x0Au                         /**< ANT-FS upload request command ID. */
#define ANTFS_CMD_ERASE_ID                 0x0Bu                         /**< ANT-FS erase request command ID. */
#define ANTFS_CMD_UPLOAD_DATA_ID           0x0Cu                         /**< ANT-FS upload command ID. */

// Responses.
#define ANTFS_RSP_AUTHENTICATE_ID          0x84u                         /**< ANT-FS authenticate response command ID. */
#define ANTFS_RSP_DOWNLOAD_ID              0x89u                         /**< ANT-FS download request response command ID. */
#define ANTFS_RSP_UPLOAD_REQ_ID            0x8Au                         /**< ANT-FS upload request response command ID. */
#define ANTFS_RSP_ERASE_ID                 0x8Bu                         /**< ANT-FS erase response command ID. */
#define ANTFS_RSP_UPLOAD_DATA_ID           0x8Cu                         /**< ANT-FS upload data response command ID. */

// Link command.
#define TRANSPORT_CHANNEL_FREQUENCY_OFFSET 0x02u                         /**< Channel frequency field offset within link command. */
#define TRANSPORT_MESSAGE_PERIOD_OFFSET    0x03u                         /**< Channel period field offset within link command. */
#define HOST_ID_OFFSET_0                   0x04u                         /**< Host serial number period field LSB offset within link command. */
#define HOST_ID_OFFSET_1                   0x05u                         /**< Host serial number period field LSB + 1 offset within link command. */
#define HOST_ID_OFFSET_2                   0x06u                         /**< Host serial number period field LSB + 2 offset within link command. */
#define HOST_ID_OFFSET_3                   0x07u                         /**< Host serial number period field MSB offset within link command. */

// Authenticate command.
#define COMMAND_TYPE_OFFSET                0x02u                         /**< Command type field offset within authenticate command. */
   #define COMMAND_TYPE_PROCEED            0x00u                         /**< Command type proceed to transport in the authenticate command. */
   #define COMMAND_TYPE_REQUEST_SERIAL     0x01u                         /**< Command type request client device serial number in the authenticate command. */
   #define COMMAND_TYPE_REQUEST_PAIR       0x02u                         /**< Command type request pairing in the authenticate command. */
   #define COMMAND_TYPE_REQUEST_PASSKEY    0x03u                         /**< Command type request passkey exchange in the authenticate command. */

// Authenticate response.
#define RESPONSE_TYPE_OFFSET               0x02u                         /**< Command type field offset within authenticate response command. */
   #define AUTH_RESPONSE_N_A               0x00u                         /**< Command response type N/A (response for client serial number request). */
   #define AUTH_RESPONSE_ACCEPT            0x01u                         /**< Command response type accept. */
   #define AUTH_RESPONSE_REJECT            0x02u                         /**< Command response type reject. */

// Authenticate command/response.
#define AUTH_STRING_LENGTH_OFFSET          0x03u                         /**< Authenticate Command/Response authentication string length offset. */
#define SERIAL_NUMBER_OFFSET_0             0x04u                         /**< Authenticate Command/Response client/host serial number LSB offset. */
#define SERIAL_NUMBER_OFFSET_1             0x05u                         /**< Authenticate Command/Response client/host serial number LSB + 1 offset. */
#define SERIAL_NUMBER_OFFSET_2             0x06u                         /**< Authenticate Command/Response client/host serial number LSB + 2 offset. */
#define SERIAL_NUMBER_OFFSET_3             0x07u                         /**< Authenticate Command/Response client/host serial number MSB offset. */

// Download/Upload/Erase commands.
#define INITIAL_REQUEST_OFFSET             0x01u                         /**< Download/Upload/Erase command initial request command offset. */
#define DATA_INDEX_OFFSET_LOW              0x02u                         /**< Download/Upload/Erase command offset index low. */
#define DATA_INDEX_OFFSET_HIGH             0x03u                         /**< Download/Upload/Erase command offset index high. */

#define ADDRESS_PARAMETER_OFFSET_0         0x04u                         /**< Download/Upload command parameter LSB offset. */
#define ADDRESS_PARAMETER_OFFSET_1         0x05u                         /**< Download/Upload command parameter LSB + 1 offset. */
#define ADDRESS_PARAMETER_OFFSET_2         0x06u                         /**< Download/Upload command parameter LSB + 2 offset. */
#define ADDRESS_PARAMETER_OFFSET_3         0x07u                         /**< Download/Upload command parameter MSB offset. */
#define UPLOAD_CRC_OFFSET_LOW              0x06u                         /**< Upload data CRC offset low. */
#define UPLOAD_CRC_OFFSET_HIGH             0x07u                         /**< Upload data CRC offset high. */

// Authentication type. The highest level of authentication available is included in the beacon.
#if ANTFS_CONFIG_AUTH_TYPE_PASSKEY_ENABLED
   #define AUTHENTICATION_TYPE             COMMAND_TYPE_REQUEST_PASSKEY  /**< Passkey and pairing only mode set as authentication type in beacon. */
#elif ANTFS_CONFIG_AUTH_TYPE_PAIRING_ENABLED
   #define AUTHENTICATION_TYPE             COMMAND_TYPE_REQUEST_PAIR     /**< Pairing only mode set as authentication type in beacon. */
#elif ANTFS_CONFIG_AUTH_TYPE_PASSTHROUGH_ENABLED
   #define AUTHENTICATION_TYPE             COMMAND_TYPE_PROCEED          /**< Pass-through mode set as authentication type in beacon. */
#else
   #error "No valid auth type defined"
#endif

#define AUTHENTICATION_RETRIES             0x05u                         /**< Max number of retries for authentication responses */

#define ANTFS_EVENT_QUEUE_SIZE             0x04u                         /**< ANT-FS event queue size. */
#define SAVE_DISTANCE                       256u                         /**< Save distance required because of nRF buffer to line up data offset on retry. */

// Buffer Indices.
#define BUFFER_INDEX_MESG_SIZE             0x00u                         /**< ANT message buffer index length offset. */
#define BUFFER_INDEX_MESG_ID               0x01u                         /**< ANT message buffer index ID offset. */
#define BUFFER_INDEX_CHANNEL_NUM           0x02u                         /**< ANT message buffer index channel number offset. */
#define BUFFER_INDEX_MESG_DATA             0x03u                         /**< ANT message buffer index begin of data offset. */
#define BUFFER_INDEX_RESPONSE_CODE         0x04u                         /**< ANT message buffer index response code offset. */

typedef struct
{
    char     friendly_name[ANTFS_FRIENDLY_NAME_MAX];                      /**< Friendly Name. */
    bool     is_name_set;                                                 /**< Is the name set. */
    uint32_t index;                                                       /**< Current index (for reading the friendly name). */
    uint32_t friendly_name_size;                                          /**< Friendly name size. */
} friendly_name_t;

typedef union
{
    antfs_link_substate_t         link_sub_state;                         /**< Sub-state (Link layer). */
    antfs_authenticate_substate_t auth_sub_state;                         /**< Sub-state (Authentication layer). */
    antfs_transport_substate_t    trans_sub_state;                        /**< Sub-state (Transport layer). */
} antfs_substate_t;

typedef struct
{
    antfs_state_t    state;                                               /**< ANT-FS state. */
    antfs_substate_t sub_state;                                           /**< ANT-FS sub-state. */
} antfs_states_t;

typedef struct
{
    antfs_event_return_t * p_queue;                                       /**< ANT-FS event queue. */
    uint32_t head;                                                        /**< ANT-FS event queue head index. */
    uint32_t tail;                                                        /**< ANT-FS event queue tail index. */
} antfs_event_queue_t;

static antfs_params_t              m_initial_parameters;                  /**< Initial parameters. */
static antfs_beacon_status_byte1_t m_active_beacon_status1_field;         /**< Status 1 field in beacon. */
static uint32_t                    m_active_beacon_frequency;             /**< Active beacon frequency. */
static antfs_states_t              m_current_state;                       /**< Current state. */
static friendly_name_t             m_friendly_name;                       /**< Host's friendly name. */
static ulong_union_t               m_link_host_serial_number;             /**< Host's serial number. */
static uint32_t                    m_link_command_in_progress;            /**< ANT-FS command in progress. */
static uint32_t                    m_authenticate_command_type;           /**< Authenticate command type in progress. */
static volatile uint8_t            m_burst_wait;                          /**< Polling status flag for data unlock on burst handler input. */
static uint8_t                     m_retry;                               /**< Retry counter */
APP_TIMER_DEF(m_timer_id);                                                /**< Timer ID used with the timer module. */

#if ANTFS_CONFIG_AUTH_TYPE_PASSKEY_ENABLED
   static uint32_t m_passkey_index;                                       /**< Current location of Tx block (auth string). */
#endif // ANTFS_CONFIG_AUTH_TYPE_PASSKEY_ENABLED

// Download/upload.
static bool            m_is_data_request_pending;                         /**< Requested data pending. */
static bool            m_is_crc_pending;                                  /**< CRC for data packets pending. */
static ushort_union_t  m_file_index;                                      /**< File index of current upload/download. */
static ulong_union_t   m_file_size;                                       /**< File size of current upload/download (bytes). */
static ulong_union_t   m_max_block_size;                                  /**< Maximum number of bytes expected to be downloaded in a single burst block of data (set by host). */
static ulong_union_t   m_link_burst_index;                                /**< Current location of Tx block (bytes). */
static ulong_union_t   m_bytes_remaining;                                 /**< Total remaining data length (bytes). */
static ulong_union_t   m_max_transfer_index;                              /**< Upper limit of the current Tx burst block (bytes). */
static uint32_t        m_bytes_to_write;                                  /**< Number of bytes to write to file (upload). */
static const uint8_t * mp_upload_data;                                    /**< Address of begin of the buffer that holds data received from upload. */
#if ANTFS_CONFIG_UPLOAD_ENABLED
    static ulong_union_t m_block_size;                                    /**< Number of bytes the client can receive in a single burst. */
#endif // ANTFS_CONFIG_UPLOAD_ENABLED

// CRC verification.
static uint32_t m_saved_crc_offset;                                       /**< CRC data offset (bytes) saved at last CRC update (save point). */
static uint32_t m_saved_buffer_crc_offset;                                /**< Data offset to track how much data has been buffered into nRF */
static uint32_t m_temp_crc_offset;                                        /**< Temporary CRC data offset used in CRC calculation. */
static uint16_t m_compared_crc;                                           /**< 16-bit CRC for all data packets in the block (provided by download request). */
static uint16_t m_transfer_crc;                                           /**< 16-bit CRC for all data packets in the block (calculated by client). */
static uint16_t m_saved_transfer_crc;                                     /**< 16-bit CRC saved at last CRC update (save point). */
static uint16_t m_saved_buffer_crc;                                       /**< 16-bit CRC saved at last CRC update (save point) for buffering the nRF */

// ANT-FS event handling.
static antfs_event_return_t m_event_queue_buffer[ANTFS_EVENT_QUEUE_SIZE]; /**< Event queue storage. */
static antfs_event_queue_t m_event_queue;                                 /**< Event queue. */

static antfs_burst_wait_handler_t m_burst_wait_handler = NULL;            /**< Burst wait handler */


const char * antfs_hostname_get(void)
{
    if (m_friendly_name.is_name_set)
    {
        return (m_friendly_name.friendly_name);
    }

    return NULL;
}


/**@brief Function for waiting for the burst transmission request to complete.
 */
static void wait_burst_request_to_complete(void)
{
    while (m_burst_wait != 0)
    {
        if (m_burst_wait_handler != NULL)
        {
            m_burst_wait_handler();
        }
    };
}


/**@brief Function for stopping ANT-FS timeout, which is possibly currently running.
 */
static void timeout_disable(void)
{
    m_link_command_in_progress = ANTFS_CMD_NONE;

    const uint32_t err_code = app_timer_stop(m_timer_id);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for transmitting a beacon.
 *
 * Transmits a beacon either using a broadcast or burst transmission mode.
 *
 * @param[in] message_type     Defines the used transmission mode.
 */
static void beacon_transmit(uint32_t message_type)
{
    uint8_t beacon_status_byte2;

    // Set beacon status byte 2.

    if (m_link_command_in_progress == ANTFS_CMD_NONE)
    {
        switch (m_current_state.state)
        {
            case ANTFS_STATE_AUTH:
                beacon_status_byte2 = DEVICE_STATE_AUTHENTICATE;
                break;

            case ANTFS_STATE_TRANS:
                beacon_status_byte2 = DEVICE_STATE_TRANSPORT;
                break;

            default:
                beacon_status_byte2 = DEVICE_STATE_LINK;
                break;
        }
    }
    else
    {
        beacon_status_byte2 = DEVICE_STATE_BUSY;
    }

    // Set remaining beacon fields.

    uint8_t tx_buffer[BURST_PACKET_SIZE];

    tx_buffer[0] = ANTFS_BEACON_ID;
    tx_buffer[1] = m_active_beacon_status1_field.status;
    tx_buffer[2] = beacon_status_byte2;
    tx_buffer[3] = AUTHENTICATION_TYPE;

    if ((m_current_state.state == ANTFS_STATE_AUTH) ||
        (m_current_state.state == ANTFS_STATE_TRANS))
    {
        tx_buffer[4] = m_link_host_serial_number.bytes.byte0;
        tx_buffer[5] = m_link_host_serial_number.bytes.byte1;
        tx_buffer[6] = m_link_host_serial_number.bytes.byte2;
        tx_buffer[7] = m_link_host_serial_number.bytes.byte3;
    }
    else
    {
        tx_buffer[4] = m_initial_parameters.beacon_device_type;
        tx_buffer[5] = (m_initial_parameters.beacon_device_type >> 8u);
        tx_buffer[6] = m_initial_parameters.beacon_device_manufacturing_id;
        tx_buffer[7] = (m_initial_parameters.beacon_device_manufacturing_id >> 8u);
    }

    if (message_type == MESG_BROADCAST_DATA_ID)
    {
        if (sd_ant_broadcast_message_tx(ANTFS_CONFIG_CHANNEL_NUMBER, sizeof(tx_buffer), tx_buffer) != NRF_SUCCESS)
        {
            // @note: No implementation needed, as it is a valid and accepted use case for this call
            // to fail. This call can fail if we are in middle of bursting.
        }
    }
    else if (message_type == MESG_BURST_DATA_ID)
    {
        // Send as the first packet of a burst.
        const uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                               sizeof(tx_buffer),
                                                               tx_buffer,
                                                               BURST_SEGMENT_START);
        APP_ERROR_CHECK(err_code);

        wait_burst_request_to_complete();

        // This is the first packet of a burst response, disable command timeout while bursting.
        timeout_disable();
    }
    else
    {
        // This should not happen.
        APP_ERROR_HANDLER(message_type);
    }
}


/**@brief Function for transmitting a authenticate response message.
 *
 * @param[in] response_type    Authenticate response code.
 * @param[in] password_length  Length of authentication string.
 * @param[in] p_password       Authentication string transmitted.
 */
static void authenticate_response_transmit(uint8_t response_type,
                                           uint32_t password_length,
                                           const uint8_t * p_password)
{
    ulong_union_t serial_number;

    serial_number.data = m_initial_parameters.client_serial_number;

    // First packet is beacon.
    beacon_transmit(MESG_BURST_DATA_ID);

    uint8_t tx_buffer[BURST_PACKET_SIZE];

    tx_buffer[ANTFS_CONNECTION_TYPE_OFFSET] = ANTFS_COMMAND_ID;
    tx_buffer[ANTFS_RESPONSE_OFFSET]        = ANTFS_RSP_AUTHENTICATE_ID;
    tx_buffer[RESPONSE_TYPE_OFFSET]         = response_type;
    tx_buffer[AUTH_STRING_LENGTH_OFFSET]    = password_length;
    tx_buffer[SERIAL_NUMBER_OFFSET_0]       = serial_number.bytes.byte0;
    tx_buffer[SERIAL_NUMBER_OFFSET_1]       = serial_number.bytes.byte1;
    tx_buffer[SERIAL_NUMBER_OFFSET_2]       = serial_number.bytes.byte2;
    tx_buffer[SERIAL_NUMBER_OFFSET_3]       = serial_number.bytes.byte3;

    uint32_t err_code;
    if ((m_current_state.state == ANTFS_STATE_AUTH) &&
        (
            (response_type != AUTH_RESPONSE_REJECT) &&
            password_length &&
            (password_length <= ANTFS_AUTH_STRING_MAX)
        )
       )
    {
        // Send second packet (auth response).
        err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                sizeof(tx_buffer),
                                                tx_buffer,
                                                BURST_SEGMENT_CONTINUE);
        APP_ERROR_CHECK(err_code);

        wait_burst_request_to_complete();

        // Round size to a multiple of 8 bytes.
        uint8_t tx_buffer_authenticate[ANTFS_AUTH_STRING_MAX + 1u];

        memset(tx_buffer_authenticate, 0, ANTFS_AUTH_STRING_MAX + 1u);
        memcpy(tx_buffer_authenticate, p_password, password_length);

        // Round up total number bytes to a multiple of 8 to be sent to burst handler.
        if (password_length & (BURST_PACKET_SIZE - 1u))
        {
            password_length &= ~(BURST_PACKET_SIZE - 1u);
            password_length += BURST_PACKET_SIZE;
        }

        // Send auth string (last packets of the burst).
        err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                password_length,
                                                tx_buffer_authenticate,
                                                BURST_SEGMENT_END);
        APP_ERROR_CHECK(err_code);

        wait_burst_request_to_complete();

        m_link_command_in_progress = ANTFS_RSP_AUTHENTICATE_ID;
    }
    else
    {
        // If the authorization is rejected or there is no valid password, the auth response is the
        // last packet.

        err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                sizeof(tx_buffer),
                                                tx_buffer,
                                                BURST_SEGMENT_END);
        APP_ERROR_CHECK(err_code);

        wait_burst_request_to_complete();
    }

    // Switch to appropiate state.
    if (response_type == AUTH_RESPONSE_REJECT)
    {
        m_current_state.sub_state.auth_sub_state = ANTFS_AUTH_SUBSTATE_REJECT;
    }
    else if (response_type == AUTH_RESPONSE_ACCEPT)
    {
        m_current_state.sub_state.auth_sub_state = ANTFS_AUTH_SUBSTATE_ACCEPT;
    }
    else
    {
        // No implementation needed.
    }
}


bool antfs_pairing_resp_transmit(bool accept)
{
#if ANTFS_CONFIG_AUTH_TYPE_PAIRING_ENABLED
    // This function should only be called when ANT-FS is in PAIRING mode.
    if ((m_current_state.state != ANTFS_STATE_AUTH) ||
        (m_current_state.sub_state.auth_sub_state != ANTFS_AUTH_SUBSTATE_PAIR))
    {
        return false;
    }

    m_link_command_in_progress = ANTFS_CMD_AUTHENTICATE_ID;

    if (accept)
    {
        // Accept request and send passkey if authentication passed.
        authenticate_response_transmit(AUTH_RESPONSE_ACCEPT,
                                       ANTFS_PASSKEY_SIZE,
                                       m_initial_parameters.p_pass_key);
    }
    else
    {
        // Reject authentication request.
        authenticate_response_transmit(AUTH_RESPONSE_REJECT, 0, NULL);
    }

    return true;
#else
    return false;
#endif // ANTFS_CONFIG_AUTH_TYPE_PAIRING_ENABLED
}


/**@brief Function for adding an ANT-FS event to the event queue.
 *
 * @param[in] event_code       The event to be added.
 */
static void event_queue_write(antfs_event_t event_code)
{
    antfs_event_return_t * p_event = NULL;
#if ANTFS_CONFIG_DEBUG_LED_ENABLED
    uint32_t err_code;
#endif // ANTFS_CONFIG_DEBUG_LED_ENABLED

    // Check if there is room in the queue for a new event.
    if (((m_event_queue.head + 1u) & (ANTFS_EVENT_QUEUE_SIZE - 1u)) != m_event_queue.tail)
    {
        p_event = &(m_event_queue.p_queue[m_event_queue.head]);
    }

    if (p_event != NULL)
    {
        // Initialize event parameters.
        p_event->event = event_code;

        // Set parameters depending on event type.
        switch (event_code)
        {
            case ANTFS_EVENT_ERASE_REQUEST:
                p_event->file_index = m_file_index.data;
                p_event->offset     = 0;
                p_event->bytes      = 0;
                p_event->crc        = 0;
                break;

            case ANTFS_EVENT_DOWNLOAD_REQUEST:
                p_event->file_index = m_file_index.data;
                // Requested offset for the download.
                p_event->offset     = m_link_burst_index.data;
                p_event->bytes      = 0;
                p_event->crc        = 0;
                break;

            case ANTFS_EVENT_DOWNLOAD_REQUEST_DATA:
                p_event->file_index = m_file_index.data;
                // Current offset.
                p_event->offset     = m_link_burst_index.data;

                if (m_bytes_remaining.data > (ANTFS_BURST_BLOCK_SIZE * BURST_PACKET_SIZE))
                {
                    // If remaining bytes > burst block size then grab one block at a time.
                    p_event->bytes = ANTFS_BURST_BLOCK_SIZE * BURST_PACKET_SIZE;
                }
                else
                {
                    p_event->bytes = m_bytes_remaining.data;
                }

                p_event->crc = 0;
                break;

            case ANTFS_EVENT_UPLOAD_REQUEST:
                p_event->file_index = m_file_index.data;
                // Requested offset for the upload.
                p_event->offset     = m_link_burst_index.data;
                // Upper limit of the download (offset + remaining bytes).
                p_event->bytes      = m_max_transfer_index.data;
                // CRC Seed (from last save point if resuming).
                p_event->crc        = m_transfer_crc;
                break;

            case ANTFS_EVENT_UPLOAD_DATA:
                p_event->file_index = m_file_index.data;
                // Current offset.
                p_event->offset     = m_link_burst_index.data;
                // Current CRC.
                p_event->crc        = m_transfer_crc;
                // Number of bytes to write.
                p_event->bytes      = m_bytes_to_write;
                // Upload to appication data buffer.
                memcpy(p_event->data, mp_upload_data, m_bytes_to_write);
                break;

            case ANTFS_EVENT_PAIRING_REQUEST:
#if ANTFS_CONFIG_DEBUG_LED_ENABLED
                err_code = bsp_indication_set(BSP_INDICATE_BONDING);
                APP_ERROR_CHECK(err_code);
#endif // ANTFS_CONFIG_DEBUG_LED_ENABLED
                break;

            default:
                // No parameters need to be set.

                p_event->file_index = 0;
                p_event->offset     = 0;
                p_event->bytes      = 0;
                p_event->crc        = 0;
                break;
        }

        // Put the event in the queue.
        m_event_queue.head = ((m_event_queue.head + 1u) & (ANTFS_EVENT_QUEUE_SIZE - 1u));
    }
    else
    {
        // No free space left in the queue.
        APP_ERROR_HANDLER(0);
    }
}


/**@brief Function for transmitting download request response message.
 *
 * @param[in] response         Download response code.
 */
static void download_request_response_transmit(uint8_t response)
{
    // First burst packet is beacon.
    beacon_transmit(MESG_BURST_DATA_ID);

    uint8_t tx_buffer[BURST_PACKET_SIZE];

    // Next send the first part of the download response.
    tx_buffer[0] = ANTFS_COMMAND_ID;
    tx_buffer[1] = ANTFS_RSP_DOWNLOAD_ID;
    tx_buffer[2] = response;
    tx_buffer[3] = 0;
    // Total number of bytes remaining in the data block.
    tx_buffer[4] = m_bytes_remaining.bytes.byte0;
    tx_buffer[5] = m_bytes_remaining.bytes.byte1;
    tx_buffer[6] = m_bytes_remaining.bytes.byte2;
    tx_buffer[7] = m_bytes_remaining.bytes.byte3;

    uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                     sizeof(tx_buffer),
                                                     tx_buffer,
                                                     BURST_SEGMENT_CONTINUE);
    APP_ERROR_CHECK(err_code);

    wait_burst_request_to_complete();

    // Second part of the download response.

    // The offset the data will start from in this block.
    tx_buffer[0] = m_link_burst_index.bytes.byte0;
    tx_buffer[1] = m_link_burst_index.bytes.byte1;
    tx_buffer[2] = m_link_burst_index.bytes.byte2;
    tx_buffer[3] = m_link_burst_index.bytes.byte3;
    // The file size in the client device.
    tx_buffer[4] = m_file_size.bytes.byte0;
    tx_buffer[5] = m_file_size.bytes.byte1;
    tx_buffer[6] = m_file_size.bytes.byte2;
    tx_buffer[7] = m_file_size.bytes.byte3;

    if (response || (m_bytes_remaining.data == 0))
    {
        // If the download was rejected or there is no data to send.

        // Set response to end since we're not downloading any data.
        err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                sizeof(tx_buffer),
                                                tx_buffer,
                                                BURST_SEGMENT_END);
        APP_ERROR_CHECK(err_code);

        wait_burst_request_to_complete();
    }
    else
    {
        // Response will continue (data packets + CRC footer to follow).
        err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                sizeof(tx_buffer),
                                                tx_buffer,
                                                BURST_SEGMENT_CONTINUE);
        APP_ERROR_CHECK(err_code);

        wait_burst_request_to_complete();
    }

    m_link_command_in_progress = ANTFS_CMD_DOWNLOAD_ID;

    if (response == 0)
    {
        // If we are going to download (response OK), enter the downloading substate.
        m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_DOWNLOADING;
        event_queue_write(ANTFS_EVENT_DOWNLOAD_START);
    }
    else
    {
        // Download rejected.
        event_queue_write(ANTFS_EVENT_DOWNLOAD_FAIL);
    }
}


void antfs_download_req_resp_prepare(uint8_t response,
                                     const antfs_request_info_t * const p_request_info)
{
    // This function should only be called after receiving a download request.
    APP_ERROR_CHECK_BOOL((m_current_state.state == ANTFS_STATE_TRANS) &&
                         (m_link_command_in_progress == ANTFS_CMD_DOWNLOAD_ID));

    if (response == 0)
    {
        // Download request OK.

        // File size of the requested download.
        m_file_size.data = p_request_info->file_size.data;

        if (m_link_burst_index.data > m_file_size.data)
        {
            // Offset should not exceed file size.
            m_link_burst_index.data = m_file_size.data;
        }

        // If the host is not limiting download size or the file size does not exceed the host's
        // download size limit.
        if ((m_max_block_size.data == 0) || (m_file_size.data < m_max_block_size.data))
        {
            //  Number of bytes remaining to be downloaded in this block is the file size.
            m_bytes_remaining.data = m_file_size.data;
        }

        if ((m_file_size.data - m_link_burst_index.data) < m_bytes_remaining.data)
        {
            // Calculate number of remaining bytes in this block based on the offset.
            m_bytes_remaining.data = m_file_size.data - m_link_burst_index.data;
        }

        // If the application is limiting the Tx block size.
        if (m_bytes_remaining.data > p_request_info->max_burst_block_size.data)
        {
            // Number of remaining bytes in this block is the application defined block size.
            m_bytes_remaining.data = p_request_info->max_burst_block_size.data;
        }

        // Find upper limit of the burst Tx.
        m_max_transfer_index.data = m_link_burst_index.data + m_bytes_remaining.data;

        if (m_saved_crc_offset == ANTFS_MAX_FILE_SIZE)
        {
            // CRC checking was set as invalid. An invalid download was requested, so reject it.
            response = RESPONSE_INVALID_OPERATION;
        }
    }

    if ((response != 0) || (m_file_size.data == 0))
    {
        // Send the response right away if the download request was rejected or there is no data to
        // send.
        download_request_response_transmit(response);
    }
    else
    {
        // Proceed to download data.
        if (m_link_burst_index.data != m_saved_crc_offset)
        {
            uint32_t temp;

            // If requesting to resume exactly where we left off, we can start from the same block.
            if (m_link_burst_index.data == m_temp_crc_offset)
            {
                // Move last save point to end of last block sent.
                m_saved_crc_offset   = m_link_burst_index.data;
                m_saved_transfer_crc = m_transfer_crc;
            }

            // To resume the download, request a block of data starting from the last save point.
            // Update the remaining number of bytes per the last save point.
            m_bytes_remaining.data += (m_link_burst_index.data - m_saved_crc_offset);

            // Swap the current burst Tx index with the saved CRC index, to make sure we do not
            // start updating the CRC until we get to the requested index.
            temp                    = m_link_burst_index.data;
            m_link_burst_index.data = m_saved_crc_offset;
            m_saved_crc_offset      = temp;

            // Set CRC to previous save point, to check the CRC provided by the host.
            m_transfer_crc = m_saved_transfer_crc;
        }

        m_temp_crc_offset = m_saved_crc_offset;

        m_is_data_request_pending = true;

        // Request data from application.
        event_queue_write(ANTFS_EVENT_DOWNLOAD_REQUEST_DATA);

        m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_VERIFY_CRC;
    }
}


uint32_t antfs_input_data_download(uint16_t index,
                                   uint32_t offset,
                                   uint32_t num_bytes,
                                   const uint8_t * const p_message)
{
    // Verify that this is the requested data.
    APP_ERROR_CHECK_BOOL((offset == m_link_burst_index.data) && (index == m_file_index.data));

    // If file offset is greater than the upper limit, this is not data we need.
    APP_ERROR_CHECK_BOOL(offset <= m_max_transfer_index.data);

    if ((m_current_state.state == ANTFS_STATE_TRANS) &&
        (
            (m_current_state.sub_state.trans_sub_state == ANTFS_TRANS_SUBSTATE_VERIFY_CRC) ||
            // Only send data if we were processing a download request.
            (m_current_state.sub_state.trans_sub_state == ANTFS_TRANS_SUBSTATE_DOWNLOADING)
        )
       )
    {
        uint32_t block_offset = 0;

        if (m_current_state.sub_state.trans_sub_state == ANTFS_TRANS_SUBSTATE_VERIFY_CRC)
        {
            // Make sure download_request_response_transmit defaults to RESPONSE_INVALID_CRC.
            uint32_t response = RESPONSE_MESSAGE_OK;

            // Check CRC.
            if (m_link_burst_index.data == m_saved_crc_offset)
            {
                // If indexes match, we can compare CRC directly.
                if (m_transfer_crc != m_compared_crc)
                {
                    response = RESPONSE_INVALID_CRC;
                }
                else
                {
                    // Set up the save point
                    m_temp_crc_offset = m_link_burst_index.data; // Reset save point counter
                    m_saved_crc_offset = m_link_burst_index.data;
                    m_saved_buffer_crc_offset = m_link_burst_index.data;

                    // Set up the CRC save points
                    m_saved_transfer_crc = m_compared_crc;
                    m_saved_buffer_crc = m_compared_crc;
                    m_is_crc_pending     = true;
                }
                // Start bursting beacon and the download response (3 burst packets).
                download_request_response_transmit(response);
            }
            // If the data is in this block, advance to the requested offset
            else if ((m_link_burst_index.data < m_saved_crc_offset) &&
                     ((m_saved_crc_offset - m_link_burst_index.data) < num_bytes))
            {
                // Update the offset within this block for the requested transmission.
                block_offset = m_saved_crc_offset - m_link_burst_index.data;
                // Update the number of bytes that will actually be transmitted.
                num_bytes   -= block_offset;

                // Update CRC calculation up to requested index.
                m_transfer_crc          = crc_crc16_update(m_transfer_crc, p_message, block_offset);
                // Update the remaining number of bytes.
                m_bytes_remaining.data -= block_offset;

                // Check CRC
                if (m_transfer_crc != m_compared_crc)
                {
                    response = RESPONSE_INVALID_CRC;
                }
                else
                {
                    // Move index back to point where transmission will resume.
                    m_link_burst_index.data = m_saved_crc_offset;

                    // Set up the save point
                    m_temp_crc_offset = m_link_burst_index.data; // Reset save point counter
                    m_saved_buffer_crc_offset = m_link_burst_index.data;

                    // Set up the CRC save points
                    m_saved_transfer_crc = m_compared_crc;
                    m_saved_buffer_crc = m_compared_crc;

                    m_is_crc_pending = true;
                }
                download_request_response_transmit(response);
            }
            // Data index has gone too far and it is not possible to check CRC, fail and let host retry
            else if (m_link_burst_index.data > m_saved_crc_offset)
            {
                response = RESPONSE_INVALID_CRC;
                download_request_response_transmit(response);
            }
            // Keep getting data and recalculate the CRC until the indexes match
            else
            {
                m_is_data_request_pending = false;

                // Update the current burst index and bytes remaining
                m_link_burst_index.data += num_bytes;
                m_bytes_remaining.data -= num_bytes;

                // Update CRC
                m_transfer_crc = crc_crc16_update(m_transfer_crc, p_message, num_bytes);

                // Request more data.
                event_queue_write(ANTFS_EVENT_DOWNLOAD_REQUEST_DATA);
            }
        }

        // Append data.
        if (m_current_state.sub_state.trans_sub_state == ANTFS_TRANS_SUBSTATE_DOWNLOADING)
        {
            uint32_t num_of_bytes_to_burst = num_bytes;

            if (num_of_bytes_to_burst & (BURST_PACKET_SIZE - 1u))
            {
                // Round up total number bytes to a multiple of BURST_PACKET_SIZE to be sent to
                // burst handler.
                num_of_bytes_to_burst &= ~(BURST_PACKET_SIZE - 1u);
                num_of_bytes_to_burst += BURST_PACKET_SIZE;
            }

            uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                             num_of_bytes_to_burst,
                                                             (uint8_t*)&(p_message[block_offset]),
                                                             BURST_SEGMENT_CONTINUE);
            if (err_code != NRF_ANT_ERROR_TRANSFER_SEQUENCE_NUMBER_ERROR)
            {
                // If burst failed before we are able to catch it, we will get a TRANSFER_SEQUENCE_NUMBER_ERROR
                // The message processing will send client back to correct state
                APP_ERROR_CHECK(err_code);
            }

            wait_burst_request_to_complete();

            // Update current burst index.
            m_link_burst_index.data += num_bytes;
            // Update remaining bytes.
            m_bytes_remaining.data  -= num_bytes;

            m_is_data_request_pending = false;

            m_transfer_crc = crc_crc16_update(m_transfer_crc,
                                              &(p_message[block_offset]),
                                              num_bytes);

            if ((m_link_burst_index.data - m_temp_crc_offset) > SAVE_DISTANCE)
            {
                // Set CRC save point
                m_saved_transfer_crc = m_saved_buffer_crc;  // Set CRC at buffer save point (will always be one behind to account for buffering)
                m_saved_buffer_crc = m_transfer_crc;        // Set CRC at save point

                // Set offset save point
                m_saved_crc_offset = m_saved_buffer_crc_offset;     // Set offset at buffer save point (will always be one behind to account for buffering)
                m_saved_buffer_crc_offset = m_link_burst_index.data;    // Set buffer offset to current data offset

                // Reset save counter offset
                m_temp_crc_offset = m_link_burst_index.data;    // Set to current location; next save point will take place after SAVE_DISTANCE bytes
            }

            if (!m_is_data_request_pending && (m_link_burst_index.data < m_max_transfer_index.data))
            {
                // If we have not finished the download.

                // Request more data.
                event_queue_write(ANTFS_EVENT_DOWNLOAD_REQUEST_DATA);

                m_is_data_request_pending = true;
            }
            else if (m_link_burst_index.data >= m_max_transfer_index.data && m_is_crc_pending)
            {
                // We are done, send CRC footer.

                uint8_t tx_buffer[BURST_PACKET_SIZE];

                tx_buffer[0] = 0;
                tx_buffer[1] = 0;
                tx_buffer[2] = 0;
                tx_buffer[3] = 0;
                tx_buffer[4] = 0;
                tx_buffer[5] = 0;
                tx_buffer[6] = (uint8_t)m_transfer_crc;
                tx_buffer[7] = (uint8_t)(m_transfer_crc >> 8u);

                err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                        sizeof(tx_buffer),
                                                        tx_buffer,
                                                        BURST_SEGMENT_END);
                if (err_code != NRF_ANT_ERROR_TRANSFER_SEQUENCE_NUMBER_ERROR)
                {
                    // If burst failed before we are able to catch it, we will get a TRANSFER_SEQUENCE_NUMBER_ERROR
                    // The message processing will send client back to correct state
                    APP_ERROR_CHECK(err_code);
                }

                wait_burst_request_to_complete();

                m_is_crc_pending          = false;
                m_max_transfer_index.data = 0;
            }

            // Return the number of bytes we accepted.
            return num_bytes;
        }
    }

    // No bytes were accepted.
    return 0;
}


bool antfs_upload_req_resp_transmit(uint8_t response,
                                    const antfs_request_info_t * const p_request_info)
{
#if ANTFS_CONFIG_UPLOAD_ENABLED
    if (m_current_state.state != ANTFS_STATE_TRANS ||
        // Only send the response if we were processing an upload request.
        (m_link_command_in_progress != ANTFS_CMD_UPLOAD_REQUEST_ID))
    {
        return false;
    }

    // If the application is sending a response for a different file than requested, the upload
    // will fail.
    if (p_request_info->file_index.data != m_file_index.data)
    {
        event_queue_write(ANTFS_EVENT_UPLOAD_FAIL);

        return false;
    }

    ulong_union_t max_mem_size;
    // Set maximum number of bytes that can be written to the file.
    max_mem_size.data = p_request_info->max_file_size;

    if (p_request_info->max_burst_block_size.data != 0)
    {
        // If the client is limiting the block size set the block size requested by the client.
        m_block_size.data = p_request_info->max_burst_block_size.data;
    }
    else
    {
        // Try to get the entire file in a single block.
        m_block_size.data = max_mem_size.data;
    }

    if (response == 0)
    {
        if (m_max_transfer_index.data > max_mem_size.data)
        {
            // Not enough space to write file, reject download.
            response = RESPONSE_MESSAGE_NOT_ENOUGH_SPACE;
        }
    }

    // Get last valid CRC and last valid offset.
    m_transfer_crc          = p_request_info->file_crc;
    m_link_burst_index.data = p_request_info->file_size.data;

    // First packet to transmit is the beacon.
    beacon_transmit(MESG_BURST_DATA_ID);

    // Second packet.

    uint8_t tx_buffer[BURST_PACKET_SIZE];

    tx_buffer[0] = ANTFS_COMMAND_ID;
    tx_buffer[1] = ANTFS_RSP_UPLOAD_REQ_ID;
    tx_buffer[2] = response;
    tx_buffer[3] = 0;
    // Last valid data offset written to the file.
    tx_buffer[4] = m_link_burst_index.bytes.byte0;
    tx_buffer[5] = m_link_burst_index.bytes.byte1;
    tx_buffer[6] = m_link_burst_index.bytes.byte2;
    tx_buffer[7] = m_link_burst_index.bytes.byte3;


    uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                     sizeof(tx_buffer),
                                                     tx_buffer,
                                                     BURST_SEGMENT_CONTINUE);
    APP_ERROR_CHECK(err_code);

    wait_burst_request_to_complete();

    // Third packet.

    // Maximum number of bytes that can be written to the file.
    tx_buffer[0] = max_mem_size.bytes.byte0;
    tx_buffer[1] = max_mem_size.bytes.byte1;
    tx_buffer[2] = max_mem_size.bytes.byte2;
    tx_buffer[3] = max_mem_size.bytes.byte3;
    // Maximum upload block size.
    tx_buffer[4] = m_block_size.bytes.byte0;
    tx_buffer[5] = m_block_size.bytes.byte1;
    tx_buffer[6] = m_block_size.bytes.byte2;
    tx_buffer[7] = m_block_size.bytes.byte3;

    err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                            sizeof(tx_buffer),
                                            tx_buffer,
                                            BURST_SEGMENT_CONTINUE);
    APP_ERROR_CHECK(err_code);

    wait_burst_request_to_complete();

    // Fourth packet.

    tx_buffer[0] = 0;
    tx_buffer[1] = 0;
    tx_buffer[2] = 0;
    tx_buffer[3] = 0;
    tx_buffer[4] = 0;
    tx_buffer[5] = 0;
    // Value of CRC at last data offset.
    tx_buffer[6] = (uint8_t) m_transfer_crc;
    tx_buffer[7] = (uint8_t)(m_transfer_crc >> 8);

    err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                            sizeof(tx_buffer),
                                            tx_buffer,
                                            BURST_SEGMENT_END);
    APP_ERROR_CHECK(err_code);

    wait_burst_request_to_complete();

    m_link_command_in_progress = ANTFS_CMD_UPLOAD_REQUEST_ID;

    if (response != 0)
    {
        // Failed upload request. Reset max transfer index to 0 (do not accept any data if the host
        // sends it anyway).
        m_max_transfer_index.data = 0;
    }
    else
    {
        // Wait for upload data request.
        m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_UPLOAD_WAIT_FOR_DATA;
    }

    return true;
#else
    return false;
#endif  // ANTFS_CONFIG_UPLOAD_ENABLED
}


bool antfs_upload_data_resp_transmit(bool data_upload_success)
{
#if ANTFS_CONFIG_UPLOAD_ENABLED
    // Should be in TRANSPORT layer to send this response.
    if (m_current_state.state != ANTFS_STATE_TRANS)
    {
        return false;
    }

    uint8_t tx_buffer[BURST_PACKET_SIZE];

    // Response.
    tx_buffer[0] = ANTFS_COMMAND_ID;
    tx_buffer[1] = ANTFS_RSP_UPLOAD_DATA_ID;
    tx_buffer[2] = (data_upload_success) ? RESPONSE_MESSAGE_OK : RESPONSE_MESSAGE_FAIL;
    tx_buffer[3] = 0;
    tx_buffer[4] = 0;
    tx_buffer[5] = 0;
    tx_buffer[6] = 0;
    tx_buffer[7] = 0;

    // First packet is beacon.
    beacon_transmit(MESG_BURST_DATA_ID);

    // Send last packet.
    uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                     sizeof(tx_buffer),
                                                     tx_buffer,
                                                     BURST_SEGMENT_END);
    APP_ERROR_CHECK(err_code);

    wait_burst_request_to_complete();

    m_link_command_in_progress = ANTFS_CMD_UPLOAD_REQUEST_ID;

    // Reset maximum index.
    m_max_transfer_index.data = 0;

    return true;
#else
    return false;
#endif  // ANTFS_CONFIG_UPLOAD_ENABLED
}


void antfs_erase_req_resp_transmit(uint8_t response)
{
    // This function should only be called after receiving an erase request.
    APP_ERROR_CHECK_BOOL((m_current_state.state == ANTFS_STATE_TRANS) &&
                         (m_link_command_in_progress == ANTFS_CMD_ERASE_ID));

    beacon_transmit(MESG_BURST_DATA_ID);

    uint8_t tx_buffer[BURST_PACKET_SIZE];

    // Erase response.
    tx_buffer[0] = ANTFS_COMMAND_ID;
    tx_buffer[1] = ANTFS_RSP_ERASE_ID;
    tx_buffer[2] = response;
    tx_buffer[3] = 0;
    tx_buffer[4] = 0;
    tx_buffer[5] = 0;
    tx_buffer[6] = 0;
    tx_buffer[7] = 0;

    uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                     sizeof(tx_buffer),
                                                     tx_buffer,
                                                     BURST_SEGMENT_END);
    APP_ERROR_CHECK(err_code);

    wait_burst_request_to_complete();
}


bool antfs_event_extract(antfs_event_return_t * const p_event)
{
    bool return_value = false;

    if (m_event_queue.head != m_event_queue.tail)
    {
        // Pending events exist. Copy event parameters into return event.
        p_event->event      = m_event_queue.p_queue[m_event_queue.tail].event;
        p_event->file_index = m_event_queue.p_queue[m_event_queue.tail].file_index;
        p_event->offset     = m_event_queue.p_queue[m_event_queue.tail].offset;
        p_event->bytes      = m_event_queue.p_queue[m_event_queue.tail].bytes;
        p_event->crc        = m_event_queue.p_queue[m_event_queue.tail].crc;
        memcpy(p_event->data,
               m_event_queue.p_queue[m_event_queue.tail].data,
               sizeof(p_event->data));

        // Release the event queue.
        m_event_queue.tail = ((m_event_queue.tail + 1u) & (ANTFS_EVENT_QUEUE_SIZE - 1u));

        return_value = true;
    }

    return return_value;
}


/**@brief Function for setting the channel period.
 *
 * Sets the channel period. The only allowed frequencies are 0.5, 1, 2, 4 and 8 Hz.
 *
 * @param[in] link_period      Link period for the beacon transmission.
 */
static void channel_period_set(uint32_t link_period)
{
    uint32_t period;

    switch (link_period)
    {
        default:
            // Shouldn't happen, but just in case default to 0,5Hz.
        case BEACON_PERIOD_0_5_HZ:
            period = 65535u;
            break;

        case BEACON_PERIOD_1_HZ:
            period = 32768u;
            break;

        case BEACON_PERIOD_2_HZ:
            period = 16384u;
            break;

        case BEACON_PERIOD_4_HZ:
            period = 8192u;
            break;

        case BEACON_PERIOD_8_HZ:
            period = 4096u;
            break;
    }

    const uint32_t err_code = sd_ant_channel_period_set(ANTFS_CONFIG_CHANNEL_NUMBER, period);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for starting ANT-FS timeout.
 *
 * @param[in] timeout_in_secs  Timeout requested in unit of seconds.
 */
static void timeout_start(uint32_t timeout_in_secs)
{
    uint32_t err_code = app_timer_stop(m_timer_id);
    APP_ERROR_CHECK(err_code);

    err_code = app_timer_start(m_timer_id,
                               APP_TIMER_TICKS((uint32_t)(timeout_in_secs * 1000u)),
                               NULL);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for switching to authentication layer.
 */
static void authenticate_layer_transit(void)
{
    if (m_current_state.state != ANTFS_STATE_OFF)
    {
        m_current_state.state                    = ANTFS_STATE_AUTH;
        m_current_state.sub_state.auth_sub_state = ANTFS_AUTH_SUBSTATE_NONE;
        m_link_command_in_progress               = ANTFS_CMD_NONE;

        timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);

        uint32_t err_code = sd_ant_channel_radio_freq_set(ANTFS_CONFIG_CHANNEL_NUMBER, m_active_beacon_frequency);
        APP_ERROR_CHECK(err_code);

        event_queue_write(ANTFS_EVENT_AUTH);
    }
}


/**@brief Function for decoding an ANT-FS command received at the link layer.
 *
 * @param[in] p_command_buffer The ANT-FS command buffer.
 */
static void link_layer_cmd_decode(const uint8_t * p_command_buffer)
{
    if (p_command_buffer[ANTFS_CONNECTION_TYPE_OFFSET] != ANTFS_COMMAND_ID)
    {
        return;
    }

    switch (p_command_buffer[ANTFS_COMMAND_OFFSET])
    {
        case ANTFS_CMD_LINK_ID:
            // Channel frequency.
            m_active_beacon_frequency                            =
                p_command_buffer[TRANSPORT_CHANNEL_FREQUENCY_OFFSET];
            // Channel message period.
            m_active_beacon_status1_field.parameters.link_period =
                p_command_buffer[TRANSPORT_MESSAGE_PERIOD_OFFSET];
            // Host serial Number.
            m_link_host_serial_number.bytes.byte0                =
                p_command_buffer[HOST_ID_OFFSET_0];
            m_link_host_serial_number.bytes.byte1                =
                p_command_buffer[HOST_ID_OFFSET_1];
            m_link_host_serial_number.bytes.byte2                =
                p_command_buffer[HOST_ID_OFFSET_2];
            m_link_host_serial_number.bytes.byte3                =
                p_command_buffer[HOST_ID_OFFSET_3];

            // Move to the channel period issued by the host.
            channel_period_set(m_active_beacon_status1_field.parameters.link_period);

            authenticate_layer_transit();
            break;

        default:
            break;
    }
}


/**@brief Function for switching to link layer.
 */
static void link_layer_transit(void)
{
    if (m_current_state.state != ANTFS_STATE_OFF)
    {
        uint32_t err_code;
#if ANTFS_CONFIG_DEBUG_LED_ENABLED
        err_code = bsp_indication_set(BSP_INDICATE_IDLE);
        APP_ERROR_CHECK(err_code);
#endif // ANTFS_CONFIG_DEBUG_LED_ENABLED

        m_current_state.state                    = ANTFS_STATE_LINK;
        m_current_state.sub_state.link_sub_state = ANTFS_LINK_SUBSTATE_NONE;
        m_link_command_in_progress               = ANTFS_CMD_NONE;
        m_active_beacon_status1_field            = m_initial_parameters.beacon_status_byte1;
        m_active_beacon_frequency                = m_initial_parameters.beacon_frequency;

        timeout_disable();

        err_code = sd_ant_channel_radio_freq_set(ANTFS_CONFIG_CHANNEL_NUMBER, m_active_beacon_frequency);
        APP_ERROR_CHECK(err_code);

        event_queue_write(ANTFS_EVENT_LINK);
    }
}


/**@brief Function for decoding an ANT-FS command received at the authenticate layer.
 *
 * @param[in] control_byte     The command control byte.
 * @param[in] p_command_buffer The ANT-FS command buffer.
 */
static void authenticate_layer_cmd_decode(uint8_t control_byte,
                                          const uint8_t * p_command_buffer)
{
    // @note: Response variable must have a static storage allocation as it keeps track of the
    // passkey authentication progress between multiple burst packets.
#if ANTFS_CONFIG_AUTH_TYPE_PASSKEY_ENABLED
    static uint32_t response;
#endif // ANTFS_CONFIG_AUTH_TYPE_PASSKEY_ENABLED

    if ((control_byte & ~SEQUENCE_LAST_MESSAGE) == 0 && m_link_command_in_progress != ANTFS_CMD_NONE)
    {
        // This is something new, and we're busy processing something already, so don't respond
        return;
    }

    if (p_command_buffer[ANTFS_CONNECTION_TYPE_OFFSET] == ANTFS_COMMAND_ID &&
        m_link_command_in_progress == ANTFS_CMD_NONE)
    {
        if (p_command_buffer[ANTFS_COMMAND_OFFSET] == ANTFS_CMD_AUTHENTICATE_ID)
        {
            // Make sure it is the correct host
            if (m_link_host_serial_number.bytes.byte0 != p_command_buffer[HOST_ID_OFFSET_0] ||
               m_link_host_serial_number.bytes.byte1 != p_command_buffer[HOST_ID_OFFSET_1] ||
               m_link_host_serial_number.bytes.byte2 != p_command_buffer[HOST_ID_OFFSET_2] ||
               m_link_host_serial_number.bytes.byte3 != p_command_buffer[HOST_ID_OFFSET_3])
                return;

            m_link_command_in_progress  = ANTFS_CMD_AUTHENTICATE_ID;
            m_authenticate_command_type = p_command_buffer[COMMAND_TYPE_OFFSET];
            m_retry = AUTHENTICATION_RETRIES;
        }
    }

    if (m_link_command_in_progress == ANTFS_CMD_AUTHENTICATE_ID)
    {
        switch (m_authenticate_command_type)
        {
            case COMMAND_TYPE_REQUEST_SERIAL:
                if (control_byte & SEQUENCE_LAST_MESSAGE)
                {
                    // Don't do anything before the burst completes (last burst message received).
                    timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);
                    authenticate_response_transmit(AUTH_RESPONSE_N_A,
                                                   ANTFS_REMOTE_FRIENDLY_NAME_MAX,
                                                   // Send device friendly name if it exists.
                                                   m_initial_parameters.p_remote_friendly_name);
                }
                break;

#if ANTFS_CONFIG_AUTH_TYPE_PASSTHROUGH_ENABLED
            case COMMAND_TYPE_PROCEED:
                if (control_byte & SEQUENCE_LAST_MESSAGE)
                {
                    // Don't do anything before the burst completes (last burst message received).
                    timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);
                    // Proceed directly to transport layer (no authentication required).
                    authenticate_response_transmit(AUTH_RESPONSE_ACCEPT, 0, NULL);
                }
                break;

#endif // ANTFS_CONFIG_AUTH_TYPE_PASSTHROUGH_ENABLED
#if ANTFS_CONFIG_AUTH_TYPE_PAIRING_ENABLED
            case COMMAND_TYPE_REQUEST_PAIR:
                if ((control_byte & SEQUENCE_NUMBER_ROLLOVER) == 0)
                {
                    // First burst packet.

                    // Friendly name length.
                    m_friendly_name.friendly_name_size =
                        p_command_buffer[AUTH_STRING_LENGTH_OFFSET];

                    if (m_friendly_name.friendly_name_size > 0)
                    {
                        if (m_friendly_name.friendly_name_size > ANTFS_FRIENDLY_NAME_MAX)
                        {
                            m_friendly_name.friendly_name_size = ANTFS_FRIENDLY_NAME_MAX;
                        }

                        m_friendly_name.index = 0;
                    }
                }
                else
                {
                    // Next burst packets: read host friendly name.

                    if (m_friendly_name.index < ANTFS_FRIENDLY_NAME_MAX)
                    {
                        uint32_t num_of_bytes = ANTFS_FRIENDLY_NAME_MAX - m_friendly_name.index;
                        if (num_of_bytes > 8u)
                        {
                            num_of_bytes = 8u;
                        }
                        memcpy((uint8_t*)&m_friendly_name.friendly_name[m_friendly_name.index],
                               p_command_buffer,
                               num_of_bytes);
                        m_friendly_name.index += num_of_bytes;
                    }
                }

                if (control_byte & SEQUENCE_LAST_MESSAGE)
                {
                    // Last burst packet.

                    timeout_start(ANTFS_CONFIG_PAIRING_TIMEOUT);
                    if (m_friendly_name.friendly_name_size > 0)
                    {
                        m_friendly_name.is_name_set = true;
                    }

                    m_current_state.sub_state.auth_sub_state = ANTFS_AUTH_SUBSTATE_PAIR;
                    // If pairing is supported, send request to UI.
                    event_queue_write(ANTFS_EVENT_PAIRING_REQUEST);
                }
                break;

#endif // ANTFS_CONFIG_AUTH_TYPE_PAIRING_ENABLED
#if ANTFS_CONFIG_AUTH_TYPE_PASSKEY_ENABLED
            case COMMAND_TYPE_REQUEST_PASSKEY:
                if ((control_byte & SEQUENCE_NUMBER_ROLLOVER) == 0)
                {
                    // First burst packet.

                    // Passkey length.
                    const uint32_t passkey_size = p_command_buffer[AUTH_STRING_LENGTH_OFFSET];

                    // Default the algorithm to accept.
                    response = AUTH_RESPONSE_ACCEPT;

                    // Check if the passkey length is valid.
                    if (passkey_size == ANTFS_PASSKEY_SIZE)
                    {
                        m_passkey_index = 0;
                    }
                    else
                    {
                        // Invalid lenght supplied - the authentication will be rejected.
                        response = AUTH_RESPONSE_REJECT;
                    }
                }
                else
                {
                    // Next burst packets: read host friendly name.

                    if ((response == AUTH_RESPONSE_ACCEPT) &&
                        // Prevent buffer overrun.
                        (m_passkey_index != ANTFS_PASSKEY_SIZE))
                    {
                        // Passkey length was valid and the host supplied key matches so far.
                        uint32_t idx = 0;

                        // Check the current received burst packet for passkey match.
                        do
                        {
                            if (m_initial_parameters.p_pass_key[m_passkey_index++] !=
                                p_command_buffer[idx])
                            {
                                // Reject the authentication request and further processing of
                                // passkey matching if a mismatch is found.
                                response = AUTH_RESPONSE_REJECT;
                                break;
                            }

                            ++idx;
                        }
                        while (idx < BURST_PACKET_SIZE);
                    }
                }

                if (control_byte & SEQUENCE_LAST_MESSAGE)
                {
                    // Last burst packet.

                    if (m_passkey_index < ANTFS_PASSKEY_SIZE)
                    {
                        // We did not get the complete passkey, reject authentication request.
                        response = AUTH_RESPONSE_REJECT;
                    }

                    timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);
                    m_current_state.sub_state.auth_sub_state = ANTFS_AUTH_SUBSTATE_PASSKEY;
                    authenticate_response_transmit(response, 0, NULL);
                }
                break;

#endif // ANTFS_CONFIG_AUTH_TYPE_PASSKEY_ENABLED
            default:
                break;
        }
    }
    else if (p_command_buffer[ANTFS_COMMAND_OFFSET] == ANTFS_CMD_DISCONNECT_ID)
    {
        if (control_byte & SEQUENCE_LAST_MESSAGE)
        {
            // Don't do anything before the burst completes (last burst message received).
            link_layer_transit();
        }
    }
    else if (p_command_buffer[ANTFS_COMMAND_OFFSET] == ANTFS_CMD_PING_ID)
    {
        // Reset timeout.
        timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);
        m_link_command_in_progress = ANTFS_CMD_NONE;
    }
    else
    {
        // No implementation needed.
    }
}


/**@brief Function for decoding an ANT-FS command received at the transport layer.
 *
 * @param[in] control_byte     The command control byte.
 * @param[in] p_command_buffer The ANT-FS command buffer.
 */
static void transport_layer_cmd_decode(uint8_t control_byte, const uint8_t * p_command_buffer)
{
    ulong_union_t host_serial_number = {0};

    if (p_command_buffer[ANTFS_CONNECTION_TYPE_OFFSET] == ANTFS_COMMAND_ID)
    {
        m_link_command_in_progress = p_command_buffer[ANTFS_COMMAND_OFFSET];
    }

    switch (m_link_command_in_progress)
    {
        case ANTFS_CMD_PING_ID:
            // Reset timeout.
            timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);
            m_link_command_in_progress = ANTFS_CMD_NONE;
            break;

        case ANTFS_CMD_DISCONNECT_ID:
            if (control_byte & SEQUENCE_LAST_MESSAGE)
            {
                // Don't do anything before the burst completes (last burst message received).
                link_layer_transit();
            }
            break;

        case ANTFS_CMD_ERASE_ID:
            if (control_byte & SEQUENCE_LAST_MESSAGE)
            {
                // Don't do anything before the burst completes (last burst message received).

                // Requested index.
                m_file_index.bytes.low  = p_command_buffer[DATA_INDEX_OFFSET_LOW];
                m_file_index.bytes.high = p_command_buffer[DATA_INDEX_OFFSET_HIGH];

                // Send erase request to the application.
                event_queue_write(ANTFS_EVENT_ERASE_REQUEST);
                timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);
                m_link_command_in_progress = ANTFS_CMD_ERASE_ID;
            }
            break;

        case ANTFS_CMD_DOWNLOAD_ID:
            if (m_current_state.sub_state.trans_sub_state != ANTFS_TRANS_SUBSTATE_NONE)
            {
                // Ignore the command if we are busy.
                break;
            }

            if ((control_byte & ~SEQUENCE_LAST_MESSAGE) == 0x00)
            {
                // First burst packet.

                if ((m_file_index.bytes.low != p_command_buffer[DATA_INDEX_OFFSET_LOW]) ||
                    (m_file_index.bytes.high != p_command_buffer[DATA_INDEX_OFFSET_HIGH]))
                {
                    // This is a new index, so we can not check the CRC against the previous saved
                    // CRC.

                    // CRC seed checking is made invalid by setting the last saved offset to the
                    // maximum file size.
                    m_saved_crc_offset = ANTFS_MAX_FILE_SIZE;
                }

                // Requested data file index.
                m_file_index.bytes.low  = p_command_buffer[DATA_INDEX_OFFSET_LOW];
                m_file_index.bytes.high = p_command_buffer[DATA_INDEX_OFFSET_HIGH];

                // Initialize current position in the TX burst to the requested offset.
                m_link_burst_index.bytes.byte0 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_0];
                m_link_burst_index.bytes.byte1 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_1];
                m_link_burst_index.bytes.byte2 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_2];
                m_link_burst_index.bytes.byte3 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_3];
            }
            else if (control_byte & SEQUENCE_LAST_MESSAGE)
            {
                // Last burst packet (download command should be two packets long).

                // Get CRC seed from host.
                m_compared_crc  = (uint16_t)p_command_buffer[DATA_INDEX_OFFSET_LOW];
                m_compared_crc |= ((uint16_t)p_command_buffer[DATA_INDEX_OFFSET_HIGH] << 8u);

                // Maximum block size allowed by host.
                m_max_block_size.bytes.byte0 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_0];
                m_max_block_size.bytes.byte1 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_1];
                m_max_block_size.bytes.byte2 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_2];
                m_max_block_size.bytes.byte3 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_3];

                // Initialize number of remaining bytes for this block to the maximum block size.
                m_bytes_remaining.data = m_max_block_size.data;

                if (p_command_buffer[INITIAL_REQUEST_OFFSET])
                {
                    // This request is the start of a new transfer.

                    // Initialize data offset for CRC calculation to the requested data offset.
                    m_saved_crc_offset = m_link_burst_index.data;
                    m_saved_buffer_crc_offset = m_link_burst_index.data;

                    // Use CRC seed provided by host for CRC checking of the data.
                    m_transfer_crc                            = m_compared_crc;
                    m_saved_transfer_crc                      = m_compared_crc;
                    m_saved_buffer_crc                        = m_compared_crc;
                    m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_VERIFY_CRC;
                }
                else
                {
                    // This is a request to resume a partially completed transfer.

                    if (m_saved_crc_offset > m_link_burst_index.data)
                    {
                        // We can not check the received CRC seed as the requested offset is before
                        // our last save point.

                        // Set CRC checking as invalid.
                        m_saved_crc_offset = ANTFS_MAX_FILE_SIZE;
                    }
                    else
                    {
                        m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_VERIFY_CRC;
                    }
                }

                m_is_data_request_pending = false;

                // Send download request to the application for further handling.
                event_queue_write(ANTFS_EVENT_DOWNLOAD_REQUEST);

                timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);
                m_link_command_in_progress = ANTFS_CMD_DOWNLOAD_ID;
            }
            break;

        case ANTFS_CMD_UPLOAD_REQUEST_ID:
#if ANTFS_CONFIG_UPLOAD_ENABLED
            if ((control_byte & ~SEQUENCE_LAST_MESSAGE) == 0x00)
            {
                // First burst packet.

                if ((m_file_index.bytes.low != p_command_buffer[DATA_INDEX_OFFSET_LOW]) ||
                    (
                        (m_file_index.bytes.high != p_command_buffer[DATA_INDEX_OFFSET_HIGH]) ||
                        (m_current_state.sub_state.trans_sub_state == ANTFS_TRANS_SUBSTATE_NONE)
                    )
                   )
                {
                    // If it is a new index or we completed the last upload.

                    // Get the file index.
                    m_file_index.bytes.low  = p_command_buffer[DATA_INDEX_OFFSET_LOW];
                    m_file_index.bytes.high = p_command_buffer[DATA_INDEX_OFFSET_HIGH];

                    // As this is a new upload, reset save point to the beginning of the file.

                    // Set CRC to zero.
                    m_saved_crc_offset   = 0;
                    m_saved_transfer_crc = 0;
                }

                // Get the upper limit of upload from request message.
                m_max_transfer_index.bytes.byte0 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_0];
                m_max_transfer_index.bytes.byte1 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_1];
                m_max_transfer_index.bytes.byte2 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_2];
                m_max_transfer_index.bytes.byte3 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_3];
            }
            else if (control_byte & SEQUENCE_LAST_MESSAGE)
            {
                // Last burst (second) packet.

                // Get data offset the requested upload will start at.
                m_link_burst_index.bytes.byte0 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_0];
                m_link_burst_index.bytes.byte1 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_1];
                m_link_burst_index.bytes.byte2 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_2];
                m_link_burst_index.bytes.byte3 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_3];

                if (m_link_burst_index.data != ANTFS_MAX_FILE_SIZE)
                {
                    // If this is a new upload.

                    // The data offset specified in the upload request will be used.
                    m_saved_crc_offset = m_link_burst_index.data;

                    m_saved_transfer_crc = 0;
                }

                m_transfer_crc = m_saved_transfer_crc;

                // Send upload request to the application for further handling.
                event_queue_write(ANTFS_EVENT_UPLOAD_REQUEST);

                timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);
                m_link_command_in_progress = ANTFS_CMD_UPLOAD_REQUEST_ID;
            }
#endif  // ANTFS_CONFIG_UPLOAD_ENABLED
            break;

        case ANTFS_CMD_UPLOAD_DATA_ID:
#if ANTFS_CONFIG_UPLOAD_ENABLED
            if ((control_byte & ~SEQUENCE_LAST_MESSAGE) == 0x00)
            {
                // First burst packet.

                if (m_current_state.sub_state.trans_sub_state ==
                    ANTFS_TRANS_SUBSTATE_UPLOAD_WAIT_FOR_DATA)
                {
                    antfs_event_t event;

                    // Get CRC Seed from host.
                    m_compared_crc  = (uint16_t)p_command_buffer[DATA_INDEX_OFFSET_LOW];
                    m_compared_crc |= ((uint16_t)p_command_buffer[DATA_INDEX_OFFSET_HIGH] << 8u);

                    // Set download offset.
                    m_link_burst_index.bytes.byte0 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_0];
                    m_link_burst_index.bytes.byte1 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_1];
                    m_link_burst_index.bytes.byte2 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_2];
                    m_link_burst_index.bytes.byte3 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_3];

                    if ((m_link_burst_index.data + m_block_size.data) < m_max_transfer_index.data)
                    {
                        // Adjust block size as set by client.
                        m_max_transfer_index.data = m_link_burst_index.data + m_block_size.data;
                    }

                    if (m_compared_crc != m_transfer_crc)
                    {
                        // Check that the request matches the CRC sent on the upload response.

                        // Do not accept any data.
                        m_max_transfer_index.data = 0;

                        // Failure will be reported when upload is done.
                        event = (antfs_event_t)0;
                    }

                    // Set ready to receive a file.
                    m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_UPLOADING;

                    event          = ANTFS_EVENT_UPLOAD_START;
                    m_transfer_crc = m_compared_crc;

                    if (m_link_burst_index.data > m_max_transfer_index.data)
                    {
                        // If the requested offset is too high.

                        // Clear the max transfer index, so we'll report fail when the transfer
                        // finishes.
                        m_max_transfer_index.data = 0;
                        // Clear the event because we normally would not send an event at this point
                        // in this case.
                        event = (antfs_event_t)0;
                    }

                    if (control_byte & SEQUENCE_LAST_MESSAGE)
                    {
                        // If this upload contains no data.

                        // Leave the upload state.
                        m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_NONE;

                        // if it was a valid index, report it as a successful upload, otherwise
                        // report it as a failure.
                        if (event == 0)
                        {
                            event = ANTFS_EVENT_UPLOAD_FAIL;
                        }
                        else
                        {
                            event = ANTFS_EVENT_UPLOAD_COMPLETE;
                        }
                    }

                    if (event != 0)
                    {
                        event_queue_write(event);
                    }
                }
            }
#endif // ANTFS_CONFIG_UPLOAD_ENABLED
            break;

        case ANTFS_CMD_LINK_ID:
            host_serial_number.bytes.byte0 = p_command_buffer[HOST_ID_OFFSET_0];
            host_serial_number.bytes.byte1 = p_command_buffer[HOST_ID_OFFSET_1];
            host_serial_number.bytes.byte2 = p_command_buffer[HOST_ID_OFFSET_2];
            host_serial_number.bytes.byte3 = p_command_buffer[HOST_ID_OFFSET_3];

            if (m_link_host_serial_number.data == host_serial_number.data)
            {
                m_active_beacon_frequency = p_command_buffer[TRANSPORT_CHANNEL_FREQUENCY_OFFSET];
                m_active_beacon_status1_field.parameters.link_period =
                    p_command_buffer[TRANSPORT_MESSAGE_PERIOD_OFFSET];

                const uint32_t err_code = sd_ant_channel_radio_freq_set(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                                        m_active_beacon_frequency);
                APP_ERROR_CHECK(err_code);

                channel_period_set(m_active_beacon_status1_field.parameters.link_period);
            }

            m_link_command_in_progress = 0;
            break;

        default:
            // Don't do anything, this is an invalid message.
            m_link_command_in_progress = 0;
            break;
    }
}


/**@brief Function for handling data upload.
 *
 * @param[in] control_byte     The command control byte.
 * @param[in] p_buffer         The data buffer.
 */
static void upload_data_process(uint8_t control_byte, const uint8_t * p_buffer)
{
#if ANTFS_CONFIG_UPLOAD_ENABLED
    if (control_byte & SEQUENCE_LAST_MESSAGE)
    {
        // Last burst packet: upload complete.

        m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_NONE;

        // CRC for data packets contained in this upload block.
        m_compared_crc  = p_buffer[UPLOAD_CRC_OFFSET_LOW ];
        m_compared_crc |= (p_buffer[UPLOAD_CRC_OFFSET_HIGH] << 8u);

        if (m_max_transfer_index.data && (m_compared_crc == m_transfer_crc))
        {
            // CRC OK, upload was completed successfully.
            event_queue_write(ANTFS_EVENT_UPLOAD_COMPLETE);
        }
        else
        {
            // CRC mismatch, upload failed.
            event_queue_write(ANTFS_EVENT_UPLOAD_FAIL);
        }

        m_max_transfer_index.data = 0;
    }
    else
    {
        // Not the last burst packet: upload not complete.

        // Set initial number of bytes to 8 (size of burst packet).
        m_bytes_to_write = BURST_PACKET_SIZE;
        timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);

        if (m_link_burst_index.data > m_max_transfer_index.data)
        {
            // We are past the main index, we do not need to write any more data.
            m_bytes_to_write = 0;
        }
        else
        {
            if ((m_bytes_to_write + m_link_burst_index.data) > m_max_transfer_index.data)
            {
                // if we're less than 8 bytes away from the end, adjust the number of bytes to write
                // in this block.
                m_bytes_to_write = m_max_transfer_index.data - m_link_burst_index.data;
            }
        }

        if (m_bytes_to_write != 0)
        {
            APP_ERROR_CHECK_BOOL(m_bytes_to_write <= BURST_PACKET_SIZE);

            // Store begin of upload data.
            mp_upload_data = p_buffer;

            m_transfer_crc = crc_crc16_update(m_transfer_crc, p_buffer, m_bytes_to_write);

            // Send data to application.
            event_queue_write(ANTFS_EVENT_UPLOAD_DATA);

            // Update current offset.
            m_link_burst_index.data += m_bytes_to_write;

            // Store save point.
            m_saved_crc_offset       = m_link_burst_index.data;
            m_saved_transfer_crc     = m_transfer_crc;
        }
    }
#endif // ANTFS_CONFIG_UPLOAD_ENABLED
}


/**@brief Function for switching to transport layer.
 */
static void transport_layer_transit(void)
{
    if (m_current_state.state != ANTFS_STATE_OFF)
    {
#if ANTFS_CONFIG_DEBUG_LED_ENABLED
        uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
        APP_ERROR_CHECK(err_code);
#endif // ANTFS_CONFIG_DEBUG_LED_ENABLED
        m_current_state.state                     = ANTFS_STATE_TRANS;
        m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_NONE;

        timeout_start(ANTFS_CONFIG_LINK_COMMAND_TIMEOUT);

        beacon_transmit(MESG_BROADCAST_DATA_ID);

        event_queue_write(ANTFS_EVENT_TRANS);
    }
}


void antfs_message_process(uint8_t * p_message)
{
#if ANTFS_CONFIG_DEBUG_LED_ENABLED
    uint32_t err_code;
#endif // ANTFS_CONFIG_DEBUG_LED_ENABLED

    if (p_message != NULL)
    {
        if ((p_message[BUFFER_INDEX_CHANNEL_NUM] & CHANNEL_NUMBER_MASK) != ANTFS_CONFIG_CHANNEL_NUMBER)
        {
            // Only process messages corresponding to the ANT-FS channel here.
            return;
        }

        if ((m_current_state.state == ANTFS_STATE_OFF) &&
            (
                !(
                    (p_message[BUFFER_INDEX_MESG_ID] == MESG_RESPONSE_EVENT_ID) &&
                    (p_message[BUFFER_INDEX_RESPONSE_CODE] == NO_EVENT)
                  )
            )
           )
        {
            return;
        }

        switch (p_message[BUFFER_INDEX_MESG_ID])
        {
            case MESG_BROADCAST_DATA_ID:
                // We are not going to process broadcast messages or pass them to the app to handle.
                break;

            case MESG_ACKNOWLEDGED_DATA_ID:
                // Mark it as being the last message if it's an ack message.
                p_message[ANTFS_CONTROL_OFFSET] |= SEQUENCE_LAST_MESSAGE;

            /* fall-through */
            case MESG_BURST_DATA_ID:
                switch (m_current_state.state)
                {
                    case ANTFS_STATE_LINK:
                        link_layer_cmd_decode(&p_message[ANTFS_DATA_OFFSET]);
                        break;

                    case ANTFS_STATE_AUTH:
                        authenticate_layer_cmd_decode(p_message[ANTFS_CONTROL_OFFSET],
                                                      &p_message[ANTFS_DATA_OFFSET]);
                        break;

                    case ANTFS_STATE_TRANS:
                        if (m_current_state.sub_state.trans_sub_state !=
                            ANTFS_TRANS_SUBSTATE_UPLOADING)
                        {
                            transport_layer_cmd_decode(p_message[ANTFS_CONTROL_OFFSET],
                                                       &p_message[ANTFS_DATA_OFFSET]);
                        }
                        else
                        {
                            upload_data_process(p_message[ANTFS_CONTROL_OFFSET],
                                                &p_message[ANTFS_DATA_OFFSET]);
                        }
                        break;

                    default:
                        // If in any other state or sub-state, do nothing.
                        break;
                }
                break;

            case MESG_RESPONSE_EVENT_ID:
                // Branch on event ID.
                switch (p_message[BUFFER_INDEX_RESPONSE_CODE])
                {
                    case EVENT_TRANSFER_TX_FAILED:
                        m_link_command_in_progress = ANTFS_CMD_NONE;
                        // Switch into the appropriate state after the failure. Must be ready for
                        // the host to do a retry.
                        switch (m_current_state.state)
                        {
                            case ANTFS_STATE_LINK:
                                link_layer_transit();
                                break;

                            case ANTFS_STATE_AUTH:
                                // Burst failed, retry sending the response
                                if (!m_retry)
                                {
                                    authenticate_layer_transit(); // Reload beacon
                                }
                                else
                                {
                                    if (m_current_state.sub_state.auth_sub_state == ANTFS_AUTH_SUBSTATE_ACCEPT)
                                    {
                                        if (m_authenticate_command_type == COMMAND_TYPE_REQUEST_PAIR)
                                        {
                                            authenticate_response_transmit(AUTH_RESPONSE_ACCEPT, ANTFS_PASSKEY_SIZE,
                                               m_initial_parameters.p_pass_key);
                                        }
                                        else
                                        {
                                            authenticate_response_transmit(AUTH_RESPONSE_ACCEPT, 0, NULL);
                                        }
                                    }
                                    else if (m_current_state.sub_state.auth_sub_state == ANTFS_AUTH_SUBSTATE_REJECT)
                                    {
                                        authenticate_response_transmit(AUTH_RESPONSE_REJECT, 0, NULL);
                                    }
                                    else if (m_authenticate_command_type == COMMAND_TYPE_REQUEST_SERIAL)
                                    {
                                        authenticate_response_transmit(AUTH_RESPONSE_N_A,
                                                           ANTFS_REMOTE_FRIENDLY_NAME_MAX,
                                                           // Send device friendly name if it exists.
                                                           m_initial_parameters.p_remote_friendly_name);
                                    }
                                    else
                                    {
                                        // No implementation needed
                                    }

                                    m_retry--;
                                }

                                break;

                            case ANTFS_STATE_TRANS:
                                if (m_current_state.sub_state.trans_sub_state ==
                                    ANTFS_TRANS_SUBSTATE_DOWNLOADING)
                                {
                                    event_queue_write(ANTFS_EVENT_DOWNLOAD_FAIL);
                                }
                                transport_layer_transit();
                                break;

                            default:
                                // No implementation needed.
                                break;
                        }
                        break;

                    case EVENT_TRANSFER_RX_FAILED:
                        m_link_command_in_progress = ANTFS_CMD_NONE;

                        if (m_current_state.sub_state.trans_sub_state ==
                            ANTFS_TRANS_SUBSTATE_UPLOADING)
                        {
                            event_queue_write(ANTFS_EVENT_UPLOAD_FAIL);

                            m_current_state.sub_state.trans_sub_state =
                                ANTFS_TRANS_SUBSTATE_UPLOAD_RESUME;
                        }
                        else
                        {
                            // No implementation needed
                        }

                        break;

                    case EVENT_TRANSFER_TX_COMPLETED:
                        m_link_command_in_progress = ANTFS_CMD_NONE;

                        // Switch into appropiate state after successful command.
                        switch (m_current_state.state)
                        {
                            case ANTFS_STATE_AUTH:
                                if (m_current_state.sub_state.auth_sub_state ==
                                    ANTFS_AUTH_SUBSTATE_ACCEPT)
                                {
                                    // We passed authentication, so go to transport state.
                                    transport_layer_transit();
                                }
                                else if (m_current_state.sub_state.auth_sub_state ==
                                         ANTFS_AUTH_SUBSTATE_REJECT)
                                {
                                    // We failed authentication, so go to link state.
                                    link_layer_transit();
                                }
                                else
                                {
                                    // Reload beacon.
                                    authenticate_layer_transit();
                                }
                                break;

                            case ANTFS_STATE_TRANS:
                                if (m_current_state.sub_state.trans_sub_state ==
                                    ANTFS_TRANS_SUBSTATE_DOWNLOADING)
                                {
                                    event_queue_write(ANTFS_EVENT_DOWNLOAD_COMPLETE);
                                }
                                if (m_current_state.sub_state.trans_sub_state !=
                                    ANTFS_TRANS_SUBSTATE_UPLOAD_WAIT_FOR_DATA)
                                {
                                    transport_layer_transit();  // Reload beacon.
                                }
                                break;

                            default:
                                link_layer_transit();       // Reload beacon.
                                break;
                        }
                        break;

                    case EVENT_TX:
#if ANTFS_CONFIG_DEBUG_LED_ENABLED
                        err_code = bsp_indication_set(BSP_INDICATE_SENT_OK);
                        APP_ERROR_CHECK(err_code);
#endif // ANTFS_CONFIG_DEBUG_LED_ENABLED
                        // Load beacon.
                        beacon_transmit(MESG_BROADCAST_DATA_ID);
                        break;

                    case EVENT_CHANNEL_CLOSED:
                        event_queue_write(ANTFS_EVENT_CLOSE_COMPLETE);
                        break;

                    case NO_EVENT:
                        // This shouldn't happen... command responses should not occur.
                        APP_ERROR_HANDLER(p_message[BUFFER_INDEX_RESPONSE_CODE]);
                        break;

                    default:
                        // No implementation needed.
                        return;
                }
                break;

            default:
                // No implementation needed.
                return;
        }
    }
}


void antfs_channel_setup(void)
{
    // Start channel configuration.
    uint32_t err_code = ant_fs_key_set(ANTFS_CONFIG_NETWORK_NUMBER);
    APP_ERROR_CHECK(err_code);

    err_code = sd_ant_channel_assign(ANTFS_CONFIG_CHANNEL_NUMBER,
                                     ANTFS_CHANNEL_TYPE,
                                     ANTFS_CONFIG_NETWORK_NUMBER,
                                     0);
    APP_ERROR_CHECK(err_code);

    // Use the lower 2 bytes of the ESN for device number.
    uint16_t device_number = (uint16_t)(m_initial_parameters.client_serial_number & 0x0000FFFFu);
    if (device_number == 0)
    {
        // Device number of 0 is not allowed.
        device_number = 2;
    }

    err_code = sd_ant_channel_id_set(ANTFS_CONFIG_CHANNEL_NUMBER,
                                     device_number,
                                     ANTFS_CONFIG_DEVICE_TYPE,
                                     ANTFS_CONFIG_TRANS_TYPE);
    APP_ERROR_CHECK(err_code);

    // Remain in initialization state until channel is open.
    m_current_state.state = ANTFS_STATE_INIT;
    // @note: Channel frequency is set by function below.
    link_layer_transit();
    m_current_state.state = ANTFS_STATE_INIT;

    channel_period_set(m_active_beacon_status1_field.parameters.link_period);

    err_code = sd_ant_channel_open(ANTFS_CONFIG_CHANNEL_NUMBER);
    APP_ERROR_CHECK(err_code);

    err_code = sd_ant_channel_radio_tx_power_set(ANTFS_CONFIG_CHANNEL_NUMBER,
                                                 ANTFS_CONFIG_TRANSMIT_POWER,
                                                 ANTFS_CONFIG_CUSTOM_TRANSMIT_POWER);
    APP_ERROR_CHECK(err_code);

    m_current_state.state                    = ANTFS_STATE_LINK;
    m_current_state.sub_state.link_sub_state = ANTFS_LINK_SUBSTATE_NONE;

    event_queue_write(ANTFS_EVENT_OPEN_COMPLETE);

    // Start beacon broadcast.
    beacon_transmit(MESG_BROADCAST_DATA_ID);
}


/**@brief Function for resetting the ANT-FS state machine.
 */
static void state_machine_reset(void)
{
    m_current_state.state      = ANTFS_STATE_OFF;
    m_link_command_in_progress = ANTFS_CMD_NONE;

    timeout_disable();

    // Reset the ANT-FS event queue.
    m_event_queue.p_queue = m_event_queue_buffer;
    m_event_queue.head    = 0;
    m_event_queue.tail    = 0;

    // Set as invalid.
    m_authenticate_command_type = 0xFFu;
    m_retry = 0;

    m_saved_crc_offset        = 0xFFFFFFFFu;
    m_max_transfer_index.data = 0;
    m_is_crc_pending          = false;
    m_is_data_request_pending = false;

    m_friendly_name.is_name_set = false;
    m_friendly_name.index       = 0;

    memset(m_friendly_name.friendly_name, 0, ANTFS_FRIENDLY_NAME_MAX);
}


/**@brief Function for ANT-FS timer event.
 *
 * Handles pairing and command timeouts.
 *
 * @param[in] p_context        The callback context.
 */
static void timeout_handle(void * p_context)
{
    if (m_current_state.state == ANTFS_STATE_OFF)
    {
        return;
    }

    if ((m_current_state.state == ANTFS_STATE_AUTH) &&
        // Pairing timeout.
        (m_current_state.sub_state.auth_sub_state == ANTFS_AUTH_SUBSTATE_PAIR))
    {
        // Reject authentication request and send pairing timeout event.
        authenticate_response_transmit(AUTH_RESPONSE_REJECT, 0, NULL);
        event_queue_write(ANTFS_EVENT_PAIRING_TIMEOUT);
    }

    // Fall back to link layer when an ANT-FS event times out.
    link_layer_transit();
}


void antfs_init(const antfs_params_t * const p_params,
                antfs_burst_wait_handler_t burst_wait_handler)
{
    m_initial_parameters          = *p_params;
    m_burst_wait_handler          = burst_wait_handler;
    m_active_beacon_status1_field = m_initial_parameters.beacon_status_byte1;

    uint32_t err_code = app_timer_create(&m_timer_id, APP_TIMER_MODE_SINGLE_SHOT, timeout_handle);
    APP_ERROR_CHECK(err_code);

    state_machine_reset();

    err_code = sd_ant_burst_handler_wait_flag_enable((uint8_t *)(&m_burst_wait));
    APP_ERROR_CHECK(err_code);
}
#endif // NRF_MODULE_ENABLED(ANTFS)