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 /// <since_tizen> 3 </since_tizen>
26 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. Calls this API to start IoTCon.
34 /// <since_tizen> 3 </since_tizen>
36 /// @a filePath point to a file for handling secure virtual resources.
37 /// The file that is CBOR(Concise Binary Object Representation)-format must already exist
38 /// in @a filePath. We recommend to use application-local file for @a filePath.
41 /// http://tizen.org/privilege/network.get \n
42 /// http://tizen.org/privilege/internet
44 /// <privlevel>public</privlevel>
45 /// <param name="filePath">The file path to point to storage for handling secure virtual resources.</param>
46 /// <feature>http://tizen.org/feature/iot.ocf</feature>
48 /// You must call Deinitialize() if IoTCon API is no longer needed.
50 /// <seealso cref="Deinitialize()"/>
51 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
52 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter.</exception>
53 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have privilege to access</exception>
55 /// string filePath = "../../res/iotcon-test-svr-db-server.dat";
56 /// IoTConnectivityServerManager.Initialize(filePath);
58 public static void Initialize(string filePath)
60 int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath);
61 if (ret != (int)IoTConnectivityError.None)
63 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
64 throw IoTConnectivityErrorFactory.GetException(ret);
69 /// Deinitializes IoTCon.
71 /// <since_tizen> 3 </since_tizen>
73 /// This API must be called if IoTCon API is no longer needed.
75 /// <feature>http://tizen.org/feature/iot.ocf</feature>
77 /// Initialize() should be called to initialize.
79 /// <seealso cref="Initialize()"/>
81 /// IoTConnectivityServerManager.Deinitialize();
83 public static void Deinitialize()
87 s_RequestHandlerCallbackMap.Clear();
89 Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
93 /// Registers a resource in IoTCon server.
95 /// <since_tizen> 3 </since_tizen>
97 /// http://tizen.org/privilege/internet
99 /// <privlevel>public</privlevel>
100 /// <param name="resource">The resource to register.</param>
101 /// <feature>http://tizen.org/feature/iot.ocf</feature>
103 /// Initialize() should be called to initialize.
105 /// <seealso cref="Resource"/>
106 /// <seealso cref="LiteResource"/>
107 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
108 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter.</exception>
109 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid.</exception>
110 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory.</exception>
111 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have privilege to access.</exception>
113 /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
114 /// Attributes attributes = new Attributes { { "state", "ON" }};
115 /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
117 /// IoTConnectivityServerManager.RegisterResource(res);
118 /// } catch(Exception ex) {
119 /// Console.Log("Exception caught : " + ex.Message);
122 public static void RegisterResource(Resource resource)
124 Log.Info(IoTConnectivityErrorFactory.LogTag, "...");
126 IntPtr id = IntPtr.Zero;
127 lock (s_RequestHandlerCallbackMap)
129 id = (IntPtr)s_requestId++;
132 s_RequestHandlerCallbackMap[id] = (IntPtr r_resource, IntPtr request, IntPtr userData) =>
134 int requestId = (int)userData;
136 Log.Info(IoTConnectivityErrorFactory.LogTag, "Received s_RequestHandlerCallbackMap : " + requestId);
138 if (request == IntPtr.Zero)
140 Log.Error(IoTConnectivityErrorFactory.LogTag, "request is IntPtr.Zero");
143 resource.OnRequest(r_resource, request, userData);
146 IntPtr handle = IntPtr.Zero;
147 int errorCode = Interop.IoTConnectivity.Server.Resource.Create(resource.UriPath, resource.Types._resourceTypeHandle, resource.Interfaces.ResourceInterfacesHandle, (int)resource.Policy, s_RequestHandlerCallbackMap[id], id, out handle);
148 if (errorCode != (int)IoTConnectivityError.None)
150 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed create resource");
151 lock (s_RequestHandlerCallbackMap)
153 s_RequestHandlerCallbackMap.Remove(id);
155 throw IoTConnectivityErrorFactory.GetException(errorCode);
159 resource.ResourceHandle = handle;
161 _resources.Add(resource);
165 /// Unregisters a resource in IoTCon server.
167 /// <since_tizen> 3 </since_tizen>
169 /// http://tizen.org/privilege/internet
171 /// <privlevel>public</privlevel>
172 /// <param name="resource">The resource to unregister.</param>
173 /// <feature>http://tizen.org/feature/iot.ocf</feature>
175 /// Initialize() should be called to initialize.
177 /// <seealso cref="Resource"/>
178 /// <seealso cref="LiteResource"/>
179 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
180 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have privilege to access.</exception>
182 /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
183 /// Attributes attributes = new Attributes { { "state", "ON" }};
184 /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
185 /// IoTConnectivityServerManager.RegisterResource(res);
187 /// IoTConnectivityServerManager.UnregisterResource(res);
188 /// } catch(Exception ex) {
189 /// Console.Log("Exception caught : " + ex.Message);
192 public static void UnregisterResource(Resource resource)
194 if (resource != null)
196 if (resource.ResourceHandle != IntPtr.Zero)
198 Interop.IoTConnectivity.Server.Resource.Destroy(resource.ResourceHandle);
199 resource.ResourceHandle = IntPtr.Zero;
202 _resources.Remove(resource);
207 /// Starts presence of a server.
209 /// <since_tizen> 3 </since_tizen>
211 /// Use this API to send server's announcements to clients.
212 /// Server can call this API when online for the first time or come back from offline to online.\n
213 /// If @a time is 0, server will set default value as 60 seconds.\n
214 /// If @a time is very big, server will set maximum value as (60 * 60 * 24) seconds, (24 hours).
217 /// http://tizen.org/privilege/internet
219 /// <privlevel>public</privlevel>
220 /// <param name="time">The interval of announcing presence in seconds.</param>
221 /// <feature>http://tizen.org/feature/iot.ocf</feature>
223 /// Initialize() should be called to initialize.
225 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
226 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
227 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
228 /// <seealso cref="StopSendingPresence()"/>
229 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
230 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid.</exception>
231 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have privilege to access.</exception>
234 /// IoTConnectivityServerManager.StartSendingPresence(120);
235 /// } catch(Exception ex) {
236 /// Console.Log("Exception caught : " + ex.Message);
239 public static void StartSendingPresence(uint time)
241 int ret = Interop.IoTConnectivity.Server.IoTCon.StartPresence(time);
242 if (ret != (int)IoTConnectivityError.None)
244 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to start presence");
245 throw IoTConnectivityErrorFactory.GetException(ret);
250 /// Stops presence of a server.
252 /// <since_tizen> 3 </since_tizen>
254 /// Use this API to stop sending server's announcements to clients.
255 /// Server can call this API when terminating, entering to offline or out of network.
258 /// http://tizen.org/privilege/internet
260 /// <privlevel>public</privlevel>
261 /// <feature>http://tizen.org/feature/iot.ocf</feature>
263 /// Initialize() should be called to initialize.
265 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
266 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
267 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
268 /// <seealso cref="StartSendingPresence()"/>
269 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
270 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid.</exception>
271 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have privilege to access.</exception>
273 /// IoTConnectivityServerManager.StopSendingPresence();
275 public static void StopSendingPresence()
277 int ret = Interop.IoTConnectivity.Server.IoTCon.StopPresence();
278 if (ret != (int)IoTConnectivityError.None)
280 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed cancel presence");
281 throw IoTConnectivityErrorFactory.GetException(ret);
286 /// Sets the device name.
288 /// <since_tizen> 3 </since_tizen>
290 /// This API sets the name of the local device (the device calling the API).\n
291 /// If the device name is set, clients can get the name using <see cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>.
293 /// <param name="deviceName">The device name.</param>
294 /// <feature>http://tizen.org/feature/iot.ocf</feature>
295 /// <seealso cref="IoTConnectivityClientManager.DeviceInformationFound"/>
296 /// <seealso cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>
297 /// <seealso cref="DeviceInformationFoundEventArgs"/>
298 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
299 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid.</exception>
300 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have privilege to access.</exception>
302 /// IoTConnectivityServerManager.SetDeviceName("my-tizen");
304 public static void SetDeviceName(string deviceName)
306 int ret = Interop.IoTConnectivity.Server.IoTCon.SetDeviceName(deviceName);
307 if (ret != (int)IoTConnectivityError.None)
309 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed set device name");
310 throw IoTConnectivityErrorFactory.GetException(ret);
313 private static List<Resource> _resources = new List<Resource>();