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 interfaces and provides APIs to manage, add, or remove those interfaces.
27 /// A resource interface indicates a class or category of resources.
29 /// <since_tizen> 3 </since_tizen>
30 public class ResourceInterfaces : IEnumerable<string>, IDisposable
33 /// Default Interface.
35 /// <since_tizen> 3 </since_tizen>
36 public const string DefaultInterface = "oic.if.baseline";
39 /// List Links Interface, which is used to list the references to other resources contained in a resource.
41 /// <since_tizen> 3 </since_tizen>
42 public const string LinkInterface = "oic.if.ll";
45 /// Batch Interface, which is used to manipulate (GET, PUT, POST, DELETE) on other resource contained in a resource.
47 /// <since_tizen> 3 </since_tizen>
48 public const string BatchInterface = "oic.if.b";
51 /// Group Interface, which is used to manipulate (GET, PUT, POST) a group of remote resources.
53 /// <since_tizen> 3 </since_tizen>
54 public const string GroupInterface = "oic.mi.grp";
57 /// Read-Only Interface, which is used to limit the methods that can be applied to a resource to GET only.
59 /// <since_tizen> 3 </since_tizen>
60 public const string ReadonlyInterface = "oic.if.r";
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;
68 /// Constructor of ResourceInterfaces.
70 /// <since_tizen> 3 </since_tizen>
71 /// <feature>http://tizen.org/feature/iot.ocf</feature>
72 /// <seealso cref="Add(string)"/>
73 /// <seealso cref="Remove(string)"/>
74 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
75 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory.</exception>
77 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
79 public ResourceInterfaces()
81 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
82 if (ret != (int)IoTConnectivityError.None)
84 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
85 throw IoTConnectivityErrorFactory.GetException(ret);
90 /// Constructor of ResourceInterfaces using list of interfaces.
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>
99 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
100 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
102 public ResourceInterfaces(IEnumerable<string> ifaces)
104 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
105 if (ret != (int)IoTConnectivityError.None)
107 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
108 throw IoTConnectivityErrorFactory.GetException(ret);
110 foreach (string iface in ifaces)
116 internal ResourceInterfaces(IntPtr ifacesHandleToClone)
118 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Clone(ifacesHandleToClone, out _resourceInterfacesHandle);
119 if (ret != (int)IoTConnectivityError.None)
121 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
122 throw IoTConnectivityErrorFactory.GetException(ret);
125 Interop.IoTConnectivity.Common.ResourceInterfaces.ForeachCallback cb = (string iface, IntPtr data) =>
127 _resourceInterfaces.Add(iface);
131 ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Foreach(ifacesHandleToClone, cb, IntPtr.Zero);
132 if (ret != (int)IoTConnectivityError.None)
134 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
135 throw IoTConnectivityErrorFactory.GetException(ret);
140 /// Destructor of the ResourceInterfaces class.
142 ~ResourceInterfaces()
147 internal IntPtr ResourceInterfacesHandle
151 return _resourceInterfacesHandle;
156 /// Indicates count of interfaces in the list
158 /// <since_tizen> 3 </since_tizen>
159 /// <value>Count of interfaces in the list.</value>
161 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
162 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
163 /// Console.WriteLine("There are {0} interfaces", resourceInterfaces.Count);
169 return _resourceInterfaces.Count;
174 /// Adds a resource interface into the list.
176 /// <since_tizen> 3 </since_tizen>
178 /// <paramref name="item" /> could be a value, such as <see cref="DefaultInterface"/>.
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(string)"/>
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>
187 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
188 /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
190 public void Add(string item)
194 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Add(_resourceInterfacesHandle, item);
195 if (ret != (int)IoTConnectivityError.None)
197 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
198 throw IoTConnectivityErrorFactory.GetException(ret);
200 _resourceInterfaces.Add(item);
204 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid interface");
205 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
210 /// Removes a resource interface from the list.
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(string)"/>
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>
220 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.BatchInterface });
221 /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
223 public void Remove(string item)
225 bool isRemoved = _resourceInterfaces.Remove(item);
228 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Remove(_resourceInterfacesHandle, item);
229 if (ret != (int)IoTConnectivityError.None)
231 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove interface");
232 throw IoTConnectivityErrorFactory.GetException(ret);
236 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
240 /// Returns enumerator for the list of interfaces.
242 /// <since_tizen> 3 </since_tizen>
243 /// <returns>The enumerator.</returns>
245 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
246 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
247 /// foreach(string item in resourceInterfaces)
249 /// Console.WriteLine("Interface : {0}", item);
252 public IEnumerator<string> GetEnumerator()
254 return _resourceInterfaces.GetEnumerator();
258 /// Returns enumerator for the list of interfaces.
260 /// <since_tizen> 3 </since_tizen>
261 /// <returns>The enumerator.</returns>
263 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
264 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
265 /// foreach(string item in resourceInterfaces)
267 /// Console.WriteLine("Interface : {0}", item);
270 IEnumerator IEnumerable.GetEnumerator()
272 return _resourceInterfaces.GetEnumerator();
276 /// Releases any unmanaged resources used by this object.
278 /// <since_tizen> 3 </since_tizen>
279 /// <feature>http://tizen.org/feature/iot.ocf</feature>
280 public void Dispose()
283 GC.SuppressFinalize(this);
286 internal static bool IsValid(string type)
288 Regex r = new Regex("^[a-zA-Z0-9.-]+$");
289 return (type.Length <= MaxLength && type.Length > 0 && char.IsLower(type[0]) && r.IsMatch(type));
293 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
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)
305 // Free managed objects
308 Interop.IoTConnectivity.Common.ResourceInterfaces.Destroy(_resourceInterfacesHandle);