Azure IoT / Mbed 2 deprecated remote_monitoring

Dependencies:   azure_c_shared_utility EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed azure_uamqp_c serializer wolfSSL

Committer:
Azure.IoT Build
Date:
Thu Feb 04 11:31:34 2016 -0800
Revision:
31:3f7731d8c71a
Parent:
24:8e6112373e4e
Child:
36:c5f21f14d53f
1.0.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 20:ea66de0f1a06 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 20:ea66de0f1a06 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 20:ea66de0f1a06 3
AzureIoTClient 23:ff89811f3fa3 4 #ifndef WINCE
AzureIoTClient 20:ea66de0f1a06 5 #include "iothubtransportamqp.h"
AzureIoTClient 23:ff89811f3fa3 6 #else
AzureIoTClient 23:ff89811f3fa3 7 #include "iothubtransporthttp.h"
AzureIoTClient 23:ff89811f3fa3 8 #endif
AzureIoTClient 20:ea66de0f1a06 9 #include "schemalib.h"
AzureIoTClient 20:ea66de0f1a06 10 #include "iothub_client.h"
AzureIoTClient 20:ea66de0f1a06 11 #include "serializer.h"
AzureIoTClient 20:ea66de0f1a06 12 #include "schemaserializer.h"
AzureIoTClient 20:ea66de0f1a06 13 #include "threadapi.h"
AzureIoTClient 20:ea66de0f1a06 14
AzureIoTClient 20:ea66de0f1a06 15 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 20:ea66de0f1a06 16 #include "certs.h"
AzureIoTClient 20:ea66de0f1a06 17 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 20:ea66de0f1a06 18
AzureIoTClient 23:ff89811f3fa3 19
AzureIoTClient 20:ea66de0f1a06 20 static const char* deviceId = "[Device Id]";
AzureIoTClient 20:ea66de0f1a06 21 static const char* deviceKey = "[Device Key]";
AzureIoTClient 20:ea66de0f1a06 22 static const char* hubName = "[IoTHub Name]";
AzureIoTClient 20:ea66de0f1a06 23 static const char* hubSuffix = "[IoTHub Suffix, i.e. azure-devices.net]";
AzureIoTClient 20:ea66de0f1a06 24
AzureIoTClient 20:ea66de0f1a06 25 // Define the Model
AzureIoTClient 20:ea66de0f1a06 26 BEGIN_NAMESPACE(Contoso);
AzureIoTClient 20:ea66de0f1a06 27
AzureIoTClient 20:ea66de0f1a06 28 DECLARE_STRUCT(SystemProperties,
AzureIoTClient 20:ea66de0f1a06 29 ascii_char_ptr, DeviceID,
AzureIoTClient 20:ea66de0f1a06 30 _Bool, Enabled
AzureIoTClient 20:ea66de0f1a06 31 );
AzureIoTClient 20:ea66de0f1a06 32
AzureIoTClient 20:ea66de0f1a06 33 DECLARE_STRUCT(DeviceProperties,
AzureIoTClient 20:ea66de0f1a06 34 ascii_char_ptr, DeviceID,
AzureIoTClient 20:ea66de0f1a06 35 _Bool, HubEnabledState
AzureIoTClient 20:ea66de0f1a06 36 );
AzureIoTClient 20:ea66de0f1a06 37
AzureIoTClient 20:ea66de0f1a06 38 DECLARE_MODEL(Thermostat,
AzureIoTClient 20:ea66de0f1a06 39
AzureIoTClient 20:ea66de0f1a06 40 /* Event data (temperature, external temperature and humidity) */
Azure.IoT Build 31:3f7731d8c71a 41 WITH_DATA(int, Temperature),
Azure.IoT Build 31:3f7731d8c71a 42 WITH_DATA(int, ExternalTemperature),
Azure.IoT Build 31:3f7731d8c71a 43 WITH_DATA(int, Humidity),
Azure.IoT Build 31:3f7731d8c71a 44 WITH_DATA(ascii_char_ptr, DeviceId),
AzureIoTClient 20:ea66de0f1a06 45
Azure.IoT Build 31:3f7731d8c71a 46 /* Device Info - This is command metadata + some extra fields */
Azure.IoT Build 31:3f7731d8c71a 47 WITH_DATA(ascii_char_ptr, ObjectType),
Azure.IoT Build 31:3f7731d8c71a 48 WITH_DATA(_Bool, IsSimulatedDevice),
Azure.IoT Build 31:3f7731d8c71a 49 WITH_DATA(ascii_char_ptr, Version),
Azure.IoT Build 31:3f7731d8c71a 50 WITH_DATA(DeviceProperties, DeviceProperties),
AzureIoTClient 20:ea66de0f1a06 51 WITH_DATA(ascii_char_ptr_no_quotes, Commands),
AzureIoTClient 20:ea66de0f1a06 52
AzureIoTClient 20:ea66de0f1a06 53 /* Commands implemented by the device */
AzureIoTClient 24:8e6112373e4e 54 WITH_ACTION(SetTemperature, int, temperature),
Azure.IoT Build 31:3f7731d8c71a 55 WITH_ACTION(SetHumidity, int, humidity)
AzureIoTClient 20:ea66de0f1a06 56 );
AzureIoTClient 20:ea66de0f1a06 57
AzureIoTClient 20:ea66de0f1a06 58 END_NAMESPACE(Contoso);
AzureIoTClient 20:ea66de0f1a06 59
AzureIoTClient 24:8e6112373e4e 60 EXECUTE_COMMAND_RESULT SetTemperature(Thermostat* thermostat, int temperature)
AzureIoTClient 20:ea66de0f1a06 61 {
AzureIoTClient 24:8e6112373e4e 62 (void)printf("Received temperature %d\r\n", temperature);
Azure.IoT Build 31:3f7731d8c71a 63 thermostat->Temperature = temperature;
AzureIoTClient 20:ea66de0f1a06 64 return EXECUTE_COMMAND_SUCCESS;
AzureIoTClient 20:ea66de0f1a06 65 }
AzureIoTClient 20:ea66de0f1a06 66
AzureIoTClient 24:8e6112373e4e 67 EXECUTE_COMMAND_RESULT SetHumidity(Thermostat* thermostat, int humidity)
AzureIoTClient 20:ea66de0f1a06 68 {
Azure.IoT Build 31:3f7731d8c71a 69 (void)printf("Received humidity %d\r\n", humidity);
Azure.IoT Build 31:3f7731d8c71a 70 thermostat->Humidity = humidity;
Azure.IoT Build 31:3f7731d8c71a 71 return EXECUTE_COMMAND_SUCCESS;
AzureIoTClient 20:ea66de0f1a06 72 }
AzureIoTClient 20:ea66de0f1a06 73
AzureIoTClient 20:ea66de0f1a06 74 static void sendMessage(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
AzureIoTClient 20:ea66de0f1a06 75 {
AzureIoTClient 20:ea66de0f1a06 76 IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
AzureIoTClient 20:ea66de0f1a06 77 if (messageHandle == NULL)
AzureIoTClient 20:ea66de0f1a06 78 {
AzureIoTClient 20:ea66de0f1a06 79 printf("unable to create a new IoTHubMessage\r\n");
AzureIoTClient 20:ea66de0f1a06 80 }
AzureIoTClient 20:ea66de0f1a06 81 else
AzureIoTClient 20:ea66de0f1a06 82 {
AzureIoTClient 20:ea66de0f1a06 83 if (IoTHubClient_SendEventAsync(iotHubClientHandle, messageHandle, NULL, NULL) != IOTHUB_CLIENT_OK)
AzureIoTClient 20:ea66de0f1a06 84 {
AzureIoTClient 20:ea66de0f1a06 85 printf("failed to hand over the message to IoTHubClient");
AzureIoTClient 20:ea66de0f1a06 86 }
AzureIoTClient 20:ea66de0f1a06 87 else
AzureIoTClient 20:ea66de0f1a06 88 {
AzureIoTClient 20:ea66de0f1a06 89 printf("IoTHubClient accepted the message for delivery\r\n");
AzureIoTClient 20:ea66de0f1a06 90 }
AzureIoTClient 20:ea66de0f1a06 91
AzureIoTClient 20:ea66de0f1a06 92 IoTHubMessage_Destroy(messageHandle);
AzureIoTClient 20:ea66de0f1a06 93 }
AzureIoTClient 20:ea66de0f1a06 94 free((void*)buffer);
AzureIoTClient 20:ea66de0f1a06 95 }
AzureIoTClient 20:ea66de0f1a06 96
AzureIoTClient 20:ea66de0f1a06 97 /*this function "links" IoTHub to the serialization library*/
AzureIoTClient 20:ea66de0f1a06 98 static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
AzureIoTClient 20:ea66de0f1a06 99 {
AzureIoTClient 20:ea66de0f1a06 100 IOTHUBMESSAGE_DISPOSITION_RESULT result;
AzureIoTClient 20:ea66de0f1a06 101 const unsigned char* buffer;
AzureIoTClient 20:ea66de0f1a06 102 size_t size;
AzureIoTClient 20:ea66de0f1a06 103 if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
AzureIoTClient 20:ea66de0f1a06 104 {
AzureIoTClient 20:ea66de0f1a06 105 printf("unable to IoTHubMessage_GetByteArray\r\n");
AzureIoTClient 20:ea66de0f1a06 106 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 20:ea66de0f1a06 107 }
AzureIoTClient 20:ea66de0f1a06 108 else
AzureIoTClient 20:ea66de0f1a06 109 {
AzureIoTClient 20:ea66de0f1a06 110 /*buffer is not zero terminated*/
AzureIoTClient 20:ea66de0f1a06 111 char* temp = malloc(size + 1);
AzureIoTClient 20:ea66de0f1a06 112 if (temp == NULL)
AzureIoTClient 20:ea66de0f1a06 113 {
AzureIoTClient 20:ea66de0f1a06 114 printf("failed to malloc\r\n");
AzureIoTClient 20:ea66de0f1a06 115 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 20:ea66de0f1a06 116 }
AzureIoTClient 20:ea66de0f1a06 117 else
AzureIoTClient 20:ea66de0f1a06 118 {
Azure.IoT Build 31:3f7731d8c71a 119 EXECUTE_COMMAND_RESULT executeCommandResult;
AzureIoTClient 23:ff89811f3fa3 120
AzureIoTClient 20:ea66de0f1a06 121 memcpy(temp, buffer, size);
AzureIoTClient 20:ea66de0f1a06 122 temp[size] = '\0';
AzureIoTClient 23:ff89811f3fa3 123 executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
AzureIoTClient 20:ea66de0f1a06 124 result =
AzureIoTClient 20:ea66de0f1a06 125 (executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
AzureIoTClient 20:ea66de0f1a06 126 (executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
AzureIoTClient 20:ea66de0f1a06 127 IOTHUBMESSAGE_REJECTED;
AzureIoTClient 20:ea66de0f1a06 128 free(temp);
AzureIoTClient 20:ea66de0f1a06 129 }
AzureIoTClient 20:ea66de0f1a06 130 }
AzureIoTClient 20:ea66de0f1a06 131 return result;
AzureIoTClient 20:ea66de0f1a06 132 }
AzureIoTClient 20:ea66de0f1a06 133
AzureIoTClient 20:ea66de0f1a06 134 void remote_monitoring_run(void)
AzureIoTClient 20:ea66de0f1a06 135 {
AzureIoTClient 20:ea66de0f1a06 136 if (serializer_init(NULL) != SERIALIZER_OK)
AzureIoTClient 20:ea66de0f1a06 137 {
AzureIoTClient 20:ea66de0f1a06 138 printf("Failed on serializer_init\r\n");
AzureIoTClient 20:ea66de0f1a06 139 }
AzureIoTClient 20:ea66de0f1a06 140 else
AzureIoTClient 20:ea66de0f1a06 141 {
Azure.IoT Build 31:3f7731d8c71a 142 IOTHUB_CLIENT_CONFIG config;
Azure.IoT Build 31:3f7731d8c71a 143 IOTHUB_CLIENT_HANDLE iotHubClientHandle;
AzureIoTClient 20:ea66de0f1a06 144
Azure.IoT Build 31:3f7731d8c71a 145 config.deviceId = deviceId;
Azure.IoT Build 31:3f7731d8c71a 146 config.deviceKey = deviceKey;
Azure.IoT Build 31:3f7731d8c71a 147 config.iotHubName = hubName;
Azure.IoT Build 31:3f7731d8c71a 148 config.iotHubSuffix = hubSuffix;
AzureIoTClient 23:ff89811f3fa3 149 #ifndef WINCE
Azure.IoT Build 31:3f7731d8c71a 150 config.protocol = AMQP_Protocol;
AzureIoTClient 23:ff89811f3fa3 151 #else
Azure.IoT Build 31:3f7731d8c71a 152 config.protocol = HTTP_Protocol;
AzureIoTClient 23:ff89811f3fa3 153 #endif
Azure.IoT Build 31:3f7731d8c71a 154 iotHubClientHandle = IoTHubClient_Create(&config);
AzureIoTClient 20:ea66de0f1a06 155 if (iotHubClientHandle == NULL)
AzureIoTClient 20:ea66de0f1a06 156 {
AzureIoTClient 20:ea66de0f1a06 157 (void)printf("Failed on IoTHubClient_CreateFromConnectionString\r\n");
AzureIoTClient 20:ea66de0f1a06 158 }
AzureIoTClient 20:ea66de0f1a06 159 else
AzureIoTClient 20:ea66de0f1a06 160 {
AzureIoTClient 20:ea66de0f1a06 161 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 20:ea66de0f1a06 162 // For mbed add the certificate information
AzureIoTClient 20:ea66de0f1a06 163 if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
AzureIoTClient 20:ea66de0f1a06 164 {
AzureIoTClient 20:ea66de0f1a06 165 printf("failure to set option \"TrustedCerts\"\r\n");
AzureIoTClient 20:ea66de0f1a06 166 }
AzureIoTClient 20:ea66de0f1a06 167 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 20:ea66de0f1a06 168
AzureIoTClient 20:ea66de0f1a06 169 Thermostat* thermostat = CREATE_MODEL_INSTANCE(Contoso, Thermostat);
AzureIoTClient 20:ea66de0f1a06 170 if (thermostat == NULL)
AzureIoTClient 20:ea66de0f1a06 171 {
AzureIoTClient 20:ea66de0f1a06 172 (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
AzureIoTClient 20:ea66de0f1a06 173 }
AzureIoTClient 20:ea66de0f1a06 174 else
AzureIoTClient 20:ea66de0f1a06 175 {
AzureIoTClient 20:ea66de0f1a06 176 STRING_HANDLE commandsMetadata;
AzureIoTClient 20:ea66de0f1a06 177
AzureIoTClient 20:ea66de0f1a06 178 if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, thermostat) != IOTHUB_CLIENT_OK)
AzureIoTClient 20:ea66de0f1a06 179 {
AzureIoTClient 20:ea66de0f1a06 180 printf("unable to IoTHubClient_SetMessageCallback\r\n");
AzureIoTClient 20:ea66de0f1a06 181 }
AzureIoTClient 20:ea66de0f1a06 182 else
AzureIoTClient 20:ea66de0f1a06 183 {
AzureIoTClient 20:ea66de0f1a06 184
AzureIoTClient 20:ea66de0f1a06 185 /* send the device info upon startup so that the cloud app knows
AzureIoTClient 20:ea66de0f1a06 186 what commands are available and the fact that the device is up */
AzureIoTClient 20:ea66de0f1a06 187 thermostat->ObjectType = "DeviceInfo";
Azure.IoT Build 31:3f7731d8c71a 188 thermostat->IsSimulatedDevice = false;
Azure.IoT Build 31:3f7731d8c71a 189 thermostat->Version = "1.0";
Azure.IoT Build 31:3f7731d8c71a 190 thermostat->DeviceProperties.HubEnabledState = true;
AzureIoTClient 20:ea66de0f1a06 191 thermostat->DeviceProperties.DeviceID = (char*)deviceId;
AzureIoTClient 20:ea66de0f1a06 192
AzureIoTClient 20:ea66de0f1a06 193 commandsMetadata = STRING_new();
AzureIoTClient 20:ea66de0f1a06 194 if (commandsMetadata == NULL)
AzureIoTClient 20:ea66de0f1a06 195 {
AzureIoTClient 20:ea66de0f1a06 196 (void)printf("Failed on creating string for commands metadata\r\n");
AzureIoTClient 20:ea66de0f1a06 197 }
AzureIoTClient 20:ea66de0f1a06 198 else
AzureIoTClient 20:ea66de0f1a06 199 {
AzureIoTClient 20:ea66de0f1a06 200 /* Serialize the commands metadata as a JSON string before sending */
AzureIoTClient 20:ea66de0f1a06 201 if (SchemaSerializer_SerializeCommandMetadata(GET_MODEL_HANDLE(Contoso, Thermostat), commandsMetadata) != SCHEMA_SERIALIZER_OK)
AzureIoTClient 20:ea66de0f1a06 202 {
AzureIoTClient 20:ea66de0f1a06 203 (void)printf("Failed serializing commands metadata\r\n");
AzureIoTClient 20:ea66de0f1a06 204 }
AzureIoTClient 20:ea66de0f1a06 205 else
AzureIoTClient 20:ea66de0f1a06 206 {
AzureIoTClient 20:ea66de0f1a06 207 unsigned char* buffer;
AzureIoTClient 20:ea66de0f1a06 208 size_t bufferSize;
AzureIoTClient 20:ea66de0f1a06 209 thermostat->Commands = (char*)STRING_c_str(commandsMetadata);
AzureIoTClient 20:ea66de0f1a06 210
AzureIoTClient 20:ea66de0f1a06 211 /* Here is the actual send of the Device Info */
AzureIoTClient 20:ea66de0f1a06 212 if (SERIALIZE(&buffer, &bufferSize, thermostat->ObjectType, thermostat->Version, thermostat->IsSimulatedDevice, thermostat->DeviceProperties, thermostat->Commands) != IOT_AGENT_OK)
AzureIoTClient 20:ea66de0f1a06 213 {
AzureIoTClient 20:ea66de0f1a06 214 (void)printf("Failed serializing\r\n");
AzureIoTClient 20:ea66de0f1a06 215 }
AzureIoTClient 20:ea66de0f1a06 216 else
AzureIoTClient 20:ea66de0f1a06 217 {
AzureIoTClient 20:ea66de0f1a06 218 sendMessage(iotHubClientHandle, buffer, bufferSize);
AzureIoTClient 20:ea66de0f1a06 219 }
AzureIoTClient 20:ea66de0f1a06 220
AzureIoTClient 20:ea66de0f1a06 221 }
AzureIoTClient 20:ea66de0f1a06 222
AzureIoTClient 20:ea66de0f1a06 223 STRING_delete(commandsMetadata);
AzureIoTClient 20:ea66de0f1a06 224 }
AzureIoTClient 20:ea66de0f1a06 225
Azure.IoT Build 31:3f7731d8c71a 226 thermostat->Temperature = 50;
Azure.IoT Build 31:3f7731d8c71a 227 thermostat->ExternalTemperature = 55;
Azure.IoT Build 31:3f7731d8c71a 228 thermostat->Humidity = 50;
Azure.IoT Build 31:3f7731d8c71a 229 thermostat->DeviceId = (char*)deviceId;
AzureIoTClient 20:ea66de0f1a06 230
AzureIoTClient 20:ea66de0f1a06 231 while (1)
AzureIoTClient 20:ea66de0f1a06 232 {
AzureIoTClient 20:ea66de0f1a06 233 unsigned char*buffer;
AzureIoTClient 20:ea66de0f1a06 234 size_t bufferSize;
AzureIoTClient 20:ea66de0f1a06 235
Azure.IoT Build 31:3f7731d8c71a 236 (void)printf("Sending sensor value Temperature = %d, Humidity = %d\r\n", thermostat->Temperature, thermostat->Humidity);
AzureIoTClient 20:ea66de0f1a06 237
AzureIoTClient 22:afe08a0ed332 238 if (SERIALIZE(&buffer, &bufferSize, thermostat->DeviceId, thermostat->Temperature, thermostat->Humidity, thermostat->ExternalTemperature) != IOT_AGENT_OK)
AzureIoTClient 20:ea66de0f1a06 239 {
AzureIoTClient 20:ea66de0f1a06 240 (void)printf("Failed sending sensor value\r\n");
AzureIoTClient 20:ea66de0f1a06 241 }
AzureIoTClient 20:ea66de0f1a06 242 else
AzureIoTClient 20:ea66de0f1a06 243 {
AzureIoTClient 20:ea66de0f1a06 244 sendMessage(iotHubClientHandle, buffer, bufferSize);
AzureIoTClient 20:ea66de0f1a06 245 }
AzureIoTClient 20:ea66de0f1a06 246
AzureIoTClient 20:ea66de0f1a06 247 ThreadAPI_Sleep(1000);
AzureIoTClient 20:ea66de0f1a06 248 }
AzureIoTClient 20:ea66de0f1a06 249 }
AzureIoTClient 20:ea66de0f1a06 250
AzureIoTClient 20:ea66de0f1a06 251 DESTROY_MODEL_INSTANCE(thermostat);
AzureIoTClient 20:ea66de0f1a06 252 }
AzureIoTClient 20:ea66de0f1a06 253 IoTHubClient_Destroy(iotHubClientHandle);
AzureIoTClient 20:ea66de0f1a06 254 }
AzureIoTClient 20:ea66de0f1a06 255 serializer_deinit();
AzureIoTClient 20:ea66de0f1a06 256 }
AzureIoTClient 20:ea66de0f1a06 257 }