2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Text.RegularExpressions;
23 namespace Tizen.Network.IoTConnectivity
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.
29 /// <since_tizen> 3 </since_tizen>
30 public class ResourceTypes : IEnumerable<string>, IDisposable
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;
38 /// Constructor of ResourceTypes.
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>
47 /// ResourceTypes types = new ResourceTypes();
49 public ResourceTypes()
51 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
52 if (ret != (int)IoTConnectivityError.None)
54 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
55 throw IoTConnectivityErrorFactory.GetException(ret);
60 /// Constructor of ResourceTypes using list of types.
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>
67 /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
69 public ResourceTypes(IEnumerable<string> types)
71 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
72 if (ret != (int)IoTConnectivityError.None)
74 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
75 throw IoTConnectivityErrorFactory.GetException(ret);
78 foreach (string type in types)
84 internal ResourceTypes(IntPtr typesHandleToClone)
86 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle);
87 if (ret != (int)IoTConnectivityError.None)
89 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
90 throw IoTConnectivityErrorFactory.GetException(ret);
93 Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) =>
95 _resourceTypes.Add(type);
99 ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero);
100 if (ret != (int)IoTConnectivityError.None)
102 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
103 throw IoTConnectivityErrorFactory.GetException(ret);
108 /// Destructor of the ResourceTypes class.
116 /// Indicates count of types in the list.
118 /// <since_tizen> 3 </since_tizen>
119 /// <value>Count of types in the list.</value>
121 /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
122 /// Console.WriteLine("There are {0} items", types.Count);
128 return _resourceTypes.Count;
133 /// Adds a resource type into the list.
135 /// <since_tizen> 3 </since_tizen>
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.
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>
149 /// ResourceTypes resourceTypes = new ResourceTypes();
150 /// resourceTypes.Add("org.tizen.light");
152 public void Add(string item)
156 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item);
157 if (ret != (int)IoTConnectivityError.None)
159 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type");
160 throw IoTConnectivityErrorFactory.GetException(ret);
162 _resourceTypes.Add(item);
166 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
167 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
172 /// Removes a resource type from the list.
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>
182 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
183 /// resourceTypes.Remove("oic.if.room");
185 public void Remove(string item)
187 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item);
188 if (ret != (int)IoTConnectivityError.None)
190 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type");
191 throw IoTConnectivityErrorFactory.GetException(ret);
194 _resourceTypes.Remove(item);
198 /// Returns an enumerator for the list of types.
200 /// <since_tizen> 3 </since_tizen>
201 /// <returns>The enumerator.</returns>
203 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
204 /// foreach(string item in resourceTypes)
206 /// Console.WriteLine("Type : {0}", item);
209 public IEnumerator<string> GetEnumerator()
211 return _resourceTypes.GetEnumerator();
215 /// Returns an enumerator for the list of types.
217 /// <since_tizen> 3 </since_tizen>
218 /// <returns>The enumerator.</returns>
220 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
221 /// foreach(string item in resourceTypes)
223 /// Console.WriteLine("Type : {0}", item);
226 IEnumerator IEnumerable.GetEnumerator()
228 return _resourceTypes.GetEnumerator();
232 /// Releases any unmanaged resources used by this object.
234 /// <since_tizen> 3 </since_tizen>
235 /// <feature>http://tizen.org/feature/iot.ocf</feature>
236 public void Dispose()
239 GC.SuppressFinalize(this);
242 internal static bool IsValid(string type)
244 Regex r = new Regex("^[a-zA-Z0-9.-]+$");
245 return (type.Length <= MaxLength && type.Length > 0 && char.IsLower(type[0]) && r.IsMatch(type));
249 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
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)
261 // Free managed objects
264 Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle);