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;
23 namespace Tizen.Network.IoTConnectivity
26 /// This class represents resource options. It provides APIs to manage them.\n
27 /// The iotcon options API provides methods for managing vendor specific options of coap packet.\n
28 /// See more about coap packet in http://tools.ietf.org/html/rfc7252.
30 /// <since_tizen> 3 </since_tizen>
31 public class ResourceOptions : IDictionary<ushort, string>, IDisposable
33 internal const int MaxSize = 2;
34 internal const int IdMin = 2048;
35 internal const int IdMax = 3000;
36 internal const int DataMax = 15;
38 internal IntPtr _resourceOptionsHandle = IntPtr.Zero;
39 private readonly IDictionary<ushort, string> _options = new Dictionary<ushort, string>();
40 private bool _disposed = false;
43 /// The resource options constructor.
45 /// <since_tizen> 3 </since_tizen>
46 /// <feature>http://tizen.org/feature/iot.ocf</feature>
47 /// <seealso cref="Add()"/>
48 /// <seealso cref="Remove()"/>
49 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
50 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory.</exception>
52 /// ResourceOptions options = new ResourceOptions();
54 public ResourceOptions()
56 int ret = Interop.IoTConnectivity.Common.Options.Create(out _resourceOptionsHandle);
57 if (ret != (int)IoTConnectivityError.None)
59 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create options");
60 throw IoTConnectivityErrorFactory.GetException(ret);
64 // internal constructor
65 internal ResourceOptions(IntPtr handleToClone)
67 int ret = Interop.IoTConnectivity.Common.Options.Create(out _resourceOptionsHandle);
68 if (ret != (int)IoTConnectivityError.None)
70 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create options");
71 throw IoTConnectivityErrorFactory.GetException(ret);
74 Interop.IoTConnectivity.Common.Options.OptionsCallback forEachCallback = (ushort id, string value, IntPtr userData) =>
80 ret = Interop.IoTConnectivity.Common.Options.ForEach(handleToClone, forEachCallback, IntPtr.Zero);
81 if (ret != (int)IoTConnectivityError.None)
83 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to iterate options");
84 throw IoTConnectivityErrorFactory.GetException(ret);
89 /// Destructor of the ResourceOptions class.
97 /// Contains all the Option keys.
99 /// <since_tizen> 3 </since_tizen>
100 /// <value>All the Option keys.</value>
102 /// ResourceOptions options = new ResourceOptions();
103 /// options.Add(2050, "sample-data");
104 /// options.Add(2055, "sample value");
105 /// var keys = options.Keys;
106 /// Console.WriteLine("Resource options contains keys {0} and {1}", keys.ElementAt(0), keys.ElementAt(1));
108 public ICollection<ushort> Keys
112 return _options.Keys;
117 /// Contains all the Option values.
119 /// <since_tizen> 3 </since_tizen>
120 /// <value>All the Option values.</value>
122 /// ResourceOptions options = new ResourceOptions();
123 /// options.Add(2050, "sample-data");
124 /// options.Add(2055, "sample value");
125 /// var values = options.Values;
126 /// Console.WriteLine("Resource options contains values {0} and {1}", values.ElementAt(0), values.ElementAt(1));
128 public ICollection<string> Values
132 return _options.Values;
137 /// Gets the number of options.
139 /// <since_tizen> 3 </since_tizen>
140 /// <value>The number of options.</value>
142 /// ResourceOptions options = new ResourceOptions();
143 /// options.Add(2050, "sample-data");
144 /// options.Add(2055, "sample value");
145 /// var count = options.Count;
146 /// Console.WriteLine("There are {0} keys in the options object", count);
152 return _options.Count;
157 /// Represents whether the collection is readonly.
159 /// <since_tizen> 3 </since_tizen>
160 /// <value>Whether the collection is readonly.</value>
162 /// ResourceOptions options = new ResourceOptions();
163 /// if (options.IsReadOnly)
164 /// Console.WriteLine("Read only options");
166 public bool IsReadOnly
170 return _options.IsReadOnly;
175 /// Gets or sets the option data.
177 /// <since_tizen> 3 </since_tizen>
178 /// <value>The option data.</value>
179 /// <param name="key">The option ID to get or set.</param>
180 /// <returns>The option with the specified ID.</returns>
182 /// ResourceOptions options = new ResourceOptions();
183 /// options[2055] = "sample-data";
184 /// Console.WriteLine("Option has : {0}", options[2055]);
186 public string this[ushort key]
190 return _options[key];
199 /// Checks whether the given key exists in Options collection.
201 /// <since_tizen> 3 </since_tizen>
202 /// <param name="key">The key to look for.</param>
203 /// <returns>true if exists. Otherwise, false.</returns>
205 /// ResourceOptions options = new ResourceOptions();
206 /// options.Add(2050, "sample-data");
207 /// if (options.ContainsKey(2050))
208 /// Console.WriteLine("options conatins key : 2050");
210 public bool ContainsKey(ushort key)
212 return _options.ContainsKey(key);
216 /// Adds a new ID and a correspoding data into the options.
218 /// <since_tizen> 3 </since_tizen>
220 /// ResourceOptions can have up to 2 options. \n
221 /// key is always situated between 2048 and 3000. \n
222 /// Length of option data is less than or equal to 15.
224 /// <param name="key">The ID of the option to insert.</param>
225 /// <param name="value">The string data to insert into the options.</param>
226 /// <feature>http://tizen.org/feature/iot.ocf</feature>
227 /// <seealso cref="Remove()"/>
228 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
229 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter.</exception>
231 /// ResourceOptions options = new ResourceOptions();
232 /// options.Add(2050, "sample-data");
234 public void Add(ushort key, string value)
236 int ret = (int)IoTConnectivityError.InvalidParameter;
237 if (IsValid(key, value))
239 ret = Interop.IoTConnectivity.Common.Options.Add(_resourceOptionsHandle, key, value);
240 if (ret != (int)IoTConnectivityError.None)
242 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add option");
243 throw IoTConnectivityErrorFactory.GetException(ret);
245 _options.Add(key, value);
249 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid options");
250 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
255 /// Removes the ID and its associated data from the options.
257 /// <since_tizen> 3 </since_tizen>
258 /// <param name="key">The ID of the option to delete.</param>
259 /// <returns>True if operation is successful. Otherwise, false.</returns>
260 /// <feature>http://tizen.org/feature/iot.ocf</feature>
261 /// <seealso cref="Add()"/>
262 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
263 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter.</exception>
265 /// ResourceOptions options = new ResourceOptions();
266 /// options.Add(2050, "12345");
267 /// var result = options.Remove(2050);
269 public bool Remove(ushort key)
271 int ret = Interop.IoTConnectivity.Common.Options.Remove(_resourceOptionsHandle, key);
272 if (ret != (int)IoTConnectivityError.None)
274 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove option");
275 throw IoTConnectivityErrorFactory.GetException(ret);
278 bool isRemoved = _options.Remove(key);
284 /// Gets the value associated with the specified key.
286 /// <since_tizen> 3 </since_tizen>
287 /// <param name="key">The option ID.</param>
288 /// <param name="value">Value corresponding to option ID.</param>
289 /// <returns>True if the key exists, false otherwise.</returns>
291 /// ResourceOptions options = new ResourceOptions();
292 /// options.Add(2050, "12345");
294 /// var isPresent = options.TryGetValue(2050, out value);
296 /// Console.WriteLine("value : {0}", value);
298 public bool TryGetValue(ushort key, out string value)
300 return _options.TryGetValue(key, out value);
304 /// Adds options key and value as a key value pair.
306 /// <since_tizen> 3 </since_tizen>
307 /// <param name="item">The key value pair.</param>
308 /// <feature>http://tizen.org/feature/iot.ocf</feature>
309 /// <seealso cref="Remove()"/>
311 /// ResourceOptions options = new ResourceOptions();
312 /// options.Add(new KeyValuePair<ushort, string>(2050, "12345"));
314 public void Add(KeyValuePair<ushort, string> item)
316 Add(item.Key, item.Value);
320 /// Clears the Options collection.
322 /// <since_tizen> 3 </since_tizen>
323 /// <feature>http://tizen.org/feature/iot.ocf</feature>
324 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
326 /// ResourceOptions options = new ResourceOptions();
327 /// options.Add(2050, "12345");
328 /// options.Add(2055, "sample");
333 foreach (ushort key in Keys)
335 int ret = Interop.IoTConnectivity.Common.Options.Remove(_resourceOptionsHandle, key);
336 if (ret != (int)IoTConnectivityError.None)
338 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove option");
339 throw IoTConnectivityErrorFactory.GetException(ret);
346 /// Checks if the given option pair exists.
348 /// <since_tizen> 3 </since_tizen>
349 /// <param name="item">The key value pair.</param>
350 /// <returns>True if exists. Otherwise, false.</returns>
352 /// ResourceOptions options = new ResourceOptions();
353 /// options.Add(new KeyValuePair<ushort, string>(2050, "12345"));
354 /// var isPresent = options.Contains(new KeyValuePair<ushort, string>(2050, "12345"));
356 /// Console.WriteLine("Key value pair is present");
358 public bool Contains(KeyValuePair<ushort, string> item)
360 return _options.Contains(item);
364 /// Copies the elements of the options collection to an array, starting at a particular index.
366 /// <since_tizen> 3 </since_tizen>
367 /// <param name="array">The destination array.</param>
368 /// <param name="arrayIndex">Index parameter.</param>
370 /// ResourceOptions options = new ResourceOptions();
371 /// options.Add(new KeyValuePair<ushort, string>(2050, "12345"));
372 /// KeyValuePair<ushort, string>[] dest = new KeyValuePair<ushort, string>[options.Count];
373 /// options.CopyTo(dest, 0);
374 /// Console.WriteLine("Dest conatins ({0}, {1})", dest[0].Key, dest[0].Value);
376 public void CopyTo(KeyValuePair<ushort, string>[] array, int arrayIndex)
378 _options.CopyTo(array, arrayIndex);
382 /// Removes the given key value pair from the options.
384 /// <since_tizen>3</since_tizen>
385 /// <param name="item">The key value pair to remove</param>
386 /// <returns>True if operation is successful. Otherwise, false</returns>
387 /// <feature>http://tizen.org/feature/iot.ocf</feature>
388 /// <seealso cref="Add()"/>
389 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
391 /// ResourceOptions options = new ResourceOptions();
392 /// options.Add(new KeyValuePair<ushort, string>(2050, "12345"));
393 /// var result = options.Remove(new KeyValuePair<ushort, string>(2050, "12345"));
395 public bool Remove(KeyValuePair<ushort, string> item)
397 return Remove(item.Key);
401 /// Get the enumerator to options collection.
403 /// <since_tizen> 3 </since_tizen>
404 /// <returns>Enumerator to option pairs.</returns>
406 /// ResourceOptions options = new ResourceOptions();
407 /// options.Add(new KeyValuePair<ushort, string>(2050, "sample1"));
408 /// options.Add(new KeyValuePair<ushort, string>(2055, "sample2"));
409 /// foreach (KeyValuePair<string, object> pair in options)
411 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
414 public IEnumerator<KeyValuePair<ushort, string>> GetEnumerator()
416 return _options.GetEnumerator();
420 /// Releases any unmanaged resources used by this object.
422 /// <since_tizen> 3 </since_tizen>
423 /// <feature>http://tizen.org/feature/iot.ocf</feature>
424 public void Dispose()
427 GC.SuppressFinalize(this);
431 /// Gets the enumerator to options collection.
433 /// <since_tizen> 3 </since_tizen>
434 /// <returns>Enumerator to option pairs.</returns>
436 /// ResourceOptions options = new ResourceOptions();
437 /// options.Add(new KeyValuePair<ushort, string>(2050, "sample1"));
438 /// options.Add(new KeyValuePair<ushort, string>(2055, "sample2"));
439 /// foreach (KeyValuePair<string, object> pair in options)
441 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
444 IEnumerator IEnumerable.GetEnumerator()
446 return _options.GetEnumerator();
450 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
452 /// <since_tizen> 3 </since_tizen>
453 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
454 /// <feature>http://tizen.org/feature/iot.ocf</feature>
455 protected virtual void Dispose(bool disposing)
462 // Free managed objects
465 Interop.IoTConnectivity.Common.Options.Destroy(_resourceOptionsHandle);
469 private bool IsValid(ushort key, string value)
471 return (key > IdMin && key < IdMax && value.Length <= DataMax && _options.Count() < MaxSize);