Remove device name in remote resource
[platform/core/csapi/iotcon.git] / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / IoTConnectivityServerManager.cs
1  /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19
20 namespace Tizen.Network.IoTConnectivity
21 {
22     /// <summary>
23     /// IoT connectivity server manager consists of server side APIs.
24     /// </summary>
25     public static class IoTConnectivityServerManager
26     {
27
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>();
30
31         /// <summary>
32         /// Initializes IoTCon. Call this API to start IoTCon.
33         /// </summary>
34         /// <remarks>
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.
38         /// </remarks>
39         /// <privilege>
40         /// http://tizen.org/privilege/network.get \n
41         /// http://tizen.org/privilege/internet
42         /// </privilege>
43         /// <param name="filePath">The file path to point to storage for handling secure virtual resources.</param>
44         /// <post>
45         /// You must call Deinitialize() if IoTCon API is no longer needed.
46         /// </post>
47         /// <seealso cref="Deinitialize()"/>
48         /// <code>
49         /// string filePath = "../../res/iotcon-test-svr-db-server.dat";
50         /// IoTConnectivityServerManager.Initialize(filePath);
51         /// </code>
52         public static void Initialize(string filePath)
53         {
54             int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath);
55             if (ret != (int)IoTConnectivityError.None)
56             {
57                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
58                 throw IoTConnectivityErrorFactory.GetException(ret);
59             }
60         }
61
62         /// <summary>
63         /// Deinitializes IoTCon.
64         /// </summary>
65         /// <remarks>
66         /// This API must be called if IoTCon API is no longer needed.
67         /// </remarks>
68         /// <pre>
69         /// Initialize() should be called to initialize.
70         /// </pre>
71         /// <seealso cref="Initialize()"/>
72         /// <code>
73         /// IoTConnectivityServerManager.Deinitialize();
74         /// </code>
75         public static void Deinitialize()
76         {
77             _resources.Clear();
78             s_requestId = 1;
79             s_RequestHandlerCallbackMap.Clear();
80
81             Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
82         }
83
84         /// <summary>
85         /// Registers a resource in IoTCon server
86         /// </summary>
87         /// <privilege>
88         /// http://tizen.org/privilege/internet
89         /// </privilege>
90         /// <param name="resource">The resource to register</param>
91         /// <pre>
92         /// Initialize() should be called to initialize.
93         /// </pre>
94         /// <seealso cref="Resource"/>
95         /// <seealso cref="LiteResource"/>
96         /// <code>
97         /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
98         /// Attributes attributes = new Attributes { { "state", "ON" }};
99         /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
100         /// try {
101         ///     IoTConnectivityServerManager.RegisterResource(res);
102         /// } catch(Exception ex) {
103         ///     Console.Log("Exception caught : " + ex.Message);
104         /// }
105         /// </code>
106         public static void RegisterResource(Resource resource)
107         {
108             Log.Error(IoTConnectivityErrorFactory.LogTag, "...");
109
110             IntPtr id = IntPtr.Zero;
111             lock (s_RequestHandlerCallbackMap)
112             {
113                 id = (IntPtr)s_requestId++;
114             }
115
116             s_RequestHandlerCallbackMap[id] = (IntPtr r_resource, IntPtr request, IntPtr userData) =>
117             {
118                 int requestId = (int)userData;
119
120                 Log.Info(IoTConnectivityErrorFactory.LogTag, "Received s_RequestHandlerCallbackMap : " + requestId);
121
122                 if (request == null)
123                 {
124                     Log.Error(IoTConnectivityErrorFactory.LogTag, "request is null");
125                     return;
126                 }
127                 resource.OnRequest(r_resource, request, userData);
128             };
129
130             IntPtr handle = IntPtr.Zero;
131             int errorCode = Interop.IoTConnectivity.Server.Resource.Create(resource.UriPath, resource.Types._resourceTypeHandle, resource.Interfaces.ResourceInterfacesHandle, (int)resource.Policy, s_RequestHandlerCallbackMap[id], id, out handle);
132             if (errorCode != (int)IoTConnectivityError.None)
133             {
134                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed create resource");
135                 lock (s_RequestHandlerCallbackMap)
136                 {
137                     s_RequestHandlerCallbackMap.Remove(id);
138                 }
139                 throw IoTConnectivityErrorFactory.GetException(errorCode);
140             }
141             else
142             {
143                 resource.ResourceHandle = handle;
144             }
145             _resources.Add(resource);
146         }
147
148         /// <summary>
149         /// Unregisters a resource in IoTCon server
150         /// </summary>
151         /// <privilege>
152         /// http://tizen.org/privilege/internet
153         /// </privilege>
154         /// <param name="resource">The resource to unregister</param>
155         /// <pre>
156         /// Initialize() should be called to initialize.
157         /// </pre>
158         /// <seealso cref="Resource"/>
159         /// <seealso cref="LiteResource"/>
160         /// <code>
161         /// ResourceTypes types = new ResourceTypes(new List<string>(){ "org.tizen.light" });
162         /// Attributes attributes = new Attributes { { "state", "ON" }};
163         /// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
164         /// IoTConnectivityServerManager.RegisterResource(res);
165         /// try {
166         ///     IoTConnectivityServerManager.UnregisterResource(res);
167         /// } catch(Exception ex) {
168         ///     Console.Log("Exception caught : " + ex.Message);
169         /// }
170         /// </code>
171         public static void UnregisterResource(Resource resource)
172         {
173             if (resource != null)
174             {
175                 if (resource.ResourceHandle != IntPtr.Zero)
176                 {
177                     Interop.IoTConnectivity.Server.Resource.Destroy(resource.ResourceHandle);
178                 }
179
180                 _resources.Remove(resource);
181             }
182         }
183
184         /// <summary>
185         /// Starts presence of a server
186         /// </summary>
187         /// <remarks>
188         /// Use this API to send server's announcements to clients.
189         /// Server can call this API when online for the first time or come back from offline to online.\n
190         /// If @a time is 0, server will set default value as 60 seconds.\n
191         /// If @a time is very big, server will set maximum value as (60 * 60 * 24) seconds, (24 hours).
192         /// </remarks>
193         /// <privilege>
194         /// http://tizen.org/privilege/internet
195         /// </privilege>
196         /// <param name="time">The interval of announcing presence in seconds.</param>
197         /// <pre>
198         /// Initialize() should be called to initialize.
199         /// </pre>
200         /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
201         /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
202         /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
203         /// <seealso cref="StopSendingPresence()"/>
204         /// <code>
205         /// try {
206         ///     IoTConnectivityServerManager.StartSendingPresence(120);
207         /// } catch(Exception ex) {
208         ///     Console.Log("Exception caught : " + ex.Message);
209         /// }
210         /// </code>
211         public static void StartSendingPresence(uint time)
212         {
213             int ret = Interop.IoTConnectivity.Server.IoTCon.StartPresence(time);
214             if (ret != (int)IoTConnectivityError.None)
215             {
216                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to start presence");
217                 throw IoTConnectivityErrorFactory.GetException(ret);
218             }
219         }
220
221         /// <summary>
222         /// Stops presence of a server.
223         /// </summary>
224         /// <remarks>
225         /// Use this API to stop sending server's announcements to clients.
226         /// Server can call this API when terminating, entering to offline or out of network.
227         /// </remarks>
228         /// <privilege>
229         /// http://tizen.org/privilege/internet
230         /// </privilege>
231         /// <pre>
232         /// Initialize() should be called to initialize.
233         /// </pre>
234         /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
235         /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
236         /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
237         /// <seealso cref="StartSendingPresence()"/>
238         /// <code>
239         /// IoTConnectivityServerManager.StopSendingPresence();
240         /// </code>
241         public static void StopSendingPresence()
242         {
243             int ret = Interop.IoTConnectivity.Server.IoTCon.StopPresence();
244             if (ret != (int)IoTConnectivityError.None)
245             {
246                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed cancel presence");
247                 throw IoTConnectivityErrorFactory.GetException(ret);
248             }
249         }
250
251         /// <summary>
252         /// Sets the device name
253         /// </summary>
254         /// <remarks>
255         /// This API sets the name of the local device (the device calling the API).\n
256         /// If the device name is set, clients can get the name using <see cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>.
257         /// </remarks>
258         /// <param name="deviceName">The device name</param>
259         /// <seealso cref="IoTConnectivityClientManager.DeviceInformationFound"/>
260         /// <seealso cref="IoTConnectivityClientManager.StartFindingDeviceInformation()"/>
261         /// <seealso cref="DeviceInformationFoundEventArgs"/>
262         /// <code>
263         /// IoTConnectivityServerManager.SetDeviceName("my-tizen");
264         /// </code>
265         public static void SetDeviceName(string deviceName)
266         {
267             int ret = Interop.IoTConnectivity.Server.IoTCon.SetDeviceName(deviceName);
268             if (ret != (int)IoTConnectivityError.None)
269             {
270                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed set device name");
271                 throw IoTConnectivityErrorFactory.GetException(ret);
272             }
273         }
274         private static List<Resource> _resources = new List<Resource>();
275     }
276 }