Skip to content

Commit 99dc804

Browse files
authored
refactor nx client helper class (#164)
* refactor nx client helper class * update all boards to use new helper function interface
1 parent effb148 commit 99dc804

File tree

19 files changed

+351
-567
lines changed

19 files changed

+351
-567
lines changed

MXChip/AZ3166/app/azure_config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef enum
3333

3434
// ----------------------------------------------------------------------------
3535
// Azure IoT DPS Self-Signed X509Certificate
36-
// Define this to connect to DPS or Iot Hub using a self-signed X509
36+
// Define this to connect to DPS or Iot Hub using a self-signed X509
3737
/// certificate
3838
// ----------------------------------------------------------------------------
3939
// #define ENABLE_X509
@@ -43,15 +43,15 @@ typedef enum
4343
// Make sure this is the same as the Device ID on the corresponding IoT Hub
4444
// NOTE: To be used only when ENABLE_DPS is NOT defined
4545
// ----------------------------------------------------------------------------
46-
#define IOT_DEVICE_ID ""
46+
#define IOT_DEVICE_ID ""
4747

4848
// ----------------------------------------------------------------------------
4949
// Azure IoT SAS Key
5050
// The SAS key generated by configuring an IoT Hub device or DPS individual
5151
// enrollment
5252
// NOTE: To be used only when ENABLE_X509 is not defined
5353
// ----------------------------------------------------------------------------
54-
#define IOT_PRIMARY_KEY ""
54+
#define IOT_PRIMARY_KEY ""
5555

5656
// ----------------------------------------------------------------------------
5757
// Azure IoT Hub Hostname

MXChip/AZ3166/app/legacy/mqtt.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ static void mqtt_device_twin_desired_prop(AZURE_IOT_MQTT* iot_mqtt, CHAR* messag
8686
tx_event_flags_set(&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR);
8787

8888
// Confirm reception back to hub
89-
azure_iot_mqtt_respond_int_writeable_property(
90-
iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
89+
azure_iot_mqtt_respond_int_writeable_property(iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
9190
}
9291
}
9392

@@ -185,7 +184,7 @@ UINT azure_iot_mqtt_entry(NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_p
185184
{
186185
// Sleep
187186
tx_event_flags_get(
188-
&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR_CLEAR, &events, telemetry_interval * NX_IP_PERIODIC_RATE);
187+
&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR_CLEAR, &events, telemetry_interval * NX_IP_PERIODIC_RATE);
189188

190189
switch (telemetry_state)
191190
{

MXChip/AZ3166/app/nx_client.c

Lines changed: 24 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -334,72 +334,37 @@ UINT azure_iot_nx_client_entry(
334334
return status;
335335
}
336336

337-
#ifdef ENABLE_DPS
338-
# ifdef ENABLE_X509
339-
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
340-
ip_ptr,
341-
pool_ptr,
342-
dns_ptr,
343-
unix_time_callback,
344-
IOT_DPS_ENDPOINT,
345-
IOT_DPS_ID_SCOPE,
346-
IOT_DPS_REGISTRATION_ID,
347-
"",
337+
status = azure_iot_nx_client_create(
338+
&azure_iot_nx_client, ip_ptr, pool_ptr, dns_ptr, unix_time_callback, IOT_MODEL_ID);
339+
if (status != NX_SUCCESS)
340+
{
341+
printf("ERROR: azure_iot_nx_client_create failed (0x%08x)\r\n", status);
342+
return status;
343+
}
344+
345+
#ifdef ENABLE_X509
346+
status = azure_iot_nx_client_cert_set(&azure_iot_nx_client,
348347
(UCHAR*)iot_x509_device_cert,
349348
iot_x509_device_cert_len,
350349
(UCHAR*)iot_x509_private_key,
351-
iot_x509_private_key_len,
352-
IOT_MODEL_ID);
353-
# else
354-
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
355-
ip_ptr,
356-
pool_ptr,
357-
dns_ptr,
358-
unix_time_callback,
359-
IOT_DPS_ENDPOINT,
360-
IOT_DPS_ID_SCOPE,
361-
IOT_DPS_REGISTRATION_ID,
362-
IOT_PRIMARY_KEY,
363-
NULL,
364-
0,
365-
NULL,
366-
0,
367-
IOT_MODEL_ID);
368-
# endif
350+
iot_x509_private_key_len);
369351
#else
370-
# ifdef ENABLE_X509
371-
status = azure_iot_nx_client_create(&azure_iot_nx_client,
372-
ip_ptr,
373-
pool_ptr,
374-
dns_ptr,
375-
unix_time_callback,
376-
IOT_HUB_HOSTNAME,
377-
IOT_DEVICE_ID,
378-
"",
379-
(UCHAR*)iot_x509_device_cert,
380-
iot_x509_device_cert_len,
381-
(UCHAR*)iot_x509_private_key,
382-
iot_x509_private_key_len,
383-
IOT_MODEL_ID);
384-
# else
385-
status = azure_iot_nx_client_create(&azure_iot_nx_client,
386-
ip_ptr,
387-
pool_ptr,
388-
dns_ptr,
389-
unix_time_callback,
390-
IOT_HUB_HOSTNAME,
391-
IOT_DEVICE_ID,
392-
IOT_PRIMARY_KEY,
393-
NULL,
394-
0,
395-
NULL,
396-
0,
397-
IOT_MODEL_ID);
398-
# endif
352+
status = azure_iot_nx_client_sas_set(&azure_iot_nx_client, IOT_PRIMARY_KEY);
353+
#endif
354+
if (status != NX_SUCCESS)
355+
{
356+
printf("ERROR: azure_iot_nx_client_[sas|cert]_set failed (0x%08x)\r\n", status);
357+
return status;
358+
}
359+
360+
#ifdef ENABLE_DPS
361+
azure_iot_nx_client_dps_create(&azure_iot_nx_client, IOT_DPS_ENDPOINT, IOT_DPS_ID_SCOPE, IOT_DPS_REGISTRATION_ID);
362+
#else
363+
azure_iot_nx_client_hub_create(&azure_iot_nx_client, IOT_HUB_HOSTNAME, IOT_DEVICE_ID);
399364
#endif
400365
if (status != NX_SUCCESS)
401366
{
402-
printf("ERROR: failed to create iot client 0x%04x\r\n", status);
367+
printf("ERROR: azure_iot_nx_client_[hub|dps]_create failed (0x%08x)\r\n", status);
403368
return status;
404369
}
405370

Microchip/ATSAME54-XPRO/app/azure_config.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
// ----------------------------------------------------------------------------
2626
// Azure IoT DPS Self-Signed X509Certificate
27-
// Define this to connect to DPS or Iot Hub using a self-signed X509
27+
// Define this to connect to DPS or Iot Hub using a self-signed X509
2828
/// certificate
2929
// ----------------------------------------------------------------------------
3030
// #define ENABLE_X509
@@ -34,27 +34,23 @@
3434
// Make sure this is the same as the Device ID on the corresponding IoT Hub
3535
// NOTE: To be used only when ENABLE_DPS is NOT defined
3636
// ----------------------------------------------------------------------------
37-
#define IOT_DEVICE_ID ""
37+
#define IOT_DEVICE_ID ""
3838

39-
#ifndef ENABLE_X509
4039
// ----------------------------------------------------------------------------
4140
// Azure IoT SAS Key
4241
// The SAS key generated by configuring an IoT Hub device or DPS individual
4342
// enrollment
4443
// NOTE: To be used only when ENABLE_X509 is not defined
4544
// ----------------------------------------------------------------------------
46-
#define IOT_PRIMARY_KEY ""
47-
#endif
45+
#define IOT_PRIMARY_KEY ""
4846

49-
#ifndef ENABLE_DPS
5047
// ----------------------------------------------------------------------------
5148
// Azure IoT Hub Hostname
5249
// The Hostname found on your IoT Hub Overview page
5350
// NOTE: To be used only when ENABLE_DPS is not defined
5451
// ----------------------------------------------------------------------------
5552
#define IOT_HUB_HOSTNAME ""
5653

57-
#else
5854
// ----------------------------------------------------------------------------
5955
// Azure IoT DPS config
6056
// DPS connection information
@@ -66,6 +62,5 @@
6662
#define IOT_DPS_ENDPOINT "global.azure-devices-provisioning.net"
6763
#define IOT_DPS_ID_SCOPE ""
6864
#define IOT_DPS_REGISTRATION_ID ""
69-
#endif
7065

7166
#endif // _AZURE_CONFIG_H

Microchip/ATSAME54-XPRO/app/legacy/mqtt.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ static void mqtt_c2d_message(AZURE_IOT_MQTT* iot_mqtt, CHAR* properties, CHAR* m
7272
printf("Received C2D message, properties='%s', message='%s'\r\n", properties, message);
7373
}
7474

75-
7675
static void mqtt_device_twin_desired_prop(AZURE_IOT_MQTT* iot_mqtt, CHAR* message)
7776
{
7877
jsmn_parser parser;
@@ -88,8 +87,7 @@ static void mqtt_device_twin_desired_prop(AZURE_IOT_MQTT* iot_mqtt, CHAR* messag
8887
tx_event_flags_set(&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR);
8988

9089
// Confirm reception back to hub
91-
azure_iot_mqtt_respond_int_writeable_property(
92-
iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
90+
azure_iot_mqtt_respond_int_writeable_property(iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
9391
}
9492
}
9593

@@ -133,7 +131,7 @@ UINT azure_iot_mqtt_entry(NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_p
133131
time_get,
134132
IOT_DPS_ENDPOINT,
135133
IOT_DPS_ID_SCOPE,
136-
IOT_DEVICE_ID,
134+
IOT_DPS_REGISTRATION_ID,
137135
IOT_PRIMARY_KEY,
138136
IOT_MODEL_ID);
139137
#else
@@ -181,7 +179,7 @@ UINT azure_iot_mqtt_entry(NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_p
181179
// Sleep
182180
tx_event_flags_get(
183181
&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR_CLEAR, &events, telemetry_interval * NX_IP_PERIODIC_RATE);
184-
182+
185183
#if __SENSOR_BME280__ == 1
186184
// Print the compensated temperature readings
187185
WeatherClick_waitforRead();
@@ -192,7 +190,6 @@ UINT azure_iot_mqtt_entry(NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_p
192190

193191
// Send the temperature as a telemetry event
194192
azure_iot_mqtt_publish_float_telemetry(&azure_iot_mqtt, "temperature", temperature);
195-
196193
}
197194

198195
return NXD_MQTT_SUCCESS;

Microchip/ATSAME54-XPRO/app/nx_client.c

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -194,76 +194,41 @@ UINT azure_iot_nx_client_entry(
194194

195195
if ((status = tx_event_flags_create(&azure_iot_flags, "Azure IoT flags")))
196196
{
197-
printf("FAIL: Unable to create nx_client event flags (0x%04x)\r\n", status);
197+
printf("FAIL: Unable to create nx_client event flags (0x%08x)\r\n", status);
198198
return status;
199199
}
200200

201-
#ifdef ENABLE_DPS
202-
# ifdef ENABLE_X509
203-
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
204-
ip_ptr,
205-
pool_ptr,
206-
dns_ptr,
207-
unix_time_callback,
208-
IOT_DPS_ENDPOINT,
209-
IOT_DPS_ID_SCOPE,
210-
IOT_DPS_REGISTRATION_ID,
211-
"",
201+
status = azure_iot_nx_client_create(
202+
&azure_iot_nx_client, ip_ptr, pool_ptr, dns_ptr, unix_time_callback, IOT_MODEL_ID);
203+
if (status != NX_SUCCESS)
204+
{
205+
printf("ERROR: azure_iot_nx_client_create failed (0x%08x)\r\n", status);
206+
return status;
207+
}
208+
209+
#ifdef ENABLE_X509
210+
status = azure_iot_nx_client_cert_set(&azure_iot_nx_client,
212211
(UCHAR*)iot_x509_device_cert,
213212
iot_x509_device_cert_len,
214213
(UCHAR*)iot_x509_private_key,
215-
iot_x509_private_key_len,
216-
IOT_MODEL_ID);
217-
# else
218-
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
219-
ip_ptr,
220-
pool_ptr,
221-
dns_ptr,
222-
unix_time_callback,
223-
IOT_DPS_ENDPOINT,
224-
IOT_DPS_ID_SCOPE,
225-
IOT_DPS_REGISTRATION_ID,
226-
IOT_PRIMARY_KEY,
227-
NULL,
228-
0,
229-
NULL,
230-
0,
231-
IOT_MODEL_ID);
232-
# endif
214+
iot_x509_private_key_len);
233215
#else
234-
# ifdef ENABLE_X509
235-
status = azure_iot_nx_client_create(&azure_iot_nx_client,
236-
ip_ptr,
237-
pool_ptr,
238-
dns_ptr,
239-
unix_time_callback,
240-
IOT_HUB_HOSTNAME,
241-
IOT_DEVICE_ID,
242-
"",
243-
(UCHAR*)iot_x509_device_cert,
244-
iot_x509_device_cert_len,
245-
(UCHAR*)iot_x509_private_key,
246-
iot_x509_private_key_len,
247-
IOT_MODEL_ID);
248-
# else
249-
status = azure_iot_nx_client_create(&azure_iot_nx_client,
250-
ip_ptr,
251-
pool_ptr,
252-
dns_ptr,
253-
unix_time_callback,
254-
IOT_HUB_HOSTNAME,
255-
IOT_DEVICE_ID,
256-
IOT_PRIMARY_KEY,
257-
NULL,
258-
0,
259-
NULL,
260-
0,
261-
IOT_MODEL_ID);
262-
# endif
216+
status = azure_iot_nx_client_sas_set(&azure_iot_nx_client, IOT_PRIMARY_KEY);
217+
#endif
218+
if (status != NX_SUCCESS)
219+
{
220+
printf("ERROR: azure_iot_nx_client_[sas|cert]_set failed (0x%08x)\r\n", status);
221+
return status;
222+
}
223+
224+
#ifdef ENABLE_DPS
225+
azure_iot_nx_client_dps_create(&azure_iot_nx_client, IOT_DPS_ENDPOINT, IOT_DPS_ID_SCOPE, IOT_DPS_REGISTRATION_ID);
226+
#else
227+
azure_iot_nx_client_hub_create(&azure_iot_nx_client, IOT_HUB_HOSTNAME, IOT_DEVICE_ID);
263228
#endif
264229
if (status != NX_SUCCESS)
265230
{
266-
printf("ERROR: failed to create iot client 0x%04x\r\n", status);
231+
printf("ERROR: azure_iot_nx_client_[hub|dps]_create failed (0x%08x)\r\n", status);
267232
return status;
268233
}
269234

NXP/MIMXRT1050-EVKB/app/azure_config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
// ----------------------------------------------------------------------------
2020
// Azure IoT DPS Self-Signed X509Certificate
21-
// Define this to connect to DPS or Iot Hub using a self-signed X509
21+
// Define this to connect to DPS or Iot Hub using a self-signed X509
2222
/// certificate
2323
// ----------------------------------------------------------------------------
2424
// #define ENABLE_X509
@@ -28,15 +28,15 @@
2828
// Make sure this is the same as the Device ID on the corresponding IoT Hub
2929
// NOTE: To be used only when ENABLE_DPS is NOT defined
3030
// ----------------------------------------------------------------------------
31-
#define IOT_DEVICE_ID ""
31+
#define IOT_DEVICE_ID ""
3232

3333
// ----------------------------------------------------------------------------
3434
// Azure IoT SAS Key
3535
// The SAS key generated by configuring an IoT Hub device or DPS individual
3636
// enrollment
3737
// NOTE: To be used only when ENABLE_X509 is not defined
3838
// ----------------------------------------------------------------------------
39-
#define IOT_PRIMARY_KEY ""
39+
#define IOT_PRIMARY_KEY ""
4040

4141
// ----------------------------------------------------------------------------
4242
// Azure IoT Hub Hostname

NXP/MIMXRT1050-EVKB/app/legacy/mqtt.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ static void mqtt_device_twin_desired_prop(AZURE_IOT_MQTT* iot_mqtt, CHAR* messag
8484
tx_event_flags_set(&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR);
8585

8686
// Confirm reception back to hub
87-
azure_iot_mqtt_respond_int_writeable_property(
88-
iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
87+
azure_iot_mqtt_respond_int_writeable_property(iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
8988
}
9089
}
9190

0 commit comments

Comments
 (0)