[IoTConnectivity] Added Base implementation
[platform/core/csapi/iotcon.git] / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / ResourceOptions.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.Linq;
13 using Tizen;
14
15 namespace Tizen.Network.IoTConnectivity
16 {
17     /// <summary>
18     /// Class representing Resource options
19     /// </summary>
20     public class ResourceOptions : IDictionary<ushort, string>, IDisposable
21     {
22         internal const int MaxSize = 2;
23         internal const int IdMin = 2048;
24         internal const int IdMax = 3000;
25         internal const int DataMax = 15;
26
27         internal IntPtr _resourceOptionsHandle = IntPtr.Zero;
28         private readonly IDictionary<ushort, string> _options = new Dictionary<ushort, string>();
29         private bool _disposed = false;
30
31         public ResourceOptions()
32         {
33             int ret = Interop.IoTConnectivity.Common.Options.Create(out _resourceOptionsHandle);
34             if (ret != (int)IoTConnectivityError.None)
35             {
36                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create options");
37                 throw IoTConnectivityErrorFactory.GetException(ret);
38             }
39         }
40
41         internal ResourceOptions(IntPtr handleToClone)
42         {
43             int ret = Interop.IoTConnectivity.Common.Options.Create(out _resourceOptionsHandle);
44             if (ret != (int)IoTConnectivityError.None)
45             {
46                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create options");
47                 throw IoTConnectivityErrorFactory.GetException(ret);
48             }
49
50             Interop.IoTConnectivity.Common.Options.OptionsCallback forEachCallback = (ushort id, string value, IntPtr userData) =>
51             {
52                 Add(id, value);
53                 return true;
54             };
55
56             ret = Interop.IoTConnectivity.Common.Options.ForEach(handleToClone, forEachCallback, IntPtr.Zero);
57             if (ret != (int)IoTConnectivityError.None)
58             {
59                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to iterate options");
60                 throw IoTConnectivityErrorFactory.GetException(ret);
61             }
62         }
63
64         ~ResourceOptions()
65         {
66             Dispose(false);
67         }
68
69         /// <summary>
70         /// Contains all the Option keys
71         /// </summary>
72         public ICollection<ushort> Keys
73         {
74             get
75             {
76                 return _options.Keys;
77             }
78         }
79
80         /// <summary>
81         /// Contains all the Option values
82         /// </summary>
83         public ICollection<string> Values
84         {
85             get
86             {
87                 return _options.Values;
88             }
89         }
90
91         /// <summary>
92         /// Gets the number of options
93         /// </summary>
94         public int Count
95         {
96             get
97             {
98                 return _options.Count;
99             }
100         }
101
102         /// <summary>
103         /// Represents whether the collection is readonly
104         /// </summary>
105         public bool IsReadOnly
106         {
107             get
108             {
109                 return _options.IsReadOnly;
110             }
111         }
112
113         /// <summary>
114         /// Gets or sets the option
115         /// </summary>
116         /// <param name="key">The option id to get or set.</param>
117         /// <returns>The option with the specified id.</returns>
118         public string this[ushort key]
119         {
120             get
121             {
122                 return _options[key];
123             }
124
125             set
126             {
127                 Add(key, value);
128             }
129         }
130
131         /// <summary>
132         /// Checks the given key exists in Options collection
133         /// </summary>
134         /// <param name="key">The key to look for</param>
135         /// <returns></returns>
136         public bool ContainsKey(ushort key)
137         {
138             return _options.ContainsKey(key);
139         }
140
141         /// <summary>
142         ///  Adds option key and value
143         /// </summary>
144         /// <param name="key">Option ID</param>
145         /// <param name="value">Value coresponding to option</param>
146         public void Add(ushort key, string value)
147         {
148             int ret = (int)IoTConnectivityError.InvalidParameter;
149             if (IsValid(key, value))
150             {
151                 ret = Interop.IoTConnectivity.Common.Options.Add(_resourceOptionsHandle, key, value);
152                 if (ret != (int)IoTConnectivityError.None)
153                 {
154                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add option");
155                     throw IoTConnectivityErrorFactory.GetException(ret);
156                 }
157                 _options.Add(key, value);
158             }
159             else
160             {
161                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid options");
162                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
163             }
164         }
165
166         /// <summary>
167         /// Removes an option
168         /// </summary>
169         /// <param name="key">The option to remvoe</param>
170         /// <returns></returns>
171         public bool Remove(ushort key)
172         {
173             int ret = Interop.IoTConnectivity.Common.Options.Remove(_resourceOptionsHandle, key);
174             if (ret != (int)IoTConnectivityError.None)
175             {
176                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove option");
177                 throw IoTConnectivityErrorFactory.GetException(ret);
178             }
179
180             return _options.Remove(key);
181         }
182
183         /// <summary>
184         /// Gets the value associated with the specified key.
185         /// </summary>
186         /// <param name="key">The option id</param>
187         /// <param name="value">value corresponding to option id</param>
188         /// <returns>true if the key exists, false otherwise</returns>
189         public bool TryGetValue(ushort key, out string value)
190         {
191             return _options.TryGetValue(key, out value);
192         }
193
194         /// <summary>
195         ///  Adds options key and value as a key value pair
196         /// </summary>
197         /// <param name="item">The key value pair</param>
198         public void Add(KeyValuePair<ushort, string> item)
199         {
200             Add(item.Key, item.Value);
201         }
202
203         /// <summary>
204         /// Clears the Options collection
205         /// </summary>
206         public void Clear()
207         {
208             foreach (ushort key in Keys)
209             {
210                 int ret = Interop.IoTConnectivity.Common.Options.Remove(_resourceOptionsHandle, key);
211                 if (ret != (int)IoTConnectivityError.None)
212                 {
213                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove option");
214                     throw IoTConnectivityErrorFactory.GetException(ret);
215                 };
216             }
217             _options.Clear();
218         }
219
220         /// <summary>
221         /// Checks if the given option pair exists
222         /// </summary>
223         /// <param name="item">The key value pair</param>
224         /// <returns></returns>
225         public bool Contains(KeyValuePair<ushort, string> item)
226         {
227             return _options.Contains(item);
228         }
229
230         /// <summary>
231         /// Copies the elements of the options collection to an Array, starting at a particular index.
232         /// </summary>
233         /// <param name="array">The destination array</param>
234         /// <param name="arrayIndex">Index parameter</param>
235         public void CopyTo(KeyValuePair<ushort, string>[] array, int arrayIndex)
236         {
237             _options.CopyTo(array, arrayIndex);
238         }
239
240         /// <summary>
241         /// Remove the gien option pair
242         /// </summary>
243         /// <param name="item">The option pair to remove</param>
244         /// <returns></returns>
245         public bool Remove(KeyValuePair<ushort, string> item)
246         {
247             return Remove(item.Key);
248         }
249
250         /// <summary>
251         /// Get the enumerator to options collection
252         /// </summary>
253         /// <returns> Enumerator to option pairs</returns>
254         public IEnumerator<KeyValuePair<ushort, string>> GetEnumerator()
255         {
256             return _options.GetEnumerator();
257         }
258
259         public void Dispose()
260         {
261             Dispose(true);
262             GC.SuppressFinalize(this);
263         }
264
265         IEnumerator IEnumerable.GetEnumerator()
266         {
267             return _options.GetEnumerator();
268         }
269
270         protected virtual void Dispose(bool disposing)
271         {
272             if (_disposed)
273                 return;
274
275             if (disposing)
276             {
277                 // Free managed objects
278             }
279
280             Interop.IoTConnectivity.Common.Options.Destroy(_resourceOptionsHandle);
281             _disposed = true;
282         }
283
284         private bool IsValid(ushort key, string value)
285         {
286             return (key > IdMin && key < IdMax && value.Length <= DataMax && _options.Count() < MaxSize);
287         }
288     }
289 }