Skip to content

Commit

Permalink
Add extensions for json messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gfurtadoalmeida committed Jul 6, 2024
1 parent 4f27624 commit a5f2f2b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern "C"
/**
* @brief Send telemetry data to IoT Hub, from a specific device component.
* @note Contract: https://learn.microsoft.com/en-us/azure/iot-central/core/concepts-telemetry-properties-commands#telemetry
* @note Content type and encoding will be not set.
* @param[in] context IoT context.
* @param[in] component_name Component name.
* @param[in] component_name_length Component name length.
Expand All @@ -43,6 +44,29 @@ extern "C"
AzureIoTHubMessageQoS_t qos,
uint16_t *packet_id);

/**
* @brief Send telemetry data to IoT Hub, from a specific device component,
* setting content type as "application/json" and content encoding as "utf-8".
* @note Contract: https://learn.microsoft.com/en-us/azure/iot-central/core/concepts-telemetry-properties-commands#telemetry
* @param[in] context IoT context.
* @param[in] component_name Component name.
* @param[in] component_name_length Component name length.
* @param[in] payload User defined telemetry payload.
* @param[in] payload_length Payload length.
* @param[in] qos The QOS to use for the telemetry.
* @param[out] packet_id Packet id for the sent telemetry, generated by the IoT context.
* Can be notified of PUBACK for QOS 1 using the @ref AzureIoTHubClientOptions_t `xTelemetryCallback` option.
* If \p qos is @ref eAzureIoTHubMessageQoS0 this value will not be sent on return. Can be `NULL`.
* @return @ref AzureIoTResult_t with the result of the operation.
*/
AzureIoTResult_t azure_iot_hub_send_json_telemetry_from_component(azure_iot_hub_context_t *context,
const uint8_t *component_name,
uint32_t component_name_length,
const uint8_t *payload,
uint32_t payload_length,
AzureIoTHubMessageQoS_t qos,
uint16_t *packet_id);

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,38 @@ extern "C"
const uint8_t *component_name,
uint32_t component_name_length);

/**
* @brief Append a property that refers to the message content type.
* @note It will add a property with name "$.ct" (4 chars) and value \p content_type
* @note The properties init API will not encode properties. In order to support the following characters,
* they must be percent-encoded (RFC3986) as follows: - `/` : `%2F` - `%` : `%25` - `#` : `%23` - `&` : `%26`.
* Only these characters would have to be encoded. If you would like to avoid the need to encode the names/values,
* avoid using these characters in names and values.
* @param[in] message_properties @ref AzureIoTMessageProperties_t to use for the operation.
* @param[in] content_type Content type name.
* @param[in] content_type_length Content type name length.
* @return @ref AzureIoTResult_t with the result of the operation.
*/
AzureIoTResult_t AzureIoTMessage_PropertiesAppendContentType(AzureIoTMessageProperties_t *message_properties,
const uint8_t *content_type,
uint32_t content_type_length);

/**
* @brief Append a property that refers to the message content encoding.
* @note It will add a property with name "$.ce" (4 chars) and value \p content_encoding
* @note The properties init API will not encode properties. In order to support the following characters,
* they must be percent-encoded (RFC3986) as follows: - `/` : `%2F` - `%` : `%25` - `#` : `%23` - `&` : `%26`.
* Only these characters would have to be encoded. If you would like to avoid the need to encode the names/values,
* avoid using these characters in names and values.
* @param[in] message_properties @ref AzureIoTMessageProperties_t to use for the operation.
* @param[in] content_encoding Content type name.
* @param[in] content_encoding_length Content type name length.
* @return @ref AzureIoTResult_t with the result of the operation.
*/
AzureIoTResult_t AzureIoTMessage_PropertiesAppendContentEncoding(AzureIoTMessageProperties_t *message_properties,
const uint8_t *content_encoding,
uint32_t content_encoding_length);

#ifdef __cplusplus
}
#endif
Expand Down
38 changes: 32 additions & 6 deletions components/esp32_iot_azure/src/extension/azure_iot_hub_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ AzureIoTResult_t azure_iot_hub_send_telemetry_from_component(azure_iot_hub_conte
AzureIoTHubMessageQoS_t qos,
uint16_t *packet_id)
{
// Message format: /?prop=value
// 5 -> $.sub
// 1 -> =
// There's no need to count for -> /?
// Message format: /?property=value
// 6 -> $.sub=

uint8_t properties_buffer[AZURE_CONST_COMPONENT_NAME_MAX_LENGTH + 5 + 1];
uint8_t properties_buffer[AZURE_CONST_COMPONENT_NAME_MAX_LENGTH + 6];
AzureIoTMessageProperties_t properties_message;

AZ_CHECK_BEGIN()
Expand All @@ -37,4 +35,32 @@ AzureIoTResult_t azure_iot_hub_send_telemetry_from_component(azure_iot_hub_conte
AZ_CHECK(azure_iot_hub_send_telemetry(context, payload, payload_length, &properties_message, qos, packet_id))

AZ_CHECK_RETURN_LAST()
}
}

AzureIoTResult_t azure_iot_hub_send_json_telemetry_from_component(azure_iot_hub_context_t *context,
const uint8_t *component_name,
uint32_t component_name_length,
const uint8_t *payload,
uint32_t payload_length,
AzureIoTHubMessageQoS_t qos,
uint16_t *packet_id)
{
// Message format: /?property=value
// 6 -> $.sub=
// 5 -> $.ct=
// 5 -> $.ce=

uint8_t properties_buffer[AZURE_CONST_COMPONENT_NAME_MAX_LENGTH + 6 + 5 + 5];
AzureIoTMessageProperties_t properties_message;

AZ_CHECK_BEGIN()

AZ_CHECK(AzureIoTMessage_PropertiesInit(&properties_message, properties_buffer, 0, sizeof(properties_buffer)))
AZ_CHECK(AzureIoTMessage_PropertiesAppendComponentName(&properties_message, component_name, component_name_length))
AZ_CHECK(AzureIoTMessage_PropertiesAppendContentType(&properties_message, (uint8_t*)"application/json", sizeof("application/json") - 1))
AZ_CHECK(AzureIoTMessage_PropertiesAppendContentEncoding(&properties_message, (uint8_t*)"utf-8", sizeof("utf-8") - 1))

AZ_CHECK(azure_iot_hub_send_telemetry(context, payload, payload_length, &properties_message, qos, packet_id))

AZ_CHECK_RETURN_LAST()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,25 @@ AzureIoTResult_t AzureIoTMessage_PropertiesAppendComponentName(AzureIoTMessagePr
component_name,
component_name_length);
}

AzureIoTResult_t AzureIoTMessage_PropertiesAppendContentType(AzureIoTMessageProperties_t *message_properties,
const uint8_t *content_type,
uint32_t content_type_length)
{
return AzureIoTMessage_PropertiesAppend(message_properties,
(uint8_t *)"$.ct",
sizeof("$.ct") - 1,
content_type,
content_type_length);
}

AzureIoTResult_t AzureIoTMessage_PropertiesAppendContentEncoding(AzureIoTMessageProperties_t *message_properties,
const uint8_t *content_encoding,
uint32_t content_encoding_length)
{
return AzureIoTMessage_PropertiesAppend(message_properties,
(uint8_t *)"$.ce",
sizeof("$.ce") - 1,
content_encoding,
content_encoding_length);
}
2 changes: 1 addition & 1 deletion docs/wiki/how_to_use.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 1. Clone the Repository

Clone this repository **_recursively_** and copy the folder ["components/esp32_iot_azure"](../../components/esp32_iot_azure/) to the components folder of your ESP-IDF project.
Clone this repository **_recursively_** (`--recurse-submodules`) and copy the folder ["components/esp32_iot_azure"](../../components/esp32_iot_azure/) to the components folder of your ESP-IDF project.

## 2. Configure

Expand Down

0 comments on commit a5f2f2b

Please sign in to comment.