28aea2ac2677ecfbda644ec7cde5df4056be9441
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / ResourceInterfaces.cs
1 /// Copyright 2016 by Samsung Electronics, Inc.,
2 ///
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.
8
9 using System;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Text.RegularExpressions;
13
14 namespace Tizen.Network.IoTConnectivity
15 {
16     /// <summary>
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.
19     /// </summary>
20     public class ResourceInterfaces : IEnumerable<string>, IDisposable
21     {
22         /// <summary>
23         /// Default Interface
24         /// </summary>
25         public const string DefaultInterface = "oic.if.baseline";
26
27         /// <summary>
28         /// List Links Interface which is used to list the references to other resources contained in a resource.
29         /// </summary>
30         public const string LinkInterface = "oic.if.ll";
31
32         /// <summary>
33         /// Batch Interface which is used to manipulate (GET, PUT, POST, DELETE) on other resource contained in a resource.
34         /// </summary>
35         public const string BatchInterface = "oic.if.b";
36
37         /// <summary>
38         /// Group Interface which is used to manipulate (GET, PUT, POST) a group of remote resources.
39         /// </summary>
40         public const string GroupInterface = "oic.mi.grp";
41
42         /// <summary>
43         /// Read-Only Interface which is used to limit the methods that can be applied to a resource to GET only.
44         /// </summary>
45         public const string ReadonlyInterface = "oic.if.r";
46
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;
51
52         /// <summary>
53         /// Constructor of ResourceInterfaces
54         /// </summary>
55         /// <seealso cref="Add()"/>
56         /// <seealso cref="Remove()"/>
57         /// <code>
58         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
59         /// </code>
60         public ResourceInterfaces()
61         {
62             int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
63             if (ret != (int)IoTConnectivityError.None)
64             {
65                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
66                 throw IoTConnectivityErrorFactory.GetException(ret);
67             }
68         }
69
70         /// <summary>
71         /// Constructor of ResourceInterfaces using list of interfaces
72         /// </summary>
73         /// <param name="ifaces">List of resource interfaces</param>
74         /// <code>
75         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
76         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
77         /// </code>
78         public ResourceInterfaces(IEnumerable<string> ifaces)
79         {
80             int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle);
81             if (ret != (int)IoTConnectivityError.None)
82             {
83                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
84                 throw IoTConnectivityErrorFactory.GetException(ret);
85             }
86             foreach (string iface in ifaces)
87             {
88                 Add(iface);
89             }
90         }
91
92         internal ResourceInterfaces(IntPtr ifacesHandleToClone)
93         {
94             int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Clone(ifacesHandleToClone, out _resourceInterfacesHandle);
95             if (ret != (int)IoTConnectivityError.None)
96             {
97                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface");
98                 throw IoTConnectivityErrorFactory.GetException(ret);
99             }
100
101             Interop.IoTConnectivity.Common.ResourceInterfaces.ForeachCallback cb = (string iface, IntPtr data) =>
102             {
103                 _resourceInterfaces.Add(iface);
104                 return true;
105             };
106
107             ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Foreach(ifacesHandleToClone, cb, IntPtr.Zero);
108             if (ret != (int)IoTConnectivityError.None)
109             {
110                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
111                 throw IoTConnectivityErrorFactory.GetException(ret);
112             }
113         }
114
115         /// <summary>
116         /// Destructor of the ResourceInterfaces class.
117         /// </summary>
118         ~ResourceInterfaces()
119         {
120             Dispose(false);
121         }
122
123         internal IntPtr ResourceInterfacesHandle
124         {
125             get
126             {
127                 return _resourceInterfacesHandle;
128             }
129         }
130
131         /// <summary>
132         /// Indicates count of interfaces in the list
133         /// </summary>
134         /// <code>
135         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
136         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
137         /// Console.WriteLine("There are {0} interfaces", resourceInterfaces.Count);
138         /// </code>
139         public int Count
140         {
141             get
142             {
143                 return _resourceInterfaces.Count;
144             }
145         }
146
147         /// <summary>
148         /// Adds a resource interface into the list.
149         /// </summary>
150         /// <remarks>
151         /// @a item could be a value such as <see cref="DefaultInterface"/>
152         /// </remarks>
153         /// <param name="item">The string data to insert into the resource interfaces</param>
154         /// <seealso cref="Remove()"/>
155         /// <code>
156         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces();
157         /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
158         /// </code>
159         public void Add(string item)
160         {
161             if (IsValid(item))
162             {
163                 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Add(_resourceInterfacesHandle, item);
164                 if (ret != (int)IoTConnectivityError.None)
165                 {
166                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
167                     throw IoTConnectivityErrorFactory.GetException(ret);
168                 }
169                 _resourceInterfaces.Add(item);
170             }
171             else
172             {
173                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid interface");
174                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
175             }
176         }
177
178         /// <summary>
179         /// Removes a resource interface from the list
180         /// </summary>
181         /// <param name="item">The string data to delete from the resource ifaces</param>
182         /// <seealso cref="Add()"/>
183         /// <code>
184         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.BatchInterface });
185         /// resourceInterfaces.Add(ResourceInterfaces.BatchInterface);
186         /// </code>
187         public void Remove(string item)
188         {
189             bool isRemoved = _resourceInterfaces.Remove(item);
190             if (isRemoved)
191             {
192                 int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Remove(_resourceInterfacesHandle, item);
193                 if (ret != (int)IoTConnectivityError.None)
194                 {
195                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface");
196                     throw IoTConnectivityErrorFactory.GetException(ret);
197                 }
198             }
199             else
200                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
201         }
202
203         /// <summary>
204         /// Return enumerator for the list of interfaces
205         /// </summary>
206         /// <returns>The enumerator</returns>
207         /// <code>
208         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
209         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
210         /// foreach(string item in resourceInterfaces)
211         /// {
212         ///     Console.WriteLine("Interface : {0}", item);
213         /// }
214         /// </code>
215         public IEnumerator<string> GetEnumerator()
216         {
217             return _resourceInterfaces.GetEnumerator();
218         }
219
220         /// <summary>
221         /// Return enumerator for the list of interfaces
222         /// </summary>
223         /// <returns>The enumerator</returns>
224         /// <code>
225         /// ResourceInterfaces resourceInterfaces = new ResourceInterfaces(new List<string>()
226         ///     { ResourceInterfaces.LinkInterface, ResourceInterfaces.ReadonlyInterface });
227         /// foreach(string item in resourceInterfaces)
228         /// {
229         ///     Console.WriteLine("Interface : {0}", item);
230         /// }
231         /// </code>
232         IEnumerator IEnumerable.GetEnumerator()
233         {
234             return _resourceInterfaces.GetEnumerator();
235         }
236
237         /// <summary>
238         /// Releases any unmanaged resources used by this object.
239         /// </summary>
240         public void Dispose()
241         {
242             Dispose(true);
243             GC.SuppressFinalize(this);
244         }
245
246         internal static bool IsValid(string type)
247         {
248             Regex r = new Regex("^[a-zA-Z0-9.-]+$");
249             return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
250         }
251
252         /// <summary>
253         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
254         /// </summary>
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)
257         {
258             if (_disposed)
259                 return;
260
261             if (disposing)
262             {
263                 // Free managed objects
264             }
265
266             Interop.IoTConnectivity.Common.ResourceInterfaces.Destroy(_resourceInterfacesHandle);
267             _disposed = true;
268         }
269     }
270 }