From 26bb6a4a1aa47e2cf4ce0ea29dd072412065b61e Mon Sep 17 00:00:00 2001 From: Jooseok Park Date: Mon, 20 Mar 2017 21:41:09 +0900 Subject: Update RequestHandlerCallback - Add RequestHandlerCallbackMap for unmanaged callback function Change-Id: I95c0d5534da3b0f9e72ac6bdd22704d846d98dd1 --- .../Interop/Interop.IoTConnectivity.Server.cs | 1 + .../IoTConnectivityServerManager.cs | 39 ++++++++++++++++++++-- .../Representation.cs | 5 ++- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Server.cs b/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Server.cs index 8d1745f..59b9355 100755 --- a/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Server.cs +++ b/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Server.cs @@ -47,6 +47,7 @@ internal static partial class Interop internal static partial class Resource { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate void RequestHandlerCallback(IntPtr resource, IntPtr request, IntPtr userData); [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_create")] diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityServerManager.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityServerManager.cs index 3c527b0..071c34c 100755 --- a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityServerManager.cs +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityServerManager.cs @@ -24,6 +24,10 @@ namespace Tizen.Network.IoTConnectivity /// public static class IoTConnectivityServerManager { + + private static int s_requestId = 1; + private static Dictionary s_RequestHandlerCallbackMap = new Dictionary(); + /// /// Initializes IoTCon. Call this API to start IoTCon. /// @@ -71,6 +75,9 @@ namespace Tizen.Network.IoTConnectivity public static void Deinitialize() { _resources.Clear(); + s_requestId = 1; + s_RequestHandlerCallbackMap.Clear(); + Interop.IoTConnectivity.Client.IoTCon.Deinitialize(); } @@ -98,12 +105,38 @@ namespace Tizen.Network.IoTConnectivity /// public static void RegisterResource(Resource resource) { + Log.Error(IoTConnectivityErrorFactory.LogTag, "..."); + + IntPtr id = IntPtr.Zero; + lock (s_RequestHandlerCallbackMap) + { + id = (IntPtr)s_requestId++; + } + + s_RequestHandlerCallbackMap[id] = (IntPtr r_resource, IntPtr request, IntPtr userData) => + { + int requestId = (int)userData; + + Log.Info(IoTConnectivityErrorFactory.LogTag, "Received s_RequestHandlerCallbackMap : " + requestId); + + if (request == null) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "request is null"); + return; + } + resource.OnRequest(r_resource, request, userData); + }; + IntPtr handle = IntPtr.Zero; - int ret = Interop.IoTConnectivity.Server.Resource.Create(resource.UriPath, resource.Types._resourceTypeHandle, resource.Interfaces.ResourceInterfacesHandle, (int)resource.Policy, resource.OnRequest, IntPtr.Zero, out handle); - if (ret != (int)IoTConnectivityError.None) + int errorCode = Interop.IoTConnectivity.Server.Resource.Create(resource.UriPath, resource.Types._resourceTypeHandle, resource.Interfaces.ResourceInterfacesHandle, (int)resource.Policy, s_RequestHandlerCallbackMap[id], id, out handle); + if (errorCode != (int)IoTConnectivityError.None) { Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed create resource"); - throw IoTConnectivityErrorFactory.GetException(ret); + lock (s_RequestHandlerCallbackMap) + { + s_RequestHandlerCallbackMap.Remove(id); + } + throw IoTConnectivityErrorFactory.GetException(errorCode); } else { diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Representation.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Representation.cs index 13d795c..4070b94 100755 --- a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Representation.cs +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Representation.cs @@ -148,7 +148,10 @@ namespace Tizen.Network.IoTConnectivity Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type"); return null; } - return new ResourceTypes(typeHandle); + if (typeHandle == IntPtr.Zero) + return null; + else + return new ResourceTypes(typeHandle); } set { -- cgit v1.2.3