summaryrefslogtreecommitdiff
path: root/addons/azure_iot/nx_azure_iot_hub_client_properties.c
blob: ae8a057c735e6ab7215297263b87e2aefbeb1ea8 (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
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
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

#include "nx_azure_iot_hub_client_properties.h"

extern UINT nx_azure_iot_hub_client_adjust_payload(NX_PACKET *packet_ptr);
VOID nx_azure_iot_hub_client_properties_component_process(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr,
                                                          NX_PACKET *packet_ptr, UINT message_type);

UINT nx_azure_iot_hub_client_reported_properties_component_begin(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr,
                                                                 NX_AZURE_IOT_JSON_WRITER *writer_ptr,
                                                                 const UCHAR *component_name_ptr,
                                                                 USHORT component_name_length)
{
az_result core_result;
az_span component_name;

    if ((hub_client_ptr == NX_NULL) ||
        (writer_ptr == NX_NULL) ||
        (component_name_ptr == NX_NULL) ||
        (component_name_length == 0))
    {
        LogError(LogLiteralArgs("IoT PnP reported property begin fail: INVALID POINTER"));
        return(NX_AZURE_IOT_INVALID_PARAMETER);
    }

    component_name = az_span_create((UCHAR *)component_name_ptr, (INT)component_name_length);

    core_result = az_iot_hub_client_properties_writer_begin_component(&(hub_client_ptr -> iot_hub_client_core),
                                                                      &(writer_ptr -> json_writer), component_name);
    if (az_result_failed(core_result))
    {
        LogError(LogLiteralArgs("IoT PnP failed to append component, core error : %d"), core_result);
        return(NX_AZURE_IOT_SDK_CORE_ERROR);
    }

    return(NX_AZURE_IOT_SUCCESS);
}

UINT nx_azure_iot_hub_client_reported_properties_component_end(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr,
                                                               NX_AZURE_IOT_JSON_WRITER *writer_ptr)
{
az_result core_result;

    if ((hub_client_ptr == NX_NULL) ||
        (writer_ptr == NX_NULL))
    {
        LogError(LogLiteralArgs("IoT PnP reported property end fail: INVALID POINTER"));
        return(NX_AZURE_IOT_INVALID_PARAMETER);
    }

    core_result = az_iot_hub_client_properties_writer_end_component(&(hub_client_ptr -> iot_hub_client_core),
                                                                    &(writer_ptr -> json_writer));
    if (az_result_failed(core_result))
    {
        LogError(LogLiteralArgs("IoT PnP failed to append component, core error : %d"), core_result);
        return(NX_AZURE_IOT_SDK_CORE_ERROR);
    }

    return(NX_AZURE_IOT_SUCCESS);
}

UINT nx_azure_iot_hub_client_reported_properties_status_begin(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr,
                                                              NX_AZURE_IOT_JSON_WRITER *writer_ptr,
                                                              const UCHAR *property_name_ptr, UINT property_name_length,
                                                              UINT status_code, ULONG version,
                                                              const UCHAR *description_ptr, UINT description_length)
{
az_span property_name;
az_span description;
az_result core_result;

    if ((hub_client_ptr == NX_NULL) ||
        (writer_ptr == NX_NULL) ||
        (property_name_ptr == NX_NULL) ||
        (property_name_length == 0) )
    {
        LogError(LogLiteralArgs("IoT PnP client begin reported status failed: INVALID POINTER"));
        return(NX_AZURE_IOT_INVALID_PARAMETER);
    }

    property_name = az_span_create((UCHAR *)property_name_ptr, (INT)property_name_length);
    description = az_span_create((UCHAR *)description_ptr, (INT)description_length);

    core_result = az_iot_hub_client_properties_writer_begin_response_status(&(hub_client_ptr -> iot_hub_client_core),
                                                                            &(writer_ptr -> json_writer),
                                                                            property_name, (int32_t)status_code,
                                                                            (int32_t)version, description);
    if (az_result_failed(core_result))
    {
        LogError(LogLiteralArgs("Failed to prefix data with core error : %d"), core_result);
        return(NX_AZURE_IOT_SDK_CORE_ERROR);
    }

    return(NX_AZURE_IOT_SUCCESS);
}

UINT nx_azure_iot_hub_client_reported_properties_status_end(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr,
                                                            NX_AZURE_IOT_JSON_WRITER *writer_ptr)
{
az_result core_result;

    if ((hub_client_ptr == NX_NULL) ||
        (writer_ptr == NX_NULL))
    {
        LogError(LogLiteralArgs("IoT PnP client end reported status failed: INVALID POINTER"));
        return(NX_AZURE_IOT_INVALID_PARAMETER);
    }

    core_result = az_iot_hub_client_properties_writer_end_response_status(&(hub_client_ptr -> iot_hub_client_core),
                                                                           &(writer_ptr -> json_writer));
    if (az_result_failed(core_result))
    {
        LogError(LogLiteralArgs("Failed to suffix data with core error : %d"), core_result);
        return(NX_AZURE_IOT_SDK_CORE_ERROR);
    }

    return(NX_AZURE_IOT_SUCCESS);
}

UINT nx_azure_iot_hub_client_properties_version_get(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr,
                                                    NX_AZURE_IOT_JSON_READER *reader_ptr,
                                                    UINT message_type, ULONG *version_ptr)
{
az_result core_result;
az_iot_hub_client_properties_message_type core_message_type;

    if ((hub_client_ptr == NX_NULL) ||
        (reader_ptr == NX_NULL) ||
        (version_ptr == NX_NULL) ||
        ((message_type != NX_AZURE_IOT_HUB_PROPERTIES) && 
         (message_type != NX_AZURE_IOT_HUB_WRITABLE_PROPERTIES)))
    {
        LogError(LogLiteralArgs("IoTHub client get properties version failed: INVALID POINTER"));
        return(NX_AZURE_IOT_INVALID_PARAMETER);
    }

    core_message_type = (message_type == NX_AZURE_IOT_HUB_PROPERTIES) ? AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_GET_RESPONSE :
                        AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_WRITABLE_UPDATED;

    core_result = az_iot_hub_client_properties_get_properties_version(&(hub_client_ptr -> iot_hub_client_core),
                                                                      &(reader_ptr -> json_reader),
                                                                      core_message_type,
                                                                      (int32_t *)version_ptr);
    if (az_result_failed(core_result))
    {
        LogError(LogLiteralArgs("IoTHub client get properties version failed : %d"), core_result);
        return(NX_AZURE_IOT_SDK_CORE_ERROR);
    }

    return(NX_AZURE_IOT_SUCCESS);
}

static UINT nx_azure_iot_hub_client_properties_component_property_next_get_internal(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr,
                                                                                    NX_AZURE_IOT_JSON_READER *reader_ptr,
                                                                                    UINT message_type, UINT property_type,
                                                                                    const UCHAR **component_name_pptr, 
                                                                                    USHORT *component_name_length_ptr,
                                                                                    UINT *component_index,
                                                                                    UINT parse_system_component)
{
az_span component_name;
az_iot_hub_client_properties_message_type core_message_type;
az_iot_hub_client_property_type core_property_type;
az_result core_result;
UINT index;
UINT system_component;

    if (((message_type != NX_AZURE_IOT_HUB_WRITABLE_PROPERTIES) && 
         (message_type != NX_AZURE_IOT_HUB_PROPERTIES)) ||
        ((property_type != NX_AZURE_IOT_HUB_CLIENT_PROPERTY_REPORTED_FROM_DEVICE) &&
         (property_type != NX_AZURE_IOT_HUB_CLIENT_PROPERTY_WRITABLE)) ||
        ((property_type == NX_AZURE_IOT_HUB_CLIENT_PROPERTY_REPORTED_FROM_DEVICE) && 
         (message_type != NX_AZURE_IOT_HUB_PROPERTIES)))
    {
        LogError(LogLiteralArgs("Invalid response type or property type passed"));
        return(NX_AZURE_IOT_INVALID_PARAMETER);
    }

    component_name = az_span_create((UCHAR *)*component_name_pptr, (INT)*component_name_length_ptr);
    core_message_type = (message_type == NX_AZURE_IOT_HUB_PROPERTIES) ? AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_GET_RESPONSE :
                        AZ_IOT_HUB_CLIENT_PROPERTIES_MESSAGE_TYPE_WRITABLE_UPDATED;
    core_property_type = (property_type == NX_AZURE_IOT_HUB_CLIENT_PROPERTY_REPORTED_FROM_DEVICE) ? AZ_IOT_HUB_CLIENT_PROPERTY_REPORTED_FROM_DEVICE :
                        AZ_IOT_HUB_CLIENT_PROPERTY_WRITABLE;

    do
    {
        core_result = az_iot_hub_client_properties_get_next_component_property(&(hub_client_ptr -> iot_hub_client_core),
                                                                               &(reader_ptr -> json_reader),
                                                                               core_message_type, core_property_type, 
                                                                               &component_name);
        if (core_result == AZ_ERROR_IOT_END_OF_PROPERTIES)
        {
            return(NX_AZURE_IOT_NOT_FOUND);
        }
        else if (az_result_failed(core_result))
        {
            LogError(LogLiteralArgs("Failed to parse document with core error : %d"), core_result);
            return(NX_AZURE_IOT_SDK_CORE_ERROR);
        }

        *component_name_pptr = az_span_ptr(component_name);
        *component_name_length_ptr = (USHORT)az_span_size(component_name);

        /* Check if it is system component.  */
        system_component = NX_FALSE;
        for (index = 0; index < (UINT)hub_client_ptr -> iot_hub_client_core._internal.options.component_names_length; index++)
        {
            if ((az_span_is_content_equal(component_name, hub_client_ptr -> nx_azure_iot_hub_client_component_list[index]))&&
                (hub_client_ptr -> nx_azure_iot_hub_client_component_callback[index] != NX_NULL))
            {
                system_component = NX_TRUE;
                break;
            }
        }

        /* System component.  */
        if ((parse_system_component == NX_TRUE) && (system_component == NX_TRUE))
        {
            *component_index = index;
            return(NX_AZURE_IOT_SUCCESS);
        }
        else if ((parse_system_component == NX_FALSE) && (system_component == NX_FALSE))
        {
            return(NX_AZURE_IOT_SUCCESS);
        }

        /* Skip it and find the next one.  */
        nx_azure_iot_json_reader_next_token(reader_ptr);
 
        /* Skip children in case the property value is an object.  */
        if (nx_azure_iot_json_reader_token_type(reader_ptr) == NX_AZURE_IOT_READER_TOKEN_BEGIN_OBJECT)
        {
            nx_azure_iot_json_reader_skip_children(reader_ptr);
        }
        nx_azure_iot_json_reader_next_token(reader_ptr);

    } while(1);
}

UINT nx_azure_iot_hub_client_properties_component_property_next_get(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr,
                                                                    NX_AZURE_IOT_JSON_READER *reader_ptr,
                                                                    UINT message_type, UINT property_type,
                                                                    const UCHAR **component_name_pptr,
                                                                    USHORT *component_name_length_ptr)
{

UINT status;

    if ((hub_client_ptr == NX_NULL) ||
        (reader_ptr == NX_NULL) ||
        (component_name_pptr == NX_NULL) ||
        (component_name_length_ptr == NX_NULL))
    {
        LogError(LogLiteralArgs("IoTHub client component next property failed: INVALID POINTER"));
        return(NX_AZURE_IOT_INVALID_PARAMETER);
    }

    status = nx_azure_iot_hub_client_properties_component_property_next_get_internal(hub_client_ptr, reader_ptr,
                                                                                     message_type, property_type,
                                                                                     component_name_pptr, component_name_length_ptr,
                                                                                     NX_NULL, NX_FALSE);

    return(status);
}

VOID nx_azure_iot_hub_client_properties_component_process(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr,
                                                          NX_PACKET *packet_ptr, UINT message_type)
{

UINT status;
ULONG version;
NX_AZURE_IOT_JSON_READER reader_ptr;
const UCHAR *component_name_ptr = NX_NULL;
USHORT component_name_length = 0;
UINT component_index;
UCHAR *prepend_ptr = NX_NULL;


    /* Check the message type.  */
    if ((message_type != NX_AZURE_IOT_HUB_PROPERTIES) &&
        (message_type != NX_AZURE_IOT_HUB_WRITABLE_PROPERTIES))
    {
        return;
    }

    /* Record the prepend pointer.  */
    prepend_ptr = packet_ptr -> nx_packet_prepend_ptr;

    if (nx_azure_iot_hub_client_adjust_payload(packet_ptr))
    {
        return;
    }

    if (nx_azure_iot_json_reader_init(&reader_ptr, packet_ptr))
    {

        /* Recover the prepend pointer since other functions will retreive the topic again.  */
        packet_ptr -> nx_packet_length += (ULONG)(packet_ptr -> nx_packet_prepend_ptr - prepend_ptr);
        packet_ptr -> nx_packet_prepend_ptr = prepend_ptr;
        return;
    }

    /* Get the version.  */
    status = nx_azure_iot_hub_client_properties_version_get(hub_client_ptr, &reader_ptr,
                                                            message_type, &version);
    if (status)
    {

        /* Recover the prepend pointer since other functions will retreive the topic again.  */
        packet_ptr -> nx_packet_length += (ULONG)(packet_ptr -> nx_packet_prepend_ptr - prepend_ptr);
        packet_ptr -> nx_packet_prepend_ptr = prepend_ptr;
        return;
    }

    /* Re-initialize the JSON reader state */
    if (nx_azure_iot_json_reader_init(&reader_ptr, packet_ptr))
    {

        /* Recover the prepend pointer since other functions will retreive the topic again.  */
        packet_ptr -> nx_packet_length += (ULONG)(packet_ptr -> nx_packet_prepend_ptr - prepend_ptr);
        packet_ptr -> nx_packet_prepend_ptr = prepend_ptr;
        return;
    }

    /* Loop to process system component.  */
    while (nx_azure_iot_hub_client_properties_component_property_next_get_internal(hub_client_ptr,
                                                                                   &reader_ptr, message_type,
                                                                                   NX_AZURE_IOT_HUB_CLIENT_PROPERTY_WRITABLE,
                                                                                   &component_name_ptr, &component_name_length,
                                                                                   &component_index, NX_TRUE) == NX_AZURE_IOT_SUCCESS)
    {

        /* Check if it is system component.  */
        if ((component_name_ptr) && (component_name_length) &&
            (hub_client_ptr -> nx_azure_iot_hub_client_component_callback[component_index]))
        {
            status = hub_client_ptr -> nx_azure_iot_hub_client_component_callback[component_index](&reader_ptr, version,
                                                                                                   hub_client_ptr -> nx_azure_iot_hub_client_component_callback_args[component_index]);
            if (status)
            {

                /* Recover the prepend pointer since other functions will retreive the topic again.  */
                packet_ptr -> nx_packet_length += (ULONG)(packet_ptr -> nx_packet_prepend_ptr - prepend_ptr);
                packet_ptr -> nx_packet_prepend_ptr = prepend_ptr;
                return;
            }

        }
        else
        {

            /* The JSON reader must be advanced regardless of whether the property
                is of interest or not.  */
            nx_azure_iot_json_reader_next_token(&reader_ptr);
 
            /* Skip children in case the property value is an object.  */
            if (nx_azure_iot_json_reader_token_type(&reader_ptr) == NX_AZURE_IOT_READER_TOKEN_BEGIN_OBJECT)
            {
                nx_azure_iot_json_reader_skip_children(&reader_ptr);
            }
            nx_azure_iot_json_reader_next_token(&reader_ptr);
        }
    }

    /* Recover the prepend pointer since other functions will retreive the topic again.  */
    packet_ptr -> nx_packet_length += (ULONG)(packet_ptr -> nx_packet_prepend_ptr - prepend_ptr);
    packet_ptr -> nx_packet_prepend_ptr = prepend_ptr;

    return;
}