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, remove those interfaces.
27 /// A resource interface indicates a class or category of resources.
29 public class ResourceInterfaces : IEnumerable<string>, IDisposable
34 public const string DefaultInterface = "oic.if.baseline";
37 /// List Links Interface which is used to list the references to other resources contained in a resource.
39 public const string LinkInterface = "oic.if.ll";
42 /// Batch Interface which is used to manipulate (GET, PUT, POST, DELETE) on other resource contained in a resource.
44 public const string BatchInterface = "oic.if.b";
47 /// Group Interface which is used to manipulate (GET, PUT, POST) a group of remote resources.
49 public const string GroupInterface = "oic.mi.grp";
52 /// Read-Only Interface which is used to limit the methods that can be applied to a resource to GET only.
54 public const string ReadonlyInterface = "oic.if.r";
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;
62 /// Constructor of ResourceInterfaces
64 /// <seealso cref="Add()"/>
65 /// <seealso cref="Remove()"/>
66 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
67 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
69 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
71 public ResourceInterfaces()
73 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
74 if (ret != (int)IoTConnectivityError.None)
76 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
77 throw IoTConnectivityErrorFactory.GetException(ret);
82 /// Constructor of ResourceInterfaces using list of interfaces
84 /// <param name="ifaces">List of resource interfaces</param>
85 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
86 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
87 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
89 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
90 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
92 public ResourceInterfaces(IEnumerable<string> ifaces)
94 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
95 if (ret != (int)IoTConnectivityError.None)
97 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
98 throw IoTConnectivityErrorFactory.GetException(ret);
100 foreach (string iface in ifaces)
106 internal ResourceInterfaces(IntPtr ifacesHandleToClone)
108 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Clone(ifacesHandleToClone, out _resourceInterfacesHandle);
109 if (ret != (int)IoTConnectivityError.None)
111 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
112 throw IoTConnectivityErrorFactory.GetException(ret);
115 Interop.IoTConnectivity.Common.ResourceInterfaces.ForeachCallback cb = (string iface, IntPtr data) =>
117 _resourceInterfaces.Add(iface);
121 ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Foreach(ifacesHandleToClone, cb, IntPtr.Zero);
122 if (ret != (int)IoTConnectivityError.None)
124 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
125 throw IoTConnectivityErrorFactory.GetException(ret);
130 /// Destructor of the ResourceInterfaces class.
132 ~ResourceInterfaces()
137 internal IntPtr ResourceInterfacesHandle
141 return _resourceInterfacesHandle;
146 /// Indicates count of interfaces in the list
149 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
150 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
151 /// Console.WriteLine("There are {0} interfaces", resourceInterfaces.Count);
157 return _resourceInterfaces.Count;
162 /// Adds a resource interface into the list.
165 /// @a item could be a value such as <see cref="DefaultInterface"/>
167 /// <param name="item">The string data to insert into the resource interfaces</param>
168 /// <seealso cref="Remove()"/>
169 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
170 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
171 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
173 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
174 /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
176 public void Add(string item)
180 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Add(_resourceInterfacesHandle, item);
181 if (ret != (int)IoTConnectivityError.None)
183 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
184 throw IoTConnectivityErrorFactory.GetException(ret);
186 _resourceInterfaces.Add(item);
190 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid interface");
191 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
196 /// Removes a resource interface from the list
198 /// <param name="item">The string data to delete from the resource ifaces</param>
199 /// <seealso cref="Add()"/>
200 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
201 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
202 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
204 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.BatchInterface });
205 /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
207 public void Remove(string item)
209 bool isRemoved = _resourceInterfaces.Remove(item);
212 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Remove(_resourceInterfacesHandle, item);
213 if (ret != (int)IoTConnectivityError.None)
215 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove interface");
216 throw IoTConnectivityErrorFactory.GetException(ret);
220 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
224 /// Return enumerator for the list of interfaces
226 /// <returns>The enumerator</returns>
228 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
229 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
230 /// foreach(string item in resourceInterfaces)
232 /// Console.WriteLine("Interface : {0}", item);
235 public IEnumerator<string> GetEnumerator()
237 return _resourceInterfaces.GetEnumerator();
241 /// Return enumerator for the list of interfaces
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 IEnumerator IEnumerable.GetEnumerator()
254 return _resourceInterfaces.GetEnumerator();
258 /// Releases any unmanaged resources used by this object.
260 public void Dispose()
263 GC.SuppressFinalize(this);
266 internal static bool IsValid(string type)
268 Regex r = new Regex("^[a-zA-Z0-9.-]+$");
269 return (type.Length <= MaxLength && type.Length > 0 && char.IsLower(type[0]) && r.IsMatch(type));
273 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
275 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
276 protected virtual void Dispose(bool disposing)
283 // Free managed objects
286 Interop.IoTConnectivity.Common.ResourceInterfaces.Destroy(_resourceInterfacesHandle);