ble_serialization.h 55.0 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
/**
 * Copyright (c) 2013 - 2017, Nordic Semiconductor ASA
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 * 
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 * 
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 * 
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
#ifndef BLE_SERIALIZATION_H__
#define BLE_SERIALIZATION_H__

#include "nordic_common.h"
#include "nrf_error.h"
#include <stdint.h>
#include <stddef.h>
#include "cond_field_serialization.h"

#ifdef __cplusplus
extern "C" {
#endif

/**@brief Types of serialization packets. */
typedef enum
{
    SER_PKT_TYPE_CMD = 0,     /**< Command packet type. */
    SER_PKT_TYPE_RESP,        /**< Command Response packet type. */
    SER_PKT_TYPE_EVT,         /**< Event packet type. */
    SER_PKT_TYPE_DTM_CMD,     /**< DTM Command packet type. */
    SER_PKT_TYPE_DTM_RESP,    /**< DTM Response packet type. */
    SER_PKT_TYPE_RESET_CMD,   /**< System Reset Command packet type. */
#if defined(ANT_STACK_SUPPORT_REQD)
    SER_PKT_TYPE_ANT_CMD,     /**< ANT Command packet type. */
    SER_PKT_TYPE_ANT_RESP,    /**< ANT Response packet type. */
    SER_PKT_TYPE_ANT_EVT,     /**< ANT Event packet type. */
#endif
    SER_PKT_TYPE_MAX          /**< Upper bound. */
} ser_pkt_type_t;

#define  LOW16(a) ((uint16_t)((a & 0x0000FFFF) >> 0))
#define HIGH16(a) ((uint16_t)((a & 0xFFFF0000) >> 16))

//lint -esym(516,__INTADDR__) Symbol '__INTADDR__()' has arg. type conflict
//lint -esym(628,__INTADDR__) no argument information provided for function '__INTADDR__()'

/** Size in bytes of the Error Code field in a Command Response packet. */
#define SER_ERR_CODE_SIZE              4
/** Size in bytes of the Packet Type field (@ref ser_pkt_type_t). */
#define SER_PKT_TYPE_SIZE              1
/** Size in bytes of the Operation Code field. */
#define SER_OP_CODE_SIZE               1

/** Position of the Packet Type field in a serialized packet buffer. */
#define SER_PKT_TYPE_POS               0
/** Position of the Operation Code field in a serialized packet buffer. */
#define SER_PKT_OP_CODE_POS            (SER_PKT_TYPE_SIZE)
/** Position of the Data in a serialized packet buffer. */
#define SER_PKT_DATA_POS               (SER_PKT_TYPE_SIZE + SER_OP_CODE_SIZE)

/** Position of the Operation Code field in a command buffer. */
#define SER_CMD_OP_CODE_POS            0
/** Position of the Data in a command buffer.*/
#define SER_CMD_DATA_POS               (SER_OP_CODE_SIZE)
/** Size of the Command header. */
#define SER_CMD_HEADER_SIZE            (SER_OP_CODE_SIZE)
/** Size of the Command Response header. */
#define SER_CMD_RSP_HEADER_SIZE        (SER_OP_CODE_SIZE + SER_ERR_CODE_SIZE)
/** Position of the Command Response code. */
#define SER_CMD_RSP_STATUS_CODE_POS    (SER_OP_CODE_SIZE)

/** Size of event ID field. */
#define SER_EVT_ID_SIZE                2
/** Position of event ID field. */
#define SER_EVT_ID_POS                 0
/** Size of event header. */
#define SER_EVT_HEADER_SIZE            (SER_EVT_ID_SIZE)
/** Size of event connection handler. */
#define SER_EVT_CONN_HANDLE_SIZE       2

#if defined(ANT_STACK_SUPPORT_REQD)
/** Size of event ID field. */
#define SER_ANT_EVT_ID_SIZE            2
/** Position of event ID field. */
#define SER_ANT_EVT_ID_POS             0
/** Size of event header. */
#define SER_ANT_EVT_HEADER_SIZE        (SER_ANT_EVT_ID_SIZE)
#endif

/** Position of the Op Code in the DTM command buffer.*/
#define SER_DTM_CMD_OP_CODE_POS        0
/** Position of the data in the DTM command buffer.*/
#define SER_DTM_CMD_DATA_POS           1

/** Position of the Op Code in the DTM command response buffer.*/
#define SER_DTM_RESP_OP_CODE_POS       1
/** Position of the status field in the DTM command response buffer.*/
#define SER_DTM_RESP_STATUS_POS        2

/** Value to indicate that an optional field is encoded in the serialized packet, e.g. white list.*/
#define SER_FIELD_PRESENT              0x01
/** Value to indicate that an optional field is not encoded in the serialized packet. */
#define SER_FIELD_NOT_PRESENT          0x00


/** Enable SER_ASSERT<*> asserts */
#define SER_ASSERTS_ENABLED 1

/** Returns with error code if expr is not true. It is used for checking error which should be
 * checked even when SER_ASSERTS_ENABLED is not set. */
#define SER_ERROR_CHECK(expr, error_code) do { if (!(expr)) return (error_code); } while (0)


#ifdef SER_ASSERTS_ENABLED
/** Returns with error code if expr is not true. */
#define SER_ASSERT(expr, error_code) SER_ERROR_CHECK(expr, error_code)
/** Returns if expression is not true. */
#define SER_ASSERT_VOID_RETURN(expr) do { if (!(expr)) return; } while (0)
/** Returns with  \ref NRF_ERROR_INVALID_LENGTH if len is not less or equal to maxlen. */
#define SER_ASSERT_LENGTH_LEQ(len, maxlen) \
    SER_ASSERT((len) <= (maxlen), NRF_ERROR_INVALID_LENGTH)
/** Returns with  \ref NRF_ERROR_INVALID_LENGTH if actual_len is not equal to expected_len. */
#define SER_ASSERT_LENGTH_EQ(actual_len, expected_len) \
    SER_ASSERT((actual_len) == (expected_len), NRF_ERROR_INVALID_LENGTH)
/** Returns with  \ref NRF_ERROR_NULL if pointer is null. */
#define SER_ASSERT_NOT_NULL(ptr) SER_ASSERT((ptr) != NULL, NRF_ERROR_NULL)
#else
#define SER_ASSERT(expr, error_code)
#define SER_ASSERT_VOID_RETURN(expr)
#define SER_ASSERT_LENGTH_LEQ(len, maxlen) UNUSED_VARIABLE(maxlen)
#define SER_ASSERT_LENGTH_EQ(actual_len, expected_len)
#define SER_ASSERT_NOT_NULL(ptr)
#endif

#if  defined(BLE_GATT_MTU_SIZE_DEFAULT) && !defined(GATT_MTU_SIZE_DEFAULT)
#define GATT_MTU_SIZE_DEFAULT BLE_GATT_MTU_SIZE_DEFAULT
#endif
/** Maximum length of p_value in \ref ble_gattc_write_params_t. See Bluetooth 4.0 spec: 3.4.5.1 and
 *  3.4.5.3. */
#define BLE_GATTC_WRITE_P_VALUE_LEN_MAX    (GATT_MTU_SIZE_DEFAULT - 3)

/** See Bluetooth 4.0 spec: 3.4.4.7. */
#define BLE_GATTC_HANDLE_COUNT_LEN_MAX     ((GATT_MTU_SIZE_DEFAULT - 1) / 2)

/** Subtract 1 from X if X is greater than 0. */
#define SUB1(X) (((X)>0) ? ((X)-1) : (X))

static inline void static_force_impl_castable_p_void(void const * const p) {}
/** Force the argument to be a double pointer. */
#define STATIC_FORCE_PP(PP) static_force_impl_castable_p_void(*(PP))

/** Field decoder for special structures containing variable length data. */
typedef uint32_t (*field_ext_decoder_handler_t)(uint8_t const * const p_buf,
                                                uint32_t              buf_len,
                                                uint32_t * const      p_index,
                                                uint32_t * const      p_ext_length,
                                                void *                p_field);


/** Push uint8_t field into the output packet. */
#define SER_PUSH_uint8(P_VAR) do { \
                err_code = uint8_t_enc((P_VAR), p_buf, buf_len, p_index); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);            \
            } while (0)

/** Push uint16_t field into the output packet. */
#define SER_PUSH_uint16(P_VAR) do { \
                err_code = uint16_t_enc((P_VAR), p_buf, buf_len, p_index); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);             \
            } while (0)

/** Push uint32_t field into the output packet. */
#define SER_PUSH_uint32(P_VAR) do { \
                err_code = uint32_t_enc((P_VAR), p_buf, buf_len, p_index); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);             \
            } while (0)

/** Push int8_t field into the output packet. */
#define SER_PUSH_int8(P_VAR)  SER_PUSH_uint8(P_VAR)

/** Push uint16_t field into the output packet. */
#define SER_PUSH_int16(P_VAR) SER_PUSH_uint16(P_VAR)

/** Push uint32_t field into the output packet. */
#define SER_PUSH_int32(P_VAR) SER_PUSH_uint32(P_VAR)

/** Push a constant length array of bytes into the output packet. */
#define SER_PUSH_uint8array(P_DATA, LEN) do { \
                err_code = uint8_vector_enc((P_DATA), (LEN), p_buf, buf_len, p_index); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                         \
            } while (0)

/** Push a variable length (8-bit) array of bytes into the output packet. */
#define SER_PUSH_len8data(P_DATA, LEN) do { \
                err_code = len8data_enc((P_DATA), (LEN), p_buf, buf_len, p_index); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                     \
            } while (0)

/** Push a variable length (16-bit) array of bytes into the output packet. */
#define SER_PUSH_len16data(P_DATA, LEN) do { \
                err_code = len16data_enc((P_DATA), (LEN), p_buf, buf_len, p_index); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                      \
            } while (0)

/** Push a variable length (16-bit) array of 16-bit words into the output packet. */
#define SER_PUSH_len16data16(P_DATA, LEN) do { \
                err_code = count16_cond_data16_enc((P_DATA), (LEN), p_buf, buf_len, p_index); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                                \
            } while (0)

/** Push a buffer into the output packet. */
#define SER_PUSH_buf(P_DATA, LEN) do { \
                err_code = buf_enc((P_DATA), (LEN), p_buf, buf_len, p_index); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                \
            } while (0)

/** Push a structure into the output packet. */
#define SER_PUSH_FIELD(P_VAR, P_ENCODER) do { \
                err_code = field_enc((P_VAR), p_buf, buf_len, p_index, (P_ENCODER)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                       \
            } while (0)

/** Push an array of structures into the output packet. */
#define SER_PUSH_FIELD_ARRAY(P_ARRAY, P_ENCODER, COUNT) do { \
                for (uint32_t _idx = 0; _idx < (COUNT); ++_idx)  \
                {                                                \
                    SER_PUSH_FIELD(&((P_ARRAY)[_idx]),P_ENCODER);\
                }                                                \
            } while (0)

/** Conditionally push a field if the specified pointer is not null. */
#define SER_PUSH_COND(P_VAR, P_ENCODER) do { \
                err_code = cond_field_enc((P_VAR), p_buf, buf_len, p_index, (P_ENCODER)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                            \
            } while (0)


/** Pull a uint8_t field from the input packet. */
#define SER_PULL_uint8(P_VAR) do { \
                err_code = uint8_t_dec(p_buf, packet_len, p_index, (P_VAR)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);               \
            } while (0)

/** Pull a uint16_t field from the input packet. */
#define SER_PULL_uint16(P_VAR) do { \
                err_code = uint16_t_dec(p_buf, packet_len, p_index, (P_VAR)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                \
            } while (0)

/** Pull a uint32_t field from the input packet. */
#define SER_PULL_uint32(P_VAR) do { \
                err_code = uint32_t_dec(p_buf, packet_len, p_index, (P_VAR)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                \
            } while (0)

/** Pull an int8_t field from the input packet. */
#define SER_PULL_int8(P_VAR)  SER_PULL_uint8(P_VAR)

/** Pull an int16_t field from the input packet. */
#define SER_PULL_int16(P_VAR) SER_PULL_uint16(P_VAR)

/** Pull an int32_t field from the input packet. */
#define SER_PULL_int32(P_VAR) SER_PULL_uint32(P_VAR)

/** Pull a constant length byte array from the input packet. */
#define SER_PULL_uint8array(P_DATA, LEN) do { \
                err_code = uint8_vector_dec(p_buf, packet_len, p_index, (P_DATA), (LEN)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                            \
            } while (0)

/** Pull a variable length (8-bit) byte array from the input packet. */
#define SER_PULL_len8data(PP_DATA, LEN) do { \
                STATIC_FORCE_PP(PP_DATA);                                              \
                err_code = len8data_dec(p_buf, packet_len, p_index, (PP_DATA), (LEN)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                         \
            } while (0)

/** Pull a variable length (16-bit) byte array from the input packet. */
#define SER_PULL_len16data(PP_DATA, P_LEN) do { \
                STATIC_FORCE_PP(PP_DATA);                                                 \
                err_code = len16data_dec(p_buf, packet_len, p_index, (PP_DATA), (P_LEN)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                            \
            } while (0)

/** Pull a variable length (16-bit) word (16-bit) array from the input packet. */
#define SER_PULL_len16data16(PP_DATA, P_LEN) do { \
                STATIC_FORCE_PP(PP_DATA);                                                           \
                err_code = count16_cond_data16_dec(p_buf, packet_len, p_index, (PP_DATA), (P_LEN)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                                      \
            } while (0)

/** Pull a buffer from the input packet. */
#define SER_PULL_buf(PP_DATA, OUT_BUF_LEN, LEN) do { \
                STATIC_FORCE_PP(PP_DATA);                                                        \
                err_code = buf_dec(p_buf, packet_len, p_index, (PP_DATA), (OUT_BUF_LEN), (LEN)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                                   \
            } while (0)

/** Pull a structure from the input packet. */
#define SER_PULL_FIELD(P_VAR, P_DECODER) do { \
                err_code = field_dec(p_buf, packet_len, p_index, (P_VAR), (P_DECODER)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                          \
            } while (0)

/** Pull an array of structures from the input packet. */
#define SER_PULL_FIELD_ARRAY(P_ARRAY, P_DECODER, COUNT) do { \
                for (uint32_t _idx = 0; _idx < (COUNT); ++_idx)    \
                {                                                  \
                    SER_PULL_FIELD(&((P_ARRAY)[_idx]),P_DECODER);  \
                }                                                  \
            } while (0)

/** Conditionally pull a structure from the input packet. */
#define SER_PULL_COND(PP_VAR, P_DECODER) do { \
                STATIC_FORCE_PP(PP_VAR);                                                                \
                err_code = cond_field_dec(p_buf, packet_len, p_index, (void * *)(PP_VAR), (P_DECODER)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);                                          \
            } while (0)


/** Start the encoding of a structure and prepare local variables for the usage of SER_PUSH_ macros. */
#define SER_STRUCT_ENC_BEGIN(STRUCT_TYPE)                           \
            SER_ASSERT_NOT_NULL(p_buf);                             \
            SER_ASSERT_NOT_NULL(p_index);                           \
            SER_ASSERT_NOT_NULL(p_void_struct);                     \
            STRUCT_TYPE * p_struct = (STRUCT_TYPE *) p_void_struct; \
            uint32_t err_code = NRF_SUCCESS

/** End the encoding of a structure. */
#define SER_STRUCT_ENC_END return err_code

/** Start the decoding of a structure and prepare local variables for the usage of SER_PULL_ macros. */
#define SER_STRUCT_DEC_BEGIN(STRUCT_TYPE)                           \
            SER_ASSERT_NOT_NULL(p_buf);                             \
            SER_ASSERT_NOT_NULL(p_index);                           \
            SER_ASSERT_NOT_NULL(p_void_struct);                     \
            STRUCT_TYPE * p_struct = (STRUCT_TYPE *) p_void_struct; \
            uint32_t err_code = NRF_SUCCESS;                        \
            uint32_t packet_len = buf_len

/** End the encoding of a structure. */
#define SER_STRUCT_DEC_END return err_code


/** Start the encoding of command request and prepare local variables for the usage of SER_PUSH_ macros. */
#define SER_REQ_ENC_BEGIN(OPCODE)              \
            SER_ASSERT_NOT_NULL(p_buf);        \
            SER_ASSERT_NOT_NULL(p_buf_len);    \
                                               \
            uint32_t index    = 0;             \
            uint32_t * const p_index = &index; \
            uint32_t err_code = NRF_SUCCESS;   \
            uint32_t buf_len  = *p_buf_len;    \
                                               \
            uint8_t  opcode   = (OPCODE);      \
            SER_PUSH_uint8(&opcode)

/** End the encoding of command request. */
#define SER_REQ_ENC_END    \
            *p_buf_len = index; \
            return NRF_SUCCESS  \

/** Start the decoding of command response that does not contain any data except the result code. */
#define SER_RSP_DEC_RESULT_ONLY(OPCODE) \
            return ser_ble_cmd_rsp_dec(p_buf, packet_len, (OPCODE), p_result_code)

/** Start the decoding of command response and prepare local variables for the usage of SER_PULL_ macros. */
#define SER_RSP_DEC_BEGIN(OPCODE)                           \
            SER_ASSERT_NOT_NULL(p_buf);                     \
            SER_ASSERT_NOT_NULL(p_result_code);             \
            uint32_t err_code = NRF_SUCCESS;                \
            uint32_t index    = 0;                          \
            uint32_t * const  p_index = &index;             \
            /* Decode the result code and exit if decoding has failed or \
               the decoded result is not NRF_SUCCESS. */        \
            err_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, (OPCODE), p_result_code); \
                                                            \
            SER_ASSERT(err_code == NRF_SUCCESS, err_code);  \
            if (*p_result_code != NRF_SUCCESS)              \
            {                                               \
                SER_ASSERT_LENGTH_EQ(index, packet_len);    \
                return NRF_SUCCESS;                         \
            }

/** End the decoding of command response. */
#define SER_RSP_DEC_END                                  \
                /* Require all data to be pulled. */     \
                SER_ASSERT_LENGTH_EQ(index, packet_len); \
                return err_code


/** Start the decoding of command request and prepare local variables for the usage of SER_PULL_ macros. */
#define SER_REQ_DEC_BEGIN(OPCODE)                                           \
            SER_ASSERT_NOT_NULL(p_buf);                                     \
            SER_ASSERT(packet_len>0, NRF_ERROR_INVALID_PARAM);              \
            uint32_t index = 0;                                             \
            uint32_t * const p_index = &index;                              \
            uint32_t err_code = NRF_SUCCESS;                                \
            SER_ASSERT(p_buf[index] == (OPCODE), NRF_ERROR_INVALID_PARAM);  \
            (void)err_code;                                                 \
            (void)p_index;                                                  \
            ++index

/** End the decoding of command request. */
#define SER_REQ_DEC_END                              \
            SER_ASSERT_LENGTH_EQ(index, packet_len); \
            return NRF_SUCCESS

/** Start the encoding of command response and prepare local variables for the usage of SER_PUSH_ macros. */
#define SER_RSP_ENC_BEGIN(OPCODE)                           \
            SER_ASSERT_NOT_NULL(p_buf);                     \
            SER_ASSERT_NOT_NULL(p_buf_len);                 \
            uint32_t index    = 0;                          \
            uint32_t * const p_index = &index;              \
            uint32_t err_code = NRF_SUCCESS;                \
            uint32_t buf_len  = *p_buf_len;                 \
            /* Push the opcode + result and exit if result  \
               is not NRF_SUCCESS. */                       \
            uint8_t  opcode   = (OPCODE);                   \
            SER_PUSH_uint8(&opcode);                        \
            SER_PUSH_uint32(&return_code);                  \
                                                            \
            SER_ASSERT(err_code == NRF_SUCCESS, err_code);  \
            if (return_code != NRF_SUCCESS)                 \
            {                                               \
                SER_RSP_ENC_END;                            \
            }

/** Start the encoding of command response that contains the result code only. */
#define SER_RSP_ENC_RESULT_ONLY(OPCODE) \
    return ser_ble_cmd_rsp_status_code_enc((OPCODE), return_code, p_buf, p_buf_len)

/** End the encoding of command response. */
#define SER_RSP_ENC_END         \
            *p_buf_len = index; \
            return NRF_SUCCESS


/** Start the encoding of an event and prepare local variables for the usage of SER_PUSH_ macros. */
#define SER_EVT_ENC_BEGIN(EVT_HEADER)           \
            SER_ASSERT_NOT_NULL(p_event);       \
            SER_ASSERT_NOT_NULL(p_buf);         \
            SER_ASSERT_NOT_NULL(p_buf_len);     \
            SER_ASSERT(p_event->header.evt_id == (EVT_HEADER), NRF_ERROR_INVALID_PARAM); \
            uint32_t index      = 0;            \
            uint32_t * p_index    = &index;     \
            uint32_t buf_len    = *p_buf_len;   \
            uint32_t err_code   = NRF_SUCCESS;  \
            uint16_t evt_header = (EVT_HEADER); \
            /* Push event header. */            \
            SER_PUSH_uint16(&evt_header)

/** End the encoding of an event. */
#define SER_EVT_ENC_END         \
            *p_buf_len = index; \
            return err_code


/** Start the decoding of an event that has an event-specific data structure
    and prepare local variables for the usage of SER_PULL_ macros. */
#define SER_EVT_DEC_BEGIN(EVT_CODE, EVT_GROUP, EVT_NAME)            \
            uint32_t err_code = NRF_SUCCESS;                        \
            uint32_t index = 0;                                     \
            uint32_t * p_index = &index;                            \
            SER_ASSERT_NOT_NULL(p_buf);                             \
            SER_ASSERT_NOT_NULL(p_event_len);                       \
            /* Calculate the base event structure length     */     \
            /* and make sure that there is enough free space */     \
            /* in the output buffer. */                             \
            uint32_t evt_struct_len =                               \
                    offsetof(ble_evt_t, evt.EVT_GROUP##_evt.params) \
                    - offsetof(ble_evt_t, evt)                      \
                    + sizeof(ble_##EVT_GROUP##_evt_##EVT_NAME##_t); \
            SER_ASSERT_LENGTH_LEQ(evt_struct_len, *p_event_len);    \
            *p_event_len -= evt_struct_len;                         \
            /* Some structures contains variable length arrays */   \
            /* and the overall size may be greater.            */   \
            uint32_t evt_extended_len = 0;                          \
            (void) evt_extended_len;                                \
            p_event->header.evt_id = EVT_CODE

/** Start the decoding of an event that has no event-specific data structure.
    and prepare local variables for the usage of SER_PULL_ macros. */
#define SER_EVT_DEC_BEGIN_NO_STRUCT(EVT_CODE, EVT_GROUP)            \
            uint32_t err_code = NRF_SUCCESS;                        \
            uint32_t index = 0;                                     \
            uint32_t * p_index = &index;                            \
            SER_ASSERT_NOT_NULL(p_buf);                             \
            SER_ASSERT_NOT_NULL(p_event_len);                       \
            /* Calculate the base event structure length     */     \
            /* and make sure that there is enough free space */     \
            /* in the output buffer. */                             \
            uint32_t evt_struct_len =                               \
                    offsetof(ble_evt_t, evt.EVT_GROUP##_evt.params) \
                    - offsetof(ble_evt_t, evt)          ;           \
            SER_ASSERT_LENGTH_LEQ(evt_struct_len, *p_event_len);    \
            *p_event_len -= evt_struct_len;                         \
            /* Some structures contain variable length arrays */   \
            /* and the overall size may be greater.            */   \
            uint32_t evt_extended_len = 0;                          \
            (void) evt_extended_len;                                \
            p_event->header.evt_id = EVT_CODE

/** End the decoding of an event. */
#define SER_EVT_DEC_END                                       \
            SER_ASSERT_LENGTH_EQ(index, packet_len);          \
            /*p_event_len = index;                */          \
            /*p_event->header.evt_len = index;     */         \
            *p_event_len = evt_struct_len + evt_extended_len; \
            return NRF_SUCCESS

/** Push an event-specific special field that contains variable length fields and get the extended data size. */
#define SER_PULL_FIELD_EXTENDED(P_VAR, P_DECODER)               \
            do                                                  \
            {                                                   \
                uint32_t field_ext_len = *p_event_len;          \
                err_code = field_ext_dec(p_buf, packet_len, p_index, &field_ext_len, (P_VAR), (P_DECODER)); \
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);  \
                *p_event_len     -= field_ext_len;              \
                evt_extended_len += field_ext_len;              \
            } while(0)                                          \


/** Generic command response status code encoder. */
uint32_t ser_ble_cmd_rsp_status_code_enc(uint8_t          op_code,
                                         uint32_t         command_status,
                                         uint8_t * const  p_buf,
                                         uint32_t * const p_buf_len);

/** Generic command response result code decoder. */
uint32_t ser_ble_cmd_rsp_result_code_dec(uint8_t const * const p_buf,
                                         uint32_t * const      p_pos,
                                         uint32_t              packet_len,
                                         uint8_t               op_code,
                                         uint32_t * const      p_result_code);

/** Generic command response decoder. */
uint32_t ser_ble_cmd_rsp_dec(uint8_t const * const p_buf,
                             uint32_t              packet_len,
                             uint8_t               op_code,
                             uint32_t * const      p_result_code);



/**@brief Function for safe field encoding field.
 *
 * @param[in]      p_field          Pointer to the input struct. Must not be a null.
 * @param[in]      p_buf            Pointer to the beginning of the output buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to start of uint8 value in buffer.
 *                                  \c out: Index in the buffer to the first byte after the encoded data.
 * @param[in]      fp_field_encoder Pointer to the function that implements fields encoding.
 *
 * @return NRF_SUCCESS              Field encoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 * @retval NRF_ERROR_NULL           p_field or fp_field_encoder is NULL.
 */
static inline uint32_t field_enc(void const * const      p_field,
                                 uint8_t * const         p_buf,
                                 uint32_t                buf_len,
                                 uint32_t * const        p_index,
                                 field_encoder_handler_t fp_field_encoder)
{
    SER_ASSERT_NOT_NULL(fp_field_encoder);
    SER_ASSERT_NOT_NULL(p_field);

    return fp_field_encoder(p_field, p_buf, buf_len, p_index);
}

/**@brief Function for safe field decoding.
 *
 * Function checks if conditional field is present in the input buffer and if it is set, it calls
 * the provided parser function that attempts to parse the buffer content to the known field.
 *
 * @param[in]      p_buf            Pointer to the beginning of the input buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of uint8 value in buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded data.
 * @param[in]      p_field          Pointer to the output location.
 * @param[in]      fp_field_decoder Pointer to the function that implements field decoding.
 *
 * @return NRF_SUCCESS              Field decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 * @retval NRF_ERROR_NULL           p_field or fp_field_decoder is NULL.
 */
static inline uint32_t field_dec(uint8_t const * const   p_buf,
                                 uint32_t                buf_len,
                                 uint32_t * const        p_index,
                                 void * const            p_field,
                                 field_decoder_handler_t fp_field_decoder)
{
    SER_ASSERT_NOT_NULL(fp_field_decoder);
    SER_ASSERT_NOT_NULL(p_field);

    return fp_field_decoder(p_buf, buf_len, p_index, p_field);
}

/**@brief Function for safe decoding of an event-specific field that contains extended data.
 *
 * Some event structures contain a variable length array (extended data),
 * that may be written next to the event structure.
 *
 * @param[in]      p_buf            Pointer to the beginning of the input buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded data.
 * @param[in,out]  p_ext_length     \c in: Maximum size of extended data.
 *                                  \c out: Extended data length in bytes.
 * @param[in]      p_field          Pointer to output location.
 * @param[in]      fp_field_decoder Pointer to the function that implements field decoding.
 *
 * @return NRF_SUCCESS              Field decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 * @retval NRF_ERROR_NULL           p_field or fp_field_decoder is NULL.
 */
static inline uint32_t field_ext_dec(uint8_t const * const        p_buf,
                                     uint32_t                     buf_len,
                                     uint32_t * const             p_index,
                                     uint32_t * const             p_ext_length,
                                     void * const                 p_field,
                                     field_ext_decoder_handler_t  fp_field_decoder)
{
    SER_ASSERT_NOT_NULL(fp_field_decoder);
    SER_ASSERT_NOT_NULL(p_field);

    return fp_field_decoder(p_buf, buf_len, p_index, p_ext_length, p_field);
}

/**@brief Function for safe encoding an uint16 value.
 *
 * Safe decoding of a uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_field          A uint16 value to be encoded.
 * @param[out]     p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint16 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t uint16_t_enc(const void * const p_field,
                      uint8_t * const    p_buf,
                      uint32_t           buf_len,
                      uint32_t * const   p_index);

/**@brief Function for safe decoding of a uint16 value.
 *
 * Safe decoding of a uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint16 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 * @param[out]     p_field          Pointer to the location where the uint16 value will be decoded.
 */
uint32_t uint16_t_dec(uint8_t const * const p_buf,
                      uint32_t              buf_len,
                      uint32_t * const      p_index,
                      void *                p_field);

/**@brief Function for safe decoding of a uint16 value.
 *
 * Safe decoding of a uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  index            \c in: Index to the start of the uint16 value in buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 * @param[out]     value            Decoded uint16 value.
 */
void uint16_dec(uint8_t const * const p_buf,
                uint32_t              packet_len,
                uint32_t * const      index,
                uint16_t * const      value);

/**@brief Function for safe encoding of a uint18 value.
 *
 * Safe decoding of a uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index            \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 * @param[out]     p_field          Pointer to uint8 value to be encoded.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t uint8_t_enc(const void * const p_field,
                     uint8_t * const    p_buf,
                     uint32_t           buf_len,
                     uint32_t * const   p_index);

/**@brief Function for safe decoding of a uint8 value.
 *
 * Safe decoding of a uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 * @param[out]     p_field          Pointer to the location for decoded uint8 value.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t uint8_t_dec(uint8_t const * const p_buf,
                     uint32_t              buf_len,
                     uint32_t * const      p_index,
                     void *                p_field);

/**@brief Function for safe decoding of a uint8 value.
 *
 * Safe decoding of a uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  index            \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 * @param[out]     value            Decoded uint8 value.
 */
void uint8_dec(uint8_t const * const p_buf,
               uint32_t              packet_len,
               uint32_t * const      index,
               uint8_t * const       value);

/**@brief Function for safe decoding of a uint18 value.
 *
 * Safe decoding of a uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  index            \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 * @param[out]     value            Decoded uint8 value.
 */
void int8_dec(uint8_t const * const p_buf,
              uint32_t              packet_len,
              uint32_t * const      index,
              int8_t * const        value);

/**@brief Function for safe encoding of a variable length field encoded as length(8bit) + data.
 *
 * Safe encoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[out]     p_data           Pointer to data to encode.
 * @param[in]      dlen             Length of data to encode (0-255).
 * @param[out]     p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t len8data_enc(uint8_t const * const p_data,
                      uint8_t const         dlen,
                      uint8_t * const       p_buf,
                      uint32_t              buf_len,
                      uint32_t * const      p_index);

/**@brief Function for safe decoding of a variable length field encoded as length(8bit) + data.
 *
 * Safe decoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 * @param[out]     pp_data          Pointer to decoded data (p_data is set to NULL in
 *                                  case data is not present in the buffer).
 * @param[out]     p_len            Decoded length (0-255).
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t len8data_dec(uint8_t const * const p_buf,
                      uint32_t              buf_len,
                      uint32_t * const      p_index,
                      uint8_t * * const     pp_data,
                      uint8_t * const       p_len);

/**@brief Function for safe encoding of a variable length field encoded as length(16 bit) + data.
 *
 * Safe encoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 * It is possible that provided p_data is NULL. In that case, length is encoded and it is followed by
 * SER_FIELD_NOT_PRESENT flag. Otherwise, the SER_FIELD_PRESENT flag precedes the data.
 *
 * @param[in]      p_data           Data to encode.
 * @param[in]      dlen             Input data length (16 bit).
 * @param[in]      p_buf            Pointer to the beginning of the output buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the encoded data.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t len16data_enc(uint8_t const * const p_data,
                       uint16_t const        dlen,
                       uint8_t * const       p_buf,
                       uint32_t              buf_len,
                       uint32_t * const      p_index);

/**@brief Function for safe decoding of a variable length field encoded as length(16 bit) + data.
 *
 * Safe decoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 * Encoded data consists of a length field, a presence flag, and conditional data (present only if the presence flag
 * is set). The p_data pointer cannot be NULL if the presence flag is set.
 *
 * @param[in]      p_buf            Pointer to the beginning of the input buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded data.
 * @param[in]      pp_data          Pointer to decoded data.
 * @param[in]      p_dlen           Data length (16 bit).
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t len16data_dec(uint8_t const * const p_buf,
                       uint32_t              buf_len,
                       uint32_t * const      p_index,
                       uint8_t * * const     pp_data,
                       uint16_t * const      p_dlen);


/**@brief Function for safe encoding of a uint16 table with a given element count.
 *
 * Safe encoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 * It is possible that the provided p_data is NULL. In that case, length is encoded and it is followed by a
 * SER_FIELD_NOT_PRESENT flag. Otherwise, the SER_FIELD_PRESENT flag precedes the data.
 *
 * @param[in]      p_data           Data table to encode.
 * @param[in]      count            Table element count.
 * @param[in]      p_buf            Pointer to the beginning of the output buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the encoded data.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */

uint32_t count16_cond_data16_enc(uint16_t const * const p_data,
                                 uint16_t const         count,
                                 uint8_t * const        p_buf,
                                 uint32_t               buf_len,
                                 uint32_t * const       p_index);

/**@brief Function for safe decoding of a uint16 table with a given element count.
 *
 * Safe encoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 * It is possible that the provided p_data is NULL. In that case, length is encoded and it is followed by a
 * SER_FIELD_NOT_PRESENT flag. Otherwise, the SER_FIELD_PRESENT flag precedes the data.
 *
 * @param[in]      p_buf            Pointer to the beginning of the output buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the encoded data.
 * @param[in]      pp_data          Pointer to the table to encode.
 * @param[in,out]  p_count          Pointer to table element count - initialised with max count.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 * @retval NRF_ERROR_DATA_SIZE      Decoding failure. Initial count is smaller than actual.
 */

uint32_t count16_cond_data16_dec(uint8_t const * const p_buf,
                                 uint32_t              buf_len,
                                 uint32_t * const      p_index,
                                 uint16_t * * const    pp_data,
                                 uint16_t * const      p_count);


/**@brief Function for safe decoding of a variable length field encoded as length(16 bit) + data.
 *
 * Safe decoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 * Encoded data consists of a presence flag, an optional length field, a second presence flag, and optional data.
 *
 *
 * @param[in]      p_buf            Pointer to the beginning of the input buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded data.
 * @param[out]     pp_data          Pointer to decoded data.
 * @param[out]     pp_len           Data length (16 bit).
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */

uint32_t cond_len16_cond_data_dec(uint8_t const * const p_buf,
                                  uint32_t              buf_len,
                                  uint32_t * const      p_index,
                                  uint8_t * * const     pp_data,
                                  uint16_t * * const    pp_len);

/**@brief Command response encoder - replacement of ser_ble_cmd_rsp_status_code_enc
 * with layout aligned to the rest of encoder functions.
 *
 * @param[in]      op_code          Operation code - see BLE_GAP_SVCS.
 * @param[in]      return_code      nRF error code.
 * @param[in]      p_buff           Pointer to the start of pointer to decoded data.
 * @param[in,out]  p_buff_len       \c in: Size of the buffer.
 *                                  \c out: Used bytes in the buffer.
 * @param[in,out]  p_buff_len       \c in: Initial offset in the buffer.
 *                                  \c out: Final offset in the buffer.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 * @retval NRF_ERROR_NULL           Invalid pointer.
 */
uint32_t op_status_enc(uint8_t          op_code,
                       uint32_t         return_code,
                       uint8_t * const  p_buff,
                       uint32_t * const p_buff_len,
                       uint32_t * const p_index);

/**@brief Command response encoder with a conditional 16-bit field.
 *
 * @param[in]      op_code          Operation code - see BLE_GAP_SVCS.
 * @param[in]      return_code      nRF error code.
 * @param[in]      value            Optional 16-bit field encoded for return code == NRF_SUCCESS.
 * @param[in]      p_buff           Pointer to the start of pointer to decoded data.
 * @param[in,out]  p_buff_len       \c in: Size of the buffer.
 *                                  \c out: Used bytes in the buffer.
 * @param[in,out]  p_buff_len       \c in: Initial offset in the buffer.
 *                                  \c out: Final offset in the buffer.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 * @retval NRF_ERROR_NULL           Invalid pointer.
 */

uint32_t op_status_cond_uint16_enc(uint8_t          op_code,
                                   uint32_t         return_code,
                                   uint16_t         value,
                                   uint8_t * const  p_buff,
                                   uint32_t * const p_buff_len,
                                   uint32_t * const p_index);

/**@brief Function for safe encoding of a buffer of known size.
 *
 * Safe encoding of a buffer. Encoder assumes that the size is known to the decoder and it is not
 * encoded here. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_data           Data to encode.
 * @param[in]      dlen             Input data length (16 bit).
 * @param[in]      p_buf            Pointer to the beginning of the output buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the encoded data.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t buf_enc(uint8_t const * const p_data,
                 uint16_t const        dlen,
                 uint8_t * const       p_buf,
                 uint32_t              buf_len,
                 uint32_t * const      p_index);

/**@brief Function for safe decoding of a buffer of known size.
 *
 * Safe decoding of a buffer of known size. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 * Encoded data consists of a presence flag and conditional data (present only if the presence flag
 * is set). The p_data pointer cannot be NULL only if the presence flag is set. Length is provided
 * as input to the function.
 *
 * @param[in]      p_buf            Pointer to the beginning of the input buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded data.
 * @param[in]      pp_data          Pointer to decoded data.
 * @param[in]      data_len         Length of the buffer for decoded data (16 bit).
 * @param[in]      dlen             Length of the data to decode (16 bit).
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t buf_dec(uint8_t const * const p_buf,
                 uint32_t              buf_len,
                 uint32_t * const      p_index,
                 uint8_t * * const     pp_data,
                 uint16_t              data_len,
                 uint16_t              dlen);

/**@brief Function for safe encoding of a uint32 value.
 *
 * Safe decoding of a uint32 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_field          A uint32 value to be encoded.
 * @param[out]     p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint32 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t uint32_t_enc(void const * const p_field,
                      uint8_t * const    p_buf,
                      uint32_t           buf_len,
                      uint32_t * const   p_index);

/**@brief Function for safe decoding of a uint32 value.
 *
 * Safe decoding of a uint32 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 *
 * @param[in]      p_buf            Buffer containing the value.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint32 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded value.
 * @param[out]     value            Decoded uint32 value.
 */
uint32_t uint32_t_dec(uint8_t const * const p_buf,
                      uint32_t              buf_len,
                      uint32_t * const      p_index,
                      void *                p_field);

/**@brief Function for safe encoding of a uint8 vector.
 *
 * Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 * *
 * @param[in]      p_data           Data to encode.
 * @param[in]      dlen             Input data length (16 bit).
 * @param[in]      p_buf            Pointer to the beginning of the output buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the encoded data.
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t uint8_vector_enc(uint8_t const * const p_data,
                          uint16_t const        dlen,
                          uint8_t * const       p_buf,
                          uint32_t              buf_len,
                          uint32_t * const      p_index);

/**@brief Function for safe decoding a uint8 vector.
 *
 * Safe decoding of a buffer of known size. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
 * Vector length is provided as input to the function.
 *
 * @param[in]      p_buf            Pointer to the beginning of the input buffer.
 * @param[in]      buf_len          Size of the buffer.
 * @param[in,out]  p_index          \c in: Index to the start of the uint8 value in the buffer.
 *                                  \c out: Index in the buffer to the first byte after the decoded data.
 * @param[in]      p_data           Pointer to decoded data.
 * @param[in]      dlen             Length of data to decode (16 bit).
 *
 * @return NRF_SUCCESS              Fields decoded successfully.
 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
 */
uint32_t uint8_vector_dec(uint8_t const * const p_buf,
                 uint32_t              buf_len,
                 uint32_t * const      p_index,
                 uint8_t  * const      p_data,
                 uint16_t              dlen);


#ifdef __cplusplus
}
#endif

#endif