forked from eclipse-threadx/getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnx_client.c
286 lines (247 loc) · 10.4 KB
/
nx_client.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
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
/* Copyright (c) Microsoft Corporation.
Licensed under the MIT License. */
#include "nx_client.h"
#include <stdio.h>
#include "b_u585i_iot02a.h"
#include "b_u585i_iot02a_env_sensors.h"
#include "nx_api.h"
#include "nx_azure_iot_hub_client.h"
#include "nx_azure_iot_json_reader.h"
#include "nx_azure_iot_provisioning_client.h"
#include "azure_iot_nx_client.h"
#include "azure_config.h"
#include "azure_device_x509_cert_config.h"
#include "azure_pnp_info.h"
#include "stm_networking.h"
#define IOT_MODEL_ID "dtmi:azurertos:devkit:gsg;2"
#define TELEMETRY_HUMIDITY "humidity"
#define TELEMETRY_TEMPERATURE "temperature"
#define TELEMETRY_PRESSURE "pressure"
#define TELEMETRY_INTERVAL_PROPERTY "telemetryInterval"
#define LED_STATE_PROPERTY "ledState"
#define SET_LED_STATE_COMMAND "setLedState"
static AZURE_IOT_NX_CONTEXT azure_iot_nx_client;
static int32_t telemetry_interval = 10;
static UINT append_device_info_properties(NX_AZURE_IOT_JSON_WRITER* json_writer)
{
if (nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
(UCHAR*)DEVICE_INFO_MANUFACTURER_PROPERTY_NAME,
sizeof(DEVICE_INFO_MANUFACTURER_PROPERTY_NAME) - 1,
(UCHAR*)DEVICE_INFO_MANUFACTURER_PROPERTY_VALUE,
sizeof(DEVICE_INFO_MANUFACTURER_PROPERTY_VALUE) - 1) ||
nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
(UCHAR*)DEVICE_INFO_MODEL_PROPERTY_NAME,
sizeof(DEVICE_INFO_MODEL_PROPERTY_NAME) - 1,
(UCHAR*)DEVICE_INFO_MODEL_PROPERTY_VALUE,
sizeof(DEVICE_INFO_MODEL_PROPERTY_VALUE) - 1) ||
nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
(UCHAR*)DEVICE_INFO_SW_VERSION_PROPERTY_NAME,
sizeof(DEVICE_INFO_SW_VERSION_PROPERTY_NAME) - 1,
(UCHAR*)DEVICE_INFO_SW_VERSION_PROPERTY_VALUE,
sizeof(DEVICE_INFO_SW_VERSION_PROPERTY_VALUE) - 1) ||
nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
(UCHAR*)DEVICE_INFO_OS_NAME_PROPERTY_NAME,
sizeof(DEVICE_INFO_OS_NAME_PROPERTY_NAME) - 1,
(UCHAR*)DEVICE_INFO_OS_NAME_PROPERTY_VALUE,
sizeof(DEVICE_INFO_OS_NAME_PROPERTY_VALUE) - 1) ||
nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
(UCHAR*)DEVICE_INFO_PROCESSOR_ARCHITECTURE_PROPERTY_NAME,
sizeof(DEVICE_INFO_PROCESSOR_ARCHITECTURE_PROPERTY_NAME) - 1,
(UCHAR*)DEVICE_INFO_PROCESSOR_ARCHITECTURE_PROPERTY_VALUE,
sizeof(DEVICE_INFO_PROCESSOR_ARCHITECTURE_PROPERTY_VALUE) - 1) ||
nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
(UCHAR*)DEVICE_INFO_PROCESSOR_MANUFACTURER_PROPERTY_NAME,
sizeof(DEVICE_INFO_PROCESSOR_MANUFACTURER_PROPERTY_NAME) - 1,
(UCHAR*)DEVICE_INFO_PROCESSOR_MANUFACTURER_PROPERTY_VALUE,
sizeof(DEVICE_INFO_PROCESSOR_MANUFACTURER_PROPERTY_VALUE) - 1) ||
nx_azure_iot_json_writer_append_property_with_double_value(json_writer,
(UCHAR*)DEVICE_INFO_TOTAL_STORAGE_PROPERTY_NAME,
sizeof(DEVICE_INFO_TOTAL_STORAGE_PROPERTY_NAME) - 1,
DEVICE_INFO_TOTAL_STORAGE_PROPERTY_VALUE,
2) ||
nx_azure_iot_json_writer_append_property_with_double_value(json_writer,
(UCHAR*)DEVICE_INFO_TOTAL_MEMORY_PROPERTY_NAME,
sizeof(DEVICE_INFO_TOTAL_MEMORY_PROPERTY_NAME) - 1,
DEVICE_INFO_TOTAL_MEMORY_PROPERTY_VALUE,
2))
{
return NX_NOT_SUCCESSFUL;
}
return NX_AZURE_IOT_SUCCESS;
}
static UINT append_device_telemetry(NX_AZURE_IOT_JSON_WRITER* json_writer)
{
float temperature;
float pressure;
float humidity;
if (BSP_ENV_SENSOR_GetValue(0, ENV_TEMPERATURE, &temperature) != BSP_ERROR_NONE)
{
printf("ERROR: BSP_ENV_SENSOR_GetValue\r\n");
}
if (BSP_ENV_SENSOR_GetValue(0, ENV_HUMIDITY, &humidity) != BSP_ERROR_NONE)
{
printf("ERROR: BSP_ENV_SENSOR_GetValue\r\n");
}
if (BSP_ENV_SENSOR_GetValue(1, ENV_PRESSURE, &pressure) != BSP_ERROR_NONE)
{
printf("ERROR: BSP_ENV_SENSOR_GetValue\r\n");
}
if (nx_azure_iot_json_writer_append_property_with_double_value(
json_writer, (UCHAR*)TELEMETRY_HUMIDITY, sizeof(TELEMETRY_HUMIDITY) - 1, humidity, 2) ||
nx_azure_iot_json_writer_append_property_with_double_value(
json_writer, (UCHAR*)TELEMETRY_TEMPERATURE, sizeof(TELEMETRY_TEMPERATURE) - 1, temperature, 2) ||
nx_azure_iot_json_writer_append_property_with_double_value(
json_writer, (UCHAR*)TELEMETRY_PRESSURE, sizeof(TELEMETRY_PRESSURE) - 1, pressure, 2))
{
return NX_NOT_SUCCESSFUL;
}
return NX_AZURE_IOT_SUCCESS;
}
static void set_led_state(bool level)
{
if (level)
{
printf("\tLED is turned ON\r\n");
BSP_LED_On(LED_RED);
}
else
{
printf("\tLED is turned OFF\r\n");
BSP_LED_Off(LED_RED);
}
}
static void command_received_cb(AZURE_IOT_NX_CONTEXT* nx_context_ptr,
const UCHAR* component,
USHORT component_length,
const UCHAR* method,
USHORT method_length,
UCHAR* payload,
USHORT payload_length,
VOID* context_ptr,
USHORT context_length)
{
UINT status;
if (strncmp((CHAR*)method, SET_LED_STATE_COMMAND, method_length) == 0)
{
bool arg = (strncmp((CHAR*)payload, "true", payload_length) == 0);
set_led_state(arg);
if ((status = nx_azure_iot_hub_client_command_message_response(
&nx_context_ptr->iothub_client, 200, context_ptr, context_length, NULL, 0, NX_WAIT_FOREVER)))
{
printf("Direct method response failed! (0x%08x)\r\n", status);
return;
}
azure_iot_nx_client_publish_bool_property(&azure_iot_nx_client, NULL, LED_STATE_PROPERTY, arg);
}
else
{
printf("Direct method is not for this device\r\n");
if ((status = nx_azure_iot_hub_client_command_message_response(
&nx_context_ptr->iothub_client, 501, context_ptr, context_length, NULL, 0, NX_WAIT_FOREVER)))
{
printf("Direct method response failed! (0x%08x)\r\n", status);
return;
}
}
}
static void writable_property_received_cb(AZURE_IOT_NX_CONTEXT* nx_context,
const UCHAR* component_name,
UINT component_name_len,
UCHAR* property_name,
UINT property_name_len,
NX_AZURE_IOT_JSON_READER* json_reader_ptr,
UINT version)
{
UINT status;
if (strncmp((CHAR*)property_name, TELEMETRY_INTERVAL_PROPERTY, property_name_len) == 0)
{
status = nx_azure_iot_json_reader_token_int32_get(json_reader_ptr, &telemetry_interval);
if (status == NX_AZURE_IOT_SUCCESS)
{
printf("Updating %s to %ld\r\n", TELEMETRY_INTERVAL_PROPERTY, telemetry_interval);
// Confirm reception back to hub
azure_nx_client_respond_int_writable_property(
nx_context, NULL, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200, version);
azure_nx_client_periodic_interval_set(nx_context, telemetry_interval);
}
}
}
static void property_received_cb(AZURE_IOT_NX_CONTEXT* nx_context,
const UCHAR* component_name,
UINT component_name_len,
UCHAR* property_name,
UINT property_name_len,
NX_AZURE_IOT_JSON_READER* json_reader_ptr,
UINT version)
{
UINT status;
if (strncmp((CHAR*)property_name, TELEMETRY_INTERVAL_PROPERTY, property_name_len) == 0)
{
status = nx_azure_iot_json_reader_token_int32_get(json_reader_ptr, &telemetry_interval);
if (status == NX_AZURE_IOT_SUCCESS)
{
printf("Updating %s to %ld\r\n", TELEMETRY_INTERVAL_PROPERTY, telemetry_interval);
azure_nx_client_periodic_interval_set(nx_context, telemetry_interval);
}
}
}
static void properties_complete_cb(AZURE_IOT_NX_CONTEXT* nx_context)
{
// Device twin processing is done, send out property updates
azure_iot_nx_client_publish_properties(nx_context, DEVICE_INFO_COMPONENT_NAME, append_device_info_properties);
azure_iot_nx_client_publish_bool_property(nx_context, NULL, LED_STATE_PROPERTY, false);
azure_iot_nx_client_publish_int_writable_property(
nx_context, NULL, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval);
printf("\r\nStarting Main loop\r\n");
}
static void telemetry_cb(AZURE_IOT_NX_CONTEXT* nx_context)
{
azure_iot_nx_client_publish_telemetry(&azure_iot_nx_client, NULL, append_device_telemetry);
}
UINT azure_iot_nx_client_entry(
NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_ptr, UINT (*unix_time_callback)(ULONG* unix_time))
{
UINT status;
if ((status = azure_iot_nx_client_create(&azure_iot_nx_client,
ip_ptr,
pool_ptr,
dns_ptr,
unix_time_callback,
IOT_MODEL_ID,
sizeof(IOT_MODEL_ID) - 1)))
{
printf("ERROR: azure_iot_nx_client_create failed (0x%08x)\r\n", status);
return status;
}
// Register the callbacks
azure_iot_nx_client_register_command_callback(&azure_iot_nx_client, command_received_cb);
azure_iot_nx_client_register_writable_property_callback(&azure_iot_nx_client, writable_property_received_cb);
azure_iot_nx_client_register_property_callback(&azure_iot_nx_client, property_received_cb);
azure_iot_nx_client_register_properties_complete_callback(&azure_iot_nx_client, properties_complete_cb);
azure_iot_nx_client_register_timer_callback(&azure_iot_nx_client, telemetry_cb, telemetry_interval);
// Setup authentication
#ifdef ENABLE_X509
if ((status = azure_iot_nx_client_cert_set(&azure_iot_nx_client,
(UCHAR*)iot_x509_device_cert,
iot_x509_device_cert_len,
(UCHAR*)iot_x509_private_key,
iot_x509_private_key_len)))
{
printf("ERROR: azure_iot_nx_client_cert_set (0x%08x)\r\n", status);
return status;
}
#else
if ((status = azure_iot_nx_client_sas_set(&azure_iot_nx_client, IOT_DEVICE_SAS_KEY)))
{
printf("ERROR: azure_iot_nx_client_sas_set (0x%08x)\r\n", status);
return status;
}
#endif
// Enter the main loop
#ifdef ENABLE_DPS
azure_iot_nx_client_dps_run(&azure_iot_nx_client, IOT_DPS_ID_SCOPE, IOT_DPS_REGISTRATION_ID, stm_network_connect);
#else
azure_iot_nx_client_hub_run(&azure_iot_nx_client, IOT_HUB_HOSTNAME, IOT_HUB_DEVICE_ID, stm_network_connect);
#endif
return NX_SUCCESS;
}