Fix TC fail bugs
[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         /// Starts presence of a server
117         /// </summary>
118         /// <remarks>
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).
123         /// </remarks>
124         /// <privilege>
125         /// http://tizen.org/privilege/internet
126         /// </privilege>
127         /// <param name="time">The interval of announcing presence in seconds.</param>
128         /// <pre>
129         /// Initialize() should be called to initialize.
130         /// </pre>
131         /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
132         /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
133         /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
134         /// <seealso cref="StopSendingPresence()"/>
135         /// <code>
136         /// try {
137         ///     IoTConnectivityServerManager.StartSendingPresence(120);
138         /// } catch(Exception ex) {
139         ///     Console.Log("Exception caught : " + ex.Message);
140         /// }
141         /// </code>
142         public static void StartSendingPresence(uint time)
143         {
144             int ret = Interop.IoTConnectivity.Server.IoTCon.StartPresence(time);
145             if (ret != (int)IoTConnectivityError.None)
146             {
147                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to start presence");
148                 throw IoTConnectivityErrorFactory.GetException(ret);
149             }
150         }
151
152         /// <summary>
153         /// Stops presence of a server.
154         /// </summary>
155         /// <remarks>
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.
158         /// </remarks>
159         /// <privilege>
160         /// http://tizen.org/privilege/internet
161         /// </privilege>
162         /// <pre>
163         /// Initialize() should be called to initialize.
164         /// </pre>
165         /// <seealso cref="IoTConnectivityClientManager.StartReceivingPresence()"/>
166         /// <seealso cref="IoTConnectivityClientManager.StopReceivingPresence()"/>
167         /// <seealso cref="IoTConnectivityClientManager.PresenceReceived"/>
168         /// <seealso cref="StartSendingPresence()"/>
169         /// <code>
170         /// IoTConnectivityServerManager.StopSendingPresence();
171         /// </code>
172         public static void StopSendingPresence()
173         {
174             int ret = Interop.IoTConnectivity.Server.IoTCon.StopPresence();
175             if (ret != (int)IoTConnectivityError.None)
176             {
177                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed cancel presence");
178                 throw IoTConnectivityErrorFactory.GetException(ret);
179             }
180         }
181
182         /// <summary>
183         /// Sets the device name
184         /// </summary>
185         /// <remarks>
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.
189         /// </remarks>
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"/>
195         /// <code>
196         /// IoTConnectivityServerManager.SetDeviceName("my-tizen");
197         /// </code>
198         public static void SetDeviceName(string deviceName)
199         {
200             int ret = Interop.IoTConnectivity.Server.IoTCon.SetDeviceName(deviceName);
201             if (ret != (int)IoTConnectivityError.None)
202             {
203                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed set device name");
204                 throw IoTConnectivityErrorFactory.GetException(ret);
205             }
206         }
207         private static List<Resource> _resources = new List<Resource>();
208     }
209 }