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:
Tue Jan 24 15:24:19 2017 -0800
Revision:
21:6d3dea1abd9c
Parent:
18:58b667752399
Child:
22:422d94bd3c18
1.1.5

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