2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
20 namespace Tizen.Network.IoTConnectivity
23 /// IoT connectivity server manager consists of server side APIs.
25 public static class IoTConnectivityServerManager
28 private static int s_requestId = 1;
29 private static Dictionary<IntPtr, Interop.IoTConnectivity.Server.Resource.RequestHandlerCallback> s_RequestHandlerCallbackMap = new Dictionary<IntPtr, Interop.IoTConnectivity.Server.Resource.RequestHandlerCallback>();
32 /// Initializes IoTCon. Call this API to start IoTCon.
35 /// @a filePath point to a file for handling secure virtual resources.
36 /// The file that is CBOR(Concise Binary Object Representation)-format must already exist
37 /// in @a filePath. We recommend to use application-local file for @a filePath.
40 /// http://tizen.org/privilege/network.get \n
41 /// http://tizen.org/privilege/internet
43 /// <param name="filePath">The file path to point to storage for handling secure virtual resources.</param>
45 /// You must call Deinitialize() if IoTCon API is no longer needed.
47 /// <seealso cref="Deinitialize()"/>
49 /// string filePath = "../../res/iotcon-test-svr-db-server.dat";
50 /// IoTConnectivityServerManager.Initialize(filePath);
52 public static void Initialize(string filePath)
54 int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath);
55 if (ret != (int)IoTConnectivityError.None)
57 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
58 throw IoTConnectivityErrorFactory.GetException(ret);
63 /// Deinitializes IoTCon.
66 /// This API must be called if IoTCon API is no longer needed.
69 /// Initialize() should be called to initialize.
71 /// <seealso cref="Initialize()"/>
73 /// IoTConnectivityServerManager.Deinitialize();
75 public static void Deinitialize()
79 s_RequestHandlerCallbackMap.Clear();
81 Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
85 /// Registers a resource in IoTCon server
88 /// http://tizen.org/privilege/internet
90 /// <param name="resource">The resource to register</param>
92 /// Initialize() should be called to initialize.
94 /// <seealso cref="Resource"/>
95 /// <seealso cref="LiteResource"/>
97 /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
98 /// Attributes attributes = new Attributes { { "state", "ON" }};
99 /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
101 /// IoTConnectivityServerManager.RegisterResource(res);
102 /// } catch(Exception ex) {
103 /// Console.Log("Exception caught : " + ex.Message);
106 public static void RegisterResource(Resource resource)
108 Log.Info(IoTConnectivityErrorFactory.LogTag, "...");
110 IntPtr id = IntPtr.Zero;
111 lock (s_RequestHandlerCallbackMap)
113 id = (IntPtr)s_requestId++;
116 s_RequestHandlerCallbackMap[id] = (IntPtr r_resource, IntPtr request, IntPtr userData) =>
118 int requestId = (int)userData;
120 Log.Info(IoTConnectivityErrorFactory.LogTag, "Received s_RequestHandlerCallbackMap : " + requestId);
122 if (request == IntPtr.Zero)
124 Log.Error(IoTConnectivityErrorFactory.LogTag, "request is IntPtr.Zero");
127 resource.OnRequest(r_resource, request, userData);
130 IntPtr handle = IntPtr.Zero;
131 int errorCode = Interop.IoTConnectivity.Server.Resource.Create(resource.UriPath, resource.Types._resourceTypeHandle, resource.Interfaces.ResourceInterfacesHandle, (int)resource.Policy, s_RequestHandlerCallbackMap[id], id, out handle);
132 if (errorCode != (int)IoTConnectivityError.None)
134 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed create resource");
135 lock (s_RequestHandlerCallbackMap)
137 s_RequestHandlerCallbackMap.Remove(id);
139 throw IoTConnectivityErrorFactory.GetException(errorCode);
143 resource.ResourceHandle = handle;
145 _resources.Add(resource);
149 /// Unregisters a resource in IoTCon server
152 /// http://tizen.org/privilege/internet
154 /// <param name="resource">The resource to unregister</param>
156 /// Initialize() should be called to initialize.
158 /// <seealso cref="Resource"/>
159 /// <seealso cref="LiteResource"/>
161 /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
162 /// Attributes attributes = new Attributes { { "state", "ON" }};
163 /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
164 /// IoTConnectivityServerManager.RegisterResource(res);
166 /// IoTConnectivityServerManager.UnregisterResource(res);
167 /// } catch(Exception ex) {
168 /// Console.Log("Exception caught : " + ex.Message);
171 public static void UnregisterResource(Resource resource)
173 if (resource != null)
175 if (resource.ResourceHandle != IntPtr.Zero)
177 Interop.IoTConnectivity.Server.Resource.Destroy(resource.ResourceHandle);
180 _resources.Remove(resource);
185 /// Starts presence of a server
188 /// Use this API to send server's announcements to clients.
189 /// Server can call this API when online for the first time or come back from offline to online.\n
190 /// If @a time is 0, server will set default value as 60 seconds.\n
191 /// If @a time is very big, server will set maximum value as (60 * 60 * 24) seconds, (24 hours).
194 /// http://tizen.org/privilege/internet
196 /// <param name="time">The interval of announcing presence in seconds.</param>
198 /// Initialize() should be called to initialize.
200 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
201 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
202 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
203 /// <seealso cref="StopSendingPresence()"/>
206 /// IoTConnectivityServerManager.StartSendingPresence(120);
207 /// } catch(Exception ex) {
208 /// Console.Log("Exception caught : " + ex.Message);
211 public static void StartSendingPresence(uint time)
213 int ret = Interop.IoTConnectivity.Server.IoTCon.StartPresence(time);
214 if (ret != (int)IoTConnectivityError.None)
216 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to start presence");
217 throw IoTConnectivityErrorFactory.GetException(ret);
222 /// Stops presence of a server.
225 /// Use this API to stop sending server's announcements to clients.
226 /// Server can call this API when terminating, entering to offline or out of network.
229 /// http://tizen.org/privilege/internet
232 /// Initialize() should be called to initialize.
234 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
235 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
236 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
237 /// <seealso cref="StartSendingPresence()"/>
239 /// IoTConnectivityServerManager.StopSendingPresence();
241 public static void StopSendingPresence()
243 int ret = Interop.IoTConnectivity.Server.IoTCon.StopPresence();
244 if (ret != (int)IoTConnectivityError.None)
246 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed cancel presence");
247 throw IoTConnectivityErrorFactory.GetException(ret);
252 /// Sets the device name
255 /// This API sets the name of the local device (the device calling the API).\n
256 /// If the device name is set, clients can get the name using <see cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>.
258 /// <param name="deviceName">The device name</param>
259 /// <seealso cref="IoTConnectivityClientManager.DeviceInformationFound"/>
260 /// <seealso cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>
261 /// <seealso cref="DeviceInformationFoundEventArgs"/>
263 /// IoTConnectivityServerManager.SetDeviceName("my-tizen");
265 public static void SetDeviceName(string deviceName)
267 int ret = Interop.IoTConnectivity.Server.IoTCon.SetDeviceName(deviceName);
268 if (ret != (int)IoTConnectivityError.None)
270 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed set device name");
271 throw IoTConnectivityErrorFactory.GetException(ret);
274 private static List<Resource> _resources = new List<Resource>();