1 /// Copyright 2016 by Samsung Electronics, Inc.,
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Text.RegularExpressions;
14 namespace Tizen.Network.IoTConnectivity
17 /// This class contains resource interfaces and provides APIs to manage, add, remove those interfaces.
18 /// A resource interface indicates a class or category of resources.
20 public class ResourceInterfaces : IEnumerable<string>, IDisposable
25 public const string DefaultInterface = "oic.if.baseline";
28 /// List Links Interface which is used to list the references to other resources contained in a resource.
30 public const string LinkInterface = "oic.if.ll";
33 /// Batch Interface which is used to manipulate (GET, PUT, POST, DELETE) on other resource contained in a resource.
35 public const string BatchInterface = "oic.if.b";
38 /// Group Interface which is used to manipulate (GET, PUT, POST) a group of remote resources.
40 public const string GroupInterface = "oic.mi.grp";
43 /// Read-Only Interface which is used to limit the methods that can be applied to a resource to GET only.
45 public const string ReadonlyInterface = "oic.if.r";
47 private readonly IntPtr _resourceInterfacesHandle = IntPtr.Zero;
48 private const int MaxLength = 61;
49 private readonly HashSet<string> _resourceInterfaces = new HashSet<string>();
50 private bool _disposed = false;
53 /// Constructor of ResourceInterfaces
55 /// <seealso cref="Add()"/>
56 /// <seealso cref="Remove()"/>
58 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
60 public ResourceInterfaces()
62 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
63 if (ret != (int)IoTConnectivityError.None)
65 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
66 throw IoTConnectivityErrorFactory.GetException(ret);
71 /// Constructor of ResourceInterfaces using list of interfaces
73 /// <param name="ifaces">List of resource interfaces</param>
75 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
76 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
78 public ResourceInterfaces(IEnumerable<string> ifaces)
80 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
81 if (ret != (int)IoTConnectivityError.None)
83 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
84 throw IoTConnectivityErrorFactory.GetException(ret);
86 foreach (string iface in ifaces)
92 internal ResourceInterfaces(IntPtr ifacesHandleToClone)
94 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Clone(ifacesHandleToClone, out _resourceInterfacesHandle);
95 if (ret != (int)IoTConnectivityError.None)
97 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
98 throw IoTConnectivityErrorFactory.GetException(ret);
101 Interop.IoTConnectivity.Common.ResourceInterfaces.ForeachCallback cb = (string iface, IntPtr data) =>
103 _resourceInterfaces.Add(iface);
107 ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Foreach(ifacesHandleToClone, cb, IntPtr.Zero);
108 if (ret != (int)IoTConnectivityError.None)
110 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
111 throw IoTConnectivityErrorFactory.GetException(ret);
116 /// Destructor of the ResourceInterfaces class.
118 ~ResourceInterfaces()
123 internal IntPtr ResourceInterfacesHandle
127 return _resourceInterfacesHandle;
132 /// Indicates count of interfaces in the list
135 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
136 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
137 /// Console.WriteLine("There are {0} interfaces", resourceInterfaces.Count);
143 return _resourceInterfaces.Count;
148 /// Adds a resource interface into the list.
151 /// @a item could be a value such as <see cref="DefaultInterface"/>
153 /// <param name="item">The string data to insert into the resource interfaces</param>
154 /// <seealso cref="Remove()"/>
156 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
157 /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
159 public void Add(string item)
163 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Add(_resourceInterfacesHandle, item);
164 if (ret != (int)IoTConnectivityError.None)
166 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
167 throw IoTConnectivityErrorFactory.GetException(ret);
169 _resourceInterfaces.Add(item);
173 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid interface");
174 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
179 /// Removes a resource interface from the list
181 /// <param name="item">The string data to delete from the resource ifaces</param>
182 /// <seealso cref="Add()"/>
184 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.BatchInterface });
185 /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
187 public void Remove(string item)
189 bool isRemoved = _resourceInterfaces.Remove(item);
192 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Remove(_resourceInterfacesHandle, item);
193 if (ret != (int)IoTConnectivityError.None)
195 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
196 throw IoTConnectivityErrorFactory.GetException(ret);
200 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
204 /// Return enumerator for the list of interfaces
206 /// <returns>The enumerator</returns>
208 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
209 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
210 /// foreach(string item in resourceInterfaces)
212 /// Console.WriteLine("Interface : {0}", item);
215 public IEnumerator<string> GetEnumerator()
217 return _resourceInterfaces.GetEnumerator();
221 /// Return enumerator for the list of interfaces
223 /// <returns>The enumerator</returns>
225 /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
226 /// { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
227 /// foreach(string item in resourceInterfaces)
229 /// Console.WriteLine("Interface : {0}", item);
232 IEnumerator IEnumerable.GetEnumerator()
234 return _resourceInterfaces.GetEnumerator();
238 /// Releases any unmanaged resources used by this object.
240 public void Dispose()
243 GC.SuppressFinalize(this);
246 internal static bool IsValid(string type)
248 Regex r = new Regex("^[a-zA-Z0-9.-]+$");
249 return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
253 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
255 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
256 protected virtual void Dispose(bool disposing)
263 // Free managed objects
266 Interop.IoTConnectivity.Common.ResourceInterfaces.Destroy(_resourceInterfacesHandle);