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:
Azure.IoT.Build
Date:
Wed Dec 14 16:00:39 2016 -0800
Revision:
18:58b667752399
Parent:
17:fa1bba4c6053
Child:
21:6d3dea1abd9c
1.1.2

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