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

Revision:
18:58b667752399
Parent:
17:fa1bba4c6053
Child:
21:6d3dea1abd9c
--- a/iotdevice.c	Wed Nov 16 21:38:26 2016 -0800
+++ b/iotdevice.c	Wed Dec 14 16:00:39 2016 -0800
@@ -25,6 +25,9 @@
     DATA_PUBLISHER_HANDLE dataPublisherHandle;
     pfDeviceActionCallback deviceActionCallback;
     void* callbackUserContext;
+    pfDeviceMethodCallback deviceMethodCallback;
+    void* methodCallbackUserContext;
+
     COMMAND_DECODER_HANDLE commandDecoderHandle;
 } DEVICE_HANDLE_DATA;
 
@@ -52,12 +55,31 @@
     return result;
 }
 
-DEVICE_RESULT Device_Create(SCHEMA_MODEL_TYPE_HANDLE modelHandle, pfDeviceActionCallback deviceActionCallback, void* callbackUserContext, bool includePropertyPath, DEVICE_HANDLE* deviceHandle)
+static METHODRETURN_HANDLE DeviceInvokeMethod(void* methodCallbackContext, const char* relativeMethodPath, const char* methodName, size_t argCount, const AGENT_DATA_TYPE* args)
+{
+    METHODRETURN_HANDLE result;
+
+    if (methodCallbackContext == NULL)
+    {
+        result = NULL;
+        LogError("(Error code = %s)", ENUM_TO_STRING(DEVICE_RESULT, DEVICE_INVALID_ARG));
+    }
+    else
+    {
+        DEVICE_HANDLE_DATA* device = (DEVICE_HANDLE_DATA*)methodCallbackContext;
+
+        result = device->deviceMethodCallback((DEVICE_HANDLE)device, device->methodCallbackUserContext, relativeMethodPath, methodName, argCount, args);
+    }
+
+    return result;
+}
+
+DEVICE_RESULT Device_Create(SCHEMA_MODEL_TYPE_HANDLE modelHandle, pfDeviceActionCallback deviceActionCallback, void* callbackUserContext, pfDeviceMethodCallback methodCallback, void* methodCallbackContext, bool includePropertyPath, DEVICE_HANDLE* deviceHandle)
 {
     DEVICE_RESULT result;
 
     /* Codes_SRS_DEVICE_05_014: [If any of the modelHandle, deviceHandle or deviceActionCallback arguments are NULL, Device_Create shall return DEVICE_INVALID_ARG.]*/
-    if (modelHandle == NULL || deviceHandle == NULL || deviceActionCallback == NULL )
+    if (modelHandle == NULL || deviceHandle == NULL || deviceActionCallback == NULL || methodCallback == NULL || methodCallbackContext == NULL)
     {
         result = DEVICE_INVALID_ARG;
         LOG_DEVICE_ERROR;
@@ -89,10 +111,12 @@
                 device->model = modelHandle;
                 device->deviceActionCallback = deviceActionCallback;
                 device->callbackUserContext = callbackUserContext;
+                device->deviceMethodCallback = methodCallback;
+                device->methodCallbackUserContext = methodCallbackContext;
 
                 /* Codes_SRS_DEVICE_01_001: [Device_Create shall create a CommandDecoder instance by calling CommandDecoder_Create and passing to it the model handle.] */
                 /* Codes_SRS_DEVICE_01_002: [Device_Create shall also pass to CommandDecoder_Create a callback to be invoked when a command is received and a context that shall be the device handle.] */
-                if ((device->commandDecoderHandle = CommandDecoder_Create(modelHandle, DeviceInvokeAction, device)) == NULL)
+                if ((device->commandDecoderHandle = CommandDecoder_Create(modelHandle, DeviceInvokeAction, device, DeviceInvokeMethod, device)) == NULL)
                 {
                     DataPublisher_Destroy(device->dataPublisherHandle);
                     free(device);
@@ -260,6 +284,25 @@
     return result;
 }
 
+METHODRETURN_HANDLE Device_ExecuteMethod(DEVICE_HANDLE deviceHandle, const char* methodName, const char* methodPayload)
+{
+    METHODRETURN_HANDLE result;
+    if (
+        (deviceHandle == NULL) ||
+        (methodName == NULL) /*methodPayload can be NULL*/
+        )
+    {
+        result = NULL;
+        LogError("invalid parameter (NULL passed to Device_ExecuteMethod DEVICE_HANDLE deviceHandle=%p, const char* methodPayload=%p", deviceHandle, methodPayload);
+    }
+    else
+    {
+        DEVICE_HANDLE_DATA* device = (DEVICE_HANDLE_DATA*)deviceHandle;
+        result = CommandDecoder_ExecuteMethod(device->commandDecoderHandle, methodName, methodPayload);
+    }
+    return result;
+}
+
 REPORTED_PROPERTIES_TRANSACTION_HANDLE Device_CreateTransaction_ReportedProperties(DEVICE_HANDLE deviceHandle)
 {
     REPORTED_PROPERTIES_TRANSACTION_HANDLE result;