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()"/>
48 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
49 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
50 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
52 /// string filePath = "../../res/iotcon-test-svr-db-server.dat";
53 /// IoTConnectivityServerManager.Initialize(filePath);
55 public static void Initialize(string filePath)
57 int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath);
58 if (ret != (int)IoTConnectivityError.None)
60 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
61 throw IoTConnectivityErrorFactory.GetException(ret);
66 /// Deinitializes IoTCon.
69 /// This API must be called if IoTCon API is no longer needed.
72 /// Initialize() should be called to initialize.
74 /// <seealso cref="Initialize()"/>
76 /// IoTConnectivityServerManager.Deinitialize();
78 public static void Deinitialize()
82 s_RequestHandlerCallbackMap.Clear();
84 Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
88 /// Registers a resource in IoTCon server
91 /// http://tizen.org/privilege/internet
93 /// <param name="resource">The resource to register</param>
95 /// Initialize() should be called to initialize.
97 /// <seealso cref="Resource"/>
98 /// <seealso cref="LiteResource"/>
99 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
100 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
101 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
102 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
103 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
105 /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
106 /// Attributes attributes = new Attributes { { "state", "ON" }};
107 /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
109 /// IoTConnectivityServerManager.RegisterResource(res);
110 /// } catch(Exception ex) {
111 /// Console.Log("Exception caught : " + ex.Message);
114 public static void RegisterResource(Resource resource)
116 Log.Info(IoTConnectivityErrorFactory.LogTag, "...");
118 IntPtr id = IntPtr.Zero;
119 lock (s_RequestHandlerCallbackMap)
121 id = (IntPtr)s_requestId++;
124 s_RequestHandlerCallbackMap[id] = (IntPtr r_resource, IntPtr request, IntPtr userData) =>
126 int requestId = (int)userData;
128 Log.Info(IoTConnectivityErrorFactory.LogTag, "Received s_RequestHandlerCallbackMap : " + requestId);
130 if (request == IntPtr.Zero)
132 Log.Error(IoTConnectivityErrorFactory.LogTag, "request is IntPtr.Zero");
135 resource.OnRequest(r_resource, request, userData);
138 IntPtr handle = IntPtr.Zero;
139 int errorCode = Interop.IoTConnectivity.Server.Resource.Create(resource.UriPath, resource.Types._resourceTypeHandle, resource.Interfaces.ResourceInterfacesHandle, (int)resource.Policy, s_RequestHandlerCallbackMap[id], id, out handle);
140 if (errorCode != (int)IoTConnectivityError.None)
142 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed create resource");
143 lock (s_RequestHandlerCallbackMap)
145 s_RequestHandlerCallbackMap.Remove(id);
147 throw IoTConnectivityErrorFactory.GetException(errorCode);
151 resource.ResourceHandle = handle;
153 _resources.Add(resource);
157 /// Unregisters a resource in IoTCon server
160 /// http://tizen.org/privilege/internet
162 /// <param name="resource">The resource to unregister</param>
164 /// Initialize() should be called to initialize.
166 /// <seealso cref="Resource"/>
167 /// <seealso cref="LiteResource"/>
168 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
169 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
171 /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
172 /// Attributes attributes = new Attributes { { "state", "ON" }};
173 /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
174 /// IoTConnectivityServerManager.RegisterResource(res);
176 /// IoTConnectivityServerManager.UnregisterResource(res);
177 /// } catch(Exception ex) {
178 /// Console.Log("Exception caught : " + ex.Message);
181 public static void UnregisterResource(Resource resource)
183 if (resource != null)
185 if (resource.ResourceHandle != IntPtr.Zero)
187 Interop.IoTConnectivity.Server.Resource.Destroy(resource.ResourceHandle);
188 resource.ResourceHandle = IntPtr.Zero;
191 _resources.Remove(resource);
196 /// Starts presence of a server
199 /// Use this API to send server's announcements to clients.
200 /// Server can call this API when online for the first time or come back from offline to online.\n
201 /// If @a time is 0, server will set default value as 60 seconds.\n
202 /// If @a time is very big, server will set maximum value as (60 * 60 * 24) seconds, (24 hours).
205 /// http://tizen.org/privilege/internet
207 /// <param name="time">The interval of announcing presence in seconds.</param>
209 /// Initialize() should be called to initialize.
211 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
212 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
213 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
214 /// <seealso cref="StopSendingPresence()"/>
215 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
216 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
217 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
220 /// IoTConnectivityServerManager.StartSendingPresence(120);
221 /// } catch(Exception ex) {
222 /// Console.Log("Exception caught : " + ex.Message);
225 public static void StartSendingPresence(uint time)
227 int ret = Interop.IoTConnectivity.Server.IoTCon.StartPresence(time);
228 if (ret != (int)IoTConnectivityError.None)
230 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to start presence");
231 throw IoTConnectivityErrorFactory.GetException(ret);
236 /// Stops presence of a server.
239 /// Use this API to stop sending server's announcements to clients.
240 /// Server can call this API when terminating, entering to offline or out of network.
243 /// http://tizen.org/privilege/internet
246 /// Initialize() should be called to initialize.
248 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
249 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
250 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
251 /// <seealso cref="StartSendingPresence()"/>
252 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
253 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
254 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
256 /// IoTConnectivityServerManager.StopSendingPresence();
258 public static void StopSendingPresence()
260 int ret = Interop.IoTConnectivity.Server.IoTCon.StopPresence();
261 if (ret != (int)IoTConnectivityError.None)
263 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed cancel presence");
264 throw IoTConnectivityErrorFactory.GetException(ret);
269 /// Sets the device name
272 /// This API sets the name of the local device (the device calling the API).\n
273 /// If the device name is set, clients can get the name using <see cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>.
275 /// <param name="deviceName">The device name</param>
276 /// <seealso cref="IoTConnectivityClientManager.DeviceInformationFound"/>
277 /// <seealso cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>
278 /// <seealso cref="DeviceInformationFoundEventArgs"/>
279 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
280 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
281 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access</exception>
283 /// IoTConnectivityServerManager.SetDeviceName("my-tizen");
285 public static void SetDeviceName(string deviceName)
287 int ret = Interop.IoTConnectivity.Server.IoTCon.SetDeviceName(deviceName);
288 if (ret != (int)IoTConnectivityError.None)
290 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed set device name");
291 throw IoTConnectivityErrorFactory.GetException(ret);
294 private static List<Resource> _resources = new List<Resource>();