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

Committer:
AzureIoTClient
Date:
Fri Jul 14 16:38:27 2017 -0700
Revision:
26:7c0e6f86d034
Parent:
23:078ea26cffcd
Child:
36:7d12a5386197
1.1.19

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 0:1f9b2707ec7d 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 0:1f9b2707ec7d 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 0:1f9b2707ec7d 3
AzureIoTClient 0:1f9b2707ec7d 4 #include <stdlib.h>
AzureIoTClient 23:078ea26cffcd 5 #include "azure_c_shared_utility/optimize_size.h"
Azure.IoT Build 10:c2aee3965a83 6 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 0:1f9b2707ec7d 7
AzureIoTClient 0:1f9b2707ec7d 8 #include <stddef.h>
AzureIoTClient 0:1f9b2707ec7d 9
AzureIoTClient 0:1f9b2707ec7d 10 #include "commanddecoder.h"
AzureIoTClient 0:1f9b2707ec7d 11 #include "multitree.h"
Azure.IoT Build 10:c2aee3965a83 12 #include "azure_c_shared_utility/crt_abstractions.h"
Azure.IoT Build 13:16e88f0cfa5f 13 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 0:1f9b2707ec7d 14 #include "schema.h"
AzureIoTClient 0:1f9b2707ec7d 15 #include "codefirst.h"
AzureIoTClient 0:1f9b2707ec7d 16 #include "jsondecoder.h"
AzureIoTClient 0:1f9b2707ec7d 17
AzureIoTClient 0:1f9b2707ec7d 18 DEFINE_ENUM_STRINGS(COMMANDDECODER_RESULT, COMMANDDECODER_RESULT_VALUES);
AzureIoTClient 0:1f9b2707ec7d 19
AzureIoTClient 17:fa1bba4c6053 20 typedef struct COMMAND_DECODER_HANDLE_DATA_TAG
AzureIoTClient 0:1f9b2707ec7d 21 {
Azure.IoT.Build 18:58b667752399 22 METHOD_CALLBACK_FUNC methodCallback;
Azure.IoT.Build 18:58b667752399 23 void* methodCallbackContext;
AzureIoTClient 0:1f9b2707ec7d 24 SCHEMA_MODEL_TYPE_HANDLE ModelHandle;
AzureIoTClient 0:1f9b2707ec7d 25 ACTION_CALLBACK_FUNC ActionCallback;
AzureIoTClient 0:1f9b2707ec7d 26 void* ActionCallbackContext;
AzureIoTClient 17:fa1bba4c6053 27 } COMMAND_DECODER_HANDLE_DATA;
AzureIoTClient 0:1f9b2707ec7d 28
AzureIoTClient 0:1f9b2707ec7d 29 static int DecodeValueFromNode(SCHEMA_HANDLE schemaHandle, AGENT_DATA_TYPE* agentDataType, MULTITREE_HANDLE node, const char* edmTypeName)
AzureIoTClient 0:1f9b2707ec7d 30 {
AzureIoTClient 0:1f9b2707ec7d 31 /* because "pottentially uninitialized variable on MS compiler" */
AzureIoTClient 0:1f9b2707ec7d 32 int result = 0;
AzureIoTClient 0:1f9b2707ec7d 33 const char* argStringValue;
AzureIoTClient 0:1f9b2707ec7d 34 AGENT_DATA_TYPE_TYPE primitiveType;
AzureIoTClient 0:1f9b2707ec7d 35
AzureIoTClient 0:1f9b2707ec7d 36 /* Codes_SRS_COMMAND_DECODER_99_029:[ If the argument type is complex then a complex type value shall be built from the child nodes.] */
AzureIoTClient 0:1f9b2707ec7d 37 if ((primitiveType = CodeFirst_GetPrimitiveType(edmTypeName)) == EDM_NO_TYPE)
AzureIoTClient 0:1f9b2707ec7d 38 {
AzureIoTClient 0:1f9b2707ec7d 39 SCHEMA_STRUCT_TYPE_HANDLE structTypeHandle;
AzureIoTClient 0:1f9b2707ec7d 40 size_t propertyCount;
AzureIoTClient 0:1f9b2707ec7d 41
AzureIoTClient 0:1f9b2707ec7d 42 /* Codes_SRS_COMMAND_DECODER_99_033:[ In order to determine which are the members of a complex types, Schema APIs for structure types shall be used.] */
AzureIoTClient 0:1f9b2707ec7d 43 if (((structTypeHandle = Schema_GetStructTypeByName(schemaHandle, edmTypeName)) == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 44 (Schema_GetStructTypePropertyCount(structTypeHandle, &propertyCount) != SCHEMA_OK))
AzureIoTClient 0:1f9b2707ec7d 45 {
AzureIoTClient 0:1f9b2707ec7d 46 /* Codes_SRS_COMMAND_DECODER_99_010:[ If any Schema API fails then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.]*/
AzureIoTClient 23:078ea26cffcd 47 result = __FAILURE__;
AzureIoTClient 11:b1327861f5e0 48 LogError("Getting Struct information failed.");
AzureIoTClient 0:1f9b2707ec7d 49 }
AzureIoTClient 0:1f9b2707ec7d 50 else
AzureIoTClient 0:1f9b2707ec7d 51 {
AzureIoTClient 0:1f9b2707ec7d 52 if (propertyCount == 0)
AzureIoTClient 0:1f9b2707ec7d 53 {
AzureIoTClient 0:1f9b2707ec7d 54 /* Codes_SRS_COMMAND_DECODER_99_034:[ If Schema APIs indicate that a complex type has 0 members then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 23:078ea26cffcd 55 result = __FAILURE__;
AzureIoTClient 11:b1327861f5e0 56 LogError("Struct type with 0 members is not allowed");
AzureIoTClient 0:1f9b2707ec7d 57 }
AzureIoTClient 0:1f9b2707ec7d 58 else
AzureIoTClient 0:1f9b2707ec7d 59 {
AzureIoTClient 0:1f9b2707ec7d 60 AGENT_DATA_TYPE* memberValues = (AGENT_DATA_TYPE*)malloc(sizeof(AGENT_DATA_TYPE)* propertyCount);
AzureIoTClient 0:1f9b2707ec7d 61 if (memberValues == NULL)
AzureIoTClient 0:1f9b2707ec7d 62 {
AzureIoTClient 0:1f9b2707ec7d 63 /* Codes_SRS_COMMAND_DECODER_99_021:[ If the parsing of the command fails for any other reason the command shall not be dispatched.] */
AzureIoTClient 23:078ea26cffcd 64 result = __FAILURE__;
AzureIoTClient 11:b1327861f5e0 65 LogError("Failed allocating member values for command argument");
AzureIoTClient 0:1f9b2707ec7d 66 }
AzureIoTClient 0:1f9b2707ec7d 67 else
AzureIoTClient 0:1f9b2707ec7d 68 {
AzureIoTClient 0:1f9b2707ec7d 69 const char** memberNames = (const char**)malloc(sizeof(const char*)* propertyCount);
AzureIoTClient 0:1f9b2707ec7d 70 if (memberNames == NULL)
AzureIoTClient 0:1f9b2707ec7d 71 {
AzureIoTClient 0:1f9b2707ec7d 72 /* Codes_SRS_COMMAND_DECODER_99_021:[ If the parsing of the command fails for any other reason the command shall not be dispatched.] */
AzureIoTClient 23:078ea26cffcd 73 result = __FAILURE__;
AzureIoTClient 11:b1327861f5e0 74 LogError("Failed allocating member names for command argument.");
AzureIoTClient 0:1f9b2707ec7d 75 }
AzureIoTClient 0:1f9b2707ec7d 76 else
AzureIoTClient 0:1f9b2707ec7d 77 {
AzureIoTClient 0:1f9b2707ec7d 78 size_t j;
AzureIoTClient 0:1f9b2707ec7d 79 size_t k;
AzureIoTClient 0:1f9b2707ec7d 80
AzureIoTClient 0:1f9b2707ec7d 81 for (j = 0; j < propertyCount; j++)
AzureIoTClient 0:1f9b2707ec7d 82 {
AzureIoTClient 0:1f9b2707ec7d 83 SCHEMA_PROPERTY_HANDLE propertyHandle;
AzureIoTClient 0:1f9b2707ec7d 84 MULTITREE_HANDLE memberNode;
AzureIoTClient 0:1f9b2707ec7d 85 const char* propertyName;
AzureIoTClient 0:1f9b2707ec7d 86 const char* propertyType;
AzureIoTClient 0:1f9b2707ec7d 87
AzureIoTClient 0:1f9b2707ec7d 88 if ((propertyHandle = Schema_GetStructTypePropertyByIndex(structTypeHandle, j)) == NULL)
AzureIoTClient 0:1f9b2707ec7d 89 {
AzureIoTClient 0:1f9b2707ec7d 90 /* Codes_SRS_COMMAND_DECODER_99_010:[ If any Schema API fails then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.]*/
AzureIoTClient 23:078ea26cffcd 91 result = __FAILURE__;
AzureIoTClient 11:b1327861f5e0 92 LogError("Getting struct member failed.");
AzureIoTClient 0:1f9b2707ec7d 93 break;
AzureIoTClient 0:1f9b2707ec7d 94 }
AzureIoTClient 0:1f9b2707ec7d 95 else if (((propertyName = Schema_GetPropertyName(propertyHandle)) == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 96 ((propertyType = Schema_GetPropertyType(propertyHandle)) == NULL))
AzureIoTClient 0:1f9b2707ec7d 97 {
AzureIoTClient 0:1f9b2707ec7d 98 /* Codes_SRS_COMMAND_DECODER_99_010:[ If any Schema API fails then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.]*/
AzureIoTClient 23:078ea26cffcd 99 result = __FAILURE__;
AzureIoTClient 11:b1327861f5e0 100 LogError("Getting the struct member information failed.");
AzureIoTClient 0:1f9b2707ec7d 101 break;
AzureIoTClient 0:1f9b2707ec7d 102 }
AzureIoTClient 0:1f9b2707ec7d 103 else
AzureIoTClient 0:1f9b2707ec7d 104 {
AzureIoTClient 0:1f9b2707ec7d 105 memberNames[j] = propertyName;
AzureIoTClient 0:1f9b2707ec7d 106
AzureIoTClient 0:1f9b2707ec7d 107 /* Codes_SRS_COMMAND_DECODER_01_014: [CommandDecoder shall use the MultiTree APIs to extract a specific element from the command JSON.] */
AzureIoTClient 0:1f9b2707ec7d 108 if (MultiTree_GetChildByName(node, memberNames[j], &memberNode) != MULTITREE_OK)
AzureIoTClient 0:1f9b2707ec7d 109 {
AzureIoTClient 0:1f9b2707ec7d 110 /* Codes_SRS_COMMAND_DECODER_99_028:[ If decoding the argument fails then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 23:078ea26cffcd 111 result = __FAILURE__;
AzureIoTClient 11:b1327861f5e0 112 LogError("Getting child %s failed", propertyName);
AzureIoTClient 0:1f9b2707ec7d 113 break;
AzureIoTClient 0:1f9b2707ec7d 114 }
AzureIoTClient 0:1f9b2707ec7d 115 /* Codes_SRS_COMMAND_DECODER_99_032:[ Nesting shall be supported for complex type.] */
AzureIoTClient 0:1f9b2707ec7d 116 else if ((result = DecodeValueFromNode(schemaHandle, &memberValues[j], memberNode, propertyType)) != 0)
AzureIoTClient 0:1f9b2707ec7d 117 {
AzureIoTClient 0:1f9b2707ec7d 118 break;
AzureIoTClient 0:1f9b2707ec7d 119 }
AzureIoTClient 0:1f9b2707ec7d 120 }
AzureIoTClient 0:1f9b2707ec7d 121 }
AzureIoTClient 0:1f9b2707ec7d 122
AzureIoTClient 0:1f9b2707ec7d 123 if (j == propertyCount)
AzureIoTClient 0:1f9b2707ec7d 124 {
AzureIoTClient 0:1f9b2707ec7d 125 /* Codes_SRS_COMMAND_DECODER_99_031:[ The complex type value that aggregates the children shall be built by using the Create_AGENT_DATA_TYPE_from_Members.] */
AzureIoTClient 0:1f9b2707ec7d 126 if (Create_AGENT_DATA_TYPE_from_Members(agentDataType, edmTypeName, propertyCount, (const char* const*)memberNames, memberValues) != AGENT_DATA_TYPES_OK)
AzureIoTClient 0:1f9b2707ec7d 127 {
AzureIoTClient 0:1f9b2707ec7d 128 /* Codes_SRS_COMMAND_DECODER_99_028:[ If decoding the argument fails then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 23:078ea26cffcd 129 result = __FAILURE__;
AzureIoTClient 11:b1327861f5e0 130 LogError("Creating the agent data type from members failed.");
AzureIoTClient 0:1f9b2707ec7d 131 }
AzureIoTClient 0:1f9b2707ec7d 132 else
AzureIoTClient 0:1f9b2707ec7d 133 {
AzureIoTClient 0:1f9b2707ec7d 134 result = 0;
AzureIoTClient 0:1f9b2707ec7d 135 }
AzureIoTClient 0:1f9b2707ec7d 136 }
AzureIoTClient 0:1f9b2707ec7d 137
AzureIoTClient 0:1f9b2707ec7d 138 for (k = 0; k < j; k++)
AzureIoTClient 0:1f9b2707ec7d 139 {
AzureIoTClient 0:1f9b2707ec7d 140 Destroy_AGENT_DATA_TYPE(&memberValues[k]);
AzureIoTClient 0:1f9b2707ec7d 141 }
AzureIoTClient 0:1f9b2707ec7d 142
AzureIoTClient 0:1f9b2707ec7d 143 free((void*)memberNames);
AzureIoTClient 0:1f9b2707ec7d 144 }
AzureIoTClient 0:1f9b2707ec7d 145
AzureIoTClient 0:1f9b2707ec7d 146 free(memberValues);
AzureIoTClient 0:1f9b2707ec7d 147 }
AzureIoTClient 0:1f9b2707ec7d 148 }
AzureIoTClient 0:1f9b2707ec7d 149 }
AzureIoTClient 0:1f9b2707ec7d 150 }
AzureIoTClient 0:1f9b2707ec7d 151 else
AzureIoTClient 0:1f9b2707ec7d 152 {
AzureIoTClient 0:1f9b2707ec7d 153 /* Codes_SRS_COMMAND_DECODER_01_014: [CommandDecoder shall use the MultiTree APIs to extract a specific element from the command JSON.] */
AzureIoTClient 0:1f9b2707ec7d 154 if (MultiTree_GetValue(node, (const void **)&argStringValue) != MULTITREE_OK)
AzureIoTClient 0:1f9b2707ec7d 155 {
AzureIoTClient 0:1f9b2707ec7d 156 /* Codes_SRS_COMMAND_DECODER_99_012:[ If any argument is missing in the command text then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 23:078ea26cffcd 157 result = __FAILURE__;
AzureIoTClient 11:b1327861f5e0 158 LogError("Getting the string from the multitree failed.");
AzureIoTClient 0:1f9b2707ec7d 159 }
AzureIoTClient 0:1f9b2707ec7d 160 /* Codes_SRS_COMMAND_DECODER_99_027:[ The value for an argument of primitive type shall be decoded by using the CreateAgentDataType_From_String API.] */
AzureIoTClient 0:1f9b2707ec7d 161 else if (CreateAgentDataType_From_String(argStringValue, primitiveType, agentDataType) != AGENT_DATA_TYPES_OK)
AzureIoTClient 0:1f9b2707ec7d 162 {
AzureIoTClient 0:1f9b2707ec7d 163 /* Codes_SRS_COMMAND_DECODER_99_028:[ If decoding the argument fails then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 23:078ea26cffcd 164 result = __FAILURE__;
AzureIoTClient 11:b1327861f5e0 165 LogError("Failed parsing node %s.", argStringValue);
AzureIoTClient 0:1f9b2707ec7d 166 }
AzureIoTClient 0:1f9b2707ec7d 167 }
AzureIoTClient 0:1f9b2707ec7d 168
AzureIoTClient 0:1f9b2707ec7d 169 return result;
AzureIoTClient 0:1f9b2707ec7d 170 }
AzureIoTClient 0:1f9b2707ec7d 171
AzureIoTClient 17:fa1bba4c6053 172 static EXECUTE_COMMAND_RESULT DecodeAndExecuteModelAction(COMMAND_DECODER_HANDLE_DATA* commandDecoderInstance, SCHEMA_HANDLE schemaHandle, SCHEMA_MODEL_TYPE_HANDLE modelHandle, const char* relativeActionPath, const char* actionName, MULTITREE_HANDLE commandNode)
AzureIoTClient 0:1f9b2707ec7d 173 {
AzureIoTClient 0:1f9b2707ec7d 174 EXECUTE_COMMAND_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 175 char tempStr[128];
AzureIoTClient 0:1f9b2707ec7d 176 size_t strLength = strlen(actionName);
AzureIoTClient 0:1f9b2707ec7d 177
AzureIoTClient 0:1f9b2707ec7d 178 if (strLength <= 1)
AzureIoTClient 0:1f9b2707ec7d 179 {
AzureIoTClient 0:1f9b2707ec7d 180 /* Codes_SRS_COMMAND_DECODER_99_021:[ If the parsing of the command fails for any other reason the command shall not be dispatched.] */
AzureIoTClient 11:b1327861f5e0 181 LogError("Invalid action name");
AzureIoTClient 0:1f9b2707ec7d 182 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 183 }
AzureIoTClient 0:1f9b2707ec7d 184 else
AzureIoTClient 0:1f9b2707ec7d 185 {
AzureIoTClient 0:1f9b2707ec7d 186 /* Codes_SRS_COMMAND_DECODER_99_006:[ The action name shall be decoded from the element "Name" of the command JSON.] */
AzureIoTClient 0:1f9b2707ec7d 187 SCHEMA_ACTION_HANDLE modelActionHandle;
AzureIoTClient 0:1f9b2707ec7d 188 size_t argCount;
AzureIoTClient 0:1f9b2707ec7d 189 MULTITREE_HANDLE parametersTreeNode;
AzureIoTClient 0:1f9b2707ec7d 190
AzureIoTClient 0:1f9b2707ec7d 191 #ifdef _MSC_VER
AzureIoTClient 0:1f9b2707ec7d 192 #pragma warning(suppress: 6324) /* We intentionally use here strncpy */
AzureIoTClient 0:1f9b2707ec7d 193 #endif
AzureIoTClient 0:1f9b2707ec7d 194 if (strncpy(tempStr, actionName, strLength - 1) == NULL)
AzureIoTClient 0:1f9b2707ec7d 195 {
AzureIoTClient 0:1f9b2707ec7d 196 /* Codes_SRS_COMMAND_DECODER_99_021:[ If the parsing of the command fails for any other reason the command shall not be dispatched.] */
AzureIoTClient 11:b1327861f5e0 197 LogError("Invalid action name.");
AzureIoTClient 0:1f9b2707ec7d 198 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 199 }
AzureIoTClient 0:1f9b2707ec7d 200 /* Codes_SRS_COMMAND_DECODER_01_014: [CommandDecoder shall use the MultiTree APIs to extract a specific element from the command JSON.] */
AzureIoTClient 0:1f9b2707ec7d 201 else if (MultiTree_GetChildByName(commandNode, "Parameters", &parametersTreeNode) != MULTITREE_OK)
AzureIoTClient 0:1f9b2707ec7d 202 {
AzureIoTClient 0:1f9b2707ec7d 203 /* Codes_SRS_COMMAND_DECODER_01_015: [If any MultiTree API call fails then the processing shall stop and the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 11:b1327861f5e0 204 LogError("Error getting Parameters node.");
AzureIoTClient 0:1f9b2707ec7d 205 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 206 }
AzureIoTClient 0:1f9b2707ec7d 207 else
AzureIoTClient 0:1f9b2707ec7d 208 {
AzureIoTClient 0:1f9b2707ec7d 209 tempStr[strLength - 1] = 0;
AzureIoTClient 0:1f9b2707ec7d 210
AzureIoTClient 0:1f9b2707ec7d 211 /* Codes_SRS_COMMAND_DECODER_99_009:[ CommandDecoder shall call Schema_GetModelActionByName to obtain the information about a specific action.] */
AzureIoTClient 0:1f9b2707ec7d 212 if (((modelActionHandle = Schema_GetModelActionByName(modelHandle, tempStr)) == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 213 (Schema_GetModelActionArgumentCount(modelActionHandle, &argCount) != SCHEMA_OK))
AzureIoTClient 0:1f9b2707ec7d 214 {
AzureIoTClient 0:1f9b2707ec7d 215 /* Codes_SRS_COMMAND_DECODER_99_010:[ If any Schema API fails then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.]*/
AzureIoTClient 11:b1327861f5e0 216 LogError("Failed reading action %s from the schema", tempStr);
AzureIoTClient 0:1f9b2707ec7d 217 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 218 }
AzureIoTClient 0:1f9b2707ec7d 219 else
AzureIoTClient 0:1f9b2707ec7d 220 {
AzureIoTClient 0:1f9b2707ec7d 221 AGENT_DATA_TYPE* arguments = NULL;
AzureIoTClient 0:1f9b2707ec7d 222
AzureIoTClient 0:1f9b2707ec7d 223 if (argCount > 0)
AzureIoTClient 0:1f9b2707ec7d 224 {
AzureIoTClient 0:1f9b2707ec7d 225 arguments = (AGENT_DATA_TYPE*)malloc(sizeof(AGENT_DATA_TYPE)* argCount);
AzureIoTClient 0:1f9b2707ec7d 226 }
AzureIoTClient 0:1f9b2707ec7d 227
AzureIoTClient 0:1f9b2707ec7d 228 if ((argCount > 0) &&
AzureIoTClient 0:1f9b2707ec7d 229 (arguments == NULL))
AzureIoTClient 0:1f9b2707ec7d 230 {
AzureIoTClient 0:1f9b2707ec7d 231 /* Codes_SRS_COMMAND_DECODER_99_021:[ If the parsing of the command fails for any other reason the command shall not be dispatched.] */
AzureIoTClient 11:b1327861f5e0 232 LogError("Failed allocating arguments array");
AzureIoTClient 0:1f9b2707ec7d 233 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 234 }
AzureIoTClient 0:1f9b2707ec7d 235 else
AzureIoTClient 0:1f9b2707ec7d 236 {
AzureIoTClient 0:1f9b2707ec7d 237 size_t i;
AzureIoTClient 0:1f9b2707ec7d 238 size_t j;
AzureIoTClient 14:0c00e67a4ab1 239 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 240
AzureIoTClient 0:1f9b2707ec7d 241 /* Codes_SRS_COMMAND_DECODER_99_011:[ CommandDecoder shall attempt to extract from the command text the value for each action argument.] */
AzureIoTClient 0:1f9b2707ec7d 242 for (i = 0; i < argCount; i++)
AzureIoTClient 0:1f9b2707ec7d 243 {
AzureIoTClient 0:1f9b2707ec7d 244 SCHEMA_ACTION_ARGUMENT_HANDLE actionArgumentHandle;
AzureIoTClient 0:1f9b2707ec7d 245 MULTITREE_HANDLE argumentNode;
AzureIoTClient 0:1f9b2707ec7d 246 const char* argName;
AzureIoTClient 0:1f9b2707ec7d 247 const char* argType;
AzureIoTClient 0:1f9b2707ec7d 248
AzureIoTClient 0:1f9b2707ec7d 249 if (((actionArgumentHandle = Schema_GetModelActionArgumentByIndex(modelActionHandle, i)) == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 250 ((argName = Schema_GetActionArgumentName(actionArgumentHandle)) == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 251 ((argType = Schema_GetActionArgumentType(actionArgumentHandle)) == NULL))
AzureIoTClient 0:1f9b2707ec7d 252 {
AzureIoTClient 11:b1327861f5e0 253 LogError("Failed getting the argument information from the schema");
AzureIoTClient 0:1f9b2707ec7d 254 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 255 break;
AzureIoTClient 0:1f9b2707ec7d 256 }
AzureIoTClient 0:1f9b2707ec7d 257 /* Codes_SRS_COMMAND_DECODER_01_014: [CommandDecoder shall use the MultiTree APIs to extract a specific element from the command JSON.] */
AzureIoTClient 0:1f9b2707ec7d 258 /* Codes_SRS_COMMAND_DECODER_01_008: [Each argument shall be looked up as a field, member of the "Parameters" node.] */
AzureIoTClient 0:1f9b2707ec7d 259 else if (MultiTree_GetChildByName(parametersTreeNode, argName, &argumentNode) != MULTITREE_OK)
AzureIoTClient 0:1f9b2707ec7d 260 {
AzureIoTClient 0:1f9b2707ec7d 261 /* Codes_SRS_COMMAND_DECODER_99_012:[ If any argument is missing in the command text then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 11:b1327861f5e0 262 LogError("Missing argument %s", argName);
AzureIoTClient 0:1f9b2707ec7d 263 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 264 break;
AzureIoTClient 0:1f9b2707ec7d 265 }
AzureIoTClient 0:1f9b2707ec7d 266 else if (DecodeValueFromNode(schemaHandle, &arguments[i], argumentNode, argType) != 0)
AzureIoTClient 0:1f9b2707ec7d 267 {
AzureIoTClient 0:1f9b2707ec7d 268 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 269 break;
AzureIoTClient 0:1f9b2707ec7d 270 }
AzureIoTClient 0:1f9b2707ec7d 271 }
AzureIoTClient 0:1f9b2707ec7d 272
AzureIoTClient 0:1f9b2707ec7d 273 if (i == argCount)
AzureIoTClient 0:1f9b2707ec7d 274 {
AzureIoTClient 0:1f9b2707ec7d 275 /* Codes_SRS_COMMAND_DECODER_99_005:[ If an Invoke Action is decoded successfully then the callback actionCallback shall be called, passing to it the callback action context, decoded name and arguments.] */
AzureIoTClient 0:1f9b2707ec7d 276 result = commandDecoderInstance->ActionCallback(commandDecoderInstance->ActionCallbackContext, relativeActionPath, tempStr, argCount, arguments);
AzureIoTClient 0:1f9b2707ec7d 277 }
AzureIoTClient 0:1f9b2707ec7d 278
AzureIoTClient 0:1f9b2707ec7d 279 for (j = 0; j < i; j++)
AzureIoTClient 0:1f9b2707ec7d 280 {
AzureIoTClient 0:1f9b2707ec7d 281 Destroy_AGENT_DATA_TYPE(&arguments[j]);
AzureIoTClient 0:1f9b2707ec7d 282 }
AzureIoTClient 0:1f9b2707ec7d 283
AzureIoTClient 0:1f9b2707ec7d 284 if (arguments != NULL)
AzureIoTClient 0:1f9b2707ec7d 285 {
AzureIoTClient 0:1f9b2707ec7d 286 free(arguments);
AzureIoTClient 0:1f9b2707ec7d 287 }
AzureIoTClient 0:1f9b2707ec7d 288 }
AzureIoTClient 0:1f9b2707ec7d 289 }
AzureIoTClient 0:1f9b2707ec7d 290 }
AzureIoTClient 0:1f9b2707ec7d 291 }
AzureIoTClient 0:1f9b2707ec7d 292 return result;
AzureIoTClient 0:1f9b2707ec7d 293 }
AzureIoTClient 0:1f9b2707ec7d 294
Azure.IoT.Build 18:58b667752399 295 static METHODRETURN_HANDLE DecodeAndExecuteModelMethod(COMMAND_DECODER_HANDLE_DATA* commandDecoderInstance, SCHEMA_HANDLE schemaHandle, SCHEMA_MODEL_TYPE_HANDLE modelHandle, const char* relativeMethodPath, const char* methodName, MULTITREE_HANDLE methodTree)
Azure.IoT.Build 18:58b667752399 296 {
Azure.IoT.Build 18:58b667752399 297 METHODRETURN_HANDLE result;
Azure.IoT.Build 18:58b667752399 298 size_t strLength = strlen(methodName);
Azure.IoT.Build 18:58b667752399 299
Azure.IoT.Build 18:58b667752399 300 if (strLength == 0)
Azure.IoT.Build 18:58b667752399 301 {
Azure.IoT.Build 18:58b667752399 302 /*Codes_SRS_COMMAND_DECODER_02_023: [ If any of the previous operations fail, then CommandDecoder_ExecuteMethod shall return NULL. ]*/
Azure.IoT.Build 18:58b667752399 303 LogError("Invalid method name");
Azure.IoT.Build 18:58b667752399 304 result = NULL;
Azure.IoT.Build 18:58b667752399 305 }
Azure.IoT.Build 18:58b667752399 306 else
Azure.IoT.Build 18:58b667752399 307 {
Azure.IoT.Build 18:58b667752399 308 SCHEMA_METHOD_HANDLE modelMethodHandle;
Azure.IoT.Build 18:58b667752399 309 size_t argCount;
Azure.IoT.Build 18:58b667752399 310
Azure.IoT.Build 18:58b667752399 311 #ifdef _MSC_VER
Azure.IoT.Build 18:58b667752399 312 #pragma warning(suppress: 6324) /* We intentionally use here strncpy */
Azure.IoT.Build 18:58b667752399 313 #endif
Azure.IoT.Build 18:58b667752399 314
Azure.IoT.Build 18:58b667752399 315 /*Codes_SRS_COMMAND_DECODER_02_020: [ CommandDecoder_ExecuteMethod shall verify that the model has a method called methodName. ]*/
Azure.IoT.Build 18:58b667752399 316 if (((modelMethodHandle = Schema_GetModelMethodByName(modelHandle, methodName)) == NULL) ||
Azure.IoT.Build 18:58b667752399 317 (Schema_GetModelMethodArgumentCount(modelMethodHandle, &argCount) != SCHEMA_OK))
Azure.IoT.Build 18:58b667752399 318 {
Azure.IoT.Build 18:58b667752399 319 /*Codes_SRS_COMMAND_DECODER_02_023: [ If any of the previous operations fail, then CommandDecoder_ExecuteMethod shall return NULL. ]*/
Azure.IoT.Build 18:58b667752399 320 LogError("Failed reading method %s from the schema", methodName);
Azure.IoT.Build 18:58b667752399 321 result = NULL;
Azure.IoT.Build 18:58b667752399 322 }
Azure.IoT.Build 18:58b667752399 323 else
Azure.IoT.Build 18:58b667752399 324 {
Azure.IoT.Build 18:58b667752399 325 /*Codes_SRS_COMMAND_DECODER_02_021: [ For every argument of methodName, CommandDecoder_ExecuteMethod shall build an AGENT_DATA_TYPE from the node with the same name from the MULTITREE_HANDLE. ]*/
Azure.IoT.Build 18:58b667752399 326
Azure.IoT.Build 18:58b667752399 327 if (argCount == 0)
Azure.IoT.Build 18:58b667752399 328 {
Azure.IoT.Build 18:58b667752399 329 /*no need for any parameters*/
Azure.IoT.Build 18:58b667752399 330 result = commandDecoderInstance->methodCallback(commandDecoderInstance->methodCallbackContext, relativeMethodPath, methodName, 0, NULL);
Azure.IoT.Build 18:58b667752399 331 }
Azure.IoT.Build 18:58b667752399 332 else
Azure.IoT.Build 18:58b667752399 333 {
Azure.IoT.Build 18:58b667752399 334 AGENT_DATA_TYPE* arguments;
Azure.IoT.Build 18:58b667752399 335 arguments = (AGENT_DATA_TYPE*)malloc(sizeof(AGENT_DATA_TYPE)* argCount);
Azure.IoT.Build 18:58b667752399 336 if (arguments == NULL)
Azure.IoT.Build 18:58b667752399 337 {
Azure.IoT.Build 18:58b667752399 338 LogError("Failed allocating arguments array");
Azure.IoT.Build 18:58b667752399 339 result = NULL;
Azure.IoT.Build 18:58b667752399 340 }
Azure.IoT.Build 18:58b667752399 341 else
Azure.IoT.Build 18:58b667752399 342 {
Azure.IoT.Build 18:58b667752399 343 size_t i;
Azure.IoT.Build 18:58b667752399 344 size_t j;
Azure.IoT.Build 18:58b667752399 345 result = NULL;
Azure.IoT.Build 18:58b667752399 346
Azure.IoT.Build 18:58b667752399 347 for (i = 0; i < argCount; i++)
Azure.IoT.Build 18:58b667752399 348 {
Azure.IoT.Build 18:58b667752399 349 SCHEMA_METHOD_ARGUMENT_HANDLE methodArgumentHandle;
Azure.IoT.Build 18:58b667752399 350 MULTITREE_HANDLE argumentNode;
Azure.IoT.Build 18:58b667752399 351 const char* argName;
Azure.IoT.Build 18:58b667752399 352 const char* argType;
Azure.IoT.Build 18:58b667752399 353
Azure.IoT.Build 18:58b667752399 354 if (((methodArgumentHandle = Schema_GetModelMethodArgumentByIndex(modelMethodHandle, i)) == NULL) ||
Azure.IoT.Build 18:58b667752399 355 ((argName = Schema_GetMethodArgumentName(methodArgumentHandle)) == NULL) ||
Azure.IoT.Build 18:58b667752399 356 ((argType = Schema_GetMethodArgumentType(methodArgumentHandle)) == NULL))
Azure.IoT.Build 18:58b667752399 357 {
Azure.IoT.Build 18:58b667752399 358 /*Codes_SRS_COMMAND_DECODER_02_023: [ If any of the previous operations fail, then CommandDecoder_ExecuteMethod shall return NULL. ]*/
Azure.IoT.Build 18:58b667752399 359 LogError("Failed getting the argument information from the schema");
Azure.IoT.Build 18:58b667752399 360 result = NULL;
Azure.IoT.Build 18:58b667752399 361 break;
Azure.IoT.Build 18:58b667752399 362 }
Azure.IoT.Build 18:58b667752399 363 else if (MultiTree_GetChildByName(methodTree, argName, &argumentNode) != MULTITREE_OK)
Azure.IoT.Build 18:58b667752399 364 {
Azure.IoT.Build 18:58b667752399 365 /*Codes_SRS_COMMAND_DECODER_02_023: [ If any of the previous operations fail, then CommandDecoder_ExecuteMethod shall return NULL. ]*/
Azure.IoT.Build 18:58b667752399 366 LogError("Missing argument %s", argName);
Azure.IoT.Build 18:58b667752399 367 result = NULL;
Azure.IoT.Build 18:58b667752399 368 break;
Azure.IoT.Build 18:58b667752399 369 }
Azure.IoT.Build 18:58b667752399 370 else if (DecodeValueFromNode(schemaHandle, &arguments[i], argumentNode, argType) != 0)
Azure.IoT.Build 18:58b667752399 371 {
Azure.IoT.Build 18:58b667752399 372 /*Codes_SRS_COMMAND_DECODER_02_023: [ If any of the previous operations fail, then CommandDecoder_ExecuteMethod shall return NULL. ]*/
Azure.IoT.Build 18:58b667752399 373 LogError("failure in DecodeValueFromNode");
Azure.IoT.Build 18:58b667752399 374 result = NULL;
Azure.IoT.Build 18:58b667752399 375 break;
Azure.IoT.Build 18:58b667752399 376 }
Azure.IoT.Build 18:58b667752399 377 }
Azure.IoT.Build 18:58b667752399 378
Azure.IoT.Build 18:58b667752399 379 if (i == argCount)
Azure.IoT.Build 18:58b667752399 380 {
Azure.IoT.Build 18:58b667752399 381 /*Codes_SRS_COMMAND_DECODER_02_022: [ CommandDecoder_ExecuteMethod shall call methodCallback passing the context, the methodName, number of arguments and the AGENT_DATA_TYPE. ]*/
Azure.IoT.Build 18:58b667752399 382 /*Codes_SRS_COMMAND_DECODER_02_024: [ Otherwise, CommandDecoder_ExecuteMethod shall return what methodCallback returns. ]*/
Azure.IoT.Build 18:58b667752399 383 result = commandDecoderInstance->methodCallback(commandDecoderInstance->methodCallbackContext, relativeMethodPath, methodName, argCount, arguments);
Azure.IoT.Build 18:58b667752399 384 }
Azure.IoT.Build 18:58b667752399 385
Azure.IoT.Build 18:58b667752399 386 for (j = 0; j < i; j++)
Azure.IoT.Build 18:58b667752399 387 {
Azure.IoT.Build 18:58b667752399 388 Destroy_AGENT_DATA_TYPE(&arguments[j]);
Azure.IoT.Build 18:58b667752399 389 }
Azure.IoT.Build 18:58b667752399 390
Azure.IoT.Build 18:58b667752399 391 free(arguments);
Azure.IoT.Build 18:58b667752399 392 }
Azure.IoT.Build 18:58b667752399 393
Azure.IoT.Build 18:58b667752399 394 }
Azure.IoT.Build 18:58b667752399 395 }
Azure.IoT.Build 18:58b667752399 396
Azure.IoT.Build 18:58b667752399 397 }
Azure.IoT.Build 18:58b667752399 398 return result;
Azure.IoT.Build 18:58b667752399 399 }
Azure.IoT.Build 18:58b667752399 400
Azure.IoT.Build 18:58b667752399 401
AzureIoTClient 17:fa1bba4c6053 402 static EXECUTE_COMMAND_RESULT ScanActionPathAndExecuteAction(COMMAND_DECODER_HANDLE_DATA* commandDecoderInstance, SCHEMA_HANDLE schemaHandle, const char* actionPath, MULTITREE_HANDLE commandNode)
AzureIoTClient 0:1f9b2707ec7d 403 {
AzureIoTClient 0:1f9b2707ec7d 404 EXECUTE_COMMAND_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 405 char* relativeActionPath;
AzureIoTClient 0:1f9b2707ec7d 406 const char* actionName = actionPath;
AzureIoTClient 0:1f9b2707ec7d 407 SCHEMA_MODEL_TYPE_HANDLE modelHandle = commandDecoderInstance->ModelHandle;
AzureIoTClient 0:1f9b2707ec7d 408
AzureIoTClient 0:1f9b2707ec7d 409 /* Codes_SRS_COMMAND_DECODER_99_035:[ CommandDecoder_ExecuteCommand shall support paths to actions that are in child models (i.e. ChildModel/SomeAction.] */
AzureIoTClient 0:1f9b2707ec7d 410 do
AzureIoTClient 0:1f9b2707ec7d 411 {
AzureIoTClient 0:1f9b2707ec7d 412 /* find the slash */
AzureIoTClient 0:1f9b2707ec7d 413 const char* slashPos = strchr(actionName, '/');
AzureIoTClient 0:1f9b2707ec7d 414 if (slashPos == NULL)
AzureIoTClient 0:1f9b2707ec7d 415 {
AzureIoTClient 0:1f9b2707ec7d 416 size_t relativeActionPathLength;
AzureIoTClient 0:1f9b2707ec7d 417
AzureIoTClient 0:1f9b2707ec7d 418 /* Codes_SRS_COMMAND_DECODER_99_037:[ The relative path passed to the actionCallback shall be in the format "childModel1/childModel2/.../childModelN".] */
AzureIoTClient 0:1f9b2707ec7d 419 if (actionName == actionPath)
AzureIoTClient 0:1f9b2707ec7d 420 {
AzureIoTClient 0:1f9b2707ec7d 421 relativeActionPathLength = 0;
AzureIoTClient 0:1f9b2707ec7d 422 }
AzureIoTClient 0:1f9b2707ec7d 423 else
AzureIoTClient 0:1f9b2707ec7d 424 {
AzureIoTClient 0:1f9b2707ec7d 425 relativeActionPathLength = actionName - actionPath - 1;
AzureIoTClient 0:1f9b2707ec7d 426 }
AzureIoTClient 0:1f9b2707ec7d 427
AzureIoTClient 0:1f9b2707ec7d 428 relativeActionPath = (char*)malloc(relativeActionPathLength + 1);
AzureIoTClient 0:1f9b2707ec7d 429 if (relativeActionPath == NULL)
AzureIoTClient 0:1f9b2707ec7d 430 {
AzureIoTClient 0:1f9b2707ec7d 431 /* Codes_SRS_COMMAND_DECODER_99_021:[ If the parsing of the command fails for any other reason the command shall not be dispatched.] */
AzureIoTClient 11:b1327861f5e0 432 LogError("Failed allocating relative action path");
AzureIoTClient 0:1f9b2707ec7d 433 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 434 }
AzureIoTClient 0:1f9b2707ec7d 435 else
AzureIoTClient 0:1f9b2707ec7d 436 {
AzureIoTClient 0:1f9b2707ec7d 437 strncpy(relativeActionPath, actionPath, relativeActionPathLength);
AzureIoTClient 0:1f9b2707ec7d 438 relativeActionPath[relativeActionPathLength] = 0;
AzureIoTClient 0:1f9b2707ec7d 439
AzureIoTClient 0:1f9b2707ec7d 440 /* no slash found, this must be an action */
AzureIoTClient 0:1f9b2707ec7d 441 result = DecodeAndExecuteModelAction(commandDecoderInstance, schemaHandle, modelHandle, relativeActionPath, actionName, commandNode);
AzureIoTClient 0:1f9b2707ec7d 442
AzureIoTClient 0:1f9b2707ec7d 443 free(relativeActionPath);
AzureIoTClient 0:1f9b2707ec7d 444 actionName = NULL;
AzureIoTClient 0:1f9b2707ec7d 445 }
AzureIoTClient 0:1f9b2707ec7d 446 break;
AzureIoTClient 0:1f9b2707ec7d 447 }
AzureIoTClient 0:1f9b2707ec7d 448 else
AzureIoTClient 0:1f9b2707ec7d 449 {
AzureIoTClient 0:1f9b2707ec7d 450 /* found a slash, get the child model name */
AzureIoTClient 0:1f9b2707ec7d 451 size_t modelLength = slashPos - actionName;
AzureIoTClient 0:1f9b2707ec7d 452 char* childModelName = (char*)malloc(modelLength + 1);
AzureIoTClient 0:1f9b2707ec7d 453 if (childModelName == NULL)
AzureIoTClient 0:1f9b2707ec7d 454 {
AzureIoTClient 0:1f9b2707ec7d 455 /* Codes_SRS_COMMAND_DECODER_99_021:[ If the parsing of the command fails for any other reason the command shall not be dispatched.] */
AzureIoTClient 11:b1327861f5e0 456 LogError("Failed allocating child model name");
AzureIoTClient 0:1f9b2707ec7d 457 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 458 break;
AzureIoTClient 0:1f9b2707ec7d 459 }
AzureIoTClient 0:1f9b2707ec7d 460 else
AzureIoTClient 0:1f9b2707ec7d 461 {
AzureIoTClient 0:1f9b2707ec7d 462 strncpy(childModelName, actionName, modelLength);
AzureIoTClient 0:1f9b2707ec7d 463 childModelName[modelLength] = 0;
AzureIoTClient 0:1f9b2707ec7d 464
AzureIoTClient 0:1f9b2707ec7d 465 /* find the model */
AzureIoTClient 0:1f9b2707ec7d 466 modelHandle = Schema_GetModelModelByName(modelHandle, childModelName);
AzureIoTClient 0:1f9b2707ec7d 467 if (modelHandle == NULL)
AzureIoTClient 0:1f9b2707ec7d 468 {
AzureIoTClient 0:1f9b2707ec7d 469 /* Codes_SRS_COMMAND_DECODER_99_036:[ If a child model cannot be found by using Schema APIs then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 11:b1327861f5e0 470 LogError("Getting the model %s failed", childModelName);
AzureIoTClient 0:1f9b2707ec7d 471 free(childModelName);
AzureIoTClient 0:1f9b2707ec7d 472 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 473 break;
AzureIoTClient 0:1f9b2707ec7d 474 }
AzureIoTClient 0:1f9b2707ec7d 475 else
AzureIoTClient 0:1f9b2707ec7d 476 {
AzureIoTClient 0:1f9b2707ec7d 477 free(childModelName);
AzureIoTClient 0:1f9b2707ec7d 478 actionName = slashPos + 1;
AzureIoTClient 0:1f9b2707ec7d 479 result = EXECUTE_COMMAND_ERROR; /*this only exists to quench a compiler warning about returning an uninitialized variable, which is not possible by design*/
AzureIoTClient 0:1f9b2707ec7d 480 }
AzureIoTClient 0:1f9b2707ec7d 481 }
AzureIoTClient 0:1f9b2707ec7d 482 }
AzureIoTClient 0:1f9b2707ec7d 483 } while (actionName != NULL);
AzureIoTClient 0:1f9b2707ec7d 484 return result;
AzureIoTClient 0:1f9b2707ec7d 485 }
AzureIoTClient 0:1f9b2707ec7d 486
Azure.IoT.Build 18:58b667752399 487 static METHODRETURN_HANDLE ScanMethodPathAndExecuteMethod(COMMAND_DECODER_HANDLE_DATA* commandDecoderInstance, SCHEMA_HANDLE schemaHandle, const char* fullMethodName, MULTITREE_HANDLE methodTree)
Azure.IoT.Build 18:58b667752399 488 {
Azure.IoT.Build 18:58b667752399 489 METHODRETURN_HANDLE result;
Azure.IoT.Build 18:58b667752399 490 char* relativeMethodPath;
Azure.IoT.Build 18:58b667752399 491 const char* methodName = fullMethodName;
Azure.IoT.Build 18:58b667752399 492 SCHEMA_MODEL_TYPE_HANDLE modelHandle = commandDecoderInstance->ModelHandle;
Azure.IoT.Build 18:58b667752399 493
Azure.IoT.Build 18:58b667752399 494 /*Codes_SRS_COMMAND_DECODER_02_018: [ CommandDecoder_ExecuteMethod shall validate that consecutive segments of the fullMethodName exist in the model. ]*/
Azure.IoT.Build 18:58b667752399 495 /*Codes_SRS_COMMAND_DECODER_02_019: [ CommandDecoder_ExecuteMethod shall locate the final model to which the methodName applies. ]*/
Azure.IoT.Build 18:58b667752399 496 do
Azure.IoT.Build 18:58b667752399 497 {
Azure.IoT.Build 18:58b667752399 498 /* find the slash */
Azure.IoT.Build 18:58b667752399 499 const char* slashPos = strchr(methodName, '/');
Azure.IoT.Build 18:58b667752399 500 if (slashPos == NULL)
Azure.IoT.Build 18:58b667752399 501 {
Azure.IoT.Build 18:58b667752399 502 size_t relativeMethodPathLength;
Azure.IoT.Build 18:58b667752399 503
Azure.IoT.Build 18:58b667752399 504 if (methodName == fullMethodName)
Azure.IoT.Build 18:58b667752399 505 {
Azure.IoT.Build 18:58b667752399 506 relativeMethodPathLength = 0;
Azure.IoT.Build 18:58b667752399 507 }
Azure.IoT.Build 18:58b667752399 508 else
Azure.IoT.Build 18:58b667752399 509 {
Azure.IoT.Build 18:58b667752399 510 relativeMethodPathLength = methodName - fullMethodName - 1;
Azure.IoT.Build 18:58b667752399 511 }
Azure.IoT.Build 18:58b667752399 512
Azure.IoT.Build 18:58b667752399 513 relativeMethodPath = (char*)malloc(relativeMethodPathLength + 1);
Azure.IoT.Build 18:58b667752399 514 if (relativeMethodPath == NULL)
Azure.IoT.Build 18:58b667752399 515 {
Azure.IoT.Build 18:58b667752399 516 /*Codes_SRS_COMMAND_DECODER_02_023: [ If any of the previous operations fail, then CommandDecoder_ExecuteMethod shall return NULL. ]*/
Azure.IoT.Build 18:58b667752399 517 LogError("Failed allocating relative method path");
Azure.IoT.Build 18:58b667752399 518 result = NULL;
Azure.IoT.Build 18:58b667752399 519 }
Azure.IoT.Build 18:58b667752399 520 else
Azure.IoT.Build 18:58b667752399 521 {
Azure.IoT.Build 18:58b667752399 522 strncpy(relativeMethodPath, fullMethodName, relativeMethodPathLength);
Azure.IoT.Build 18:58b667752399 523 relativeMethodPath[relativeMethodPathLength] = 0;
Azure.IoT.Build 18:58b667752399 524
Azure.IoT.Build 18:58b667752399 525 /* no slash found, this must be an method */
Azure.IoT.Build 18:58b667752399 526 result = DecodeAndExecuteModelMethod(commandDecoderInstance, schemaHandle, modelHandle, relativeMethodPath, methodName, methodTree);
Azure.IoT.Build 18:58b667752399 527
Azure.IoT.Build 18:58b667752399 528 free(relativeMethodPath);
Azure.IoT.Build 18:58b667752399 529 methodName = NULL;
Azure.IoT.Build 18:58b667752399 530 }
Azure.IoT.Build 18:58b667752399 531 break;
Azure.IoT.Build 18:58b667752399 532 }
Azure.IoT.Build 18:58b667752399 533 else
Azure.IoT.Build 18:58b667752399 534 {
Azure.IoT.Build 18:58b667752399 535 /* found a slash, get the child model name */
Azure.IoT.Build 18:58b667752399 536 size_t modelLength = slashPos - methodName;
Azure.IoT.Build 18:58b667752399 537 char* childModelName = (char*)malloc(modelLength + 1);
Azure.IoT.Build 18:58b667752399 538 if (childModelName == NULL)
Azure.IoT.Build 18:58b667752399 539 {
Azure.IoT.Build 18:58b667752399 540 /*Codes_SRS_COMMAND_DECODER_02_023: [ If any of the previous operations fail, then CommandDecoder_ExecuteMethod shall return NULL. ]*/
Azure.IoT.Build 18:58b667752399 541 LogError("Failed allocating child model name");
Azure.IoT.Build 18:58b667752399 542 result = NULL;
Azure.IoT.Build 18:58b667752399 543 break;
Azure.IoT.Build 18:58b667752399 544 }
Azure.IoT.Build 18:58b667752399 545 else
Azure.IoT.Build 18:58b667752399 546 {
Azure.IoT.Build 18:58b667752399 547 strncpy(childModelName, methodName, modelLength);
Azure.IoT.Build 18:58b667752399 548 childModelName[modelLength] = 0;
Azure.IoT.Build 18:58b667752399 549
Azure.IoT.Build 18:58b667752399 550 /* find the model */
Azure.IoT.Build 18:58b667752399 551 modelHandle = Schema_GetModelModelByName(modelHandle, childModelName);
Azure.IoT.Build 18:58b667752399 552 if (modelHandle == NULL)
Azure.IoT.Build 18:58b667752399 553 {
Azure.IoT.Build 18:58b667752399 554 /*Codes_SRS_COMMAND_DECODER_02_023: [ If any of the previous operations fail, then CommandDecoder_ExecuteMethod shall return NULL. ]*/
Azure.IoT.Build 18:58b667752399 555 LogError("Getting the model %s failed", childModelName);
Azure.IoT.Build 18:58b667752399 556 free(childModelName);
Azure.IoT.Build 18:58b667752399 557 result = NULL;
Azure.IoT.Build 18:58b667752399 558 break;
Azure.IoT.Build 18:58b667752399 559 }
Azure.IoT.Build 18:58b667752399 560 else
Azure.IoT.Build 18:58b667752399 561 {
Azure.IoT.Build 18:58b667752399 562 free(childModelName);
Azure.IoT.Build 18:58b667752399 563 methodName = slashPos + 1;
Azure.IoT.Build 18:58b667752399 564 result = NULL; /*this only exists to quench a compiler warning about returning an uninitialized variable, which is not possible by design*/
Azure.IoT.Build 18:58b667752399 565 }
Azure.IoT.Build 18:58b667752399 566 }
Azure.IoT.Build 18:58b667752399 567 }
Azure.IoT.Build 18:58b667752399 568 } while (methodName != NULL);
Azure.IoT.Build 18:58b667752399 569 return result;
Azure.IoT.Build 18:58b667752399 570 }
Azure.IoT.Build 18:58b667752399 571
AzureIoTClient 17:fa1bba4c6053 572 static EXECUTE_COMMAND_RESULT DecodeCommand(COMMAND_DECODER_HANDLE_DATA* commandDecoderInstance, MULTITREE_HANDLE commandNode)
AzureIoTClient 0:1f9b2707ec7d 573 {
AzureIoTClient 0:1f9b2707ec7d 574 EXECUTE_COMMAND_RESULT result;
AzureIoTClient 0:1f9b2707ec7d 575 SCHEMA_HANDLE schemaHandle;
AzureIoTClient 0:1f9b2707ec7d 576
AzureIoTClient 0:1f9b2707ec7d 577 /* Codes_SRS_COMMAND_DECODER_99_022:[ CommandDecoder shall use the Schema APIs to obtain the information about the entity set name and namespace] */
AzureIoTClient 0:1f9b2707ec7d 578 if ((schemaHandle = Schema_GetSchemaForModelType(commandDecoderInstance->ModelHandle)) == NULL)
AzureIoTClient 0:1f9b2707ec7d 579 {
AzureIoTClient 0:1f9b2707ec7d 580 /* Codes_SRS_COMMAND_DECODER_99_010:[ If any Schema API fails then the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.]*/
AzureIoTClient 11:b1327861f5e0 581 LogError("Getting schema information failed");
AzureIoTClient 0:1f9b2707ec7d 582 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 583 }
AzureIoTClient 0:1f9b2707ec7d 584 else
AzureIoTClient 0:1f9b2707ec7d 585 {
AzureIoTClient 0:1f9b2707ec7d 586 const char* actionName;
AzureIoTClient 0:1f9b2707ec7d 587 MULTITREE_HANDLE nameTreeNode;
AzureIoTClient 0:1f9b2707ec7d 588
AzureIoTClient 0:1f9b2707ec7d 589 /* Codes_SRS_COMMAND_DECODER_01_014: [CommandDecoder shall use the MultiTree APIs to extract a specific element from the command JSON.] */
AzureIoTClient 0:1f9b2707ec7d 590 /* Codes_SRS_COMMAND_DECODER_99_006:[ The action name shall be decoded from the element "name" of the command JSON.] */
AzureIoTClient 0:1f9b2707ec7d 591 if ((MultiTree_GetChildByName(commandNode, "Name", &nameTreeNode) != MULTITREE_OK) ||
AzureIoTClient 0:1f9b2707ec7d 592 (MultiTree_GetValue(nameTreeNode, (const void **)&actionName) != MULTITREE_OK))
AzureIoTClient 0:1f9b2707ec7d 593 {
AzureIoTClient 0:1f9b2707ec7d 594 /* Codes_SRS_COMMAND_DECODER_01_015: [If any MultiTree API call fails then the processing shall stop and the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 11:b1327861f5e0 595 LogError("Getting action name failed.");
AzureIoTClient 0:1f9b2707ec7d 596 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 597 }
AzureIoTClient 0:1f9b2707ec7d 598 else if (strlen(actionName) < 2)
AzureIoTClient 0:1f9b2707ec7d 599 {
AzureIoTClient 0:1f9b2707ec7d 600 /* Codes_SRS_COMMAND_DECODER_99_021:[ If the parsing of the command fails for any other reason the command shall not be dispatched.] */
AzureIoTClient 11:b1327861f5e0 601 LogError("Invalid action name.");
AzureIoTClient 0:1f9b2707ec7d 602 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 603 }
AzureIoTClient 0:1f9b2707ec7d 604 else
AzureIoTClient 0:1f9b2707ec7d 605 {
AzureIoTClient 0:1f9b2707ec7d 606 actionName++;
AzureIoTClient 0:1f9b2707ec7d 607 result = ScanActionPathAndExecuteAction(commandDecoderInstance, schemaHandle, actionName, commandNode);
AzureIoTClient 0:1f9b2707ec7d 608 }
AzureIoTClient 0:1f9b2707ec7d 609 }
AzureIoTClient 0:1f9b2707ec7d 610 return result;
AzureIoTClient 0:1f9b2707ec7d 611 }
AzureIoTClient 0:1f9b2707ec7d 612
Azure.IoT.Build 18:58b667752399 613 static METHODRETURN_HANDLE DecodeMethod(COMMAND_DECODER_HANDLE_DATA* commandDecoderInstance, const char* fullMethodName, MULTITREE_HANDLE methodTree)
Azure.IoT.Build 18:58b667752399 614 {
Azure.IoT.Build 18:58b667752399 615 METHODRETURN_HANDLE result;
Azure.IoT.Build 18:58b667752399 616 SCHEMA_HANDLE schemaHandle;
Azure.IoT.Build 18:58b667752399 617
Azure.IoT.Build 18:58b667752399 618 /*Codes_SRS_COMMAND_DECODER_02_017: [ CommandDecoder_ExecuteMethod shall get the SCHEMA_HANDLE associated with the modelHandle passed at CommandDecoder_Create. ]*/
Azure.IoT.Build 18:58b667752399 619 if ((schemaHandle = Schema_GetSchemaForModelType(commandDecoderInstance->ModelHandle)) == NULL)
Azure.IoT.Build 18:58b667752399 620 {
Azure.IoT.Build 18:58b667752399 621 LogError("Getting schema information failed");
Azure.IoT.Build 18:58b667752399 622 result = NULL;
Azure.IoT.Build 18:58b667752399 623 }
Azure.IoT.Build 18:58b667752399 624 else
Azure.IoT.Build 18:58b667752399 625 {
Azure.IoT.Build 18:58b667752399 626 result = ScanMethodPathAndExecuteMethod(commandDecoderInstance, schemaHandle, fullMethodName, methodTree);
Azure.IoT.Build 18:58b667752399 627
Azure.IoT.Build 18:58b667752399 628 }
Azure.IoT.Build 18:58b667752399 629 return result;
Azure.IoT.Build 18:58b667752399 630 }
Azure.IoT.Build 18:58b667752399 631
AzureIoTClient 0:1f9b2707ec7d 632 /*Codes_SRS_COMMAND_DECODER_01_009: [Whenever CommandDecoder_ExecuteCommand is the command shall be decoded and further dispatched to the actionCallback passed in CommandDecoder_Create.]*/
AzureIoTClient 0:1f9b2707ec7d 633 EXECUTE_COMMAND_RESULT CommandDecoder_ExecuteCommand(COMMAND_DECODER_HANDLE handle, const char* command)
AzureIoTClient 0:1f9b2707ec7d 634 {
AzureIoTClient 0:1f9b2707ec7d 635 EXECUTE_COMMAND_RESULT result;
AzureIoTClient 17:fa1bba4c6053 636 COMMAND_DECODER_HANDLE_DATA* commandDecoderInstance = (COMMAND_DECODER_HANDLE_DATA*)handle;
AzureIoTClient 0:1f9b2707ec7d 637 /*Codes_SRS_COMMAND_DECODER_01_010: [If either the buffer or the receiveCallbackContext argument is NULL, the processing shall stop and the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.]*/
AzureIoTClient 0:1f9b2707ec7d 638 if (
AzureIoTClient 0:1f9b2707ec7d 639 (command == NULL) ||
AzureIoTClient 0:1f9b2707ec7d 640 (commandDecoderInstance == NULL)
AzureIoTClient 0:1f9b2707ec7d 641 )
AzureIoTClient 0:1f9b2707ec7d 642 {
AzureIoTClient 11:b1327861f5e0 643 LogError("Invalid argument, COMMAND_DECODER_HANDLE handle=%p, const char* command=%p", handle, command);
AzureIoTClient 0:1f9b2707ec7d 644 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 645 }
AzureIoTClient 0:1f9b2707ec7d 646 else
AzureIoTClient 0:1f9b2707ec7d 647 {
AzureIoTClient 0:1f9b2707ec7d 648 size_t size = strlen(command);
AzureIoTClient 0:1f9b2707ec7d 649 char* commandJSON;
AzureIoTClient 0:1f9b2707ec7d 650
AzureIoTClient 0:1f9b2707ec7d 651 /* Codes_SRS_COMMAND_DECODER_01_011: [If the size of the command is 0 then the processing shall stop and the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.]*/
AzureIoTClient 22:422d94bd3c18 652 if (size == 0)
AzureIoTClient 0:1f9b2707ec7d 653 {
AzureIoTClient 11:b1327861f5e0 654 LogError("Failed because command size is zero");
AzureIoTClient 0:1f9b2707ec7d 655 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 656 }
AzureIoTClient 0:1f9b2707ec7d 657 /*Codes_SRS_COMMAND_DECODER_01_013: [If parsing the JSON to a multi tree fails, the processing shall stop and the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.]*/
AzureIoTClient 0:1f9b2707ec7d 658 else if ((commandJSON = (char*)malloc(size + 1)) == NULL)
AzureIoTClient 0:1f9b2707ec7d 659 {
AzureIoTClient 11:b1327861f5e0 660 LogError("Failed to allocate temporary storage for the commands JSON");
AzureIoTClient 0:1f9b2707ec7d 661 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 662 }
AzureIoTClient 0:1f9b2707ec7d 663 else
AzureIoTClient 0:1f9b2707ec7d 664 {
AzureIoTClient 0:1f9b2707ec7d 665 MULTITREE_HANDLE commandsTree;
AzureIoTClient 0:1f9b2707ec7d 666
AzureIoTClient 0:1f9b2707ec7d 667 (void)memcpy(commandJSON, command, size);
AzureIoTClient 0:1f9b2707ec7d 668 commandJSON[size] = '\0';
AzureIoTClient 0:1f9b2707ec7d 669
AzureIoTClient 0:1f9b2707ec7d 670 /* Codes_SRS_COMMAND_DECODER_01_012: [CommandDecoder shall decode the command JSON contained in buffer to a multi-tree by using JSONDecoder_JSON_To_MultiTree.] */
AzureIoTClient 0:1f9b2707ec7d 671 if (JSONDecoder_JSON_To_MultiTree(commandJSON, &commandsTree) != JSON_DECODER_OK)
AzureIoTClient 0:1f9b2707ec7d 672 {
AzureIoTClient 0:1f9b2707ec7d 673 /* Codes_SRS_COMMAND_DECODER_01_013: [If parsing the JSON to a multi tree fails, the processing shall stop and the command shall not be dispatched and it shall return EXECUTE_COMMAND_ERROR.] */
AzureIoTClient 11:b1327861f5e0 674 LogError("Decoding JSON to a multi tree failed");
AzureIoTClient 0:1f9b2707ec7d 675 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 0:1f9b2707ec7d 676 }
AzureIoTClient 0:1f9b2707ec7d 677 else
AzureIoTClient 0:1f9b2707ec7d 678 {
AzureIoTClient 0:1f9b2707ec7d 679 result = DecodeCommand(commandDecoderInstance, commandsTree);
AzureIoTClient 0:1f9b2707ec7d 680
AzureIoTClient 0:1f9b2707ec7d 681 /* Codes_SRS_COMMAND_DECODER_01_016: [CommandDecoder shall ensure that the multi-tree resulting from JSONDecoder_JSON_To_MultiTree is freed after the commands are executed.] */
AzureIoTClient 0:1f9b2707ec7d 682 MultiTree_Destroy(commandsTree);
AzureIoTClient 0:1f9b2707ec7d 683 }
AzureIoTClient 0:1f9b2707ec7d 684
AzureIoTClient 0:1f9b2707ec7d 685 free(commandJSON);
AzureIoTClient 0:1f9b2707ec7d 686 }
AzureIoTClient 0:1f9b2707ec7d 687 }
AzureIoTClient 0:1f9b2707ec7d 688 return result;
AzureIoTClient 0:1f9b2707ec7d 689 }
AzureIoTClient 0:1f9b2707ec7d 690
Azure.IoT.Build 18:58b667752399 691 METHODRETURN_HANDLE CommandDecoder_ExecuteMethod(COMMAND_DECODER_HANDLE handle, const char* fullMethodName, const char* methodPayload)
Azure.IoT.Build 18:58b667752399 692 {
Azure.IoT.Build 18:58b667752399 693 METHODRETURN_HANDLE result;
Azure.IoT.Build 18:58b667752399 694 /*Codes_SRS_COMMAND_DECODER_02_014: [ If handle is NULL then CommandDecoder_ExecuteMethod shall fail and return NULL. ]*/
Azure.IoT.Build 18:58b667752399 695 /*Codes_SRS_COMMAND_DECODER_02_015: [ If fulMethodName is NULL then CommandDecoder_ExecuteMethod shall fail and return NULL. ]*/
Azure.IoT.Build 18:58b667752399 696 if (
Azure.IoT.Build 18:58b667752399 697 (handle == NULL) ||
Azure.IoT.Build 18:58b667752399 698 (fullMethodName == NULL) /*methodPayload can be NULL*/
Azure.IoT.Build 18:58b667752399 699 )
Azure.IoT.Build 18:58b667752399 700 {
Azure.IoT.Build 18:58b667752399 701 LogError("Invalid argument, COMMAND_DECODER_HANDLE handle=%p, const char* fullMethodName=%p", handle, fullMethodName);
Azure.IoT.Build 18:58b667752399 702 result = NULL;
Azure.IoT.Build 18:58b667752399 703 }
Azure.IoT.Build 18:58b667752399 704 else
Azure.IoT.Build 18:58b667752399 705 {
Azure.IoT.Build 18:58b667752399 706 COMMAND_DECODER_HANDLE_DATA* commandDecoderInstance = (COMMAND_DECODER_HANDLE_DATA*)handle;
Azure.IoT.Build 18:58b667752399 707 /*Codes_SRS_COMMAND_DECODER_02_025: [ If methodCallback is NULL then CommandDecoder_ExecuteMethod shall fail and return NULL. ]*/
Azure.IoT.Build 18:58b667752399 708 if (commandDecoderInstance->methodCallback == NULL)
Azure.IoT.Build 18:58b667752399 709 {
Azure.IoT.Build 18:58b667752399 710 LogError("unable to execute a method when the methodCallback passed in CommandDecoder_Create is NULL");
Azure.IoT.Build 18:58b667752399 711 result = NULL;
Azure.IoT.Build 18:58b667752399 712 }
Azure.IoT.Build 18:58b667752399 713 else
Azure.IoT.Build 18:58b667752399 714 {
Azure.IoT.Build 18:58b667752399 715 /*Codes_SRS_COMMAND_DECODER_02_016: [ If methodPayload is not NULL then CommandDecoder_ExecuteMethod shall build a MULTITREE_HANDLE out of methodPayload. ]*/
Azure.IoT.Build 18:58b667752399 716 if (methodPayload == NULL)
Azure.IoT.Build 18:58b667752399 717 {
Azure.IoT.Build 18:58b667752399 718 result = DecodeMethod(commandDecoderInstance, fullMethodName, NULL);
Azure.IoT.Build 18:58b667752399 719 }
Azure.IoT.Build 18:58b667752399 720 else
Azure.IoT.Build 18:58b667752399 721 {
Azure.IoT.Build 18:58b667752399 722 char* methodJSON;
Azure.IoT.Build 18:58b667752399 723
Azure.IoT.Build 18:58b667752399 724 if (mallocAndStrcpy_s(&methodJSON, methodPayload) != 0)
Azure.IoT.Build 18:58b667752399 725 {
Azure.IoT.Build 18:58b667752399 726 LogError("Failed to allocate temporary storage for the method JSON");
Azure.IoT.Build 18:58b667752399 727 result = NULL;
Azure.IoT.Build 18:58b667752399 728 }
Azure.IoT.Build 18:58b667752399 729 else
Azure.IoT.Build 18:58b667752399 730 {
Azure.IoT.Build 18:58b667752399 731 MULTITREE_HANDLE methodTree;
Azure.IoT.Build 18:58b667752399 732 if (JSONDecoder_JSON_To_MultiTree(methodJSON, &methodTree) != JSON_DECODER_OK)
Azure.IoT.Build 18:58b667752399 733 {
Azure.IoT.Build 18:58b667752399 734 LogError("Decoding JSON to a multi tree failed");
Azure.IoT.Build 18:58b667752399 735 result = NULL;
Azure.IoT.Build 18:58b667752399 736 }
Azure.IoT.Build 18:58b667752399 737 else
Azure.IoT.Build 18:58b667752399 738 {
Azure.IoT.Build 18:58b667752399 739 result = DecodeMethod(commandDecoderInstance, fullMethodName, methodTree);
Azure.IoT.Build 18:58b667752399 740 MultiTree_Destroy(methodTree);
Azure.IoT.Build 18:58b667752399 741 }
Azure.IoT.Build 18:58b667752399 742 free(methodJSON);
Azure.IoT.Build 18:58b667752399 743 }
Azure.IoT.Build 18:58b667752399 744 }
Azure.IoT.Build 18:58b667752399 745 }
Azure.IoT.Build 18:58b667752399 746 }
Azure.IoT.Build 18:58b667752399 747 return result;
Azure.IoT.Build 18:58b667752399 748 }
Azure.IoT.Build 18:58b667752399 749
Azure.IoT.Build 18:58b667752399 750
Azure.IoT.Build 18:58b667752399 751 COMMAND_DECODER_HANDLE CommandDecoder_Create(SCHEMA_MODEL_TYPE_HANDLE modelHandle, ACTION_CALLBACK_FUNC actionCallback, void* actionCallbackContext, METHOD_CALLBACK_FUNC methodCallback, void* methodCallbackContext)
AzureIoTClient 0:1f9b2707ec7d 752 {
AzureIoTClient 17:fa1bba4c6053 753 COMMAND_DECODER_HANDLE_DATA* result;
AzureIoTClient 0:1f9b2707ec7d 754 /* Codes_SRS_COMMAND_DECODER_99_019:[ For all exposed APIs argument validity checks shall precede other checks.] */
Azure.IoT.Build 18:58b667752399 755 /* Codes_SRS_COMMAND_DECODER_01_003: [ If modelHandle is NULL, CommandDecoder_Create shall return NULL. ]*/
AzureIoTClient 22:422d94bd3c18 756 if (modelHandle == NULL)
AzureIoTClient 0:1f9b2707ec7d 757 {
Azure.IoT.Build 18:58b667752399 758 LogError("Invalid arguments: SCHEMA_MODEL_TYPE_HANDLE modelHandle=%p, ACTION_CALLBACK_FUNC actionCallback=%p, void* actionCallbackContext=%p, METHOD_CALLBACK_FUNC methodCallback=%p, void* methodCallbackContext=%p",
Azure.IoT.Build 18:58b667752399 759 modelHandle, actionCallback, actionCallbackContext, methodCallback, methodCallbackContext);
AzureIoTClient 0:1f9b2707ec7d 760 result = NULL;
AzureIoTClient 0:1f9b2707ec7d 761 }
AzureIoTClient 0:1f9b2707ec7d 762 else
AzureIoTClient 0:1f9b2707ec7d 763 {
AzureIoTClient 0:1f9b2707ec7d 764 /* Codes_SRS_COMMAND_DECODER_01_001: [CommandDecoder_Create shall create a new instance of a CommandDecoder.] */
AzureIoTClient 17:fa1bba4c6053 765 result = malloc(sizeof(COMMAND_DECODER_HANDLE_DATA));
AzureIoTClient 0:1f9b2707ec7d 766 if (result == NULL)
AzureIoTClient 0:1f9b2707ec7d 767 {
AzureIoTClient 0:1f9b2707ec7d 768 /* Codes_SRS_COMMAND_DECODER_01_004: [If any error is encountered during CommandDecoder_Create CommandDecoder_Create shall return NULL.] */
AzureIoTClient 17:fa1bba4c6053 769 /*return as is*/
AzureIoTClient 0:1f9b2707ec7d 770 }
AzureIoTClient 0:1f9b2707ec7d 771 else
AzureIoTClient 0:1f9b2707ec7d 772 {
AzureIoTClient 0:1f9b2707ec7d 773 result->ModelHandle = modelHandle;
AzureIoTClient 0:1f9b2707ec7d 774 result->ActionCallback = actionCallback;
AzureIoTClient 0:1f9b2707ec7d 775 result->ActionCallbackContext = actionCallbackContext;
Azure.IoT.Build 18:58b667752399 776 result->methodCallback = methodCallback;
Azure.IoT.Build 18:58b667752399 777 result->methodCallbackContext = methodCallbackContext;
AzureIoTClient 0:1f9b2707ec7d 778 }
AzureIoTClient 0:1f9b2707ec7d 779 }
AzureIoTClient 0:1f9b2707ec7d 780
AzureIoTClient 0:1f9b2707ec7d 781 return result;
AzureIoTClient 0:1f9b2707ec7d 782 }
AzureIoTClient 0:1f9b2707ec7d 783
AzureIoTClient 0:1f9b2707ec7d 784 void CommandDecoder_Destroy(COMMAND_DECODER_HANDLE commandDecoderHandle)
AzureIoTClient 0:1f9b2707ec7d 785 {
AzureIoTClient 0:1f9b2707ec7d 786 /* Codes_SRS_COMMAND_DECODER_01_007: [If CommandDecoder_Destroy is called with a NULL handle, CommandDecoder_Destroy shall do nothing.] */
AzureIoTClient 0:1f9b2707ec7d 787 if (commandDecoderHandle != NULL)
AzureIoTClient 0:1f9b2707ec7d 788 {
AzureIoTClient 17:fa1bba4c6053 789 COMMAND_DECODER_HANDLE_DATA* commandDecoderInstance = (COMMAND_DECODER_HANDLE_DATA*)commandDecoderHandle;
AzureIoTClient 0:1f9b2707ec7d 790
AzureIoTClient 0:1f9b2707ec7d 791 /* Codes_SRS_COMMAND_DECODER_01_005: [CommandDecoder_Destroy shall free all resources associated with the commandDecoderHandle instance.] */
AzureIoTClient 0:1f9b2707ec7d 792 free(commandDecoderInstance);
AzureIoTClient 0:1f9b2707ec7d 793 }
AzureIoTClient 17:fa1bba4c6053 794 }
AzureIoTClient 17:fa1bba4c6053 795
AzureIoTClient 17:fa1bba4c6053 796 DEFINE_ENUM_STRINGS(AGENT_DATA_TYPE_TYPE, AGENT_DATA_TYPE_TYPE_VALUES);
AzureIoTClient 17:fa1bba4c6053 797
AzureIoTClient 17:fa1bba4c6053 798 /*validates that the multitree (coming from a JSON) is actually a serialization of the model (complete or incomplete)*/
AzureIoTClient 17:fa1bba4c6053 799 /*if the serialization contains more than the model, then it fails.*/
AzureIoTClient 17:fa1bba4c6053 800 /*if the serialization does not contain mandatory items from the model, it fails*/
AzureIoTClient 17:fa1bba4c6053 801 static bool validateModel_vs_Multitree(void* startAddress, SCHEMA_MODEL_TYPE_HANDLE modelHandle, MULTITREE_HANDLE desiredPropertiesTree, size_t offset)
AzureIoTClient 17:fa1bba4c6053 802 {
AzureIoTClient 17:fa1bba4c6053 803
AzureIoTClient 17:fa1bba4c6053 804 bool result;
AzureIoTClient 17:fa1bba4c6053 805 size_t nChildren;
AzureIoTClient 17:fa1bba4c6053 806 size_t nProcessedChildren = 0;
AzureIoTClient 17:fa1bba4c6053 807 (void)MultiTree_GetChildCount(desiredPropertiesTree, &nChildren);
AzureIoTClient 17:fa1bba4c6053 808 for (size_t i = 0;i < nChildren;i++)
AzureIoTClient 17:fa1bba4c6053 809 {
AzureIoTClient 17:fa1bba4c6053 810 MULTITREE_HANDLE child;
AzureIoTClient 17:fa1bba4c6053 811 if (MultiTree_GetChild(desiredPropertiesTree, i, &child) != MULTITREE_OK)
AzureIoTClient 17:fa1bba4c6053 812 {
AzureIoTClient 17:fa1bba4c6053 813 LogError("failure in MultiTree_GetChild");
AzureIoTClient 17:fa1bba4c6053 814 i = nChildren;
AzureIoTClient 17:fa1bba4c6053 815 }
AzureIoTClient 17:fa1bba4c6053 816 else
AzureIoTClient 17:fa1bba4c6053 817 {
AzureIoTClient 17:fa1bba4c6053 818 STRING_HANDLE childName = STRING_new();
AzureIoTClient 17:fa1bba4c6053 819 if (childName == NULL)
AzureIoTClient 17:fa1bba4c6053 820 {
AzureIoTClient 17:fa1bba4c6053 821 LogError("failure to STRING_new");
AzureIoTClient 17:fa1bba4c6053 822 i = nChildren;
AzureIoTClient 17:fa1bba4c6053 823 }
AzureIoTClient 17:fa1bba4c6053 824 else
AzureIoTClient 17:fa1bba4c6053 825 {
AzureIoTClient 17:fa1bba4c6053 826 if (MultiTree_GetName(child, childName) != MULTITREE_OK)
AzureIoTClient 17:fa1bba4c6053 827 {
AzureIoTClient 17:fa1bba4c6053 828 LogError("failure to MultiTree_GetName");
AzureIoTClient 17:fa1bba4c6053 829 i = nChildren;
AzureIoTClient 17:fa1bba4c6053 830 }
AzureIoTClient 17:fa1bba4c6053 831 else
AzureIoTClient 17:fa1bba4c6053 832 {
AzureIoTClient 17:fa1bba4c6053 833 const char *childName_str = STRING_c_str(childName);
AzureIoTClient 17:fa1bba4c6053 834 SCHEMA_MODEL_ELEMENT elementType = Schema_GetModelElementByName(modelHandle, childName_str);
AzureIoTClient 17:fa1bba4c6053 835 switch (elementType.elementType)
AzureIoTClient 17:fa1bba4c6053 836 {
AzureIoTClient 17:fa1bba4c6053 837 default:
AzureIoTClient 17:fa1bba4c6053 838 {
AzureIoTClient 17:fa1bba4c6053 839 LogError("INTERNAL ERROR: unexpected function return");
AzureIoTClient 17:fa1bba4c6053 840 i = nChildren;
AzureIoTClient 17:fa1bba4c6053 841 break;
AzureIoTClient 17:fa1bba4c6053 842 }
AzureIoTClient 17:fa1bba4c6053 843 case (SCHEMA_PROPERTY):
AzureIoTClient 17:fa1bba4c6053 844 {
AzureIoTClient 17:fa1bba4c6053 845 LogError("cannot ingest name (WITH_DATA instead of WITH_DESIRED_PROPERTY): %s", STRING_c_str);
AzureIoTClient 17:fa1bba4c6053 846 i = nChildren;
AzureIoTClient 17:fa1bba4c6053 847 break;
AzureIoTClient 17:fa1bba4c6053 848 }
AzureIoTClient 17:fa1bba4c6053 849 case (SCHEMA_REPORTED_PROPERTY):
AzureIoTClient 17:fa1bba4c6053 850 {
AzureIoTClient 17:fa1bba4c6053 851 LogError("cannot ingest name (WITH_REPORTED_PROPERTY instead of WITH_DESIRED_PROPERTY): %s", STRING_c_str);
AzureIoTClient 17:fa1bba4c6053 852 i = nChildren;
AzureIoTClient 17:fa1bba4c6053 853 break;
AzureIoTClient 17:fa1bba4c6053 854 }
AzureIoTClient 17:fa1bba4c6053 855 case (SCHEMA_DESIRED_PROPERTY):
AzureIoTClient 17:fa1bba4c6053 856 {
AzureIoTClient 17:fa1bba4c6053 857 /*Codes_SRS_COMMAND_DECODER_02_007: [ If the child name corresponds to a desired property then an AGENT_DATA_TYPE shall be constructed from the MULTITREE node. ]*/
AzureIoTClient 17:fa1bba4c6053 858 SCHEMA_DESIRED_PROPERTY_HANDLE desiredPropertyHandle = elementType.elementHandle.desiredPropertyHandle;
AzureIoTClient 17:fa1bba4c6053 859
AzureIoTClient 17:fa1bba4c6053 860 const char* desiredPropertyType = Schema_GetModelDesiredPropertyType(desiredPropertyHandle);
AzureIoTClient 17:fa1bba4c6053 861 AGENT_DATA_TYPE output;
AzureIoTClient 17:fa1bba4c6053 862 if (DecodeValueFromNode(Schema_GetSchemaForModelType(modelHandle), &output, child, desiredPropertyType) != 0)
AzureIoTClient 17:fa1bba4c6053 863 {
AzureIoTClient 17:fa1bba4c6053 864 LogError("failure in DecodeValueFromNode");
AzureIoTClient 17:fa1bba4c6053 865 i = nChildren;
AzureIoTClient 17:fa1bba4c6053 866 }
AzureIoTClient 17:fa1bba4c6053 867 else
AzureIoTClient 17:fa1bba4c6053 868 {
AzureIoTClient 17:fa1bba4c6053 869 /*Codes_SRS_COMMAND_DECODER_02_008: [ The desired property shall be constructed in memory by calling pfDesiredPropertyFromAGENT_DATA_TYPE. ]*/
AzureIoTClient 17:fa1bba4c6053 870 pfDesiredPropertyFromAGENT_DATA_TYPE leFunction = Schema_GetModelDesiredProperty_pfDesiredPropertyFromAGENT_DATA_TYPE(desiredPropertyHandle);
AzureIoTClient 17:fa1bba4c6053 871 if (leFunction(&output, (char*)startAddress + offset + Schema_GetModelDesiredProperty_offset(desiredPropertyHandle)) != 0)
AzureIoTClient 17:fa1bba4c6053 872 {
AzureIoTClient 17:fa1bba4c6053 873 LogError("failure in a function that converts from AGENT_DATA_TYPE to C data");
AzureIoTClient 17:fa1bba4c6053 874 }
AzureIoTClient 17:fa1bba4c6053 875 else
AzureIoTClient 17:fa1bba4c6053 876 {
AzureIoTClient 17:fa1bba4c6053 877 /*Codes_SRS_COMMAND_DECODER_02_013: [ If the desired property has a non-NULL pfOnDesiredProperty then it shall be called. ]*/
AzureIoTClient 17:fa1bba4c6053 878 pfOnDesiredProperty onDesiredProperty = Schema_GetModelDesiredProperty_pfOnDesiredProperty(desiredPropertyHandle);
AzureIoTClient 17:fa1bba4c6053 879 if (onDesiredProperty != NULL)
AzureIoTClient 17:fa1bba4c6053 880 {
AzureIoTClient 17:fa1bba4c6053 881 onDesiredProperty((char*)startAddress + offset);
AzureIoTClient 17:fa1bba4c6053 882 }
AzureIoTClient 17:fa1bba4c6053 883 nProcessedChildren++;
AzureIoTClient 17:fa1bba4c6053 884 }
AzureIoTClient 17:fa1bba4c6053 885 Destroy_AGENT_DATA_TYPE(&output);
AzureIoTClient 17:fa1bba4c6053 886 }
AzureIoTClient 17:fa1bba4c6053 887
AzureIoTClient 17:fa1bba4c6053 888 break;
AzureIoTClient 17:fa1bba4c6053 889 }
AzureIoTClient 17:fa1bba4c6053 890 case(SCHEMA_MODEL_IN_MODEL):
AzureIoTClient 17:fa1bba4c6053 891 {
AzureIoTClient 17:fa1bba4c6053 892 SCHEMA_MODEL_TYPE_HANDLE modelModel = elementType.elementHandle.modelHandle;
AzureIoTClient 17:fa1bba4c6053 893
AzureIoTClient 17:fa1bba4c6053 894 /*Codes_SRS_COMMAND_DECODER_02_009: [ If the child name corresponds to a model in model then the function shall call itself recursively. ]*/
AzureIoTClient 17:fa1bba4c6053 895 if (!validateModel_vs_Multitree(startAddress, modelModel, child, offset + Schema_GetModelModelByName_Offset(modelHandle, childName_str)))
AzureIoTClient 17:fa1bba4c6053 896 {
AzureIoTClient 17:fa1bba4c6053 897 LogError("failure in validateModel_vs_Multitree");
AzureIoTClient 17:fa1bba4c6053 898 i = nChildren;
AzureIoTClient 17:fa1bba4c6053 899 }
AzureIoTClient 17:fa1bba4c6053 900 else
AzureIoTClient 17:fa1bba4c6053 901 {
AzureIoTClient 17:fa1bba4c6053 902 /*if the model in model so happened to be a WITH_DESIRED_PROPERTY... (only those has non_NULL pfOnDesiredProperty) */
AzureIoTClient 17:fa1bba4c6053 903 /*Codes_SRS_COMMAND_DECODER_02_012: [ If the child model in model has a non-NULL pfOnDesiredProperty then pfOnDesiredProperty shall be called. ]*/
AzureIoTClient 17:fa1bba4c6053 904 pfOnDesiredProperty onDesiredProperty = Schema_GetModelModelByName_OnDesiredProperty(modelHandle, childName_str);
AzureIoTClient 17:fa1bba4c6053 905 if (onDesiredProperty != NULL)
AzureIoTClient 17:fa1bba4c6053 906 {
AzureIoTClient 17:fa1bba4c6053 907 onDesiredProperty((char*)startAddress + offset);
AzureIoTClient 17:fa1bba4c6053 908 }
AzureIoTClient 17:fa1bba4c6053 909
AzureIoTClient 17:fa1bba4c6053 910 nProcessedChildren++;
AzureIoTClient 17:fa1bba4c6053 911 }
AzureIoTClient 17:fa1bba4c6053 912
AzureIoTClient 17:fa1bba4c6053 913 break;
AzureIoTClient 17:fa1bba4c6053 914 }
AzureIoTClient 17:fa1bba4c6053 915
AzureIoTClient 17:fa1bba4c6053 916 } /*switch*/
AzureIoTClient 17:fa1bba4c6053 917 }
AzureIoTClient 17:fa1bba4c6053 918 STRING_delete(childName);
AzureIoTClient 17:fa1bba4c6053 919 }
AzureIoTClient 17:fa1bba4c6053 920 }
AzureIoTClient 17:fa1bba4c6053 921 }
AzureIoTClient 17:fa1bba4c6053 922
AzureIoTClient 17:fa1bba4c6053 923 if(nProcessedChildren == nChildren)
AzureIoTClient 17:fa1bba4c6053 924 {
AzureIoTClient 17:fa1bba4c6053 925 /*Codes_SRS_COMMAND_DECODER_02_010: [ If the complete MULTITREE has been parsed then CommandDecoder_IngestDesiredProperties shall succeed and return EXECUTE_COMMAND_SUCCESS. ]*/
AzureIoTClient 17:fa1bba4c6053 926 result = true;
AzureIoTClient 17:fa1bba4c6053 927 }
AzureIoTClient 17:fa1bba4c6053 928 else
AzureIoTClient 17:fa1bba4c6053 929 {
AzureIoTClient 17:fa1bba4c6053 930 /*Codes_SRS_COMMAND_DECODER_02_011: [ Otherwise CommandDecoder_IngestDesiredProperties shall fail and return EXECUTE_COMMAND_FAILED. ]*/
AzureIoTClient 17:fa1bba4c6053 931 LogError("not all constituents of the JSON have been ingested");
AzureIoTClient 17:fa1bba4c6053 932 result = false;
AzureIoTClient 17:fa1bba4c6053 933 }
AzureIoTClient 17:fa1bba4c6053 934 return result;
AzureIoTClient 17:fa1bba4c6053 935 }
AzureIoTClient 17:fa1bba4c6053 936
AzureIoTClient 17:fa1bba4c6053 937 static EXECUTE_COMMAND_RESULT DecodeDesiredProperties(void* startAddress, COMMAND_DECODER_HANDLE_DATA* handle, MULTITREE_HANDLE desiredPropertiesTree)
AzureIoTClient 17:fa1bba4c6053 938 {
AzureIoTClient 17:fa1bba4c6053 939 /*Codes_SRS_COMMAND_DECODER_02_006: [ CommandDecoder_IngestDesiredProperties shall parse the MULTITREEE recursively. ]*/
AzureIoTClient 17:fa1bba4c6053 940 return validateModel_vs_Multitree(startAddress, handle->ModelHandle, desiredPropertiesTree, 0 )?EXECUTE_COMMAND_SUCCESS:EXECUTE_COMMAND_FAILED;
AzureIoTClient 17:fa1bba4c6053 941 }
AzureIoTClient 17:fa1bba4c6053 942
AzureIoTClient 26:7c0e6f86d034 943 /* Raw JSON has properties we don't need; potentially nodes other than "desired" for full TWIN as well as a $version we don't pass to callees */
AzureIoTClient 26:7c0e6f86d034 944 static bool RemoveUnneededTwinProperties(MULTITREE_HANDLE initialParsedTree, bool parseDesiredNode, MULTITREE_HANDLE *desiredPropertiesTree)
AzureIoTClient 26:7c0e6f86d034 945 {
AzureIoTClient 26:7c0e6f86d034 946 MULTITREE_HANDLE updateTree;
AzureIoTClient 26:7c0e6f86d034 947 bool result;
AzureIoTClient 26:7c0e6f86d034 948
AzureIoTClient 26:7c0e6f86d034 949 if (parseDesiredNode)
AzureIoTClient 26:7c0e6f86d034 950 {
AzureIoTClient 26:7c0e6f86d034 951 /*Codes_SRS_COMMAND_DECODER_02_014: [ If parseDesiredNode is TRUE, parse only the `desired` part of JSON tree ]*/
AzureIoTClient 26:7c0e6f86d034 952 if (MultiTree_GetChildByName(initialParsedTree, "desired", &updateTree) != MULTITREE_OK)
AzureIoTClient 26:7c0e6f86d034 953 {
AzureIoTClient 26:7c0e6f86d034 954 LogError("Unable to find 'desired' in tree");
AzureIoTClient 26:7c0e6f86d034 955 return false;
AzureIoTClient 26:7c0e6f86d034 956 }
AzureIoTClient 26:7c0e6f86d034 957 }
AzureIoTClient 26:7c0e6f86d034 958 else
AzureIoTClient 26:7c0e6f86d034 959 {
AzureIoTClient 26:7c0e6f86d034 960 // Tree already starts on node we want so just use it.
AzureIoTClient 26:7c0e6f86d034 961 updateTree = initialParsedTree;
AzureIoTClient 26:7c0e6f86d034 962 }
AzureIoTClient 26:7c0e6f86d034 963
AzureIoTClient 26:7c0e6f86d034 964 /*Codes_COMMAND_DECODER_02_015: [ Remove '$version' string from node, if it is present. It not being present is not an error ]*/
AzureIoTClient 26:7c0e6f86d034 965 MULTITREE_RESULT deleteChildResult = MultiTree_DeleteChild(updateTree, "$version");
AzureIoTClient 26:7c0e6f86d034 966 if ((deleteChildResult == MULTITREE_OK) || (deleteChildResult == MULTITREE_CHILD_NOT_FOUND))
AzureIoTClient 26:7c0e6f86d034 967 {
AzureIoTClient 26:7c0e6f86d034 968 *desiredPropertiesTree = updateTree;
AzureIoTClient 26:7c0e6f86d034 969 result = true;
AzureIoTClient 26:7c0e6f86d034 970 }
AzureIoTClient 26:7c0e6f86d034 971 else
AzureIoTClient 26:7c0e6f86d034 972 {
AzureIoTClient 26:7c0e6f86d034 973 *desiredPropertiesTree = NULL;
AzureIoTClient 26:7c0e6f86d034 974 result = false;
AzureIoTClient 26:7c0e6f86d034 975 }
AzureIoTClient 26:7c0e6f86d034 976
AzureIoTClient 26:7c0e6f86d034 977 return result;
AzureIoTClient 26:7c0e6f86d034 978 }
AzureIoTClient 26:7c0e6f86d034 979
AzureIoTClient 26:7c0e6f86d034 980 EXECUTE_COMMAND_RESULT CommandDecoder_IngestDesiredProperties(void* startAddress, COMMAND_DECODER_HANDLE handle, const char* jsonPayload, bool parseDesiredNode)
AzureIoTClient 17:fa1bba4c6053 981 {
AzureIoTClient 17:fa1bba4c6053 982 EXECUTE_COMMAND_RESULT result;
AzureIoTClient 26:7c0e6f86d034 983
AzureIoTClient 17:fa1bba4c6053 984 /*Codes_SRS_COMMAND_DECODER_02_001: [ If startAddress is NULL then CommandDecoder_IngestDesiredProperties shall fail and return EXECUTE_COMMAND_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 985 /*Codes_SRS_COMMAND_DECODER_02_002: [ If handle is NULL then CommandDecoder_IngestDesiredProperties shall fail and return EXECUTE_COMMAND_ERROR. ]*/
AzureIoTClient 26:7c0e6f86d034 986 /*Codes_SRS_COMMAND_DECODER_02_003: [ If jsonPayload is NULL then CommandDecoder_IngestDesiredProperties shall fail and return EXECUTE_COMMAND_ERROR. ]*/
AzureIoTClient 17:fa1bba4c6053 987 if(
AzureIoTClient 17:fa1bba4c6053 988 (startAddress == NULL) ||
AzureIoTClient 17:fa1bba4c6053 989 (handle == NULL) ||
AzureIoTClient 26:7c0e6f86d034 990 (jsonPayload == NULL)
AzureIoTClient 17:fa1bba4c6053 991 )
AzureIoTClient 17:fa1bba4c6053 992 {
AzureIoTClient 26:7c0e6f86d034 993 LogError("invalid argument COMMAND_DECODER_HANDLE handle=%p, const char* jsonPayload=%p", handle, jsonPayload);
AzureIoTClient 17:fa1bba4c6053 994 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 17:fa1bba4c6053 995 }
AzureIoTClient 17:fa1bba4c6053 996 else
AzureIoTClient 17:fa1bba4c6053 997 {
AzureIoTClient 17:fa1bba4c6053 998 /*Codes_SRS_COMMAND_DECODER_02_004: [ CommandDecoder_IngestDesiredProperties shall clone desiredProperties. ]*/
AzureIoTClient 17:fa1bba4c6053 999 char* copy;
AzureIoTClient 26:7c0e6f86d034 1000 if (mallocAndStrcpy_s(&copy, jsonPayload) != 0)
AzureIoTClient 17:fa1bba4c6053 1001 {
AzureIoTClient 17:fa1bba4c6053 1002 LogError("failure in mallocAndStrcpy_s");
AzureIoTClient 17:fa1bba4c6053 1003 result = EXECUTE_COMMAND_FAILED;
AzureIoTClient 17:fa1bba4c6053 1004 }
AzureIoTClient 17:fa1bba4c6053 1005 else
AzureIoTClient 17:fa1bba4c6053 1006 {
AzureIoTClient 17:fa1bba4c6053 1007 /*Codes_SRS_COMMAND_DECODER_02_005: [ CommandDecoder_IngestDesiredProperties shall create a MULTITREE_HANDLE ouf of the clone of desiredProperties. ]*/
AzureIoTClient 26:7c0e6f86d034 1008 MULTITREE_HANDLE initialParsedTree;
AzureIoTClient 17:fa1bba4c6053 1009 MULTITREE_HANDLE desiredPropertiesTree;
AzureIoTClient 26:7c0e6f86d034 1010
AzureIoTClient 26:7c0e6f86d034 1011 if (JSONDecoder_JSON_To_MultiTree(copy, &initialParsedTree) != JSON_DECODER_OK)
AzureIoTClient 17:fa1bba4c6053 1012 {
AzureIoTClient 17:fa1bba4c6053 1013 LogError("Decoding JSON to a multi tree failed");
AzureIoTClient 17:fa1bba4c6053 1014 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 17:fa1bba4c6053 1015 }
AzureIoTClient 17:fa1bba4c6053 1016 else
AzureIoTClient 17:fa1bba4c6053 1017 {
AzureIoTClient 26:7c0e6f86d034 1018 if (RemoveUnneededTwinProperties(initialParsedTree, parseDesiredNode, &desiredPropertiesTree) == false)
AzureIoTClient 26:7c0e6f86d034 1019 {
AzureIoTClient 26:7c0e6f86d034 1020 LogError("Removing unneeded twin properties failed");
AzureIoTClient 26:7c0e6f86d034 1021 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 26:7c0e6f86d034 1022 }
AzureIoTClient 26:7c0e6f86d034 1023 else
AzureIoTClient 26:7c0e6f86d034 1024 {
AzureIoTClient 26:7c0e6f86d034 1025 COMMAND_DECODER_HANDLE_DATA* commandDecoderInstance = (COMMAND_DECODER_HANDLE_DATA*)handle;
AzureIoTClient 17:fa1bba4c6053 1026
AzureIoTClient 26:7c0e6f86d034 1027 /*Codes_SRS_COMMAND_DECODER_02_006: [ CommandDecoder_IngestDesiredProperties shall parse the MULTITREEE recursively. ]*/
AzureIoTClient 26:7c0e6f86d034 1028 result = DecodeDesiredProperties(startAddress, commandDecoderInstance, desiredPropertiesTree);
AzureIoTClient 17:fa1bba4c6053 1029
AzureIoTClient 26:7c0e6f86d034 1030 // Do NOT free desiredPropertiesTree. It is only a pointer into initialParsedTree.
AzureIoTClient 26:7c0e6f86d034 1031 MultiTree_Destroy(initialParsedTree);
AzureIoTClient 26:7c0e6f86d034 1032 }
AzureIoTClient 17:fa1bba4c6053 1033 }
AzureIoTClient 17:fa1bba4c6053 1034 free(copy);
AzureIoTClient 17:fa1bba4c6053 1035 }
AzureIoTClient 17:fa1bba4c6053 1036 }
AzureIoTClient 17:fa1bba4c6053 1037 return result;
AzureIoTClient 17:fa1bba4c6053 1038 }