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.
19 namespace Tizen.Network.IoTConnectivity
22 /// IoT connectivity server manager consists of server side APIs.
24 public static class IoTConnectivityServerManager
27 /// Initializes IoTCon. Call this API to start IoTCon.
30 /// @a filePath point to a file for handling secure virtual resources.
31 /// The file that is CBOR(Concise Binary Object Representation)-format must already exist
32 /// in @a filePath. We recommend to use application-local file for @a filePath.
35 /// http://tizen.org/privilege/network.get \n
36 /// http://tizen.org/privilege/internet
38 /// <param name="filePath">The file path to point to storage for handling secure virtual resources.</param>
40 /// You must call Deinitialize() if IoTCon API is no longer needed.
42 /// <seealso cref="Deinitialize()"/>
44 /// string filePath = "../../res/iotcon-test-svr-db-server.dat";
45 /// IoTConnectivityServerManager.Initialize(filePath);
47 public static void Initialize(string filePath)
49 int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath);
50 if (ret != (int)IoTConnectivityError.None)
52 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
53 throw IoTConnectivityErrorFactory.GetException(ret);
58 /// Deinitializes IoTCon.
61 /// This API must be called if IoTCon API is no longer needed.
64 /// Initialize() should be called to initialize.
66 /// <seealso cref="Initialize()"/>
68 /// IoTConnectivityServerManager.Deinitialize();
70 public static void Deinitialize()
72 Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
76 /// Registers a resource in IoTCon server
79 /// http://tizen.org/privilege/internet
81 /// <param name="resource">The resource to register</param>
83 /// Initialize() should be called to initialize.
85 /// <seealso cref="Resource"/>
86 /// <seealso cref="LiteResource"/>
88 /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
89 /// Attributes attributes = new Attributes { { "state", "ON" }};
90 /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
92 /// IoTConnectivityServerManager.RegisterResource(res);
93 /// } catch(Exception ex) {
94 /// Console.Log("Exception caught : " + ex.Message);
97 public static void RegisterResource(Resource resource)
99 IntPtr handle = IntPtr.Zero;
100 int ret = Interop.IoTConnectivity.Server.Resource.Create(resource.UriPath, resource.Types._resourceTypeHandle, resource.Interfaces.ResourceInterfacesHandle, (int)resource.Policy, resource.OnRequest, IntPtr.Zero, out handle);
101 if (ret != (int)IoTConnectivityError.None)
103 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed create resource");
104 throw IoTConnectivityErrorFactory.GetException(ret);
108 resource.ResourceHandle = handle;
113 /// Starts presence of a server
116 /// Use this API to send server's announcements to clients.
117 /// Server can call this API when online for the first time or come back from offline to online.\n
118 /// If @a time is 0, server will set default value as 60 seconds.\n
119 /// If @a time is very big, server will set maximum value as (60 * 60 * 24) seconds, (24 hours).
122 /// http://tizen.org/privilege/internet
124 /// <param name="time">The interval of announcing presence in seconds.</param>
126 /// Initialize() should be called to initialize.
128 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
129 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
130 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
131 /// <seealso cref="StopSendingPresence()"/>
134 /// IoTConnectivityServerManager.StartSendingPresence(120);
135 /// } catch(Exception ex) {
136 /// Console.Log("Exception caught : " + ex.Message);
139 public static void StartSendingPresence(uint time)
141 int ret = Interop.IoTConnectivity.Server.IoTCon.StartPresence(time);
142 if (ret != (int)IoTConnectivityError.None)
144 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to start presence");
145 throw IoTConnectivityErrorFactory.GetException(ret);
150 /// Stops presence of a server.
153 /// Use this API to stop sending server's announcements to clients.
154 /// Server can call this API when terminating, entering to offline or out of network.
157 /// http://tizen.org/privilege/internet
160 /// Initialize() should be called to initialize.
162 /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
163 /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
164 /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
165 /// <seealso cref="StartSendingPresence()"/>
167 /// IoTConnectivityServerManager.StopSendingPresence();
169 public static void StopSendingPresence()
171 int ret = Interop.IoTConnectivity.Server.IoTCon.StopPresence();
172 if (ret != (int)IoTConnectivityError.None)
174 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed cancel presence");
175 throw IoTConnectivityErrorFactory.GetException(ret);
180 /// Sets the device name
183 /// This API sets the name of the local device (the device calling the API).\n
184 /// If the device name is set, clients can get the name using <see cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>.
185 /// Clients can also get the name using <see cref="RemoteResource.DeviceName"/> property.
187 /// <param name="deviceName">The device name</param>
188 /// <seealso cref="IoTConnectivityClientManager.DeviceInformationFound"/>
189 /// <seealso cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>
190 /// <seealso cref="DeviceInformationFoundEventArgs"/>
191 /// <seealso cref="RemoteResource.DeviceName"/>
193 /// IoTConnectivityServerManager.SetDeviceName("my-tizen");
195 public static void SetDeviceName(string deviceName)
197 int ret = Interop.IoTConnectivity.Server.IoTCon.SetDeviceName(deviceName);
198 if (ret != (int)IoTConnectivityError.None)
200 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed set device name");
201 throw IoTConnectivityErrorFactory.GetException(ret);