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:
Fri Mar 11 17:03:30 2016 -0800
Revision:
36:c5f21f14d53f
Parent:
31:3f7731d8c71a
Child:
41:78043c14d4b2
1.0.2

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