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.
simplesample_amqp.c@25:9cb9b0c15218, 2016-02-04 (annotated)
- Committer:
- Azure.IoT Build
- Date:
- Thu Feb 04 11:33:25 2016 -0800
- Revision:
- 25:9cb9b0c15218
- Parent:
- 19:eae21b79ceff
- Child:
- 26:9662f727e974
1.0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 16:b2c6de619743 | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 16:b2c6de619743 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 16:b2c6de619743 | 3 | |
AzureIoTClient | 16:b2c6de619743 | 4 | #include <stdlib.h> |
AzureIoTClient | 16:b2c6de619743 | 5 | |
AzureIoTClient | 16:b2c6de619743 | 6 | #include <stdio.h> |
AzureIoTClient | 16:b2c6de619743 | 7 | #include <stdint.h> |
AzureIoTClient | 16:b2c6de619743 | 8 | |
AzureIoTClient | 16:b2c6de619743 | 9 | #include "serializer.h" |
AzureIoTClient | 16:b2c6de619743 | 10 | #include "threadapi.h" |
AzureIoTClient | 16:b2c6de619743 | 11 | #include "sastoken.h" |
AzureIoTClient | 16:b2c6de619743 | 12 | #include "iothub_client.h" |
AzureIoTClient | 16:b2c6de619743 | 13 | #include "iothubtransportamqp.h" |
AzureIoTClient | 16:b2c6de619743 | 14 | #include "iothub_client_ll.h" |
Azure.IoT Build | 25:9cb9b0c15218 | 15 | #include "platform.h" |
AzureIoTClient | 16:b2c6de619743 | 16 | |
AzureIoTClient | 16:b2c6de619743 | 17 | #ifdef MBED_BUILD_TIMESTAMP |
AzureIoTClient | 16:b2c6de619743 | 18 | #include "certs.h" |
AzureIoTClient | 16:b2c6de619743 | 19 | #endif // MBED_BUILD_TIMESTAMP |
AzureIoTClient | 16:b2c6de619743 | 20 | |
AzureIoTClient | 18:a3ec1aab72dc | 21 | /*String containing Hostname, Device Id & Device Key in the format: */ |
AzureIoTClient | 18:a3ec1aab72dc | 22 | /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */ |
AzureIoTClient | 16:b2c6de619743 | 23 | static const char* connectionString = "[device connection string]"; |
AzureIoTClient | 16:b2c6de619743 | 24 | |
AzureIoTClient | 16:b2c6de619743 | 25 | // Define the Model |
AzureIoTClient | 16:b2c6de619743 | 26 | BEGIN_NAMESPACE(WeatherStation); |
AzureIoTClient | 16:b2c6de619743 | 27 | |
AzureIoTClient | 16:b2c6de619743 | 28 | DECLARE_MODEL(ContosoAnemometer, |
AzureIoTClient | 16:b2c6de619743 | 29 | WITH_DATA(ascii_char_ptr, DeviceId), |
AzureIoTClient | 19:eae21b79ceff | 30 | WITH_DATA(int, WindSpeed), |
AzureIoTClient | 16:b2c6de619743 | 31 | WITH_ACTION(TurnFanOn), |
AzureIoTClient | 16:b2c6de619743 | 32 | WITH_ACTION(TurnFanOff), |
AzureIoTClient | 16:b2c6de619743 | 33 | WITH_ACTION(SetAirResistance, int, Position) |
AzureIoTClient | 16:b2c6de619743 | 34 | ); |
AzureIoTClient | 16:b2c6de619743 | 35 | |
AzureIoTClient | 16:b2c6de619743 | 36 | END_NAMESPACE(WeatherStation); |
AzureIoTClient | 16:b2c6de619743 | 37 | |
AzureIoTClient | 16:b2c6de619743 | 38 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES) |
AzureIoTClient | 16:b2c6de619743 | 39 | |
AzureIoTClient | 16:b2c6de619743 | 40 | EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device) |
AzureIoTClient | 16:b2c6de619743 | 41 | { |
AzureIoTClient | 16:b2c6de619743 | 42 | (void)device; |
AzureIoTClient | 16:b2c6de619743 | 43 | (void)printf("Turning fan on.\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 44 | return EXECUTE_COMMAND_SUCCESS; |
AzureIoTClient | 16:b2c6de619743 | 45 | } |
AzureIoTClient | 16:b2c6de619743 | 46 | |
AzureIoTClient | 16:b2c6de619743 | 47 | EXECUTE_COMMAND_RESULT TurnFanOff(ContosoAnemometer* device) |
AzureIoTClient | 16:b2c6de619743 | 48 | { |
AzureIoTClient | 16:b2c6de619743 | 49 | (void)device; |
AzureIoTClient | 16:b2c6de619743 | 50 | (void)printf("Turning fan off.\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 51 | return EXECUTE_COMMAND_SUCCESS; |
AzureIoTClient | 16:b2c6de619743 | 52 | } |
AzureIoTClient | 16:b2c6de619743 | 53 | |
AzureIoTClient | 16:b2c6de619743 | 54 | EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position) |
AzureIoTClient | 16:b2c6de619743 | 55 | { |
AzureIoTClient | 16:b2c6de619743 | 56 | (void)device; |
AzureIoTClient | 16:b2c6de619743 | 57 | (void)printf("Setting Air Resistance Position to %d.\r\n", Position); |
AzureIoTClient | 16:b2c6de619743 | 58 | return EXECUTE_COMMAND_SUCCESS; |
AzureIoTClient | 16:b2c6de619743 | 59 | } |
AzureIoTClient | 16:b2c6de619743 | 60 | |
AzureIoTClient | 16:b2c6de619743 | 61 | void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) |
AzureIoTClient | 16:b2c6de619743 | 62 | { |
AzureIoTClient | 16:b2c6de619743 | 63 | int messageTrackingId = (intptr_t)userContextCallback; |
AzureIoTClient | 16:b2c6de619743 | 64 | |
AzureIoTClient | 16:b2c6de619743 | 65 | (void)printf("Message Id: %d Received.\r\n", messageTrackingId); |
AzureIoTClient | 16:b2c6de619743 | 66 | |
AzureIoTClient | 16:b2c6de619743 | 67 | (void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); |
AzureIoTClient | 16:b2c6de619743 | 68 | } |
AzureIoTClient | 16:b2c6de619743 | 69 | |
AzureIoTClient | 16:b2c6de619743 | 70 | |
AzureIoTClient | 16:b2c6de619743 | 71 | static void sendMessage(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size) |
AzureIoTClient | 16:b2c6de619743 | 72 | { |
AzureIoTClient | 16:b2c6de619743 | 73 | static unsigned int messageTrackingId; |
AzureIoTClient | 16:b2c6de619743 | 74 | IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size); |
AzureIoTClient | 16:b2c6de619743 | 75 | if (messageHandle == NULL) |
AzureIoTClient | 16:b2c6de619743 | 76 | { |
AzureIoTClient | 16:b2c6de619743 | 77 | printf("unable to create a new IoTHubMessage\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 78 | } |
AzureIoTClient | 16:b2c6de619743 | 79 | else |
AzureIoTClient | 16:b2c6de619743 | 80 | { |
AzureIoTClient | 16:b2c6de619743 | 81 | if (IoTHubClient_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 16:b2c6de619743 | 82 | { |
AzureIoTClient | 16:b2c6de619743 | 83 | printf("failed to hand over the message to IoTHubClient"); |
AzureIoTClient | 16:b2c6de619743 | 84 | } |
AzureIoTClient | 16:b2c6de619743 | 85 | else |
AzureIoTClient | 16:b2c6de619743 | 86 | { |
AzureIoTClient | 16:b2c6de619743 | 87 | printf("IoTHubClient accepted the message for delivery\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 88 | } |
AzureIoTClient | 16:b2c6de619743 | 89 | |
AzureIoTClient | 16:b2c6de619743 | 90 | IoTHubMessage_Destroy(messageHandle); |
AzureIoTClient | 16:b2c6de619743 | 91 | } |
AzureIoTClient | 16:b2c6de619743 | 92 | free((void*)buffer); |
AzureIoTClient | 16:b2c6de619743 | 93 | messageTrackingId++; |
AzureIoTClient | 16:b2c6de619743 | 94 | } |
AzureIoTClient | 16:b2c6de619743 | 95 | |
AzureIoTClient | 16:b2c6de619743 | 96 | static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) |
AzureIoTClient | 16:b2c6de619743 | 97 | { |
AzureIoTClient | 16:b2c6de619743 | 98 | IOTHUBMESSAGE_DISPOSITION_RESULT result; |
AzureIoTClient | 16:b2c6de619743 | 99 | const unsigned char* buffer; |
AzureIoTClient | 16:b2c6de619743 | 100 | size_t size; |
AzureIoTClient | 16:b2c6de619743 | 101 | if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 16:b2c6de619743 | 102 | { |
AzureIoTClient | 16:b2c6de619743 | 103 | printf("unable to IoTHubMessage_GetByteArray\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 104 | result = EXECUTE_COMMAND_ERROR; |
AzureIoTClient | 16:b2c6de619743 | 105 | } |
AzureIoTClient | 16:b2c6de619743 | 106 | else |
AzureIoTClient | 16:b2c6de619743 | 107 | { |
AzureIoTClient | 16:b2c6de619743 | 108 | /*buffer is not zero terminated*/ |
AzureIoTClient | 16:b2c6de619743 | 109 | char* temp = malloc(size + 1); |
AzureIoTClient | 16:b2c6de619743 | 110 | if (temp == NULL) |
AzureIoTClient | 16:b2c6de619743 | 111 | { |
AzureIoTClient | 16:b2c6de619743 | 112 | printf("failed to malloc\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 113 | result = EXECUTE_COMMAND_ERROR; |
AzureIoTClient | 16:b2c6de619743 | 114 | } |
AzureIoTClient | 16:b2c6de619743 | 115 | else |
AzureIoTClient | 16:b2c6de619743 | 116 | { |
AzureIoTClient | 16:b2c6de619743 | 117 | memcpy(temp, buffer, size); |
AzureIoTClient | 16:b2c6de619743 | 118 | temp[size] = '\0'; |
AzureIoTClient | 16:b2c6de619743 | 119 | EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp); |
AzureIoTClient | 16:b2c6de619743 | 120 | result = |
AzureIoTClient | 16:b2c6de619743 | 121 | (executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED : |
AzureIoTClient | 16:b2c6de619743 | 122 | (executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED : |
AzureIoTClient | 16:b2c6de619743 | 123 | IOTHUBMESSAGE_REJECTED; |
AzureIoTClient | 16:b2c6de619743 | 124 | free(temp); |
AzureIoTClient | 16:b2c6de619743 | 125 | } |
AzureIoTClient | 16:b2c6de619743 | 126 | } |
AzureIoTClient | 16:b2c6de619743 | 127 | return result; |
AzureIoTClient | 16:b2c6de619743 | 128 | } |
AzureIoTClient | 16:b2c6de619743 | 129 | |
AzureIoTClient | 16:b2c6de619743 | 130 | void simplesample_amqp_run(void) |
AzureIoTClient | 16:b2c6de619743 | 131 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 132 | if (platform_init() != 0) |
AzureIoTClient | 16:b2c6de619743 | 133 | { |
AzureIoTClient | 16:b2c6de619743 | 134 | (void)printf("Failed on serializer_init\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 135 | } |
AzureIoTClient | 16:b2c6de619743 | 136 | else |
AzureIoTClient | 16:b2c6de619743 | 137 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 138 | if (serializer_init(NULL) != SERIALIZER_OK) |
AzureIoTClient | 16:b2c6de619743 | 139 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 140 | (void)printf("Failed on serializer_init\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 141 | } |
AzureIoTClient | 16:b2c6de619743 | 142 | else |
AzureIoTClient | 16:b2c6de619743 | 143 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 144 | /* Setup IoTHub client configuration */ |
Azure.IoT Build | 25:9cb9b0c15218 | 145 | IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol); |
Azure.IoT Build | 25:9cb9b0c15218 | 146 | srand((unsigned int)time(NULL)); |
Azure.IoT Build | 25:9cb9b0c15218 | 147 | int avgWindSpeed = 10; |
Azure.IoT Build | 25:9cb9b0c15218 | 148 | |
Azure.IoT Build | 25:9cb9b0c15218 | 149 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 16:b2c6de619743 | 150 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 151 | (void)printf("Failed on IoTHubClient_Create\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 152 | } |
AzureIoTClient | 16:b2c6de619743 | 153 | else |
AzureIoTClient | 16:b2c6de619743 | 154 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 155 | #ifdef MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 25:9cb9b0c15218 | 156 | // For mbed add the certificate information |
Azure.IoT Build | 25:9cb9b0c15218 | 157 | if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 25:9cb9b0c15218 | 158 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 159 | (void)printf("failure to set option \"TrustedCerts\"\r\n"); |
Azure.IoT Build | 25:9cb9b0c15218 | 160 | } |
Azure.IoT Build | 25:9cb9b0c15218 | 161 | #endif // MBED_BUILD_TIMESTAMP |
AzureIoTClient | 16:b2c6de619743 | 162 | |
Azure.IoT Build | 25:9cb9b0c15218 | 163 | ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer); |
Azure.IoT Build | 25:9cb9b0c15218 | 164 | if (myWeather == NULL) |
AzureIoTClient | 16:b2c6de619743 | 165 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 166 | (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 167 | } |
AzureIoTClient | 16:b2c6de619743 | 168 | else |
AzureIoTClient | 16:b2c6de619743 | 169 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 170 | unsigned char* destination; |
Azure.IoT Build | 25:9cb9b0c15218 | 171 | size_t destinationSize; |
AzureIoTClient | 16:b2c6de619743 | 172 | |
Azure.IoT Build | 25:9cb9b0c15218 | 173 | if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 16:b2c6de619743 | 174 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 175 | printf("unable to IoTHubClient_SetMessageCallback\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 176 | } |
AzureIoTClient | 16:b2c6de619743 | 177 | else |
AzureIoTClient | 16:b2c6de619743 | 178 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 179 | myWeather->DeviceId = "myFirstDevice"; |
Azure.IoT Build | 25:9cb9b0c15218 | 180 | myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2); |
AzureIoTClient | 16:b2c6de619743 | 181 | |
Azure.IoT Build | 25:9cb9b0c15218 | 182 | if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != IOT_AGENT_OK) |
Azure.IoT Build | 25:9cb9b0c15218 | 183 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 184 | (void)printf("Failed to serialize\r\n"); |
Azure.IoT Build | 25:9cb9b0c15218 | 185 | } |
Azure.IoT Build | 25:9cb9b0c15218 | 186 | else |
Azure.IoT Build | 25:9cb9b0c15218 | 187 | { |
Azure.IoT Build | 25:9cb9b0c15218 | 188 | sendMessage(iotHubClientHandle, destination, destinationSize); |
Azure.IoT Build | 25:9cb9b0c15218 | 189 | } |
Azure.IoT Build | 25:9cb9b0c15218 | 190 | |
Azure.IoT Build | 25:9cb9b0c15218 | 191 | /* wait for commands */ |
Azure.IoT Build | 25:9cb9b0c15218 | 192 | (void)getchar(); |
Azure.IoT Build | 25:9cb9b0c15218 | 193 | } |
Azure.IoT Build | 25:9cb9b0c15218 | 194 | DESTROY_MODEL_INSTANCE(myWeather); |
AzureIoTClient | 16:b2c6de619743 | 195 | } |
Azure.IoT Build | 25:9cb9b0c15218 | 196 | IoTHubClient_Destroy(iotHubClientHandle); |
AzureIoTClient | 16:b2c6de619743 | 197 | } |
Azure.IoT Build | 25:9cb9b0c15218 | 198 | serializer_deinit(); |
AzureIoTClient | 16:b2c6de619743 | 199 | } |
Azure.IoT Build | 25:9cb9b0c15218 | 200 | platform_deinit(); |
AzureIoTClient | 16:b2c6de619743 | 201 | } |
AzureIoTClient | 16:b2c6de619743 | 202 | } |
AzureIoTClient | 16:b2c6de619743 | 203 |