Fix https://github.com/Azure/azure-iot-pal-esp32/issues/3 and update SDK
This commit is contained in:
Родитель
9d2244e1a4
Коммит
4059c29152
|
@ -0,0 +1,241 @@
|
|||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "iothub_client.h"
|
||||
#include "iothub_message.h"
|
||||
#include "azure_c_shared_utility/threadapi.h"
|
||||
#include "azure_c_shared_utility/crt_abstractions.h"
|
||||
#include "azure_c_shared_utility/platform.h"
|
||||
#include "azure_c_shared_utility/shared_util_options.h"
|
||||
#include "iothubtransportmqtt.h"
|
||||
#include "iothub_client_options.h"
|
||||
|
||||
#ifdef MBED_BUILD_TIMESTAMP
|
||||
#define SET_TRUSTED_CERT_IN_SAMPLES
|
||||
#endif // MBED_BUILD_TIMESTAMP
|
||||
|
||||
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
|
||||
#include "certs.h"
|
||||
#endif // SET_TRUSTED_CERT_IN_SAMPLES
|
||||
|
||||
/*String containing Hostname, Device Id & Device Key in the format: */
|
||||
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
|
||||
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */
|
||||
static const char* connectionString = "HostName=royspr-m07f130.azure-devices.net;DeviceId=TestDevice01;SharedAccessKey=3s04a6jr99+Oog8KeqG2MnN9igkqu3G+c3JJOLW2tM8=";
|
||||
|
||||
static int callbackCounter;
|
||||
static char msgText[1024];
|
||||
static char propText[1024];
|
||||
static bool g_continueRunning;
|
||||
#define MESSAGE_COUNT 5
|
||||
#define DOWORK_LOOP_NUM 3
|
||||
|
||||
|
||||
typedef struct EVENT_INSTANCE_TAG
|
||||
{
|
||||
IOTHUB_MESSAGE_HANDLE messageHandle;
|
||||
size_t messageTrackingId; // For tracking the messages within the user callback.
|
||||
} EVENT_INSTANCE;
|
||||
|
||||
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
|
||||
{
|
||||
int* counter = (int*)userContextCallback;
|
||||
const char* buffer;
|
||||
size_t size;
|
||||
MAP_HANDLE mapProperties;
|
||||
const char* messageId;
|
||||
const char* correlationId;
|
||||
const char* userDefinedContentType;
|
||||
const char* userDefinedContentEncoding;
|
||||
|
||||
// Message properties
|
||||
if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
|
||||
{
|
||||
messageId = "<null>";
|
||||
}
|
||||
|
||||
if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL)
|
||||
{
|
||||
correlationId = "<null>";
|
||||
}
|
||||
|
||||
if ((userDefinedContentType = IoTHubMessage_GetContentTypeSystemProperty(message)) == NULL)
|
||||
{
|
||||
userDefinedContentType = "<null>";
|
||||
}
|
||||
|
||||
if ((userDefinedContentEncoding = IoTHubMessage_GetContentEncodingSystemProperty(message)) == NULL)
|
||||
{
|
||||
userDefinedContentEncoding = "<null>";
|
||||
}
|
||||
|
||||
// Message content
|
||||
if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
|
||||
{
|
||||
(void)printf("unable to retrieve the message data\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
(void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n Content-Type: %s\r\n Content-Encoding: %s\r\n Data: <<<%.*s>>> & Size=%d\r\n",
|
||||
*counter, messageId, correlationId, userDefinedContentType, userDefinedContentEncoding, (int)size, buffer, (int)size);
|
||||
// If we receive the work 'quit' then we stop running
|
||||
if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0)
|
||||
{
|
||||
g_continueRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve properties from the message
|
||||
mapProperties = IoTHubMessage_Properties(message);
|
||||
if (mapProperties != NULL)
|
||||
{
|
||||
const char*const* keys;
|
||||
const char*const* values;
|
||||
size_t propertyCount = 0;
|
||||
if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
|
||||
{
|
||||
if (propertyCount > 0)
|
||||
{
|
||||
size_t index;
|
||||
|
||||
printf(" Message Properties:\r\n");
|
||||
for (index = 0; index < propertyCount; index++)
|
||||
{
|
||||
(void)printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
|
||||
}
|
||||
(void)printf("\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Some device specific action code goes here... */
|
||||
(*counter)++;
|
||||
return IOTHUBMESSAGE_ACCEPTED;
|
||||
}
|
||||
|
||||
static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
|
||||
{
|
||||
EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
|
||||
(void)printf("Confirmation[%d] received for message tracking id = %zu with result = %s\r\n", callbackCounter, eventInstance->messageTrackingId, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
|
||||
/* Some device specific action code goes here... */
|
||||
callbackCounter++;
|
||||
IoTHubMessage_Destroy(eventInstance->messageHandle);
|
||||
}
|
||||
|
||||
void iothub_client_sample_mqtt_run(void)
|
||||
{
|
||||
IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
|
||||
|
||||
EVENT_INSTANCE messages[MESSAGE_COUNT];
|
||||
|
||||
g_continueRunning = true;
|
||||
srand((unsigned int)time(NULL));
|
||||
double avgWindSpeed = 10.0;
|
||||
double minTemperature = 20.0;
|
||||
double minHumidity = 60.0;
|
||||
|
||||
callbackCounter = 0;
|
||||
int receiveContext = 0;
|
||||
|
||||
if (platform_init() != 0)
|
||||
{
|
||||
(void)printf("Failed to initialize the platform.\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol)) == NULL)
|
||||
{
|
||||
(void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
bool traceOn = true;
|
||||
IoTHubClient_LL_SetOption(iotHubClientHandle, OPTION_LOG_TRACE, &traceOn);
|
||||
|
||||
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
|
||||
// For mbed add the certificate information
|
||||
if (IoTHubClient_LL_SetOption(iotHubClientHandle, OPTION_TRUSTED_CERT, certificates) != IOTHUB_CLIENT_OK)
|
||||
{
|
||||
printf("failure to set option \"TrustedCerts\"\r\n");
|
||||
}
|
||||
#endif // SET_TRUSTED_CERT_IN_SAMPLES
|
||||
|
||||
/* Setting Message call back, so we can receive Commands. */
|
||||
if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
|
||||
{
|
||||
(void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
(void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n");
|
||||
|
||||
/* Now that we are ready to receive commands, let's send some messages */
|
||||
size_t iterator = 0;
|
||||
double temperature = 0;
|
||||
double humidity = 0;
|
||||
do
|
||||
{
|
||||
if (iterator < MESSAGE_COUNT)
|
||||
{
|
||||
temperature = minTemperature + (rand() % 10);
|
||||
humidity = minHumidity + (rand() % 20);
|
||||
sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f,\"temperature\":%.2f,\"humidity\":%.2f}", avgWindSpeed + (rand() % 4 + 2), temperature, humidity);
|
||||
if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
|
||||
{
|
||||
(void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
(void)IoTHubMessage_SetMessageId(messages[iterator].messageHandle, "MSG_ID");
|
||||
(void)IoTHubMessage_SetCorrelationId(messages[iterator].messageHandle, "CORE_ID");
|
||||
(void)IoTHubMessage_SetContentTypeSystemProperty(messages[iterator].messageHandle, "application%2Fjson");
|
||||
(void)IoTHubMessage_SetContentEncodingSystemProperty(messages[iterator].messageHandle, "utf-8");
|
||||
|
||||
messages[iterator].messageTrackingId = iterator;
|
||||
MAP_HANDLE propMap = IoTHubMessage_Properties(messages[iterator].messageHandle);
|
||||
(void)sprintf_s(propText, sizeof(propText), temperature > 28 ? "true" : "false");
|
||||
if (Map_AddOrUpdate(propMap, "temperatureAlert", propText) != MAP_OK)
|
||||
{
|
||||
(void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
|
||||
}
|
||||
|
||||
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle, SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK)
|
||||
{
|
||||
(void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
(void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", (int)iterator);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
IoTHubClient_LL_DoWork(iotHubClientHandle);
|
||||
ThreadAPI_Sleep(1);
|
||||
|
||||
iterator++;
|
||||
} while (g_continueRunning);
|
||||
|
||||
(void)printf("iothub_client_sample_mqtt has gotten quit message, call DoWork %d more time to complete final sending...\r\n", DOWORK_LOOP_NUM);
|
||||
size_t index = 0;
|
||||
for (index = 0; index < DOWORK_LOOP_NUM; index++)
|
||||
{
|
||||
IoTHubClient_LL_DoWork(iotHubClientHandle);
|
||||
ThreadAPI_Sleep(1);
|
||||
}
|
||||
}
|
||||
IoTHubClient_LL_Destroy(iotHubClientHandle);
|
||||
}
|
||||
platform_deinit();
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
iothub_client_sample_mqtt_run();
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
#ifndef IOTHUB_CLIENT_SAMPLE_MQTT_H
|
||||
#define IOTHUB_CLIENT_SAMPLE_MQTT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void iothub_client_sample_mqtt_run(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* IOTHUB_CLIENT_SAMPLE_MQTT_H */
|
|
@ -20,7 +20,7 @@ cd $IDF_PATH/components/azure-iot/sample
|
|||
echo "building in" $(pwd)
|
||||
|
||||
# Copy the sample files
|
||||
sample_dir=$IDF_PATH/components/azure-iot/sdk/iothub_client/samples/iothub_client_sample_mqtt
|
||||
sample_dir=$IDF_PATH/components/azure-iot/build_all
|
||||
proj_dir=$IDF_PATH/components/azure-iot/sample/main
|
||||
cp $sample_dir/iothub_client_sample_mqtt.c $proj_dir
|
||||
cp $sample_dir/iothub_client_sample_mqtt.h $proj_dir
|
||||
|
|
|
@ -85,9 +85,11 @@ static void enter_tlsio_error_state(TLS_IO_INSTANCE* tls_io_instance)
|
|||
/* Codes_SRS_TLSIO_30_005: [ When the adapter enters TLSIO_STATE_EXT_ERROR it shall call the on_io_error function and pass the on_io_error_context that were supplied in tlsio_open . ]*/
|
||||
static void enter_open_error_state(TLS_IO_INSTANCE* tls_io_instance)
|
||||
{
|
||||
enter_tlsio_error_state(tls_io_instance);
|
||||
// on_open_complete has already been checked for non-NULL
|
||||
tls_io_instance->on_open_complete(tls_io_instance->on_open_complete_context, IO_OPEN_ERROR);
|
||||
// save instance variables in case the framework destroys this object before we exit
|
||||
ON_IO_OPEN_COMPLETE on_open_complete = tls_io_instance->on_open_complete;
|
||||
void* on_open_complete_context = tls_io_instance->on_open_complete_context;
|
||||
enter_tlsio_error_state(tls_io_instance);
|
||||
on_open_complete(on_open_complete_context, IO_OPEN_ERROR);
|
||||
}
|
||||
|
||||
// Return true if a message was available to remove
|
||||
|
|
2
sdk
2
sdk
|
@ -1 +1 @@
|
|||
Subproject commit 54dde321dcded3e6099da505ea4c984d3c53aab5
|
||||
Subproject commit fea54b04f5b832cf0789149a8fbac616b5e4ed34
|
Загрузка…
Ссылка в новой задаче