A simple IoTHub sample using AMQP as transport
Dependencies: EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed azure_c_shared_utility serializer wolfSSL azure_uamqp_c
This sample showcases the usage of Azure IoT client libraries with the AMQP transport for sending/receiving raw messages from an IoT Hub.
Diff: simplesample_amqp.c
- Revision:
- 0:caa8884fe136
- Child:
- 2:fc3132155be8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/simplesample_amqp.c Tue Sep 15 22:47:10 2015 -0700 @@ -0,0 +1,192 @@ +// 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 <stdint.h> + +#include "serializer.h" +#include "threadapi.h" +#include "sastoken.h" +#include "iothub_client.h" +#include "iothubtransportamqp.h" +#include "iothub_client_ll.h" + +#ifdef MBED_BUILD_TIMESTAMP +#include "certs.h" +#endif // MBED_BUILD_TIMESTAMP + +static const char* connectionString = "HostName=iot-device-client.df.azure-devices-int.net;CredentialScope=Device;DeviceId=test_dcristo;SharedAccessKey=YBDXkOStSnhSkZaab3E1QOGJdluYtyf/qCBOqEG8xpQ=;"; + +// Define the Model +BEGIN_NAMESPACE(MyThermostat); + +DECLARE_MODEL(ContosoThermostat505, +WITH_DATA(int, Temperature), +WITH_DATA(int, Humidity), +WITH_DATA(bool, LowTemperatureAlarm), +WITH_ACTION(TurnFanOn), +WITH_ACTION(TurnFanOff), +WITH_ACTION(SetTemperature, int, DesiredTemperature) +); + +END_NAMESPACE(MyThermostat); + +DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES) + +EXECUTE_COMMAND_RESULT TurnFanOn(ContosoThermostat505* device) +{ + (void)device; + (void)printf("Turning fan on.\r\n"); + return EXECUTE_COMMAND_SUCCESS; +} + +EXECUTE_COMMAND_RESULT TurnFanOff(ContosoThermostat505* device) +{ + (void)device; + (void)printf("Turning fan off.\r\n"); + return EXECUTE_COMMAND_SUCCESS; +} + +EXECUTE_COMMAND_RESULT SetTemperature(ContosoThermostat505* device, int DesiredTemperature) +{ + (void)device; + (void)printf("Setting home temperature to %d degrees.\r\n", DesiredTemperature); + return EXECUTE_COMMAND_SUCCESS; +} + +void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) +{ + int messageTrackingId = (intptr_t)userContextCallback; + + (void)printf("Message Id: %d Received.\r\n", messageTrackingId); + + (void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); +} + + +static void sendMessage(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size) +{ + static unsigned int messageTrackingId; + IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size); + if (messageHandle == NULL) + { + printf("unable to create a new IoTHubMessage\r\n"); + } + else + { + if (IoTHubClient_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK) + { + printf("failed to hand over the message to IoTHubClient"); + } + else + { + printf("IoTHubClient accepted the message for delivery\r\n"); + } + + IoTHubMessage_Destroy(messageHandle); + } + free((void*)buffer); + messageTrackingId++; +} + +static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubNotification(IOTHUB_MESSAGE_HANDLE notificationMessage, void* userContextCallback) +{ + IOTHUBMESSAGE_DISPOSITION_RESULT result; + const unsigned char* buffer; + size_t size; + if (IoTHubMessage_GetByteArray(notificationMessage, &buffer, &size) != IOTHUB_MESSAGE_OK) + { + printf("unable to IoTHubMessage_GetByteArray\r\n"); + result = EXECUTE_COMMAND_ERROR; + } + else + { + /*buffer is not zero terminated*/ + char* temp = malloc(size + 1); + if (temp == NULL) + { + printf("failed to malloc\r\n"); + result = EXECUTE_COMMAND_ERROR; + } + else + { + memcpy(temp, buffer, size); + temp[size] = '\0'; + EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp); + result = + (executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED : + (executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED : + IOTHUBMESSAGE_REJECTED; + free(temp); + } + } + return result; +} + +void simplesample_amqp_run(void) +{ + if (serializer_init(NULL) != SERIALIZER_OK) + { + (void)printf("Failed on serializer_init\r\n"); + } + else + { + + /* Setup IoTHub client configuration */ + + IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, IoTHubTransportAmqp_ProvideTransportInterface); + +#ifdef MBED_BUILD_TIMESTAMP + // For mbed add the certificate information + if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) + { + printf("failure to set option \"TrustedCerts\"\r\n"); + } +#endif // MBED_BUILD_TIMESTAMP + + if (iotHubClientHandle == NULL) + { + (void)printf("Failed on IoTHubClient_Create\r\n"); + } + else + { + ContosoThermostat505* Thermostat = CREATE_MODEL_INSTANCE(MyThermostat, ContosoThermostat505); + if (Thermostat == NULL) + { + (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n"); + } + else + { + unsigned char* destination; + size_t destinationSize; + + if (IoTHubClient_SetNotificationCallback(iotHubClientHandle, IoTHubNotification, Thermostat) != IOTHUB_CLIENT_OK) + { + printf("unable to IoTHubClient_SetNotificationCallback\r\n"); + } + else + { + + Thermostat->Temperature = 67; + Thermostat->Humidity = 42; + + if (SERIALIZE(&destination, &destinationSize, Thermostat->Temperature, Thermostat->Humidity) != IOT_AGENT_OK) + { + (void)printf("Failed to serialize\r\n"); + } + else + { + sendMessage(iotHubClientHandle, destination, destinationSize); + } + + /* wait for commands */ + (void)getchar(); + } + DESTROY_MODEL_INSTANCE(Thermostat); + } + IoTHubClient_Destroy(iotHubClientHandle); + } + serializer_deinit(); + } +} +