Welcome to mirror list, hosted at ThFree Co, Russian Federation.

zcl.payload.h « zcl « include « stack « zigbee « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f6419d783a393e38d14a7a198257af1c503793e0 (plain)
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
/* Copyright [2009 - 2020] Exegin Technologies Limited. All rights reserved. */

#ifndef ZCL_PAYLOAD_H
# define ZCL_PAYLOAD_H

#include "zcl/zcl.h"

/*lint -save -e9021 [ useof 'undef' is discouraged <Rule 20.5, ADVISORY> ] */
/*lint -save -e9016 [ pointer  arithmetic <Rule 18.4, ADVISORY> ] */

/*---------------------------------------------------------------
 * ZCL Payload Helpers
 *---------------------------------------------------------------
 */
#define ZCL_BUILDER_CHECK_LEN(size)                 \
    do {                                            \
        if (payload == NULL) {                      \
            return -1;                              \
        }                                           \
        if (index == NULL) {                        \
            return -1;                              \
        }                                           \
        if ((*index + (size)) > capacity) {         \
            return -1;                              \
        }                                           \
    } while (false)

#define ZCL_BUILDER_TEMPLATE(size, append_func)     \
    do {                                            \
        ZCL_BUILDER_CHECK_LEN(size);                \
        append_func;                                \
        *index += (size);                           \
        return (int)*index;                         \
    } while (false)

/*
 * Helper Functions for constructing a ZCL payload
 */
static inline int
zb_zcl_append_uint8(uint8_t *payload, const unsigned int capacity, unsigned int *index, uint8_t value)
{
    ZCL_BUILDER_TEMPLATE(1U, payload[*index] = value);
}

static inline int
zb_zcl_append_uint16(uint8_t *payload, const unsigned int capacity, unsigned int *index, uint16_t value)
{
    ZCL_BUILDER_TEMPLATE(2U, putle16(payload + *index, value));
}

static inline int
zb_zcl_append_uint24(uint8_t *payload, const unsigned int capacity, unsigned int *index, uint32_t value)
{
    ZCL_BUILDER_TEMPLATE(3U, putle24(payload + *index, value));
}

static inline int
zb_zcl_append_uint32(uint8_t *payload, const unsigned int capacity, unsigned int *index, uint32_t value)
{
    ZCL_BUILDER_TEMPLATE(4U, putle32(payload + *index, value));
}

static inline int
zb_zcl_append_uint40(uint8_t *payload, const unsigned int capacity, unsigned int *index, uint64_t value)
{
    ZCL_BUILDER_TEMPLATE(5U, putle40(payload + *index, value));
}

static inline int
zb_zcl_append_uint48(uint8_t *payload, const unsigned int capacity, unsigned int *index, uint64_t value)
{
    ZCL_BUILDER_TEMPLATE(6U, putle48(payload + *index, value));
}

static inline int
zb_zcl_append_uint56(uint8_t *payload, const unsigned int capacity, unsigned int *index, uint64_t value)
{
    ZCL_BUILDER_TEMPLATE(7U, putle56(payload + *index, value));
}

static inline int
zb_zcl_append_uint64(uint8_t *payload, const unsigned int capacity, unsigned int *index, uint64_t value)
{
    ZCL_BUILDER_TEMPLATE(8U, putle64(payload + *index, value));
}

static inline int
zb_zcl_append_uint8_array(uint8_t *payload, const unsigned int capacity, unsigned int *index,
    const uint8_t *value, const unsigned int value_length)
{
    unsigned i;

    ZCL_BUILDER_CHECK_LEN(value_length);
    /* ZbMemCpy is not portable (internal stack use only) */
    for (i = 0; i < value_length; i++) {
        payload[*index + i] = value[i];
    }
    *index += value_length;
    return (int)*index;
}

/* prevent accidental reuse */
#undef ZCL_BUILDER_TEMPLATE

#define ZCL_PARSER_TEMPLATE(size, extract_func)     \
    do {                                            \
        if (payload == NULL) {                      \
            return -1;                              \
        }                                           \
        if (index == NULL) {                        \
            return -1;                              \
        }                                           \
        if ((*index + (size)) > length) {           \
            return -1;                              \
        }                                           \
        *value = extract_func;                      \
        *index += (size);                           \
        return (int)*index;                         \
    } while (false)

/*
 * Helper Functions for processing a ZCL payload
 */
static inline int
zb_zcl_parse_uint8(const uint8_t *payload, const unsigned int length, unsigned int *index, uint8_t *value)
{
    ZCL_PARSER_TEMPLATE(1U, payload[*index]);
}

static inline int
zb_zcl_parse_uint16(const uint8_t *payload, const unsigned int length, unsigned int *index, uint16_t *value)
{
    ZCL_PARSER_TEMPLATE(2U, pletoh16(payload + *index));
}

static inline int
zb_zcl_parse_uint24(const uint8_t *payload, const unsigned int length, unsigned int *index, uint32_t *value)
{
    ZCL_PARSER_TEMPLATE(3U, pletoh24(payload + *index));
}

static inline int
zb_zcl_parse_uint32(const uint8_t *payload, const unsigned int length, unsigned int *index, uint32_t *value)
{
    ZCL_PARSER_TEMPLATE(4U, pletoh32(payload + *index));
}

static inline int
zb_zcl_parse_uint40(const uint8_t *payload, const unsigned int length, unsigned int *index, uint64_t *value)
{
    ZCL_PARSER_TEMPLATE(5U, pletoh40(payload + *index));
}

static inline int
zb_zcl_parse_uint48(const uint8_t *payload, const unsigned int length, unsigned int *index, uint64_t *value)
{
    ZCL_PARSER_TEMPLATE(6U, pletoh48(payload + *index));
}

static inline int
zb_zcl_parse_uint56(const uint8_t *payload, const unsigned int length, unsigned int *index, uint64_t *value)
{
    ZCL_PARSER_TEMPLATE(7U, pletoh56(payload + *index));
}

static inline int
zb_zcl_parse_uint64(const uint8_t *payload, const unsigned int length, unsigned int *index, uint64_t *value)
{
    ZCL_PARSER_TEMPLATE(8U, pletoh64(payload + *index));
}

/* prevent accidental reuse */
#undef ZCL_PARSER_TEMPLATE

/*lint -restore */
/*lint -restore */
#endif /* __ZCL_PAYLOAD_H */