-
Notifications
You must be signed in to change notification settings - Fork 19
/
az_iot_hub_client_methods.c
111 lines (84 loc) · 3.39 KB
/
az_iot_hub_client_methods.c
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <stdint.h>
#include <az_result.h>
#include <az_span.h>
#include <az_span_internal.h>
#include <az_iot_hub_client.h>
#include <az_iot_common_internal.h>
#include <az_log_internal.h>
#include <az_precondition_internal.h>
#include <_az_cfg.h>
static const uint8_t null_terminator = '\0';
static const az_span methods_topic_prefix = AZ_SPAN_LITERAL_FROM_STR("$iothub/methods/");
static const az_span methods_topic_filter_suffix = AZ_SPAN_LITERAL_FROM_STR("POST/");
static const az_span methods_response_topic_result = AZ_SPAN_LITERAL_FROM_STR("res/");
static const az_span methods_response_topic_properties = AZ_SPAN_LITERAL_FROM_STR("/?$rid=");
AZ_NODISCARD az_result az_iot_hub_client_methods_parse_received_topic(
az_iot_hub_client const* client,
az_span received_topic,
az_iot_hub_client_method_request* out_request)
{
_az_PRECONDITION_NOT_NULL(client);
_az_PRECONDITION_VALID_SPAN(received_topic, 1, false);
_az_PRECONDITION_NOT_NULL(out_request);
(void)client;
int32_t index = az_span_find(received_topic, methods_topic_prefix);
if (index == -1)
{
return AZ_ERROR_IOT_TOPIC_NO_MATCH;
}
_az_LOG_WRITE(AZ_LOG_MQTT_RECEIVED_TOPIC, received_topic);
received_topic = az_span_slice(
received_topic, index + az_span_size(methods_topic_prefix), az_span_size(received_topic));
index = az_span_find(received_topic, methods_topic_filter_suffix);
if (index == -1)
{
return AZ_ERROR_IOT_TOPIC_NO_MATCH;
}
received_topic = az_span_slice(
received_topic,
index + az_span_size(methods_topic_filter_suffix),
az_span_size(received_topic));
index = az_span_find(received_topic, methods_response_topic_properties);
if (index == -1)
{
return AZ_ERROR_IOT_TOPIC_NO_MATCH;
}
out_request->name = az_span_slice(received_topic, 0, index);
out_request->request_id = az_span_slice(
received_topic,
index + az_span_size(methods_response_topic_properties),
az_span_size(received_topic));
return AZ_OK;
}
AZ_NODISCARD az_result az_iot_hub_client_methods_response_get_publish_topic(
az_iot_hub_client const* client,
az_span request_id,
uint16_t status,
char* mqtt_topic,
size_t mqtt_topic_size,
size_t* out_mqtt_topic_length)
{
_az_PRECONDITION_NOT_NULL(client);
_az_PRECONDITION_VALID_SPAN(request_id, 1, false);
_az_PRECONDITION_NOT_NULL(mqtt_topic);
_az_PRECONDITION(mqtt_topic_size);
(void)client;
az_span mqtt_topic_span = az_span_create((uint8_t*)mqtt_topic, (int32_t)mqtt_topic_size);
int32_t required_length = az_span_size(methods_topic_prefix)
+ az_span_size(methods_response_topic_result) + _az_iot_u32toa_size(status)
+ az_span_size(methods_response_topic_properties) + az_span_size(request_id);
AZ_RETURN_IF_NOT_ENOUGH_SIZE(mqtt_topic_span, required_length + (int32_t)sizeof(null_terminator));
az_span remainder = az_span_copy(mqtt_topic_span, methods_topic_prefix);
remainder = az_span_copy(remainder, methods_response_topic_result);
AZ_RETURN_IF_FAILED(az_span_u32toa(remainder, (uint32_t)status, &remainder));
remainder = az_span_copy(remainder, methods_response_topic_properties);
remainder = az_span_copy(remainder, request_id);
az_span_copy_u8(remainder, null_terminator);
if (out_mqtt_topic_length)
{
*out_mqtt_topic_length = (size_t)required_length;
}
return AZ_OK;
}