Add UnregisterResource() C# API
[platform/core/csapi/tizenfx.git] / src / 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         /// <summary>
28         /// Initializes IoTCon. Call this API to start IoTCon.
29         /// </summary>
30         /// <remarks>
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.
34         /// </remarks>
35         /// <privilege>
36         /// http://tizen.org/privilege/network.get \n
37         /// http://tizen.org/privilege/internet
38         /// </privilege>
39         /// <param name="filePath">The file path to point to storage for handling secure virtual resources.</param>
40         /// <post>
41         /// You must call Deinitialize() if IoTCon API is no longer needed.
42         /// </post>
43         /// <seealso cref="Deinitialize()"/>
44         /// <code>
45         /// string filePath = "../../res/iotcon-test-svr-db-server.dat";
46         /// IoTConnectivityServerManager.Initialize(filePath);
47         /// </code>
48         public static void Initialize(string filePath)
49         {
50             int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath);
51             if (ret != (int)IoTConnectivityError.None)
52             {
53                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
54                 throw IoTConnectivityErrorFactory.GetException(ret);
55             }
56         }
57
58         /// <summary>
59         /// Deinitializes IoTCon.
60         /// </summary>
61         /// <remarks>
62         /// This API must be called if IoTCon API is no longer needed.
63         /// </remarks>
64         /// <pre>
65         /// Initialize() should be called to initialize.
66         /// </pre>
67         /// <seealso cref="Initialize()"/>
68         /// <code>
69         /// IoTConnectivityServerManager.Deinitialize();
70         /// </code>
71         public static void Deinitialize()
72         {
73             _resources.Clear();
74             Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
75         }
76
77         /// <summary>
78         /// Registers a resource in IoTCon server
79         /// </summary>
80         /// <privilege>
81         /// http://tizen.org/privilege/internet
82         /// </privilege>
83         /// <param name="resource">The resource to register</param>
84         /// <pre>
85         /// Initialize() should be called to initialize.
86         /// </pre>
87         /// <seealso cref="Resource"/>
88         /// <seealso cref="LiteResource"/>
89         /// <code>
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);
93         /// try {
94         ///     IoTConnectivityServerManager.RegisterResource(res);
95         /// } catch(Exception ex) {
96         ///     Console.Log("Exception caught : " + ex.Message);
97         /// }
98         /// </code>
99         public static void RegisterResource(Resource resource)
100         {
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)
104             {
105                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed create resource");
106                 throw IoTConnectivityErrorFactory.GetException(ret);
107             }
108             else
109             {
110                 resource.ResourceHandle = handle;
111             }
112             _resources.Add(resource);
113         }
114
115         /// <summary>
116         /// Unregisters a resource in IoTCon server
117         /// </summary>
118         /// <privilege>
119         /// http://tizen.org/privilege/internet
120         /// </privilege>
121         /// <param name="resource">The resource to unregister</param>
122         /// <pre>
123         /// Initialize() should be called to initialize.
124         /// </pre>
125         /// <seealso cref="Resource"/>
126         /// <seealso cref="LiteResource"/>
127         /// <code>
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);
132         /// try {
133         ///     IoTConnectivityServerManager.UnregisterResource(res);
134         /// } catch(Exception ex) {
135         ///     Console.Log("Exception caught : " + ex.Message);
136         /// }
137         /// </code>
138         public static void UnregisterResource(Resource resource)
139         {
140             if (resource != null)
141             {
142                 if (resource.ResourceHandle != IntPtr.Zero)
143                 {
144                     Interop.IoTConnectivity.Server.Resource.Destroy(resource.ResourceHandle);
145                 }
146
147                 _resources.Remove(resource);
148             }
149         }
150
151         /// <summary>
152         /// Starts presence of a server
153         /// </summary>
154         /// <remarks>
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).
159         /// </remarks>
160         /// <privilege>
161         /// http://tizen.org/privilege/internet
162         /// </privilege>
163         /// <param name="time">The interval of announcing presence in seconds.</param>
164         /// <pre>
165         /// Initialize() should be called to initialize.
166         /// </pre>
167         /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
168         /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
169         /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
170         /// <seealso cref="StopSendingPresence()"/>
171         /// <code>
172         /// try {
173         ///     IoTConnectivityServerManager.StartSendingPresence(120);
174         /// } catch(Exception ex) {
175         ///     Console.Log("Exception caught : " + ex.Message);
176         /// }
177         /// </code>
178         public static void StartSendingPresence(uint time)
179         {
180             int ret = Interop.IoTConnectivity.Server.IoTCon.StartPresence(time);
181             if (ret != (int)IoTConnectivityError.None)
182             {
183                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to start presence");
184                 throw IoTConnectivityErrorFactory.GetException(ret);
185             }
186         }
187
188         /// <summary>
189         /// Stops presence of a server.
190         /// </summary>
191         /// <remarks>
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.
194         /// </remarks>
195         /// <privilege>
196         /// http://tizen.org/privilege/internet
197         /// </privilege>
198         /// <pre>
199         /// Initialize() should be called to initialize.
200         /// </pre>
201         /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
202         /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
203         /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
204         /// <seealso cref="StartSendingPresence()"/>
205         /// <code>
206         /// IoTConnectivityServerManager.StopSendingPresence();
207         /// </code>
208         public static void StopSendingPresence()
209         {
210             int ret = Interop.IoTConnectivity.Server.IoTCon.StopPresence();
211             if (ret != (int)IoTConnectivityError.None)
212             {
213                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed cancel presence");
214                 throw IoTConnectivityErrorFactory.GetException(ret);
215             }
216         }
217
218         /// <summary>
219         /// Sets the device name
220         /// </summary>
221         /// <remarks>
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.
225         /// </remarks>
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"/>
231         /// <code>
232         /// IoTConnectivityServerManager.SetDeviceName("my-tizen");
233         /// </code>
234         public static void SetDeviceName(string deviceName)
235         {
236             int ret = Interop.IoTConnectivity.Server.IoTCon.SetDeviceName(deviceName);
237             if (ret != (int)IoTConnectivityError.None)
238             {
239                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed set device name");
240                 throw IoTConnectivityErrorFactory.GetException(ret);
241             }
242         }
243         private static List<Resource> _resources = new List<Resource>();
244     }
245 }