A modelling and serializer library for Microsoft Azure IoTHub client applications
Dependents: sht15_remote_monitoring f767zi_mqtt remote_monitoring simplesample_amqp ... more
This library implements a serializer library to be used in projects involving Microsoft Azure IoT Hub connectivity. The code is replicated from https://github.com/Azure/azure-iot-sdks
serializer_devicetwin.h@22:422d94bd3c18, 2017-01-28 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Sat Jan 28 09:35:06 2017 -0800
- Revision:
- 22:422d94bd3c18
- Parent:
- 18:58b667752399
- Child:
- 23:078ea26cffcd
1.1.6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 17:fa1bba4c6053 | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 17:fa1bba4c6053 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 17:fa1bba4c6053 | 3 | |
AzureIoTClient | 17:fa1bba4c6053 | 4 | |
AzureIoTClient | 17:fa1bba4c6053 | 5 | #ifndef SERIALIZER_DEVICE_TWIN_H |
AzureIoTClient | 17:fa1bba4c6053 | 6 | #define SERIALIZER_DEVICE_TWIN_H |
AzureIoTClient | 17:fa1bba4c6053 | 7 | |
AzureIoTClient | 17:fa1bba4c6053 | 8 | #include "serializer.h" |
AzureIoTClient | 17:fa1bba4c6053 | 9 | |
AzureIoTClient | 17:fa1bba4c6053 | 10 | #include "iothub_client.h" |
AzureIoTClient | 17:fa1bba4c6053 | 11 | #include "iothub_client_ll.h" |
AzureIoTClient | 17:fa1bba4c6053 | 12 | #include "parson.h" |
AzureIoTClient | 22:422d94bd3c18 | 13 | #include "azure_c_shared_utility/vector.h" |
Azure.IoT.Build | 18:58b667752399 | 14 | #include "methodreturn.h" |
AzureIoTClient | 17:fa1bba4c6053 | 15 | |
AzureIoTClient | 17:fa1bba4c6053 | 16 | static void serializer_ingest(DEVICE_TWIN_UPDATE_STATE update_state, const unsigned char* payLoad, size_t size, void* userContextCallback) |
AzureIoTClient | 17:fa1bba4c6053 | 17 | { |
AzureIoTClient | 17:fa1bba4c6053 | 18 | /*by convention, userContextCallback is a pointer to a model instance created with CodeFirst_CreateDevice*/ |
AzureIoTClient | 17:fa1bba4c6053 | 19 | |
AzureIoTClient | 17:fa1bba4c6053 | 20 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_001: [ serializer_ingest shall clone the payload into a null terminated string. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 21 | char* copyOfPayload = (char*)malloc(size + 1); |
AzureIoTClient | 17:fa1bba4c6053 | 22 | if (copyOfPayload == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 23 | { |
AzureIoTClient | 17:fa1bba4c6053 | 24 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_008: [ If any of the above operations fail, then serializer_ingest shall return. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 25 | LogError("unable to malloc\n"); |
AzureIoTClient | 17:fa1bba4c6053 | 26 | } |
AzureIoTClient | 17:fa1bba4c6053 | 27 | else |
AzureIoTClient | 17:fa1bba4c6053 | 28 | { |
AzureIoTClient | 22:422d94bd3c18 | 29 | (void)memcpy(copyOfPayload, payLoad, size); |
AzureIoTClient | 17:fa1bba4c6053 | 30 | copyOfPayload[size] = '\0'; |
AzureIoTClient | 17:fa1bba4c6053 | 31 | |
AzureIoTClient | 17:fa1bba4c6053 | 32 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_002: [ serializer_ingest shall parse the null terminated string into parson data types. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 33 | JSON_Value* allJSON = json_parse_string(copyOfPayload); |
AzureIoTClient | 17:fa1bba4c6053 | 34 | if (allJSON == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 35 | { |
AzureIoTClient | 17:fa1bba4c6053 | 36 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_008: [ If any of the above operations fail, then serializer_ingest shall return. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 37 | LogError("failure in json_parse_string"); |
AzureIoTClient | 17:fa1bba4c6053 | 38 | } |
AzureIoTClient | 17:fa1bba4c6053 | 39 | else |
AzureIoTClient | 17:fa1bba4c6053 | 40 | { |
AzureIoTClient | 17:fa1bba4c6053 | 41 | JSON_Object *allObject = json_value_get_object(allJSON); |
AzureIoTClient | 17:fa1bba4c6053 | 42 | if (allObject == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 43 | { |
AzureIoTClient | 17:fa1bba4c6053 | 44 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_008: [ If any of the above operations fail, then serializer_ingest shall return. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 45 | LogError("failure in json_value_get_object"); |
AzureIoTClient | 17:fa1bba4c6053 | 46 | } |
AzureIoTClient | 17:fa1bba4c6053 | 47 | else |
AzureIoTClient | 17:fa1bba4c6053 | 48 | { |
AzureIoTClient | 17:fa1bba4c6053 | 49 | switch (update_state) |
AzureIoTClient | 17:fa1bba4c6053 | 50 | { |
AzureIoTClient | 17:fa1bba4c6053 | 51 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_003: [ If update_state is DEVICE_TWIN_UPDATE_COMPLETE then serializer_ingest shall locate "desired" json name. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 52 | case DEVICE_TWIN_UPDATE_COMPLETE: |
AzureIoTClient | 17:fa1bba4c6053 | 53 | { |
AzureIoTClient | 17:fa1bba4c6053 | 54 | JSON_Object* desired = json_object_get_object(allObject, "desired"); |
AzureIoTClient | 17:fa1bba4c6053 | 55 | if (desired == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 56 | { |
AzureIoTClient | 17:fa1bba4c6053 | 57 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_008: [ If any of the above operations fail, then serializer_ingest shall return. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 58 | LogError("failure in json_object_get_object"); |
AzureIoTClient | 17:fa1bba4c6053 | 59 | } |
AzureIoTClient | 17:fa1bba4c6053 | 60 | else |
AzureIoTClient | 17:fa1bba4c6053 | 61 | { |
AzureIoTClient | 17:fa1bba4c6053 | 62 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_004: [ If "desired" contains "$version" then serializer_ingest shall remove it. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 63 | (void)json_object_remove(desired, "$version"); //it might not exist |
AzureIoTClient | 17:fa1bba4c6053 | 64 | JSON_Value* desiredAfterRemove = json_object_get_value(allObject, "desired"); |
AzureIoTClient | 17:fa1bba4c6053 | 65 | if (desiredAfterRemove != NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 66 | { |
AzureIoTClient | 17:fa1bba4c6053 | 67 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_005: [ The "desired" value shall be outputed to a null terminated string and serializer_ingest shall call CodeFirst_IngestDesiredProperties. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 68 | char* pretty = json_serialize_to_string(desiredAfterRemove); |
AzureIoTClient | 17:fa1bba4c6053 | 69 | if (pretty == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 70 | { |
AzureIoTClient | 17:fa1bba4c6053 | 71 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_008: [ If any of the above operations fail, then serializer_ingest shall return. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 72 | LogError("failure in json_serialize_to_string\n"); |
AzureIoTClient | 17:fa1bba4c6053 | 73 | } |
AzureIoTClient | 17:fa1bba4c6053 | 74 | else |
AzureIoTClient | 17:fa1bba4c6053 | 75 | { |
AzureIoTClient | 17:fa1bba4c6053 | 76 | if (CodeFirst_IngestDesiredProperties(userContextCallback, pretty) != CODEFIRST_OK) |
AzureIoTClient | 17:fa1bba4c6053 | 77 | { |
AzureIoTClient | 17:fa1bba4c6053 | 78 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_008: [ If any of the above operations fail, then serializer_ingest shall return. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 79 | LogError("failure ingesting desired properties\n"); |
AzureIoTClient | 17:fa1bba4c6053 | 80 | } |
AzureIoTClient | 17:fa1bba4c6053 | 81 | else |
AzureIoTClient | 17:fa1bba4c6053 | 82 | { |
AzureIoTClient | 17:fa1bba4c6053 | 83 | /*all is fine*/ |
AzureIoTClient | 17:fa1bba4c6053 | 84 | } |
AzureIoTClient | 17:fa1bba4c6053 | 85 | free(pretty); |
AzureIoTClient | 17:fa1bba4c6053 | 86 | } |
AzureIoTClient | 17:fa1bba4c6053 | 87 | } |
AzureIoTClient | 17:fa1bba4c6053 | 88 | } |
AzureIoTClient | 17:fa1bba4c6053 | 89 | break; |
AzureIoTClient | 17:fa1bba4c6053 | 90 | } |
AzureIoTClient | 17:fa1bba4c6053 | 91 | case DEVICE_TWIN_UPDATE_PARTIAL: |
AzureIoTClient | 17:fa1bba4c6053 | 92 | { |
AzureIoTClient | 17:fa1bba4c6053 | 93 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_006: [ If update_state is DEVICE_TWIN_UPDATE_PARTIAL then serializer_ingest shall remove "$version" (if it exists). ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 94 | (void)json_object_remove(allObject, "$version"); |
AzureIoTClient | 17:fa1bba4c6053 | 95 | |
AzureIoTClient | 17:fa1bba4c6053 | 96 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_007: [ The JSON shall be outputed to a null terminated string and serializer_ingest shall call CodeFirst_IngestDesiredProperties. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 97 | char* pretty = json_serialize_to_string(allJSON); |
AzureIoTClient | 17:fa1bba4c6053 | 98 | if (pretty == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 99 | { |
AzureIoTClient | 17:fa1bba4c6053 | 100 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_008: [ If any of the above operations fail, then serializer_ingest shall return. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 101 | LogError("failure in json_serialize_to_string\n"); |
AzureIoTClient | 17:fa1bba4c6053 | 102 | } |
AzureIoTClient | 17:fa1bba4c6053 | 103 | else |
AzureIoTClient | 17:fa1bba4c6053 | 104 | { |
AzureIoTClient | 17:fa1bba4c6053 | 105 | if (CodeFirst_IngestDesiredProperties(userContextCallback, pretty) != CODEFIRST_OK) |
AzureIoTClient | 17:fa1bba4c6053 | 106 | { |
AzureIoTClient | 17:fa1bba4c6053 | 107 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_008: [ If any of the above operations fail, then serializer_ingest shall return. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 108 | LogError("failure ingesting desired properties\n"); |
AzureIoTClient | 17:fa1bba4c6053 | 109 | } |
AzureIoTClient | 17:fa1bba4c6053 | 110 | else |
AzureIoTClient | 17:fa1bba4c6053 | 111 | { |
AzureIoTClient | 17:fa1bba4c6053 | 112 | /*all is fine*/ |
AzureIoTClient | 17:fa1bba4c6053 | 113 | } |
AzureIoTClient | 17:fa1bba4c6053 | 114 | free(pretty); |
AzureIoTClient | 17:fa1bba4c6053 | 115 | } |
AzureIoTClient | 17:fa1bba4c6053 | 116 | break; |
AzureIoTClient | 17:fa1bba4c6053 | 117 | } |
AzureIoTClient | 17:fa1bba4c6053 | 118 | default: |
AzureIoTClient | 17:fa1bba4c6053 | 119 | { |
AzureIoTClient | 17:fa1bba4c6053 | 120 | LogError("INTERNAL ERROR: unexpected value for update_state=%d\n", (int)update_state); |
AzureIoTClient | 17:fa1bba4c6053 | 121 | } |
AzureIoTClient | 17:fa1bba4c6053 | 122 | } |
AzureIoTClient | 17:fa1bba4c6053 | 123 | } |
AzureIoTClient | 17:fa1bba4c6053 | 124 | json_value_free(allJSON); |
AzureIoTClient | 17:fa1bba4c6053 | 125 | } |
AzureIoTClient | 17:fa1bba4c6053 | 126 | free(copyOfPayload); |
AzureIoTClient | 17:fa1bba4c6053 | 127 | } |
AzureIoTClient | 17:fa1bba4c6053 | 128 | } |
AzureIoTClient | 17:fa1bba4c6053 | 129 | |
Azure.IoT.Build | 18:58b667752399 | 130 | /*both LL and convenience layer can be served by the same callback*/ |
Azure.IoT.Build | 18:58b667752399 | 131 | static int deviceMethodCallback(const char* method_name, const unsigned char* payload, size_t size, unsigned char** response, size_t* resp_size, void* userContextCallback) |
Azure.IoT.Build | 18:58b667752399 | 132 | { |
Azure.IoT.Build | 18:58b667752399 | 133 | int result; |
Azure.IoT.Build | 18:58b667752399 | 134 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_021: [ deviceMethodCallback shall transform payload and size into a null terminated string. ]*/ |
Azure.IoT.Build | 18:58b667752399 | 135 | char* payloadZeroTerminated = (char*)malloc(size + 1); |
Azure.IoT.Build | 18:58b667752399 | 136 | if (payloadZeroTerminated == NULL) |
Azure.IoT.Build | 18:58b667752399 | 137 | { |
Azure.IoT.Build | 18:58b667752399 | 138 | LogError("failure in malloc"); |
Azure.IoT.Build | 18:58b667752399 | 139 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_026: [ If any failure occurs in the above operations, then deviceMethodCallback shall fail, return 500, set *response to NULL and '*resp_size` to 0. ]*/ |
Azure.IoT.Build | 18:58b667752399 | 140 | *response = NULL; |
Azure.IoT.Build | 18:58b667752399 | 141 | *resp_size = 0; |
Azure.IoT.Build | 18:58b667752399 | 142 | result = 500; |
Azure.IoT.Build | 18:58b667752399 | 143 | } |
Azure.IoT.Build | 18:58b667752399 | 144 | else |
Azure.IoT.Build | 18:58b667752399 | 145 | { |
AzureIoTClient | 22:422d94bd3c18 | 146 | (void)memcpy(payloadZeroTerminated, payload, size); |
Azure.IoT.Build | 18:58b667752399 | 147 | payloadZeroTerminated[size] = '\0'; |
Azure.IoT.Build | 18:58b667752399 | 148 | |
Azure.IoT.Build | 18:58b667752399 | 149 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_022: [ deviceMethodCallback shall call EXECUTE_METHOD passing the userContextCallback, method_name and the null terminated string build before. ]*/ |
Azure.IoT.Build | 18:58b667752399 | 150 | METHODRETURN_HANDLE mr = EXECUTE_METHOD(userContextCallback, method_name, payloadZeroTerminated); |
Azure.IoT.Build | 18:58b667752399 | 151 | |
Azure.IoT.Build | 18:58b667752399 | 152 | if (mr == NULL) |
Azure.IoT.Build | 18:58b667752399 | 153 | { |
Azure.IoT.Build | 18:58b667752399 | 154 | LogError("failure in EXECUTE_METHOD"); |
Azure.IoT.Build | 18:58b667752399 | 155 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_026: [ If any failure occurs in the above operations, then deviceMethodCallback shall fail, return 500, set *response to NULL and '*resp_size` to 0. ]*/ |
Azure.IoT.Build | 18:58b667752399 | 156 | *response = NULL; |
Azure.IoT.Build | 18:58b667752399 | 157 | *resp_size = 0; |
Azure.IoT.Build | 18:58b667752399 | 158 | result = 500; |
Azure.IoT.Build | 18:58b667752399 | 159 | } |
Azure.IoT.Build | 18:58b667752399 | 160 | else |
Azure.IoT.Build | 18:58b667752399 | 161 | { |
Azure.IoT.Build | 18:58b667752399 | 162 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_023: [ deviceMethodCallback shall get the MethodReturn_Data and shall copy the response JSON value into a new byte array. ]*/ |
Azure.IoT.Build | 18:58b667752399 | 163 | const METHODRETURN_DATA* data = MethodReturn_GetReturn(mr); |
Azure.IoT.Build | 18:58b667752399 | 164 | |
Azure.IoT.Build | 18:58b667752399 | 165 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_025: [ deviceMethodCallback returns the statusCode from the user. ]*/ |
Azure.IoT.Build | 18:58b667752399 | 166 | result = data->statusCode; |
Azure.IoT.Build | 18:58b667752399 | 167 | |
Azure.IoT.Build | 18:58b667752399 | 168 | if (data->jsonValue == NULL) |
Azure.IoT.Build | 18:58b667752399 | 169 | { |
Azure.IoT.Build | 18:58b667752399 | 170 | *resp_size = 0; |
Azure.IoT.Build | 18:58b667752399 | 171 | *response = NULL; |
Azure.IoT.Build | 18:58b667752399 | 172 | } |
Azure.IoT.Build | 18:58b667752399 | 173 | else |
Azure.IoT.Build | 18:58b667752399 | 174 | { |
Azure.IoT.Build | 18:58b667752399 | 175 | *resp_size = strlen(data->jsonValue); |
Azure.IoT.Build | 18:58b667752399 | 176 | *response = (unsigned char*)malloc(*resp_size); |
Azure.IoT.Build | 18:58b667752399 | 177 | if (*response == NULL) |
Azure.IoT.Build | 18:58b667752399 | 178 | { |
Azure.IoT.Build | 18:58b667752399 | 179 | LogError("failure in malloc"); |
Azure.IoT.Build | 18:58b667752399 | 180 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_026: [ If any failure occurs in the above operations, then deviceMethodCallback shall fail, return 500, set *response to NULL and '*resp_size` to 0. ]*/ |
Azure.IoT.Build | 18:58b667752399 | 181 | *response = NULL; |
Azure.IoT.Build | 18:58b667752399 | 182 | *resp_size = 0; |
Azure.IoT.Build | 18:58b667752399 | 183 | result = 500; |
Azure.IoT.Build | 18:58b667752399 | 184 | } |
Azure.IoT.Build | 18:58b667752399 | 185 | else |
Azure.IoT.Build | 18:58b667752399 | 186 | { |
Azure.IoT.Build | 18:58b667752399 | 187 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_024: [ deviceMethodCallback shall set *response to this new byte array, *resp_size to the size of the array. ]*/ |
AzureIoTClient | 22:422d94bd3c18 | 188 | (void)memcpy(*response, data->jsonValue, *resp_size); |
Azure.IoT.Build | 18:58b667752399 | 189 | } |
Azure.IoT.Build | 18:58b667752399 | 190 | } |
Azure.IoT.Build | 18:58b667752399 | 191 | MethodReturn_Destroy(mr); |
Azure.IoT.Build | 18:58b667752399 | 192 | } |
Azure.IoT.Build | 18:58b667752399 | 193 | free(payloadZeroTerminated); |
Azure.IoT.Build | 18:58b667752399 | 194 | } |
Azure.IoT.Build | 18:58b667752399 | 195 | return result; |
Azure.IoT.Build | 18:58b667752399 | 196 | } |
Azure.IoT.Build | 18:58b667752399 | 197 | |
AzureIoTClient | 17:fa1bba4c6053 | 198 | /*an enum that sets the type of the handle used to record IoTHubDeviceTwin_Create was called*/ |
AzureIoTClient | 17:fa1bba4c6053 | 199 | #define IOTHUB_CLIENT_HANDLE_TYPE_VALUES \ |
AzureIoTClient | 17:fa1bba4c6053 | 200 | IOTHUB_CLIENT_CONVENIENCE_HANDLE_TYPE, \ |
AzureIoTClient | 17:fa1bba4c6053 | 201 | IOTHUB_CLIENT_LL_HANDLE_TYPE |
AzureIoTClient | 17:fa1bba4c6053 | 202 | |
AzureIoTClient | 17:fa1bba4c6053 | 203 | DEFINE_ENUM(IOTHUB_CLIENT_HANDLE_TYPE, IOTHUB_CLIENT_HANDLE_TYPE_VALUES) |
AzureIoTClient | 17:fa1bba4c6053 | 204 | |
AzureIoTClient | 17:fa1bba4c6053 | 205 | typedef union IOTHUB_CLIENT_HANDLE_VALUE_TAG |
AzureIoTClient | 17:fa1bba4c6053 | 206 | { |
AzureIoTClient | 17:fa1bba4c6053 | 207 | IOTHUB_CLIENT_HANDLE iothubClientHandle; |
AzureIoTClient | 17:fa1bba4c6053 | 208 | IOTHUB_CLIENT_LL_HANDLE iothubClientLLHandle; |
AzureIoTClient | 17:fa1bba4c6053 | 209 | } IOTHUB_CLIENT_HANDLE_VALUE; |
AzureIoTClient | 17:fa1bba4c6053 | 210 | |
AzureIoTClient | 17:fa1bba4c6053 | 211 | typedef struct IOTHUB_CLIENT_HANDLE_VARIANT_TAG |
AzureIoTClient | 17:fa1bba4c6053 | 212 | { |
AzureIoTClient | 17:fa1bba4c6053 | 213 | IOTHUB_CLIENT_HANDLE_TYPE iothubClientHandleType; |
AzureIoTClient | 17:fa1bba4c6053 | 214 | IOTHUB_CLIENT_HANDLE_VALUE iothubClientHandleValue; |
AzureIoTClient | 17:fa1bba4c6053 | 215 | } IOTHUB_CLIENT_HANDLE_VARIANT; |
AzureIoTClient | 17:fa1bba4c6053 | 216 | |
AzureIoTClient | 17:fa1bba4c6053 | 217 | typedef struct SERIALIZER_DEVICETWIN_PROTOHANDLE_TAG /*it is called "PROTOHANDLE" because it is a primitive type of handle*/ |
AzureIoTClient | 17:fa1bba4c6053 | 218 | { |
AzureIoTClient | 17:fa1bba4c6053 | 219 | IOTHUB_CLIENT_HANDLE_VARIANT iothubClientHandleVariant; |
AzureIoTClient | 17:fa1bba4c6053 | 220 | void* deviceAssigned; |
AzureIoTClient | 17:fa1bba4c6053 | 221 | } SERIALIZER_DEVICETWIN_PROTOHANDLE; |
AzureIoTClient | 17:fa1bba4c6053 | 222 | |
AzureIoTClient | 17:fa1bba4c6053 | 223 | static VECTOR_HANDLE g_allProtoHandles=NULL; /*contains SERIALIZER_DEVICETWIN_PROTOHANDLE*/ |
AzureIoTClient | 17:fa1bba4c6053 | 224 | |
AzureIoTClient | 17:fa1bba4c6053 | 225 | static int lazilyAddProtohandle(const SERIALIZER_DEVICETWIN_PROTOHANDLE* protoHandle) |
AzureIoTClient | 17:fa1bba4c6053 | 226 | { |
AzureIoTClient | 17:fa1bba4c6053 | 227 | int result; |
AzureIoTClient | 17:fa1bba4c6053 | 228 | if ((g_allProtoHandles == NULL) && ((g_allProtoHandles = VECTOR_create(sizeof(SERIALIZER_DEVICETWIN_PROTOHANDLE))) == NULL)) |
AzureIoTClient | 17:fa1bba4c6053 | 229 | { |
AzureIoTClient | 17:fa1bba4c6053 | 230 | LogError("failure in VECTOR_create"); |
AzureIoTClient | 17:fa1bba4c6053 | 231 | result = __LINE__; |
AzureIoTClient | 17:fa1bba4c6053 | 232 | } |
AzureIoTClient | 17:fa1bba4c6053 | 233 | else |
AzureIoTClient | 17:fa1bba4c6053 | 234 | { |
AzureIoTClient | 17:fa1bba4c6053 | 235 | if (VECTOR_push_back(g_allProtoHandles, protoHandle, 1) != 0) |
AzureIoTClient | 17:fa1bba4c6053 | 236 | { |
AzureIoTClient | 17:fa1bba4c6053 | 237 | LogError("failure in VECTOR_push_back"); |
AzureIoTClient | 17:fa1bba4c6053 | 238 | result = __LINE__; |
AzureIoTClient | 17:fa1bba4c6053 | 239 | |
AzureIoTClient | 17:fa1bba4c6053 | 240 | /*leave it as it was*/ |
AzureIoTClient | 17:fa1bba4c6053 | 241 | |
AzureIoTClient | 17:fa1bba4c6053 | 242 | if (VECTOR_size(g_allProtoHandles) == 0) |
AzureIoTClient | 17:fa1bba4c6053 | 243 | { |
AzureIoTClient | 17:fa1bba4c6053 | 244 | VECTOR_destroy(g_allProtoHandles); |
AzureIoTClient | 17:fa1bba4c6053 | 245 | g_allProtoHandles = NULL; |
AzureIoTClient | 17:fa1bba4c6053 | 246 | } |
AzureIoTClient | 17:fa1bba4c6053 | 247 | } |
AzureIoTClient | 17:fa1bba4c6053 | 248 | else |
AzureIoTClient | 17:fa1bba4c6053 | 249 | { |
AzureIoTClient | 17:fa1bba4c6053 | 250 | result = 0; |
AzureIoTClient | 17:fa1bba4c6053 | 251 | } |
AzureIoTClient | 17:fa1bba4c6053 | 252 | } |
AzureIoTClient | 17:fa1bba4c6053 | 253 | return result; |
AzureIoTClient | 17:fa1bba4c6053 | 254 | } |
AzureIoTClient | 17:fa1bba4c6053 | 255 | |
Azure.IoT.Build | 18:58b667752399 | 256 | static IOTHUB_CLIENT_RESULT Generic_IoTHubClient_SetCallbacks(const SERIALIZER_DEVICETWIN_PROTOHANDLE* protoHandle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK deviceTwinCallback, void* userContextCallback) |
AzureIoTClient | 17:fa1bba4c6053 | 257 | { |
AzureIoTClient | 17:fa1bba4c6053 | 258 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 17:fa1bba4c6053 | 259 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_011: [ IoTHubDeviceTwinCreate_Impl shall set the device twin callback. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 260 | switch (protoHandle->iothubClientHandleVariant.iothubClientHandleType) |
AzureIoTClient | 17:fa1bba4c6053 | 261 | { |
AzureIoTClient | 17:fa1bba4c6053 | 262 | case IOTHUB_CLIENT_CONVENIENCE_HANDLE_TYPE: |
AzureIoTClient | 17:fa1bba4c6053 | 263 | { |
AzureIoTClient | 17:fa1bba4c6053 | 264 | if ((result = IoTHubClient_SetDeviceTwinCallback(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientHandle, deviceTwinCallback, userContextCallback)) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 17:fa1bba4c6053 | 265 | { |
AzureIoTClient | 17:fa1bba4c6053 | 266 | LogError("failure in IoTHubClient_SetDeviceTwinCallback"); |
AzureIoTClient | 17:fa1bba4c6053 | 267 | } |
Azure.IoT.Build | 18:58b667752399 | 268 | else |
Azure.IoT.Build | 18:58b667752399 | 269 | { |
Azure.IoT.Build | 18:58b667752399 | 270 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_027: [ IoTHubDeviceTwinCreate_Impl shall set the device method callback ]*/ |
Azure.IoT.Build | 18:58b667752399 | 271 | if ((result = IoTHubClient_SetDeviceMethodCallback(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientHandle, deviceMethodCallback, userContextCallback)) != IOTHUB_CLIENT_OK) |
Azure.IoT.Build | 18:58b667752399 | 272 | { |
Azure.IoT.Build | 18:58b667752399 | 273 | (void)IoTHubClient_SetDeviceTwinCallback(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientHandle, NULL, NULL); |
Azure.IoT.Build | 18:58b667752399 | 274 | LogError("failure in IoTHubClient_SetDeviceMethodCallback"); |
Azure.IoT.Build | 18:58b667752399 | 275 | } |
Azure.IoT.Build | 18:58b667752399 | 276 | } |
AzureIoTClient | 17:fa1bba4c6053 | 277 | break; |
AzureIoTClient | 17:fa1bba4c6053 | 278 | } |
AzureIoTClient | 17:fa1bba4c6053 | 279 | case IOTHUB_CLIENT_LL_HANDLE_TYPE: |
AzureIoTClient | 17:fa1bba4c6053 | 280 | { |
AzureIoTClient | 17:fa1bba4c6053 | 281 | if ((result =IoTHubClient_LL_SetDeviceTwinCallback(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientLLHandle, deviceTwinCallback, userContextCallback)) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 17:fa1bba4c6053 | 282 | { |
AzureIoTClient | 17:fa1bba4c6053 | 283 | LogError("failure in IoTHubClient_LL_SetDeviceTwinCallback"); |
AzureIoTClient | 17:fa1bba4c6053 | 284 | } |
Azure.IoT.Build | 18:58b667752399 | 285 | else |
Azure.IoT.Build | 18:58b667752399 | 286 | { |
Azure.IoT.Build | 18:58b667752399 | 287 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_027: [ IoTHubDeviceTwinCreate_Impl shall set the device method callback ]*/ |
Azure.IoT.Build | 18:58b667752399 | 288 | if ((result = IoTHubClient_LL_SetDeviceMethodCallback(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientLLHandle, deviceMethodCallback, userContextCallback)) != IOTHUB_CLIENT_OK) |
Azure.IoT.Build | 18:58b667752399 | 289 | { |
Azure.IoT.Build | 18:58b667752399 | 290 | (void)IoTHubClient_LL_SetDeviceTwinCallback(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientLLHandle, NULL, NULL); |
Azure.IoT.Build | 18:58b667752399 | 291 | LogError("failure in IoTHubClient_SetDeviceMethodCallback"); |
Azure.IoT.Build | 18:58b667752399 | 292 | } |
Azure.IoT.Build | 18:58b667752399 | 293 | } |
AzureIoTClient | 17:fa1bba4c6053 | 294 | break; |
AzureIoTClient | 17:fa1bba4c6053 | 295 | } |
AzureIoTClient | 17:fa1bba4c6053 | 296 | default: |
AzureIoTClient | 17:fa1bba4c6053 | 297 | { |
AzureIoTClient | 17:fa1bba4c6053 | 298 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 17:fa1bba4c6053 | 299 | LogError("INTERNAL ERROR"); |
AzureIoTClient | 17:fa1bba4c6053 | 300 | } |
AzureIoTClient | 17:fa1bba4c6053 | 301 | }/*switch*/ |
AzureIoTClient | 17:fa1bba4c6053 | 302 | return result; |
AzureIoTClient | 17:fa1bba4c6053 | 303 | } |
AzureIoTClient | 17:fa1bba4c6053 | 304 | |
AzureIoTClient | 17:fa1bba4c6053 | 305 | static void* IoTHubDeviceTwinCreate_Impl(const char* name, size_t sizeOfName, SERIALIZER_DEVICETWIN_PROTOHANDLE* protoHandle) |
AzureIoTClient | 17:fa1bba4c6053 | 306 | { |
AzureIoTClient | 17:fa1bba4c6053 | 307 | void* result; |
AzureIoTClient | 17:fa1bba4c6053 | 308 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_009: [ IoTHubDeviceTwinCreate_Impl shall locate the model and the metadata for name by calling Schema_GetSchemaForModel/Schema_GetMetadata/Schema_GetModelByName. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 309 | SCHEMA_HANDLE h = Schema_GetSchemaForModel(name); |
AzureIoTClient | 17:fa1bba4c6053 | 310 | if (h == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 311 | { |
AzureIoTClient | 17:fa1bba4c6053 | 312 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_014: [ Otherwise, IoTHubDeviceTwinCreate_Impl shall fail and return NULL. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 313 | LogError("failure in Schema_GetSchemaForModel."); |
AzureIoTClient | 17:fa1bba4c6053 | 314 | result = NULL; |
AzureIoTClient | 17:fa1bba4c6053 | 315 | } |
AzureIoTClient | 17:fa1bba4c6053 | 316 | else |
AzureIoTClient | 17:fa1bba4c6053 | 317 | { |
AzureIoTClient | 17:fa1bba4c6053 | 318 | void* metadata = Schema_GetMetadata(h); |
AzureIoTClient | 17:fa1bba4c6053 | 319 | SCHEMA_MODEL_TYPE_HANDLE modelType = Schema_GetModelByName(h, name); |
AzureIoTClient | 17:fa1bba4c6053 | 320 | if (modelType == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 321 | { |
AzureIoTClient | 17:fa1bba4c6053 | 322 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_014: [ Otherwise, IoTHubDeviceTwinCreate_Impl shall fail and return NULL. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 323 | LogError("failure in Schema_GetModelByName"); |
AzureIoTClient | 17:fa1bba4c6053 | 324 | result = NULL; |
AzureIoTClient | 17:fa1bba4c6053 | 325 | } |
AzureIoTClient | 17:fa1bba4c6053 | 326 | else |
AzureIoTClient | 17:fa1bba4c6053 | 327 | { |
AzureIoTClient | 17:fa1bba4c6053 | 328 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_010: [ IoTHubDeviceTwinCreate_Impl shall call CodeFirst_CreateDevice. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 329 | result = CodeFirst_CreateDevice(modelType, (REFLECTED_DATA_FROM_DATAPROVIDER *)metadata, sizeOfName, true); |
AzureIoTClient | 17:fa1bba4c6053 | 330 | if (result == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 331 | { |
AzureIoTClient | 17:fa1bba4c6053 | 332 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_014: [ Otherwise, IoTHubDeviceTwinCreate_Impl shall fail and return NULL. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 333 | LogError("failure in CodeFirst_CreateDevice"); |
AzureIoTClient | 17:fa1bba4c6053 | 334 | /*return as is*/ |
AzureIoTClient | 17:fa1bba4c6053 | 335 | } |
AzureIoTClient | 17:fa1bba4c6053 | 336 | else |
AzureIoTClient | 17:fa1bba4c6053 | 337 | { |
AzureIoTClient | 17:fa1bba4c6053 | 338 | protoHandle->deviceAssigned = result; |
Azure.IoT.Build | 18:58b667752399 | 339 | if (Generic_IoTHubClient_SetCallbacks(protoHandle, serializer_ingest, result) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 17:fa1bba4c6053 | 340 | { |
AzureIoTClient | 17:fa1bba4c6053 | 341 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_014: [ Otherwise, IoTHubDeviceTwinCreate_Impl shall fail and return NULL. ]*/ |
Azure.IoT.Build | 18:58b667752399 | 342 | LogError("failure in Generic_IoTHubClient_SetCallbacks"); |
AzureIoTClient | 17:fa1bba4c6053 | 343 | CodeFirst_DestroyDevice(result); |
AzureIoTClient | 17:fa1bba4c6053 | 344 | result = NULL; |
AzureIoTClient | 17:fa1bba4c6053 | 345 | } |
AzureIoTClient | 17:fa1bba4c6053 | 346 | else |
AzureIoTClient | 17:fa1bba4c6053 | 347 | { |
AzureIoTClient | 17:fa1bba4c6053 | 348 | /*lazily add the protohandle to the array of tracking handles*/ |
AzureIoTClient | 17:fa1bba4c6053 | 349 | |
AzureIoTClient | 17:fa1bba4c6053 | 350 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_012: [ IoTHubDeviceTwinCreate_Impl shall record the pair of (device, IoTHubClient(_LL)). ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 351 | if (lazilyAddProtohandle(protoHandle) != 0) |
AzureIoTClient | 17:fa1bba4c6053 | 352 | { |
AzureIoTClient | 17:fa1bba4c6053 | 353 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_014: [ Otherwise, IoTHubDeviceTwinCreate_Impl shall fail and return NULL. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 354 | LogError("unable to add the protohandle to the collection of handles"); |
AzureIoTClient | 17:fa1bba4c6053 | 355 | /*unsubscribe*/ |
Azure.IoT.Build | 18:58b667752399 | 356 | if (Generic_IoTHubClient_SetCallbacks(protoHandle, NULL, NULL) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 17:fa1bba4c6053 | 357 | { |
AzureIoTClient | 17:fa1bba4c6053 | 358 | /*just log the error*/ |
Azure.IoT.Build | 18:58b667752399 | 359 | LogError("failure in Generic_IoTHubClient_SetCallbacks"); |
AzureIoTClient | 17:fa1bba4c6053 | 360 | } |
AzureIoTClient | 17:fa1bba4c6053 | 361 | CodeFirst_DestroyDevice(result); |
AzureIoTClient | 17:fa1bba4c6053 | 362 | result = NULL; |
AzureIoTClient | 17:fa1bba4c6053 | 363 | } |
AzureIoTClient | 17:fa1bba4c6053 | 364 | else |
AzureIoTClient | 17:fa1bba4c6053 | 365 | { |
AzureIoTClient | 17:fa1bba4c6053 | 366 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_013: [ If all operations complete successfully then IoTHubDeviceTwinCreate_Impl shall succeeds and return a non-NULL value. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 367 | /*return as is*/ |
AzureIoTClient | 17:fa1bba4c6053 | 368 | } |
AzureIoTClient | 17:fa1bba4c6053 | 369 | } |
AzureIoTClient | 17:fa1bba4c6053 | 370 | } |
AzureIoTClient | 17:fa1bba4c6053 | 371 | } |
AzureIoTClient | 17:fa1bba4c6053 | 372 | } |
AzureIoTClient | 17:fa1bba4c6053 | 373 | return result; |
AzureIoTClient | 17:fa1bba4c6053 | 374 | } |
AzureIoTClient | 17:fa1bba4c6053 | 375 | |
AzureIoTClient | 17:fa1bba4c6053 | 376 | static bool protoHandleHasDeviceStartAddress(const void* element, const void* value) |
AzureIoTClient | 17:fa1bba4c6053 | 377 | { |
AzureIoTClient | 17:fa1bba4c6053 | 378 | const SERIALIZER_DEVICETWIN_PROTOHANDLE* protoHandle = (const SERIALIZER_DEVICETWIN_PROTOHANDLE*)element; |
AzureIoTClient | 17:fa1bba4c6053 | 379 | return protoHandle->deviceAssigned == value; |
AzureIoTClient | 17:fa1bba4c6053 | 380 | } |
AzureIoTClient | 17:fa1bba4c6053 | 381 | |
AzureIoTClient | 17:fa1bba4c6053 | 382 | static void IoTHubDeviceTwin_Destroy_Impl(void* model) |
AzureIoTClient | 17:fa1bba4c6053 | 383 | { |
AzureIoTClient | 17:fa1bba4c6053 | 384 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_020: [ If model is NULL then IoTHubDeviceTwin_Destroy_Impl shall return. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 385 | if (model == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 386 | { |
AzureIoTClient | 17:fa1bba4c6053 | 387 | LogError("invalid argument void* model=%p", model); |
AzureIoTClient | 17:fa1bba4c6053 | 388 | } |
AzureIoTClient | 17:fa1bba4c6053 | 389 | else |
AzureIoTClient | 17:fa1bba4c6053 | 390 | { |
AzureIoTClient | 17:fa1bba4c6053 | 391 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_015: [ IoTHubDeviceTwin_Destroy_Impl shall locate the saved handle belonging to model. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 392 | SERIALIZER_DEVICETWIN_PROTOHANDLE* protoHandle = (SERIALIZER_DEVICETWIN_PROTOHANDLE*)VECTOR_find_if(g_allProtoHandles, protoHandleHasDeviceStartAddress, model); |
AzureIoTClient | 17:fa1bba4c6053 | 393 | if (protoHandle == NULL) |
AzureIoTClient | 17:fa1bba4c6053 | 394 | { |
AzureIoTClient | 17:fa1bba4c6053 | 395 | LogError("failure in VECTOR_find_if [not found]"); |
AzureIoTClient | 17:fa1bba4c6053 | 396 | } |
AzureIoTClient | 17:fa1bba4c6053 | 397 | else |
AzureIoTClient | 17:fa1bba4c6053 | 398 | { |
AzureIoTClient | 17:fa1bba4c6053 | 399 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_016: [ IoTHubDeviceTwin_Destroy_Impl shall set the devicetwin callback to NULL. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 400 | switch (protoHandle->iothubClientHandleVariant.iothubClientHandleType) |
AzureIoTClient | 17:fa1bba4c6053 | 401 | { |
AzureIoTClient | 17:fa1bba4c6053 | 402 | case IOTHUB_CLIENT_CONVENIENCE_HANDLE_TYPE: |
AzureIoTClient | 17:fa1bba4c6053 | 403 | { |
AzureIoTClient | 17:fa1bba4c6053 | 404 | if (IoTHubClient_SetDeviceTwinCallback(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientHandle, NULL, NULL) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 17:fa1bba4c6053 | 405 | { |
AzureIoTClient | 17:fa1bba4c6053 | 406 | LogError("failure in IoTHubClient_SetDeviceTwinCallback"); |
AzureIoTClient | 17:fa1bba4c6053 | 407 | } |
Azure.IoT.Build | 18:58b667752399 | 408 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_028: [ IoTHubDeviceTwin_Destroy_Impl shall set the method callback to NULL. ]*/ |
Azure.IoT.Build | 18:58b667752399 | 409 | if (IoTHubClient_SetDeviceMethodCallback(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientHandle, NULL, NULL) != IOTHUB_CLIENT_OK) |
Azure.IoT.Build | 18:58b667752399 | 410 | { |
Azure.IoT.Build | 18:58b667752399 | 411 | LogError("failure in IoTHubClient_SetDeviceMethodCallback"); |
Azure.IoT.Build | 18:58b667752399 | 412 | } |
AzureIoTClient | 17:fa1bba4c6053 | 413 | break; |
AzureIoTClient | 17:fa1bba4c6053 | 414 | } |
AzureIoTClient | 17:fa1bba4c6053 | 415 | case IOTHUB_CLIENT_LL_HANDLE_TYPE: |
AzureIoTClient | 17:fa1bba4c6053 | 416 | { |
AzureIoTClient | 17:fa1bba4c6053 | 417 | if (IoTHubClient_LL_SetDeviceTwinCallback(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientLLHandle, NULL, NULL) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 17:fa1bba4c6053 | 418 | { |
AzureIoTClient | 17:fa1bba4c6053 | 419 | LogError("failure in IoTHubClient_LL_SetDeviceTwinCallback"); |
AzureIoTClient | 17:fa1bba4c6053 | 420 | } |
Azure.IoT.Build | 18:58b667752399 | 421 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_028: [ IoTHubDeviceTwin_Destroy_Impl shall set the method callback to NULL. ]*/ |
Azure.IoT.Build | 18:58b667752399 | 422 | if (IoTHubClient_LL_SetDeviceMethodCallback(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientLLHandle, NULL, NULL) != IOTHUB_CLIENT_OK) |
Azure.IoT.Build | 18:58b667752399 | 423 | { |
Azure.IoT.Build | 18:58b667752399 | 424 | LogError("failure in IoTHubClient_LL_SetDeviceMethodCallback"); |
Azure.IoT.Build | 18:58b667752399 | 425 | } |
AzureIoTClient | 17:fa1bba4c6053 | 426 | break; |
AzureIoTClient | 17:fa1bba4c6053 | 427 | } |
AzureIoTClient | 17:fa1bba4c6053 | 428 | default: |
AzureIoTClient | 17:fa1bba4c6053 | 429 | { |
AzureIoTClient | 17:fa1bba4c6053 | 430 | LogError("INTERNAL ERROR"); |
AzureIoTClient | 17:fa1bba4c6053 | 431 | } |
AzureIoTClient | 17:fa1bba4c6053 | 432 | }/*switch*/ |
AzureIoTClient | 17:fa1bba4c6053 | 433 | } |
AzureIoTClient | 17:fa1bba4c6053 | 434 | |
AzureIoTClient | 17:fa1bba4c6053 | 435 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_017: [ IoTHubDeviceTwin_Destroy_Impl shall call CodeFirst_DestroyDevice. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 436 | CodeFirst_DestroyDevice(protoHandle->deviceAssigned); |
AzureIoTClient | 17:fa1bba4c6053 | 437 | |
AzureIoTClient | 17:fa1bba4c6053 | 438 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_018: [ IoTHubDeviceTwin_Destroy_Impl shall remove the IoTHubClient_Handle and the device handle from the recorded set. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 439 | VECTOR_erase(g_allProtoHandles, protoHandle, 1); |
AzureIoTClient | 17:fa1bba4c6053 | 440 | |
AzureIoTClient | 17:fa1bba4c6053 | 441 | /*Codes_SRS_SERIALIZERDEVICETWIN_02_019: [ If the recorded set of IoTHubClient handles is zero size, then the set shall be destroyed. ]*/ |
AzureIoTClient | 17:fa1bba4c6053 | 442 | if (VECTOR_size(g_allProtoHandles) == 0) /*lazy init means more work @ destroy time*/ |
AzureIoTClient | 17:fa1bba4c6053 | 443 | { |
AzureIoTClient | 17:fa1bba4c6053 | 444 | VECTOR_destroy(g_allProtoHandles); |
AzureIoTClient | 17:fa1bba4c6053 | 445 | g_allProtoHandles = NULL; |
AzureIoTClient | 17:fa1bba4c6053 | 446 | } |
AzureIoTClient | 17:fa1bba4c6053 | 447 | } |
AzureIoTClient | 17:fa1bba4c6053 | 448 | } |
AzureIoTClient | 17:fa1bba4c6053 | 449 | |
Azure.IoT.Build | 18:58b667752399 | 450 | /*the below function sends the reported state of a model previously created by IoTHubDeviceTwin_Create*/ |
Azure.IoT.Build | 18:58b667752399 | 451 | /*this function serves both the _LL and the convenience layer because of protohandles*/ |
Azure.IoT.Build | 18:58b667752399 | 452 | static IOTHUB_CLIENT_RESULT IoTHubDeviceTwin_SendReportedState_Impl(void* model, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK deviceTwinCallback, void* context) |
Azure.IoT.Build | 18:58b667752399 | 453 | { |
Azure.IoT.Build | 18:58b667752399 | 454 | unsigned char*buffer; |
Azure.IoT.Build | 18:58b667752399 | 455 | size_t bufferSize; |
Azure.IoT.Build | 18:58b667752399 | 456 | |
Azure.IoT.Build | 18:58b667752399 | 457 | IOTHUB_CLIENT_RESULT result; |
Azure.IoT.Build | 18:58b667752399 | 458 | |
Azure.IoT.Build | 18:58b667752399 | 459 | if (SERIALIZE_REPORTED_PROPERTIES_FROM_POINTERS(&buffer, &bufferSize, model) != CODEFIRST_OK) |
Azure.IoT.Build | 18:58b667752399 | 460 | { |
Azure.IoT.Build | 18:58b667752399 | 461 | LogError("Failed serializing reported state"); |
Azure.IoT.Build | 18:58b667752399 | 462 | result = IOTHUB_CLIENT_ERROR; |
Azure.IoT.Build | 18:58b667752399 | 463 | } |
Azure.IoT.Build | 18:58b667752399 | 464 | else |
Azure.IoT.Build | 18:58b667752399 | 465 | { |
Azure.IoT.Build | 18:58b667752399 | 466 | SERIALIZER_DEVICETWIN_PROTOHANDLE* protoHandle = (SERIALIZER_DEVICETWIN_PROTOHANDLE*)VECTOR_find_if(g_allProtoHandles, protoHandleHasDeviceStartAddress, model); |
Azure.IoT.Build | 18:58b667752399 | 467 | if (protoHandle == NULL) |
Azure.IoT.Build | 18:58b667752399 | 468 | { |
Azure.IoT.Build | 18:58b667752399 | 469 | LogError("failure in VECTOR_find_if [not found]"); |
Azure.IoT.Build | 18:58b667752399 | 470 | result = IOTHUB_CLIENT_ERROR; |
Azure.IoT.Build | 18:58b667752399 | 471 | } |
Azure.IoT.Build | 18:58b667752399 | 472 | else |
Azure.IoT.Build | 18:58b667752399 | 473 | { |
Azure.IoT.Build | 18:58b667752399 | 474 | switch (protoHandle->iothubClientHandleVariant.iothubClientHandleType) |
Azure.IoT.Build | 18:58b667752399 | 475 | { |
Azure.IoT.Build | 18:58b667752399 | 476 | case IOTHUB_CLIENT_CONVENIENCE_HANDLE_TYPE: |
Azure.IoT.Build | 18:58b667752399 | 477 | { |
Azure.IoT.Build | 18:58b667752399 | 478 | if (IoTHubClient_SendReportedState(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientHandle, buffer, bufferSize, deviceTwinCallback, context) != IOTHUB_CLIENT_OK) |
Azure.IoT.Build | 18:58b667752399 | 479 | { |
Azure.IoT.Build | 18:58b667752399 | 480 | LogError("Failure sending data"); |
Azure.IoT.Build | 18:58b667752399 | 481 | result = IOTHUB_CLIENT_ERROR; |
Azure.IoT.Build | 18:58b667752399 | 482 | } |
Azure.IoT.Build | 18:58b667752399 | 483 | else |
Azure.IoT.Build | 18:58b667752399 | 484 | { |
Azure.IoT.Build | 18:58b667752399 | 485 | result = IOTHUB_CLIENT_OK; |
Azure.IoT.Build | 18:58b667752399 | 486 | } |
Azure.IoT.Build | 18:58b667752399 | 487 | break; |
Azure.IoT.Build | 18:58b667752399 | 488 | } |
Azure.IoT.Build | 18:58b667752399 | 489 | case IOTHUB_CLIENT_LL_HANDLE_TYPE: |
Azure.IoT.Build | 18:58b667752399 | 490 | { |
Azure.IoT.Build | 18:58b667752399 | 491 | if (IoTHubClient_LL_SendReportedState(protoHandle->iothubClientHandleVariant.iothubClientHandleValue.iothubClientLLHandle, buffer, bufferSize, deviceTwinCallback, context) != IOTHUB_CLIENT_OK) |
Azure.IoT.Build | 18:58b667752399 | 492 | { |
Azure.IoT.Build | 18:58b667752399 | 493 | LogError("Failure sending data"); |
Azure.IoT.Build | 18:58b667752399 | 494 | result = IOTHUB_CLIENT_ERROR; |
Azure.IoT.Build | 18:58b667752399 | 495 | } |
Azure.IoT.Build | 18:58b667752399 | 496 | else |
Azure.IoT.Build | 18:58b667752399 | 497 | { |
Azure.IoT.Build | 18:58b667752399 | 498 | result = IOTHUB_CLIENT_OK; |
Azure.IoT.Build | 18:58b667752399 | 499 | } |
Azure.IoT.Build | 18:58b667752399 | 500 | break; |
Azure.IoT.Build | 18:58b667752399 | 501 | } |
Azure.IoT.Build | 18:58b667752399 | 502 | default: |
Azure.IoT.Build | 18:58b667752399 | 503 | { |
Azure.IoT.Build | 18:58b667752399 | 504 | LogError("INTERNAL ERROR: unexpected value for enum (%d)", (int)protoHandle->iothubClientHandleVariant.iothubClientHandleType); |
Azure.IoT.Build | 18:58b667752399 | 505 | result = IOTHUB_CLIENT_ERROR; |
Azure.IoT.Build | 18:58b667752399 | 506 | break; |
Azure.IoT.Build | 18:58b667752399 | 507 | } |
Azure.IoT.Build | 18:58b667752399 | 508 | } |
Azure.IoT.Build | 18:58b667752399 | 509 | } |
Azure.IoT.Build | 18:58b667752399 | 510 | free(buffer); |
Azure.IoT.Build | 18:58b667752399 | 511 | } |
Azure.IoT.Build | 18:58b667752399 | 512 | return result; |
Azure.IoT.Build | 18:58b667752399 | 513 | } |
Azure.IoT.Build | 18:58b667752399 | 514 | |
AzureIoTClient | 17:fa1bba4c6053 | 515 | #define DECLARE_DEVICETWIN_MODEL(name, ...) \ |
AzureIoTClient | 17:fa1bba4c6053 | 516 | DECLARE_MODEL(name, __VA_ARGS__) \ |
AzureIoTClient | 17:fa1bba4c6053 | 517 | static name* C2(IoTHubDeviceTwin_Create, name)(IOTHUB_CLIENT_HANDLE iotHubClientHandle) \ |
AzureIoTClient | 17:fa1bba4c6053 | 518 | { \ |
AzureIoTClient | 17:fa1bba4c6053 | 519 | SERIALIZER_DEVICETWIN_PROTOHANDLE protoHandle; \ |
AzureIoTClient | 17:fa1bba4c6053 | 520 | protoHandle.iothubClientHandleVariant.iothubClientHandleType = IOTHUB_CLIENT_CONVENIENCE_HANDLE_TYPE; \ |
AzureIoTClient | 17:fa1bba4c6053 | 521 | protoHandle.iothubClientHandleVariant.iothubClientHandleValue.iothubClientHandle = iotHubClientHandle; \ |
AzureIoTClient | 17:fa1bba4c6053 | 522 | return (name*)IoTHubDeviceTwinCreate_Impl(#name, sizeof(name), &protoHandle); \ |
AzureIoTClient | 17:fa1bba4c6053 | 523 | } \ |
AzureIoTClient | 17:fa1bba4c6053 | 524 | \ |
AzureIoTClient | 17:fa1bba4c6053 | 525 | static void C2(IoTHubDeviceTwin_Destroy, name) (name* model) \ |
AzureIoTClient | 17:fa1bba4c6053 | 526 | { \ |
AzureIoTClient | 17:fa1bba4c6053 | 527 | IoTHubDeviceTwin_Destroy_Impl(model); \ |
AzureIoTClient | 17:fa1bba4c6053 | 528 | } \ |
AzureIoTClient | 17:fa1bba4c6053 | 529 | \ |
AzureIoTClient | 17:fa1bba4c6053 | 530 | static name* C2(IoTHubDeviceTwin_LL_Create, name)(IOTHUB_CLIENT_LL_HANDLE iotHubClientLLHandle) \ |
AzureIoTClient | 17:fa1bba4c6053 | 531 | { \ |
AzureIoTClient | 17:fa1bba4c6053 | 532 | SERIALIZER_DEVICETWIN_PROTOHANDLE protoHandle; \ |
AzureIoTClient | 17:fa1bba4c6053 | 533 | protoHandle.iothubClientHandleVariant.iothubClientHandleType = IOTHUB_CLIENT_LL_HANDLE_TYPE; \ |
AzureIoTClient | 17:fa1bba4c6053 | 534 | protoHandle.iothubClientHandleVariant.iothubClientHandleValue.iothubClientLLHandle = iotHubClientLLHandle; \ |
AzureIoTClient | 17:fa1bba4c6053 | 535 | return (name*)IoTHubDeviceTwinCreate_Impl(#name, sizeof(name), &protoHandle); \ |
AzureIoTClient | 17:fa1bba4c6053 | 536 | } \ |
AzureIoTClient | 17:fa1bba4c6053 | 537 | \ |
AzureIoTClient | 17:fa1bba4c6053 | 538 | static void C2(IoTHubDeviceTwin_LL_Destroy, name) (name* model) \ |
AzureIoTClient | 17:fa1bba4c6053 | 539 | { \ |
AzureIoTClient | 17:fa1bba4c6053 | 540 | IoTHubDeviceTwin_Destroy_Impl(model); \ |
AzureIoTClient | 17:fa1bba4c6053 | 541 | } \ |
Azure.IoT.Build | 18:58b667752399 | 542 | static IOTHUB_CLIENT_RESULT C2(IoTHubDeviceTwin_LL_SendReportedState, name) (name* model, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK deviceTwinCallback, void* context) \ |
Azure.IoT.Build | 18:58b667752399 | 543 | { \ |
Azure.IoT.Build | 18:58b667752399 | 544 | return IoTHubDeviceTwin_SendReportedState_Impl(model, deviceTwinCallback, context); \ |
Azure.IoT.Build | 18:58b667752399 | 545 | } \ |
Azure.IoT.Build | 18:58b667752399 | 546 | static IOTHUB_CLIENT_RESULT C2(IoTHubDeviceTwin_SendReportedState, name) (name* model, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK deviceTwinCallback, void* context) \ |
Azure.IoT.Build | 18:58b667752399 | 547 | { \ |
Azure.IoT.Build | 18:58b667752399 | 548 | return IoTHubDeviceTwin_SendReportedState_Impl(model, deviceTwinCallback, context); \ |
Azure.IoT.Build | 18:58b667752399 | 549 | } \ |
AzureIoTClient | 17:fa1bba4c6053 | 550 | |
AzureIoTClient | 17:fa1bba4c6053 | 551 | #endif /*SERIALIZER_DEVICE_TWIN_H*/ |
AzureIoTClient | 17:fa1bba4c6053 | 552 | |
AzureIoTClient | 17:fa1bba4c6053 | 553 |