Update content of nuspec file
[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         /// <code>
67         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
68         /// </code>
69         public ResourceInterfaces()
70         {
71             int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
72             if (ret != (int)IoTConnectivityError.None)
73             {
74                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
75                 throw IoTConnectivityErrorFactory.GetException(ret);
76             }
77         }
78
79         /// <summary>
80         /// Constructor of ResourceInterfaces using list of interfaces
81         /// </summary>
82         /// <param name="ifaces">List of resource interfaces</param>
83         /// <code>
84         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
85         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
86         /// </code>
87         public ResourceInterfaces(IEnumerable<string> ifaces)
88         {
89             int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
90             if (ret != (int)IoTConnectivityError.None)
91             {
92                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
93                 throw IoTConnectivityErrorFactory.GetException(ret);
94             }
95             foreach (string iface in ifaces)
96             {
97                 Add(iface);
98             }
99         }
100
101         internal ResourceInterfaces(IntPtr ifacesHandleToClone)
102         {
103             int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Clone(ifacesHandleToClone, out _resourceInterfacesHandle);
104             if (ret != (int)IoTConnectivityError.None)
105             {
106                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
107                 throw IoTConnectivityErrorFactory.GetException(ret);
108             }
109
110             Interop.IoTConnectivity.Common.ResourceInterfaces.ForeachCallback cb = (string iface, IntPtr data) =>
111             {
112                 _resourceInterfaces.Add(iface);
113                 return true;
114             };
115
116             ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Foreach(ifacesHandleToClone, cb, IntPtr.Zero);
117             if (ret != (int)IoTConnectivityError.None)
118             {
119                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
120                 throw IoTConnectivityErrorFactory.GetException(ret);
121             }
122         }
123
124         /// <summary>
125         /// Destructor of the ResourceInterfaces class.
126         /// </summary>
127         ~ResourceInterfaces()
128         {
129             Dispose(false);
130         }
131
132         internal IntPtr ResourceInterfacesHandle
133         {
134             get
135             {
136                 return _resourceInterfacesHandle;
137             }
138         }
139
140         /// <summary>
141         /// Indicates count of interfaces in the list
142         /// </summary>
143         /// <code>
144         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
145         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
146         /// Console.WriteLine("There are {0} interfaces", resourceInterfaces.Count);
147         /// </code>
148         public int Count
149         {
150             get
151             {
152                 return _resourceInterfaces.Count;
153             }
154         }
155
156         /// <summary>
157         /// Adds a resource interface into the list.
158         /// </summary>
159         /// <remarks>
160         /// @a item could be a value such as <see cref="DefaultInterface"/>
161         /// </remarks>
162         /// <param name="item">The string data to insert into the resource interfaces</param>
163         /// <seealso cref="Remove()"/>
164         /// <code>
165         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
166         /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
167         /// </code>
168         public void Add(string item)
169         {
170             if (IsValid(item))
171             {
172                 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Add(_resourceInterfacesHandle, item);
173                 if (ret != (int)IoTConnectivityError.None)
174                 {
175                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
176                     throw IoTConnectivityErrorFactory.GetException(ret);
177                 }
178                 _resourceInterfaces.Add(item);
179             }
180             else
181             {
182                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid interface");
183                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
184             }
185         }
186
187         /// <summary>
188         /// Removes a resource interface from the list
189         /// </summary>
190         /// <param name="item">The string data to delete from the resource ifaces</param>
191         /// <seealso cref="Add()"/>
192         /// <code>
193         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.BatchInterface });
194         /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
195         /// </code>
196         public void Remove(string item)
197         {
198             bool isRemoved = _resourceInterfaces.Remove(item);
199             if (isRemoved)
200             {
201                 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Remove(_resourceInterfacesHandle, item);
202                 if (ret != (int)IoTConnectivityError.None)
203                 {
204                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
205                     throw IoTConnectivityErrorFactory.GetException(ret);
206                 }
207             }
208             else
209                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
210         }
211
212         /// <summary>
213         /// Return enumerator for the list of interfaces
214         /// </summary>
215         /// <returns>The enumerator</returns>
216         /// <code>
217         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
218         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
219         /// foreach(string item in resourceInterfaces)
220         /// {
221         ///     Console.WriteLine("Interface : {0}", item);
222         /// }
223         /// </code>
224         public IEnumerator<string> GetEnumerator()
225         {
226             return _resourceInterfaces.GetEnumerator();
227         }
228
229         /// <summary>
230         /// Return enumerator for the list of interfaces
231         /// </summary>
232         /// <returns>The enumerator</returns>
233         /// <code>
234         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
235         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
236         /// foreach(string item in resourceInterfaces)
237         /// {
238         ///     Console.WriteLine("Interface : {0}", item);
239         /// }
240         /// </code>
241         IEnumerator IEnumerable.GetEnumerator()
242         {
243             return _resourceInterfaces.GetEnumerator();
244         }
245
246         /// <summary>
247         /// Releases any unmanaged resources used by this object.
248         /// </summary>
249         public void Dispose()
250         {
251             Dispose(true);
252             GC.SuppressFinalize(this);
253         }
254
255         internal static bool IsValid(string type)
256         {
257             Regex r = new Regex("^[a-zA-Z0-9.-]+$");
258             return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
259         }
260
261         /// <summary>
262         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
263         /// </summary>
264         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
265         protected virtual void Dispose(bool disposing)
266         {
267             if (_disposed)
268                 return;
269
270             if (disposing)
271             {
272                 // Free managed objects
273             }
274
275             Interop.IoTConnectivity.Common.ResourceInterfaces.Destroy(_resourceInterfacesHandle);
276             _disposed = true;
277         }
278     }
279 }