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()"/>
67 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
69 public ResourceInterfaces()
71 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
72 if (ret != (int)IoTConnectivityError.None)
74 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
75 throw IoTConnectivityErrorFactory.GetException(ret);
80 /// Constructor of ResourceInterfaces using list of interfaces
82 /// <param name="ifaces">List of resource interfaces</param>
84 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
85 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
87 public ResourceInterfaces(IEnumerable<string> ifaces)
89 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
90 if (ret != (int)IoTConnectivityError.None)
92 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
93 throw IoTConnectivityErrorFactory.GetException(ret);
95 foreach (string iface in ifaces)
101 internal ResourceInterfaces(IntPtr ifacesHandleToClone)
103 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Clone(ifacesHandleToClone, out _resourceInterfacesHandle);
104 if (ret != (int)IoTConnectivityError.None)
106 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
107 throw IoTConnectivityErrorFactory.GetException(ret);
110 Interop.IoTConnectivity.Common.ResourceInterfaces.ForeachCallback cb = (string iface, IntPtr data) =>
112 _resourceInterfaces.Add(iface);
116 ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Foreach(ifacesHandleToClone, cb, IntPtr.Zero);
117 if (ret != (int)IoTConnectivityError.None)
119 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
120 throw IoTConnectivityErrorFactory.GetException(ret);
125 /// Destructor of the ResourceInterfaces class.
127 ~ResourceInterfaces()
132 internal IntPtr ResourceInterfacesHandle
136 return _resourceInterfacesHandle;
141 /// Indicates count of interfaces in the list
144 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
145 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
146 /// Console.WriteLine("There are {0} interfaces", resourceInterfaces.Count);
152 return _resourceInterfaces.Count;
157 /// Adds a resource interface into the list.
160 /// @a item could be a value such as <see cref="DefaultInterface"/>
162 /// <param name="item">The string data to insert into the resource interfaces</param>
163 /// <seealso cref="Remove()"/>
165 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
166 /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
168 public void Add(string item)
172 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Add(_resourceInterfacesHandle, item);
173 if (ret != (int)IoTConnectivityError.None)
175 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
176 throw IoTConnectivityErrorFactory.GetException(ret);
178 _resourceInterfaces.Add(item);
182 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid interface");
183 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
188 /// Removes a resource interface from the list
190 /// <param name="item">The string data to delete from the resource ifaces</param>
191 /// <seealso cref="Add()"/>
193 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.BatchInterface });
194 /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
196 public void Remove(string item)
198 bool isRemoved = _resourceInterfaces.Remove(item);
201 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Remove(_resourceInterfacesHandle, item);
202 if (ret != (int)IoTConnectivityError.None)
204 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
205 throw IoTConnectivityErrorFactory.GetException(ret);
209 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
213 /// Return enumerator for the list of interfaces
215 /// <returns>The enumerator</returns>
217 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
218 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
219 /// foreach(string item in resourceInterfaces)
221 /// Console.WriteLine("Interface : {0}", item);
224 public IEnumerator<string> GetEnumerator()
226 return _resourceInterfaces.GetEnumerator();
230 /// Return enumerator for the list of interfaces
232 /// <returns>The enumerator</returns>
234 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
235 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
236 /// foreach(string item in resourceInterfaces)
238 /// Console.WriteLine("Interface : {0}", item);
241 IEnumerator IEnumerable.GetEnumerator()
243 return _resourceInterfaces.GetEnumerator();
247 /// Releases any unmanaged resources used by this object.
249 public void Dispose()
252 GC.SuppressFinalize(this);
255 internal static bool IsValid(string type)
257 Regex r = new Regex("^[a-zA-Z0-9.-]+$");
258 return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
262 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
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)
272 // Free managed objects
275 Interop.IoTConnectivity.Common.ResourceInterfaces.Destroy(_resourceInterfacesHandle);