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 /// Starts presence of a server
119 /// Use this API to send server's announcements to clients.
120 /// Server can call this API when online for the first time or come back from offline to online.\n
121 /// If @a time is 0, server will set default value as 60 seconds.\n
122 /// If @a time is very big, server will set maximum value as (60 * 60 * 24) seconds, (24 hours).
125 /// http://tizen.org/privilege/internet
127 /// <param name="time">The interval of announcing presence in seconds.</param>
129 /// Initialize() should be called to initialize.
131 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
132 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
133 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
134 /// <seealso cref="StopSendingPresence()"/>
137 /// IoTConnectivityServerManager.StartSendingPresence(120);
138 /// } catch(Exception ex) {
139 /// Console.Log("Exception caught : " + ex.Message);
142 public static void StartSendingPresence(uint time)
144 int ret = Interop.IoTConnectivity.Server.IoTCon.StartPresence(time);
145 if (ret != (int)IoTConnectivityError.None)
147 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to start presence");
148 throw IoTConnectivityErrorFactory.GetException(ret);
153 /// Stops presence of a server.
156 /// Use this API to stop sending server's announcements to clients.
157 /// Server can call this API when terminating, entering to offline or out of network.
160 /// http://tizen.org/privilege/internet
163 /// Initialize() should be called to initialize.
165 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
166 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
167 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
168 /// <seealso cref="StartSendingPresence()"/>
170 /// IoTConnectivityServerManager.StopSendingPresence();
172 public static void StopSendingPresence()
174 int ret = Interop.IoTConnectivity.Server.IoTCon.StopPresence();
175 if (ret != (int)IoTConnectivityError.None)
177 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed cancel presence");
178 throw IoTConnectivityErrorFactory.GetException(ret);
183 /// Sets the device name
186 /// This API sets the name of the local device (the device calling the API).\n
187 /// If the device name is set, clients can get the name using <see cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>.
188 /// Clients can also get the name using <see cref="RemoteResource.DeviceName"/> property.
190 /// <param name="deviceName">The device name</param>
191 /// <seealso cref="IoTConnectivityClientManager.DeviceInformationFound"/>
192 /// <seealso cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>
193 /// <seealso cref="DeviceInformationFoundEventArgs"/>
194 /// <seealso cref="RemoteResource.DeviceName"/>
196 /// IoTConnectivityServerManager.SetDeviceName("my-tizen");
198 public static void SetDeviceName(string deviceName)
200 int ret = Interop.IoTConnectivity.Server.IoTCon.SetDeviceName(deviceName);
201 if (ret != (int)IoTConnectivityError.None)
203 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed set device name");
204 throw IoTConnectivityErrorFactory.GetException(ret);
207 private static List<Resource> _resources = new List<Resource>();