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 /// Initializes IoTCon. Call this API to start IoTCon.
31 /// @a filePath point to a file for handling secure virtual resources.
32 /// The file that is CBOR(Concise Binary Object Representation)-format must already exist
33 /// in @a filePath. We recommend to use application-local file for @a filePath.
36 /// http://tizen.org/privilege/network.get \n
37 /// http://tizen.org/privilege/internet
39 /// <param name="filePath">The file path to point to storage for handling secure virtual resources.</param>
41 /// You must call Deinitialize() if IoTCon API is no longer needed.
43 /// <seealso cref="Deinitialize()"/>
45 /// string filePath = "../../res/iotcon-test-svr-db-server.dat";
46 /// IoTConnectivityServerManager.Initialize(filePath);
48 public static void Initialize(string filePath)
50 int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath);
51 if (ret != (int)IoTConnectivityError.None)
53 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
54 throw IoTConnectivityErrorFactory.GetException(ret);
59 /// Deinitializes IoTCon.
62 /// This API must be called if IoTCon API is no longer needed.
65 /// Initialize() should be called to initialize.
67 /// <seealso cref="Initialize()"/>
69 /// IoTConnectivityServerManager.Deinitialize();
71 public static void Deinitialize()
74 Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
78 /// Registers a resource in IoTCon server
81 /// http://tizen.org/privilege/internet
83 /// <param name="resource">The resource to register</param>
85 /// Initialize() should be called to initialize.
87 /// <seealso cref="Resource"/>
88 /// <seealso cref="LiteResource"/>
90 /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
91 /// Attributes attributes = new Attributes { { "state", "ON" }};
92 /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
94 /// IoTConnectivityServerManager.RegisterResource(res);
95 /// } catch(Exception ex) {
96 /// Console.Log("Exception caught : " + ex.Message);
99 public static void RegisterResource(Resource resource)
101 IntPtr handle = IntPtr.Zero;
102 int ret = Interop.IoTConnectivity.Server.Resource.Create(resource.UriPath, resource.Types._resourceTypeHandle, resource.Interfaces.ResourceInterfacesHandle, (int)resource.Policy, resource.OnRequest, IntPtr.Zero, out handle);
103 if (ret != (int)IoTConnectivityError.None)
105 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed create resource");
106 throw IoTConnectivityErrorFactory.GetException(ret);
110 resource.ResourceHandle = handle;
112 _resources.Add(resource);
116 /// Unregisters a resource in IoTCon server
119 /// http://tizen.org/privilege/internet
121 /// <param name="resource">The resource to unregister</param>
123 /// Initialize() should be called to initialize.
125 /// <seealso cref="Resource"/>
126 /// <seealso cref="LiteResource"/>
128 /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
129 /// Attributes attributes = new Attributes { { "state", "ON" }};
130 /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
131 /// IoTConnectivityServerManager.RegisterResource(res);
133 /// IoTConnectivityServerManager.UnregisterResource(res);
134 /// } catch(Exception ex) {
135 /// Console.Log("Exception caught : " + ex.Message);
138 public static void UnregisterResource(Resource resource)
140 if (resource != null)
142 if (resource.ResourceHandle != IntPtr.Zero)
144 Interop.IoTConnectivity.Server.Resource.Destroy(resource.ResourceHandle);
147 _resources.Remove(resource);
152 /// Starts presence of a server
155 /// Use this API to send server's announcements to clients.
156 /// Server can call this API when online for the first time or come back from offline to online.\n
157 /// If @a time is 0, server will set default value as 60 seconds.\n
158 /// If @a time is very big, server will set maximum value as (60 * 60 * 24) seconds, (24 hours).
161 /// http://tizen.org/privilege/internet
163 /// <param name="time">The interval of announcing presence in seconds.</param>
165 /// Initialize() should be called to initialize.
167 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
168 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
169 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
170 /// <seealso cref="StopSendingPresence()"/>
173 /// IoTConnectivityServerManager.StartSendingPresence(120);
174 /// } catch(Exception ex) {
175 /// Console.Log("Exception caught : " + ex.Message);
178 public static void StartSendingPresence(uint time)
180 int ret = Interop.IoTConnectivity.Server.IoTCon.StartPresence(time);
181 if (ret != (int)IoTConnectivityError.None)
183 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to start presence");
184 throw IoTConnectivityErrorFactory.GetException(ret);
189 /// Stops presence of a server.
192 /// Use this API to stop sending server's announcements to clients.
193 /// Server can call this API when terminating, entering to offline or out of network.
196 /// http://tizen.org/privilege/internet
199 /// Initialize() should be called to initialize.
201 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
202 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
203 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
204 /// <seealso cref="StartSendingPresence()"/>
206 /// IoTConnectivityServerManager.StopSendingPresence();
208 public static void StopSendingPresence()
210 int ret = Interop.IoTConnectivity.Server.IoTCon.StopPresence();
211 if (ret != (int)IoTConnectivityError.None)
213 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed cancel presence");
214 throw IoTConnectivityErrorFactory.GetException(ret);
219 /// Sets the device name
222 /// This API sets the name of the local device (the device calling the API).\n
223 /// If the device name is set, clients can get the name using <see cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>.
224 /// Clients can also get the name using <see cref="RemoteResource.DeviceName"/> property.
226 /// <param name="deviceName">The device name</param>
227 /// <seealso cref="IoTConnectivityClientManager.DeviceInformationFound"/>
228 /// <seealso cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>
229 /// <seealso cref="DeviceInformationFoundEventArgs"/>
230 /// <seealso cref="RemoteResource.DeviceName"/>
232 /// IoTConnectivityServerManager.SetDeviceName("my-tizen");
234 public static void SetDeviceName(string deviceName)
236 int ret = Interop.IoTConnectivity.Server.IoTCon.SetDeviceName(deviceName);
237 if (ret != (int)IoTConnectivityError.None)
239 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed set device name");
240 throw IoTConnectivityErrorFactory.GetException(ret);
243 private static List<Resource> _resources = new List<Resource>();