Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / ResourceTypes.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 types and provides APIs to manage, add, or remove those types.
27     /// A resource type indicates a class or a category of resources.
28     /// </summary>
29     /// <since_tizen> 3 </since_tizen>
30     public class ResourceTypes : IEnumerable<string>, IDisposable
31     {
32         internal const int MaxLength = 61;
33         internal IntPtr _resourceTypeHandle = IntPtr.Zero;
34         private readonly HashSet<string> _resourceTypes = new HashSet<string>();
35         private bool _disposed = false;
36
37         /// <summary>
38         /// Constructor of ResourceTypes.
39         /// </summary>
40         /// <since_tizen> 3 </since_tizen>
41         /// <feature>http://tizen.org/feature/iot.ocf</feature>
42         /// <seealso cref="Add()"/>
43         /// <seealso cref="Remove()"/>
44         /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
45         /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory.</exception>
46         /// <code>
47         /// ResourceTypes types = new ResourceTypes();
48         /// </code>
49         public ResourceTypes()
50         {
51             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
52             if (ret != (int)IoTConnectivityError.None)
53             {
54                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
55                 throw IoTConnectivityErrorFactory.GetException(ret);
56             }
57         }
58
59         /// <summary>
60         /// Constructor of ResourceTypes using list of types.
61         /// </summary>
62         /// <since_tizen> 3 </since_tizen>
63         /// <param name="types">List of resource types.</param>
64         /// <feature>http://tizen.org/feature/iot.ocf</feature>
65         /// <exception cref="ArgumentException">Thrown when there is an invalid parameter.</exception>
66         /// <code>
67         /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
68         /// </code>
69         public ResourceTypes(IEnumerable<string> types)
70         {
71             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
72             if (ret != (int)IoTConnectivityError.None)
73             {
74                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
75                 throw IoTConnectivityErrorFactory.GetException(ret);
76             }
77
78             foreach (string type in types)
79             {
80                 Add(type);
81             }
82         }
83
84         internal ResourceTypes(IntPtr typesHandleToClone)
85         {
86             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle);
87             if (ret != (int)IoTConnectivityError.None)
88             {
89                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
90                 throw IoTConnectivityErrorFactory.GetException(ret);
91             }
92
93             Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) =>
94             {
95                 _resourceTypes.Add(type);
96                 return true;
97             };
98
99             ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero);
100             if (ret != (int)IoTConnectivityError.None)
101             {
102                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
103                 throw IoTConnectivityErrorFactory.GetException(ret);
104             }
105         }
106
107         /// <summary>
108         /// Destructor of the ResourceTypes class.
109         /// </summary>
110         ~ResourceTypes()
111         {
112             Dispose(false);
113         }
114
115         /// <summary>
116         /// Indicates count of types in the list.
117         /// </summary>
118         /// <since_tizen> 3 </since_tizen>
119         /// <value>Count of types in the list.</value>
120         /// <code>
121         /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
122         /// Console.WriteLine("There are {0} items", types.Count);
123         /// </code>
124         public int Count
125         {
126             get
127             {
128                 return _resourceTypes.Count;
129             }
130         }
131
132         /// <summary>
133         /// Adds a resource type into the list.
134         /// </summary>
135         /// <since_tizen> 3 </since_tizen>
136         /// <remarks>
137         /// The length of @a item should be less than or equal to 61.\n
138         /// The @a item must start with a lowercase alphabetic character, followed by a sequence
139         /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.\n
140         /// Duplicate strings are not allowed.
141         /// </remarks>
142         /// <param name="item">The string data to insert into the resource types.</param>
143         /// <feature>http://tizen.org/feature/iot.ocf</feature>
144         /// <seealso cref="Remove()"/>
145         /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
146         /// <exception cref="ArgumentException">Thrown when there is an invalid parameter.</exception>
147         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid.</exception>
148         /// <code>
149         /// ResourceTypes resourceTypes = new ResourceTypes();
150         /// resourceTypes.Add("org.tizen.light");
151         /// </code>
152         public void Add(string item)
153         {
154             if (IsValid(item))
155             {
156                 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item);
157                 if (ret != (int)IoTConnectivityError.None)
158                 {
159                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type");
160                     throw IoTConnectivityErrorFactory.GetException(ret);
161                 }
162                 _resourceTypes.Add(item);
163             }
164             else
165             {
166                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
167                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
168             }
169         }
170
171         /// <summary>
172         /// Removes a resource type from the list.
173         /// </summary>
174         /// <since_tizen> 3 </since_tizen>
175         /// <param name="item">The string data to delete from the resource types.</param>
176         /// <feature>http://tizen.org/feature/iot.ocf</feature>
177         /// <seealso cref="Add()"/>
178         /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
179         /// <exception cref="ArgumentException">Thrown when there is an invalid parameter.</exception>
180         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid.</exception>
181         /// <code>
182         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
183         /// resourceTypes.Remove("oic.if.room");
184         /// </code>
185         public void Remove(string item)
186         {
187             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item);
188             if (ret != (int)IoTConnectivityError.None)
189             {
190                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type");
191                 throw IoTConnectivityErrorFactory.GetException(ret);
192             }
193
194             _resourceTypes.Remove(item);
195         }
196
197         /// <summary>
198         /// Returns an enumerator for the list of types.
199         /// </summary>
200         /// <since_tizen> 3 </since_tizen>
201         /// <returns>The enumerator.</returns>
202         /// <code>
203         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
204         /// foreach(string item in resourceTypes)
205         /// {
206         ///     Console.WriteLine("Type : {0}", item);
207         /// }
208         /// </code>
209         public IEnumerator<string> GetEnumerator()
210         {
211             return _resourceTypes.GetEnumerator();
212         }
213
214         /// <summary>
215         /// Returns an enumerator for the list of types.
216         /// </summary>
217         /// <since_tizen> 3 </since_tizen>
218         /// <returns>The enumerator.</returns>
219         /// <code>
220         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
221         /// foreach(string item in resourceTypes)
222         /// {
223         ///     Console.WriteLine("Type : {0}", item);
224         /// }
225         /// </code>
226         IEnumerator IEnumerable.GetEnumerator()
227         {
228             return _resourceTypes.GetEnumerator();
229         }
230
231         /// <summary>
232         /// Releases any unmanaged resources used by this object.
233         /// </summary>
234         /// <since_tizen> 3 </since_tizen>
235         /// <feature>http://tizen.org/feature/iot.ocf</feature>
236         public void Dispose()
237         {
238             Dispose(true);
239             GC.SuppressFinalize(this);
240         }
241
242         internal static bool IsValid(string type)
243         {
244             Regex r = new Regex("^[a-zA-Z0-9.-]+$");
245             return (type.Length <= MaxLength && type.Length > 0 && char.IsLower(type[0]) && r.IsMatch(type));
246         }
247
248         /// <summary>
249         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
250         /// </summary>
251         /// <since_tizen> 3 </since_tizen>
252         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
253         /// <feature>http://tizen.org/feature/iot.ocf</feature>
254         protected virtual void Dispose(bool disposing)
255         {
256             if (_disposed)
257                 return;
258
259             if (disposing)
260             {
261                 // Free managed objects
262             }
263
264             Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle);
265             _disposed = true;
266         }
267     }
268 }