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;
15 namespace Tizen.Network.IoTConnectivity
18 /// Class representing Resource options
20 public class ResourceOptions : IDictionary<ushort, string>, IDisposable
22 internal const int MaxSize = 2;
23 internal const int IdMin = 2048;
24 internal const int IdMax = 3000;
25 internal const int DataMax = 15;
27 internal IntPtr _resourceOptionsHandle = IntPtr.Zero;
28 private readonly IDictionary<ushort, string> _options = new Dictionary<ushort, string>();
29 private bool _disposed = false;
31 public ResourceOptions()
33 int ret = Interop.IoTConnectivity.Common.Options.Create(out _resourceOptionsHandle);
34 if (ret != (int)IoTConnectivityError.None)
36 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create options");
37 throw IoTConnectivityErrorFactory.GetException(ret);
41 internal ResourceOptions(IntPtr handleToClone)
43 int ret = Interop.IoTConnectivity.Common.Options.Create(out _resourceOptionsHandle);
44 if (ret != (int)IoTConnectivityError.None)
46 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create options");
47 throw IoTConnectivityErrorFactory.GetException(ret);
50 Interop.IoTConnectivity.Common.Options.OptionsCallback forEachCallback = (ushort id, string value, IntPtr userData) =>
56 ret = Interop.IoTConnectivity.Common.Options.ForEach(handleToClone, forEachCallback, IntPtr.Zero);
57 if (ret != (int)IoTConnectivityError.None)
59 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to iterate options");
60 throw IoTConnectivityErrorFactory.GetException(ret);
70 /// Contains all the Option keys
72 public ICollection<ushort> Keys
81 /// Contains all the Option values
83 public ICollection<string> Values
87 return _options.Values;
92 /// Gets the number of options
98 return _options.Count;
103 /// Represents whether the collection is readonly
105 public bool IsReadOnly
109 return _options.IsReadOnly;
114 /// Gets or sets the option
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]
122 return _options[key];
132 /// Checks the given key exists in Options collection
134 /// <param name="key">The key to look for</param>
135 /// <returns></returns>
136 public bool ContainsKey(ushort key)
138 return _options.ContainsKey(key);
142 /// Adds option key and value
144 /// <param name="key">Option ID</param>
145 /// <param name="value">Value coresponding to option</param>
146 public void Add(ushort key, string value)
148 int ret = (int)IoTConnectivityError.InvalidParameter;
149 if (IsValid(key, value))
151 ret = Interop.IoTConnectivity.Common.Options.Add(_resourceOptionsHandle, key, value);
152 if (ret != (int)IoTConnectivityError.None)
154 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add option");
155 throw IoTConnectivityErrorFactory.GetException(ret);
157 _options.Add(key, value);
161 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid options");
162 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
167 /// Removes an option
169 /// <param name="key">The option to remvoe</param>
170 /// <returns></returns>
171 public bool Remove(ushort key)
173 int ret = Interop.IoTConnectivity.Common.Options.Remove(_resourceOptionsHandle, key);
174 if (ret != (int)IoTConnectivityError.None)
176 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove option");
177 throw IoTConnectivityErrorFactory.GetException(ret);
180 return _options.Remove(key);
184 /// Gets the value associated with the specified key.
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)
191 return _options.TryGetValue(key, out value);
195 /// Adds options key and value as a key value pair
197 /// <param name="item">The key value pair</param>
198 public void Add(KeyValuePair<ushort, string> item)
200 Add(item.Key, item.Value);
204 /// Clears the Options collection
208 foreach (ushort key in Keys)
210 int ret = Interop.IoTConnectivity.Common.Options.Remove(_resourceOptionsHandle, key);
211 if (ret != (int)IoTConnectivityError.None)
213 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove option");
214 throw IoTConnectivityErrorFactory.GetException(ret);
221 /// Checks if the given option pair exists
223 /// <param name="item">The key value pair</param>
224 /// <returns></returns>
225 public bool Contains(KeyValuePair<ushort, string> item)
227 return _options.Contains(item);
231 /// Copies the elements of the options collection to an Array, starting at a particular index.
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)
237 _options.CopyTo(array, arrayIndex);
241 /// Remove the gien option pair
243 /// <param name="item">The option pair to remove</param>
244 /// <returns></returns>
245 public bool Remove(KeyValuePair<ushort, string> item)
247 return Remove(item.Key);
251 /// Get the enumerator to options collection
253 /// <returns> Enumerator to option pairs</returns>
254 public IEnumerator<KeyValuePair<ushort, string>> GetEnumerator()
256 return _options.GetEnumerator();
259 public void Dispose()
262 GC.SuppressFinalize(this);
265 IEnumerator IEnumerable.GetEnumerator()
267 return _options.GetEnumerator();
270 protected virtual void Dispose(bool disposing)
277 // Free managed objects
280 Interop.IoTConnectivity.Common.Options.Destroy(_resourceOptionsHandle);
284 private bool IsValid(ushort key, string value)
286 return (key > IdMin && key < IdMax && value.Length <= DataMax && _options.Count() < MaxSize);