3975644cf8d560c7c50ac5b195ccee1975a6642b
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / ResourceInterfaces.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
18 using System;
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Text.RegularExpressions;
22
23 namespace Tizen.Network.IoTConnectivity
24 {
25     /// <summary>
26     /// This class contains resource interfaces and provides APIs to manage, add, remove those interfaces.
27     /// A resource interface indicates a class or category of resources.
28     /// </summary>
29     public class ResourceInterfaces : IEnumerable<string>, IDisposable
30     {
31         /// <summary>
32         /// Default Interface
33         /// </summary>
34         public const string DefaultInterface = "oic.if.baseline";
35
36         /// <summary>
37         /// List Links Interface which is used to list the references to other resources contained in a resource.
38         /// </summary>
39         public const string LinkInterface = "oic.if.ll";
40
41         /// <summary>
42         /// Batch Interface which is used to manipulate (GET, PUT, POST, DELETE) on other resource contained in a resource.
43         /// </summary>
44         public const string BatchInterface = "oic.if.b";
45
46         /// <summary>
47         /// Group Interface which is used to manipulate (GET, PUT, POST) a group of remote resources.
48         /// </summary>
49         public const string GroupInterface = "oic.mi.grp";
50
51         /// <summary>
52         /// Read-Only Interface which is used to limit the methods that can be applied to a resource to GET only.
53         /// </summary>
54         public const string ReadonlyInterface = "oic.if.r";
55
56         private readonly IntPtr _resourceInterfacesHandle = IntPtr.Zero;
57         private const int MaxLength = 61;
58         private readonly HashSet<string> _resourceInterfaces = new HashSet<string>();
59         private bool _disposed = false;
60
61         /// <summary>
62         /// Constructor of ResourceInterfaces
63         /// </summary>
64         /// <seealso cref="Add()"/>
65         /// <seealso cref="Remove()"/>
66         /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
67         /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
68         /// <code>
69         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
70         /// </code>
71         public ResourceInterfaces()
72         {
73             int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
74             if (ret != (int)IoTConnectivityError.None)
75             {
76                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
77                 throw IoTConnectivityErrorFactory.GetException(ret);
78             }
79         }
80
81         /// <summary>
82         /// Constructor of ResourceInterfaces using list of interfaces
83         /// </summary>
84         /// <param name="ifaces">List of resource interfaces</param>
85         /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
86         /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
87         /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
88         /// <code>
89         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
90         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
91         /// </code>
92         public ResourceInterfaces(IEnumerable<string> ifaces)
93         {
94             int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
95             if (ret != (int)IoTConnectivityError.None)
96             {
97                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
98                 throw IoTConnectivityErrorFactory.GetException(ret);
99             }
100             foreach (string iface in ifaces)
101             {
102                 Add(iface);
103             }
104         }
105
106         internal ResourceInterfaces(IntPtr ifacesHandleToClone)
107         {
108             int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Clone(ifacesHandleToClone, out _resourceInterfacesHandle);
109             if (ret != (int)IoTConnectivityError.None)
110             {
111                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
112                 throw IoTConnectivityErrorFactory.GetException(ret);
113             }
114
115             Interop.IoTConnectivity.Common.ResourceInterfaces.ForeachCallback cb = (string iface, IntPtr data) =>
116             {
117                 _resourceInterfaces.Add(iface);
118                 return true;
119             };
120
121             ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Foreach(ifacesHandleToClone, cb, IntPtr.Zero);
122             if (ret != (int)IoTConnectivityError.None)
123             {
124                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
125                 throw IoTConnectivityErrorFactory.GetException(ret);
126             }
127         }
128
129         /// <summary>
130         /// Destructor of the ResourceInterfaces class.
131         /// </summary>
132         ~ResourceInterfaces()
133         {
134             Dispose(false);
135         }
136
137         internal IntPtr ResourceInterfacesHandle
138         {
139             get
140             {
141                 return _resourceInterfacesHandle;
142             }
143         }
144
145         /// <summary>
146         /// Indicates count of interfaces in the list
147         /// </summary>
148         /// <code>
149         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
150         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
151         /// Console.WriteLine("There are {0} interfaces", resourceInterfaces.Count);
152         /// </code>
153         public int Count
154         {
155             get
156             {
157                 return _resourceInterfaces.Count;
158             }
159         }
160
161         /// <summary>
162         /// Adds a resource interface into the list.
163         /// </summary>
164         /// <remarks>
165         /// @a item could be a value such as <see cref="DefaultInterface"/>
166         /// </remarks>
167         /// <param name="item">The string data to insert into the resource interfaces</param>
168         /// <seealso cref="Remove()"/>
169         /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
170         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
171         /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
172         /// <code>
173         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
174         /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
175         /// </code>
176         public void Add(string item)
177         {
178             if (IsValid(item))
179             {
180                 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Add(_resourceInterfacesHandle, item);
181                 if (ret != (int)IoTConnectivityError.None)
182                 {
183                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
184                     throw IoTConnectivityErrorFactory.GetException(ret);
185                 }
186                 _resourceInterfaces.Add(item);
187             }
188             else
189             {
190                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid interface");
191                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
192             }
193         }
194
195         /// <summary>
196         /// Removes a resource interface from the list
197         /// </summary>
198         /// <param name="item">The string data to delete from the resource ifaces</param>
199         /// <seealso cref="Add()"/>
200         /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
201         /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
202         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
203         /// <code>
204         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.BatchInterface });
205         /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
206         /// </code>
207         public void Remove(string item)
208         {
209             bool isRemoved = _resourceInterfaces.Remove(item);
210             if (isRemoved)
211             {
212                 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Remove(_resourceInterfacesHandle, item);
213                 if (ret != (int)IoTConnectivityError.None)
214                 {
215                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove interface");
216                     throw IoTConnectivityErrorFactory.GetException(ret);
217                 }
218             }
219             else
220                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
221         }
222
223         /// <summary>
224         /// Return enumerator for the list of interfaces
225         /// </summary>
226         /// <returns>The enumerator</returns>
227         /// <code>
228         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
229         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
230         /// foreach(string item in resourceInterfaces)
231         /// {
232         ///     Console.WriteLine("Interface : {0}", item);
233         /// }
234         /// </code>
235         public IEnumerator<string> GetEnumerator()
236         {
237             return _resourceInterfaces.GetEnumerator();
238         }
239
240         /// <summary>
241         /// Return enumerator for the list of interfaces
242         /// </summary>
243         /// <returns>The enumerator</returns>
244         /// <code>
245         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
246         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
247         /// foreach(string item in resourceInterfaces)
248         /// {
249         ///     Console.WriteLine("Interface : {0}", item);
250         /// }
251         /// </code>
252         IEnumerator IEnumerable.GetEnumerator()
253         {
254             return _resourceInterfaces.GetEnumerator();
255         }
256
257         /// <summary>
258         /// Releases any unmanaged resources used by this object.
259         /// </summary>
260         public void Dispose()
261         {
262             Dispose(true);
263             GC.SuppressFinalize(this);
264         }
265
266         internal static bool IsValid(string type)
267         {
268             Regex r = new Regex("^[a-zA-Z0-9.-]+$");
269             return (type.Length <= MaxLength && type.Length > 0 && char.IsLower(type[0]) && r.IsMatch(type));
270         }
271
272         /// <summary>
273         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
274         /// </summary>
275         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
276         protected virtual void Dispose(bool disposing)
277         {
278             if (_disposed)
279                 return;
280
281             if (disposing)
282             {
283                 // Free managed objects
284             }
285
286             Interop.IoTConnectivity.Common.ResourceInterfaces.Destroy(_resourceInterfacesHandle);
287             _disposed = true;
288         }
289     }
290 }