Skip to content

update device twin logic at startup #165

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 3 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 111 additions & 118 deletions MXChip/AZ3166/app/nx_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,115 +56,6 @@ static TX_EVENT_FLAGS_GROUP azure_iot_flags;

static int32_t telemetry_interval = 10;

static void set_led_state(bool level)
{
if (level)
{
printf("LED is turned ON\r\n");
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
}
else
{
printf("LED is turned OFF\r\n");
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
}
}

static void direct_method_cb(AZURE_IOT_NX_CONTEXT* nx_context,
const UCHAR* method,
USHORT method_length,
UCHAR* payload,
USHORT payload_length,
VOID* context,
USHORT context_length)
{
UINT status;
UINT http_status = 501;
CHAR* http_response = "{}";

if (strncmp((CHAR*)method, SET_LED_STATE_COMMAND, method_length) == 0)
{
bool arg = (strncmp((CHAR*)payload, "true", payload_length) == 0);
set_led_state(arg);

azure_iot_nx_client_publish_bool_property(&azure_iot_nx_client, LED_STATE_PROPERTY, arg);

http_status = 200;
}

else if (strncmp((CHAR*)method, SET_DISPLAY_TEXT_COMMAND, method_length) == 0)
{
// drop the first and last character to remove the quotes
screen_printn((CHAR*)payload + 1, payload_length - 2, L0);

http_status = 200;
}

if ((status = nx_azure_iot_hub_client_direct_method_message_response(&nx_context->iothub_client,
http_status,
context,
context_length,
(UCHAR*)http_response,
strlen(http_response),
NX_WAIT_FOREVER)))
{
printf("Direct method response failed! (0x%08x)\r\n", status);
return;
}
}

static void device_twin_desired_property_cb(UCHAR* component_name,
UINT component_name_len,
UCHAR* property_name,
UINT property_name_len,
NX_AZURE_IOT_JSON_READER property_value_reader,
UINT version,
VOID* userContextCallback)
{
UINT status;
AZURE_IOT_NX_CONTEXT* nx_context = (AZURE_IOT_NX_CONTEXT*)userContextCallback;

if (strncmp((CHAR*)property_name, TELEMETRY_INTERVAL_PROPERTY, property_name_len) == 0)
{
status = nx_azure_iot_json_reader_token_int32_get(&property_value_reader, &telemetry_interval);
if (status == NX_AZURE_IOT_SUCCESS)
{
// Set a telemetry event so we pick up the change immediately
tx_event_flags_set(&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR);

// Confirm reception back to hub
azure_nx_client_respond_int_writeable_property(
nx_context, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200, version);
}
}
}

static void device_twin_property_cb(UCHAR* component_name,
UINT component_name_len,
UCHAR* property_name,
UINT property_name_len,
NX_AZURE_IOT_JSON_READER property_value_reader,
UINT version,
VOID* userContextCallback)
{
UINT status;
AZURE_IOT_NX_CONTEXT* nx_context = (AZURE_IOT_NX_CONTEXT*)userContextCallback;

if (strncmp((CHAR*)property_name, TELEMETRY_INTERVAL_PROPERTY, property_name_len) == 0)
{
status = nx_azure_iot_json_reader_token_int32_get(&property_value_reader, &telemetry_interval);
if (status == NX_AZURE_IOT_SUCCESS)
{
// Set a telemetry event so we pick up the change immediately
tx_event_flags_set(&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR);
}
}

// Confirm reception back to hub
azure_nx_client_respond_int_writeable_property(
nx_context, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200, version);
}

static UINT append_device_info_properties(NX_AZURE_IOT_JSON_WRITER* json_writer, VOID* context)
{
if (nx_azure_iot_json_writer_append_property_with_string_value(json_writer,
Expand Down Expand Up @@ -321,6 +212,111 @@ static UINT append_device_telemetry_gyroscope(NX_AZURE_IOT_JSON_WRITER* json_wri
return NX_AZURE_IOT_SUCCESS;
}

static void set_led_state(bool level)
{
if (level)
{
printf("LED is turned ON\r\n");
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
}
else
{
printf("LED is turned OFF\r\n");
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
}
}

static void direct_method_cb(AZURE_IOT_NX_CONTEXT* nx_context,
const UCHAR* method,
USHORT method_length,
UCHAR* payload,
USHORT payload_length,
VOID* context,
USHORT context_length)
{
UINT status;
UINT http_status = 501;
CHAR* http_response = "{}";

if (strncmp((CHAR*)method, SET_LED_STATE_COMMAND, method_length) == 0)
{
bool arg = (strncmp((CHAR*)payload, "true", payload_length) == 0);
set_led_state(arg);

azure_iot_nx_client_publish_bool_property(&azure_iot_nx_client, LED_STATE_PROPERTY, arg);

http_status = 200;
}

else if (strncmp((CHAR*)method, SET_DISPLAY_TEXT_COMMAND, method_length) == 0)
{
// drop the first and last character to remove the quotes
screen_printn((CHAR*)payload + 1, payload_length - 2, L0);

http_status = 200;
}

if ((status = nx_azure_iot_hub_client_direct_method_message_response(&nx_context->iothub_client,
http_status,
context,
context_length,
(UCHAR*)http_response,
strlen(http_response),
NX_WAIT_FOREVER)))
{
printf("Direct method response failed! (0x%08x)\r\n", status);
return;
}
}

static void device_twin_desired_property_cb(UCHAR* component_name,
UINT component_name_len,
UCHAR* property_name,
UINT property_name_len,
NX_AZURE_IOT_JSON_READER property_value_reader,
UINT version,
VOID* userContextCallback)
{
UINT status;
AZURE_IOT_NX_CONTEXT* nx_context = (AZURE_IOT_NX_CONTEXT*)userContextCallback;

if (strncmp((CHAR*)property_name, TELEMETRY_INTERVAL_PROPERTY, property_name_len) == 0)
{
status = nx_azure_iot_json_reader_token_int32_get(&property_value_reader, &telemetry_interval);
if (status == NX_AZURE_IOT_SUCCESS)
{
// Confirm reception back to hub
azure_nx_client_respond_int_writeable_property(
nx_context, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval, 200, version);

// Set a telemetry event so we pick up the change immediately
tx_event_flags_set(&azure_iot_flags, TELEMETRY_INTERVAL_EVENT, TX_OR);
}
}
}

static void device_twin_property_cb(UCHAR* component_name,
UINT component_name_len,
UCHAR* property_name,
UINT property_name_len,
NX_AZURE_IOT_JSON_READER property_value_reader,
UINT version,
VOID* userContextCallback)
{
if (strncmp((CHAR*)property_name, TELEMETRY_INTERVAL_PROPERTY, property_name_len) == 0)
{
nx_azure_iot_json_reader_token_int32_get(&property_value_reader, &telemetry_interval);
}
}

static void device_twin_received_cb(AZURE_IOT_NX_CONTEXT* nx_context)
{
azure_iot_nx_client_publish_int_writeable_property(nx_context, TELEMETRY_INTERVAL_PROPERTY, telemetry_interval);
azure_iot_nx_client_publish_bool_property(&azure_iot_nx_client, LED_STATE_PROPERTY, false);
azure_iot_nx_client_publish_properties(
&azure_iot_nx_client, DEVICE_INFO_COMPONENT_NAME, append_device_info_properties);
}

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))
{
Expand All @@ -330,17 +326,17 @@ 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;
}

status = azure_iot_nx_client_create(
&azure_iot_nx_client, ip_ptr, pool_ptr, dns_ptr, unix_time_callback, IOT_MODEL_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,
Expand Down Expand Up @@ -372,6 +368,7 @@ UINT azure_iot_nx_client_entry(
azure_iot_nx_client_register_direct_method(&azure_iot_nx_client, direct_method_cb);
azure_iot_nx_client_register_device_twin_desired_prop(&azure_iot_nx_client, device_twin_desired_property_cb);
azure_iot_nx_client_register_device_twin_prop(&azure_iot_nx_client, device_twin_property_cb);
azure_iot_nx_client_register_device_twin_received(&azure_iot_nx_client, device_twin_received_cb);

if ((status = azure_iot_nx_client_connect(&azure_iot_nx_client)))
{
Expand All @@ -387,13 +384,9 @@ UINT azure_iot_nx_client_entry(
return status;
}

// Send properties
azure_iot_nx_client_publish_bool_property(&azure_iot_nx_client, LED_STATE_PROPERTY, false);
azure_iot_nx_client_publish_properties(
&azure_iot_nx_client, DEVICE_INFO_COMPONENT_NAME, append_device_info_properties);

printf("\r\nStarting Main loop\r\n");
screen_print("Azure IoT", L0);

while (true)
{
tx_event_flags_get(
Expand Down
2 changes: 2 additions & 0 deletions MXChip/AZ3166/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ products:
- azure-iot
- azure-iot-pnp
- azure-rtos
- azure-iot-dps
- azure-iot-hub
---

# Getting started with the MXCHIP AZ3166 IoT DevKit
Expand Down
Loading