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