Skip to content

refactor nx client helper class #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 20, 2020
6 changes: 3 additions & 3 deletions MXChip/AZ3166/app/azure_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef enum

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

// ----------------------------------------------------------------------------
// Azure IoT SAS Key
// The SAS key generated by configuring an IoT Hub device or DPS individual
// enrollment
// NOTE: To be used only when ENABLE_X509 is not defined
// ----------------------------------------------------------------------------
#define IOT_PRIMARY_KEY ""
#define IOT_PRIMARY_KEY ""

// ----------------------------------------------------------------------------
// Azure IoT Hub Hostname
Expand Down
5 changes: 2 additions & 3 deletions MXChip/AZ3166/app/legacy/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ static void mqtt_device_twin_desired_prop(AZURE_IOT_MQTT* iot_mqtt, CHAR* messag
tx_event_flags_set(&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR);

// Confirm reception back to hub
azure_iot_mqtt_respond_int_writeable_property(
iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
azure_iot_mqtt_respond_int_writeable_property(iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
}
}

Expand Down Expand Up @@ -185,7 +184,7 @@ UINT azure_iot_mqtt_entry(NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_p
{
// Sleep
tx_event_flags_get(
&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR_CLEAR, &events, telemetry_interval * NX_IP_PERIODIC_RATE);
&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR_CLEAR, &events, telemetry_interval * NX_IP_PERIODIC_RATE);

switch (telemetry_state)
{
Expand Down
83 changes: 24 additions & 59 deletions MXChip/AZ3166/app/nx_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,72 +334,37 @@ UINT azure_iot_nx_client_entry(
return status;
}

#ifdef ENABLE_DPS
# ifdef ENABLE_X509
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
ip_ptr,
pool_ptr,
dns_ptr,
unix_time_callback,
IOT_DPS_ENDPOINT,
IOT_DPS_ID_SCOPE,
IOT_DPS_REGISTRATION_ID,
"",
status = azure_iot_nx_client_create(
&azure_iot_nx_client, ip_ptr, pool_ptr, dns_ptr, unix_time_callback, IOT_MODEL_ID);
if (status != NX_SUCCESS)
{
printf("ERROR: azure_iot_nx_client_create failed (0x%08x)\r\n", status);
return status;
}

#ifdef ENABLE_X509
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,
IOT_MODEL_ID);
# else
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
ip_ptr,
pool_ptr,
dns_ptr,
unix_time_callback,
IOT_DPS_ENDPOINT,
IOT_DPS_ID_SCOPE,
IOT_DPS_REGISTRATION_ID,
IOT_PRIMARY_KEY,
NULL,
0,
NULL,
0,
IOT_MODEL_ID);
# endif
iot_x509_private_key_len);
#else
# ifdef ENABLE_X509
status = azure_iot_nx_client_create(&azure_iot_nx_client,
ip_ptr,
pool_ptr,
dns_ptr,
unix_time_callback,
IOT_HUB_HOSTNAME,
IOT_DEVICE_ID,
"",
(UCHAR*)iot_x509_device_cert,
iot_x509_device_cert_len,
(UCHAR*)iot_x509_private_key,
iot_x509_private_key_len,
IOT_MODEL_ID);
# else
status = azure_iot_nx_client_create(&azure_iot_nx_client,
ip_ptr,
pool_ptr,
dns_ptr,
unix_time_callback,
IOT_HUB_HOSTNAME,
IOT_DEVICE_ID,
IOT_PRIMARY_KEY,
NULL,
0,
NULL,
0,
IOT_MODEL_ID);
# endif
status = azure_iot_nx_client_sas_set(&azure_iot_nx_client, IOT_PRIMARY_KEY);
#endif
if (status != NX_SUCCESS)
{
printf("ERROR: azure_iot_nx_client_[sas|cert]_set failed (0x%08x)\r\n", status);
return status;
}

#ifdef ENABLE_DPS
azure_iot_nx_client_dps_create(&azure_iot_nx_client, IOT_DPS_ENDPOINT, IOT_DPS_ID_SCOPE, IOT_DPS_REGISTRATION_ID);
#else
azure_iot_nx_client_hub_create(&azure_iot_nx_client, IOT_HUB_HOSTNAME, IOT_DEVICE_ID);
#endif
if (status != NX_SUCCESS)
{
printf("ERROR: failed to create iot client 0x%04x\r\n", status);
printf("ERROR: azure_iot_nx_client_[hub|dps]_create failed (0x%08x)\r\n", status);
return status;
}

Expand Down
11 changes: 3 additions & 8 deletions Microchip/ATSAME54-XPRO/app/azure_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

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

#ifndef ENABLE_X509
// ----------------------------------------------------------------------------
// Azure IoT SAS Key
// The SAS key generated by configuring an IoT Hub device or DPS individual
// enrollment
// NOTE: To be used only when ENABLE_X509 is not defined
// ----------------------------------------------------------------------------
#define IOT_PRIMARY_KEY ""
#endif
#define IOT_PRIMARY_KEY ""

#ifndef ENABLE_DPS
// ----------------------------------------------------------------------------
// Azure IoT Hub Hostname
// The Hostname found on your IoT Hub Overview page
// NOTE: To be used only when ENABLE_DPS is not defined
// ----------------------------------------------------------------------------
#define IOT_HUB_HOSTNAME ""

#else
// ----------------------------------------------------------------------------
// Azure IoT DPS config
// DPS connection information
Expand All @@ -66,6 +62,5 @@
#define IOT_DPS_ENDPOINT "global.azure-devices-provisioning.net"
#define IOT_DPS_ID_SCOPE ""
#define IOT_DPS_REGISTRATION_ID ""
#endif

#endif // _AZURE_CONFIG_H
9 changes: 3 additions & 6 deletions Microchip/ATSAME54-XPRO/app/legacy/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ static void mqtt_c2d_message(AZURE_IOT_MQTT* iot_mqtt, CHAR* properties, CHAR* m
printf("Received C2D message, properties='%s', message='%s'\r\n", properties, message);
}


static void mqtt_device_twin_desired_prop(AZURE_IOT_MQTT* iot_mqtt, CHAR* message)
{
jsmn_parser parser;
Expand All @@ -88,8 +87,7 @@ static void mqtt_device_twin_desired_prop(AZURE_IOT_MQTT* iot_mqtt, CHAR* messag
tx_event_flags_set(&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR);

// Confirm reception back to hub
azure_iot_mqtt_respond_int_writeable_property(
iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
azure_iot_mqtt_respond_int_writeable_property(iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
}
}

Expand Down Expand Up @@ -133,7 +131,7 @@ UINT azure_iot_mqtt_entry(NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_p
time_get,
IOT_DPS_ENDPOINT,
IOT_DPS_ID_SCOPE,
IOT_DEVICE_ID,
IOT_DPS_REGISTRATION_ID,
IOT_PRIMARY_KEY,
IOT_MODEL_ID);
#else
Expand Down Expand Up @@ -181,7 +179,7 @@ UINT azure_iot_mqtt_entry(NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_p
// Sleep
tx_event_flags_get(
&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR_CLEAR, &events, telemetry_interval * NX_IP_PERIODIC_RATE);

#if __SENSOR_BME280__ == 1
// Print the compensated temperature readings
WeatherClick_waitforRead();
Expand All @@ -192,7 +190,6 @@ UINT azure_iot_mqtt_entry(NX_IP* ip_ptr, NX_PACKET_POOL* pool_ptr, NX_DNS* dns_p

// Send the temperature as a telemetry event
azure_iot_mqtt_publish_float_telemetry(&azure_iot_mqtt, "temperature", temperature);

}

return NXD_MQTT_SUCCESS;
Expand Down
85 changes: 25 additions & 60 deletions Microchip/ATSAME54-XPRO/app/nx_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,76 +194,41 @@ UINT azure_iot_nx_client_entry(

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

#ifdef ENABLE_DPS
# ifdef ENABLE_X509
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
ip_ptr,
pool_ptr,
dns_ptr,
unix_time_callback,
IOT_DPS_ENDPOINT,
IOT_DPS_ID_SCOPE,
IOT_DPS_REGISTRATION_ID,
"",
status = azure_iot_nx_client_create(
&azure_iot_nx_client, ip_ptr, pool_ptr, dns_ptr, unix_time_callback, IOT_MODEL_ID);
if (status != NX_SUCCESS)
{
printf("ERROR: azure_iot_nx_client_create failed (0x%08x)\r\n", status);
return status;
}

#ifdef ENABLE_X509
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,
IOT_MODEL_ID);
# else
status = azure_iot_nx_client_dps_create(&azure_iot_nx_client,
ip_ptr,
pool_ptr,
dns_ptr,
unix_time_callback,
IOT_DPS_ENDPOINT,
IOT_DPS_ID_SCOPE,
IOT_DPS_REGISTRATION_ID,
IOT_PRIMARY_KEY,
NULL,
0,
NULL,
0,
IOT_MODEL_ID);
# endif
iot_x509_private_key_len);
#else
# ifdef ENABLE_X509
status = azure_iot_nx_client_create(&azure_iot_nx_client,
ip_ptr,
pool_ptr,
dns_ptr,
unix_time_callback,
IOT_HUB_HOSTNAME,
IOT_DEVICE_ID,
"",
(UCHAR*)iot_x509_device_cert,
iot_x509_device_cert_len,
(UCHAR*)iot_x509_private_key,
iot_x509_private_key_len,
IOT_MODEL_ID);
# else
status = azure_iot_nx_client_create(&azure_iot_nx_client,
ip_ptr,
pool_ptr,
dns_ptr,
unix_time_callback,
IOT_HUB_HOSTNAME,
IOT_DEVICE_ID,
IOT_PRIMARY_KEY,
NULL,
0,
NULL,
0,
IOT_MODEL_ID);
# endif
status = azure_iot_nx_client_sas_set(&azure_iot_nx_client, IOT_PRIMARY_KEY);
#endif
if (status != NX_SUCCESS)
{
printf("ERROR: azure_iot_nx_client_[sas|cert]_set failed (0x%08x)\r\n", status);
return status;
}

#ifdef ENABLE_DPS
azure_iot_nx_client_dps_create(&azure_iot_nx_client, IOT_DPS_ENDPOINT, IOT_DPS_ID_SCOPE, IOT_DPS_REGISTRATION_ID);
#else
azure_iot_nx_client_hub_create(&azure_iot_nx_client, IOT_HUB_HOSTNAME, IOT_DEVICE_ID);
#endif
if (status != NX_SUCCESS)
{
printf("ERROR: failed to create iot client 0x%04x\r\n", status);
printf("ERROR: azure_iot_nx_client_[hub|dps]_create failed (0x%08x)\r\n", status);
return status;
}

Expand Down
6 changes: 3 additions & 3 deletions NXP/MIMXRT1050-EVKB/app/azure_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

// ----------------------------------------------------------------------------
// Azure IoT DPS Self-Signed X509Certificate
// Define this to connect to DPS or Iot Hub using a self-signed X509
// Define this to connect to DPS or Iot Hub using a self-signed X509
/// certificate
// ----------------------------------------------------------------------------
// #define ENABLE_X509
Expand All @@ -28,15 +28,15 @@
// Make sure this is the same as the Device ID on the corresponding IoT Hub
// NOTE: To be used only when ENABLE_DPS is NOT defined
// ----------------------------------------------------------------------------
#define IOT_DEVICE_ID ""
#define IOT_DEVICE_ID ""

// ----------------------------------------------------------------------------
// Azure IoT SAS Key
// The SAS key generated by configuring an IoT Hub device or DPS individual
// enrollment
// NOTE: To be used only when ENABLE_X509 is not defined
// ----------------------------------------------------------------------------
#define IOT_PRIMARY_KEY ""
#define IOT_PRIMARY_KEY ""

// ----------------------------------------------------------------------------
// Azure IoT Hub Hostname
Expand Down
3 changes: 1 addition & 2 deletions NXP/MIMXRT1050-EVKB/app/legacy/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ static void mqtt_device_twin_desired_prop(AZURE_IOT_MQTT* iot_mqtt, CHAR* messag
tx_event_flags_set(&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR);

// Confirm reception back to hub
azure_iot_mqtt_respond_int_writeable_property(
iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
azure_iot_mqtt_respond_int_writeable_property(iot_mqtt, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200);
}
}

Expand Down
Loading