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;
21 using System.Runtime.InteropServices;
23 namespace Tizen.Network.IoTConnectivity
26 /// This class represents current attributes of a resource.
27 /// It provides API to manage attributes.
28 /// This class is accessed by using a constructor to create a new instance of this object.
30 /// <since_tizen> 3 </since_tizen>
31 public class Attributes : IDictionary<string, object>, IDisposable
33 internal IntPtr _resourceAttributesHandle = IntPtr.Zero;
34 private readonly IDictionary<string, object> _attributes = new Dictionary<string, object>();
35 private bool _disposed = false;
38 /// The Attributes constructor.
40 /// <since_tizen> 3 </since_tizen>
41 /// <feature>http://tizen.org/feature/iot.ocf</feature>
42 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
43 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory.</exception>
45 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
49 int ret = Interop.IoTConnectivity.Common.Attributes.Create(out _resourceAttributesHandle);
50 if (ret != (int)IoTConnectivityError.None)
52 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create attributes handle");
53 throw IoTConnectivityErrorFactory.GetException(ret);
57 internal Attributes(IntPtr attributesHandleToClone)
59 int ret = Interop.IoTConnectivity.Common.Attributes.Clone(attributesHandleToClone, out _resourceAttributesHandle);
60 if (ret != (int)IoTConnectivityError.None)
62 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create attributes handle");
63 throw IoTConnectivityErrorFactory.GetException(ret);
66 SetAttributes(_resourceAttributesHandle);
70 /// Destructor of the Attributes class.
78 /// Gets the number of keys.
80 /// <since_tizen> 3 </since_tizen>
81 /// <value>The number of keys.</value>
83 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
84 /// attributes.Add("brightness", 50);
85 /// var count = attributes.Count;
86 /// Console.WriteLine("There are {0} keys in the attribute object", count);
92 return _attributes.Count;
97 /// Represents whether an attribute is readonly.
99 /// <since_tizen> 3 </since_tizen>
100 /// <value>Whether an attribute is readonly.</value>
102 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
103 /// { "state", "ON" },
106 /// if (attributes.IsReadOnly)
107 /// Console.WriteLine("Read only attribute");
108 /// </code></example>
109 public bool IsReadOnly
113 return _attributes.IsReadOnly;
118 /// Contains all the attribute keys.
120 /// <since_tizen> 3 </since_tizen>
121 /// <value>All the attribute keys.</value>
123 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
124 /// { "state", "ON" },
127 /// var keys = attributes.Keys;
128 /// Console.WriteLine("Attribute contains keys {0} and {1}", keys.ElementAt(0), keys.ElementAt(1));
129 /// </code></example>
130 public ICollection<string> Keys
134 return _attributes.Keys;
139 /// Contains all the attribute values.
141 /// <since_tizen> 3 </since_tizen>
142 /// <value>All the attribute values.</value>
144 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
145 /// { "state", "ON" },
148 /// var values = attributes.Values;
149 /// Console.WriteLine("Attribute contains values {0} and {1}", values.ElementAt(0), values.ElementAt(1));
150 /// </code></example>
151 public ICollection<object> Values
155 return _attributes.Values;
160 /// Gets or sets the attribute with the specified key.
162 /// <since_tizen> 3 </since_tizen>
163 /// <value>The attribute with the specified key.</value>
164 /// <param name="key">The key of the attribute to get or set.</param>
165 /// <returns>The element with the specified key.</returns>
167 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
168 /// attributes["state"] = "ON";
169 /// Console.WriteLine("Attribute value for key 'state' : {0}", attributes["state"]);
170 /// </code></example>
171 public object this[string key]
175 if (_attributes.ContainsKey(key))
176 return _attributes[key];
188 /// Adds the attribute key and a value as a key value pair.
190 /// <since_tizen> 3 </since_tizen>
191 /// <param name="item">The key value pair to add.</param>
192 /// <feature>http://tizen.org/feature/iot.ocf</feature>
193 /// <example><code><![CDATA[
194 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
195 /// attributes.Add(new KeyValuePair<string, object> ("state", "ON"));
196 /// ]]></code></example>
197 public void Add(KeyValuePair<string, object> item)
199 Add(item.Key, item.Value);
203 /// Adds an attribute.
205 /// <since_tizen> 3 </since_tizen>
206 /// <param name="key">The key representing the attribute.</param>
207 /// <param name="value">The value representing the attribute.</param>
208 /// <feature>http://tizen.org/feature/iot.ocf</feature>
210 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
211 /// attributes.Add("brightness", 50);
212 /// </code></example>
213 public void Add(string key, object value)
218 ret = Interop.IoTConnectivity.Common.Attributes.AddInt(_resourceAttributesHandle, key, (int)value);
219 if (ret != (int)IoTConnectivityError.None)
221 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add int");
222 throw IoTConnectivityErrorFactory.GetException(ret);
225 else if (value is Attributes)
227 Attributes attribs = (Attributes)value;
228 ret = Interop.IoTConnectivity.Common.Attributes.AddAttributes(_resourceAttributesHandle, key, attribs._resourceAttributesHandle);
229 if (ret != (int)IoTConnectivityError.None)
231 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
232 throw IoTConnectivityErrorFactory.GetException(ret);
235 else if (value is string)
237 ret = Interop.IoTConnectivity.Common.Attributes.AddStr(_resourceAttributesHandle, key, (string)value);
238 if (ret != (int)IoTConnectivityError.None)
240 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add string");
241 throw IoTConnectivityErrorFactory.GetException(ret);
244 else if (value is double)
246 ret = Interop.IoTConnectivity.Common.Attributes.AddDouble(_resourceAttributesHandle, key, (double)value);
247 if (ret != (int)IoTConnectivityError.None)
249 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add double");
250 throw IoTConnectivityErrorFactory.GetException(ret);
253 else if (value is bool)
255 ret = Interop.IoTConnectivity.Common.Attributes.AddBool(_resourceAttributesHandle, key, (bool)value);
256 if (ret != (int)IoTConnectivityError.None)
258 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool");
259 throw IoTConnectivityErrorFactory.GetException(ret);
262 else if (value is byte[])
264 byte[] val = value as byte[];
267 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get byte[] val");
268 throw new ArgumentException("Invalid Parameter");
270 ret = Interop.IoTConnectivity.Common.Attributes.AddByteStr(_resourceAttributesHandle, key, val, val.Length);
271 if (ret != (int)IoTConnectivityError.None)
273 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool");
274 throw IoTConnectivityErrorFactory.GetException(ret);
277 else if (value is IEnumerable)
279 IntPtr listHandle = List.GetListHandle(value);
280 ret = Interop.IoTConnectivity.Common.Attributes.AddList(_resourceAttributesHandle, key, listHandle);
281 if (ret != (int)IoTConnectivityError.None)
283 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add list");
284 throw IoTConnectivityErrorFactory.GetException(ret);
289 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Add");
290 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
292 _attributes.Add(key, value);
296 /// Clears attributes collection.
298 /// <since_tizen> 3 </since_tizen>
299 /// <feature>http://tizen.org/feature/iot.ocf</feature>
300 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
301 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
303 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
304 /// attributes.Add("brightness", 50);
305 /// attributes.Clear();
306 /// </code></example>
309 foreach (string key in _attributes.Keys)
311 int ret = Interop.IoTConnectivity.Common.Attributes.Remove(_resourceAttributesHandle, key);
312 if (ret != (int)IoTConnectivityError.None)
314 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to clear attributes");
315 throw IoTConnectivityErrorFactory.GetException(ret);
322 /// Checks whether the given key value pair exists in attributes collection.
324 /// <since_tizen> 3 </since_tizen>
325 /// <param name="item">The status key value pair.</param>
326 /// <returns>true if exists. Otherwise, false.</returns>
327 /// <example><code><![CDATA[
328 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
329 /// { "state", "ON" },
332 /// if (attributes.Contains(new KeyValuePair<string, object> ("dim", 10))
333 /// Console.WriteLine("Attribute conatins pair ('dim', 10)");
334 /// ]]></code></example>
335 public bool Contains(KeyValuePair<string, object> item)
337 return _attributes.Contains(item);
341 /// Checks whether the given key exists in attributes collection.
343 /// <since_tizen> 3 </since_tizen>
344 /// <param name="key">The status key to look for.</param>
345 /// <returns>true if exists. Otherwise, false.</returns>
347 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
348 /// { "state", "ON" },
351 /// if (attributes.ContainsKey("dim"))
352 /// Console.WriteLine("Attribute conatins key : dim");
353 /// </code></example>
354 public bool ContainsKey(string key)
356 return _attributes.ContainsKey(key);
360 /// Copies the elements of the attributes to an array, starting at a particular index.
362 /// <since_tizen> 3 </since_tizen>
363 /// <param name="array">The destination array.</param>
364 /// <param name="arrayIndex">The zero-based index in an array at which copying begins.</param>
365 /// <example><code><![CDATA[
366 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
367 /// { "state", "ON" },
370 /// KeyValuePair<string, object>[] dest = new KeyValuePair<string, object>[attributes.Count];
372 /// attributes.CopyTo(dest, index);
373 /// Console.WriteLine("Dest conatins ({0}, {1})", dest[0].Key, dest[0].Value);
374 /// ]]></code></example>
375 public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
377 _attributes.CopyTo(array, arrayIndex);
381 /// Returns an enumerator that iterates through the collection.
383 /// <since_tizen> 3 </since_tizen>
384 /// <returns> An enumerator that can be used to iterate through the collection.</returns>
385 /// <example><code><![CDATA[
386 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
387 /// { "state", "ON" },
390 /// foreach (KeyValuePair<string, object> pair in attributes)
392 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
394 /// ]]></code></example>
395 public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
397 return _attributes.GetEnumerator();
401 /// Removes an attribute from collection.
403 /// <since_tizen> 3 </since_tizen>
404 /// <param name="item">The attributes element to remove.</param>
405 /// <returns>true if operation is successful, otherwise, false.</returns>
406 /// <feature>http://tizen.org/feature/iot.ocf</feature>
407 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
408 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
409 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
410 /// <example><code><![CDATA[
411 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
412 /// { "state", "ON" },
415 /// if (attributes.Remove(new KeyValuePair<string, object>("dim", 10)))
416 /// Console.WriteLine("Remove was successful");
417 /// ]]></code></example>
418 public bool Remove(KeyValuePair<string, object> item)
420 return Remove(item.Key);
424 /// Removes an attribute from collection using a key.
426 /// <since_tizen> 3 </since_tizen>
427 /// <param name="key">The attributes element to remove.</param>
428 /// <returns>true if operation is successful, otherwise, false.</returns>
429 /// <feature>http://tizen.org/feature/iot.ocf</feature>
430 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
431 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
432 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
434 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
435 /// { "state", "ON" },
438 /// if (attributes.Remove("state"))
439 /// Console.WriteLine("Remove was successful");
440 /// </code></example>
441 public bool Remove(string key)
443 int ret = Interop.IoTConnectivity.Common.Attributes.Remove(_resourceAttributesHandle, key);
444 if (ret != (int)IoTConnectivityError.None)
446 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove attributes");
447 throw IoTConnectivityErrorFactory.GetException(ret);
450 bool isRemoved = _attributes.Remove(key);
456 /// Gets the value associated with the specified key.
458 /// <since_tizen> 3 </since_tizen>
459 /// <param name="key">The key whose value to get.</param>
460 /// <param name="value"> The value associated with the specified key.</param>
461 /// <returns> true if the attributes collection contains an element with the specified key, otherwise, false.</returns>
463 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
464 /// { "state", "ON" }
467 /// var isPresent = attributes.TryGetValue("state", out value);
469 /// Console.WriteLine("value : {0}", value);
470 /// </code></example>
471 public bool TryGetValue(string key, out object value)
473 return _attributes.TryGetValue(key, out value);
477 /// Returns an enumerator that iterates through the collection.
479 /// <since_tizen> 3 </since_tizen>
480 IEnumerator IEnumerable.GetEnumerator()
482 return _attributes.GetEnumerator();
486 /// Releases any unmanaged resources used by this object.
488 /// <since_tizen> 3 </since_tizen>
489 /// <feature>http://tizen.org/feature/iot.ocf</feature>
490 public void Dispose()
493 GC.SuppressFinalize(this);
497 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
499 /// <since_tizen> 3 </since_tizen>
500 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
501 /// <feature>http://tizen.org/feature/iot.ocf</feature>
502 protected virtual void Dispose(bool disposing)
509 // Free managed objects
512 Interop.IoTConnectivity.Common.Attributes.Destroy(_resourceAttributesHandle);
516 private void SetAttributes(IntPtr attributesHandle)
518 Interop.IoTConnectivity.Common.Attributes.AttributesCallback cb = (IntPtr attributes, string key, IntPtr userData) =>
520 Interop.IoTConnectivity.Common.DataType dataType;
521 int ret = Interop.IoTConnectivity.Common.Attributes.GetType(attributes, key, out dataType);
522 if (ret != (int)IoTConnectivityError.None)
524 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
525 throw IoTConnectivityErrorFactory.GetException(ret);
528 switch ((Interop.IoTConnectivity.Common.DataType)dataType)
530 case Interop.IoTConnectivity.Common.DataType.Int:
533 ret = Interop.IoTConnectivity.Common.Attributes.GetInt(attributes, key, out value);
534 if (ret != (int)IoTConnectivityError.None)
536 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
537 throw IoTConnectivityErrorFactory.GetException(ret);
539 _attributes.Add(key, value);
542 case Interop.IoTConnectivity.Common.DataType.Bool:
545 ret = Interop.IoTConnectivity.Common.Attributes.GetBool(attributes, key, out value);
546 if (ret != (int)IoTConnectivityError.None)
548 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
549 throw IoTConnectivityErrorFactory.GetException(ret);
551 _attributes.Add(key, value);
554 case Interop.IoTConnectivity.Common.DataType.Double:
557 ret = Interop.IoTConnectivity.Common.Attributes.GetDouble(attributes, key, out value);
558 if (ret != (int)IoTConnectivityError.None)
560 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
561 throw IoTConnectivityErrorFactory.GetException(ret);
563 _attributes.Add(key, value);
566 case Interop.IoTConnectivity.Common.DataType.String:
570 ret = Interop.IoTConnectivity.Common.Attributes.GetStr(attributes, key, out value);
571 if (ret != (int)IoTConnectivityError.None)
573 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
574 throw IoTConnectivityErrorFactory.GetException(ret);
576 Str = (value != IntPtr.Zero) ? Marshal.PtrToStringAnsi(value) : string.Empty;
577 _attributes.Add(key, Str);
580 case Interop.IoTConnectivity.Common.DataType.ByteStr:
584 ret = Interop.IoTConnectivity.Common.Attributes.GetByteStr(attributes, key, out byteStrPtr, out byteStrSize);
585 if (ret != (int)IoTConnectivityError.None)
587 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
588 throw IoTConnectivityErrorFactory.GetException(ret);
590 byte[] byteStr = new byte[byteStrSize];
591 Marshal.Copy(byteStrPtr, byteStr, 0, byteStrSize);
592 _attributes.Add(key, byteStr);
595 case Interop.IoTConnectivity.Common.DataType.Null:
597 _attributes.Add(key, null);
600 case Interop.IoTConnectivity.Common.DataType.List:
603 ret = Interop.IoTConnectivity.Common.Attributes.GetList(attributes, key, out listHandle);
604 if (ret != (int)IoTConnectivityError.None)
606 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
607 throw IoTConnectivityErrorFactory.GetException(ret);
609 _attributes.Add(key, List.GetList(listHandle));
612 case Interop.IoTConnectivity.Common.DataType.Attributes:
614 IntPtr attribsHandle;
615 ret = Interop.IoTConnectivity.Common.Attributes.GetAttributes(attributes, key, out attribsHandle);
616 if (ret != (int)IoTConnectivityError.None)
618 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
619 throw IoTConnectivityErrorFactory.GetException(ret);
621 _attributes.Add(key, new Attributes(attribsHandle));
631 int res = Interop.IoTConnectivity.Common.Attributes.Foreach(attributesHandle, cb, IntPtr.Zero);
632 if (res != (int)IoTConnectivityError.None)
634 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove attributes");
635 throw IoTConnectivityErrorFactory.GetException(res);
640 internal static class List
642 internal static IntPtr GetListHandle(object list)
644 IntPtr listHandle = IntPtr.Zero;
648 if (list is IEnumerable<IEnumerable>)
650 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.List, out listHandle);
652 foreach (IEnumerable val in (IEnumerable<IEnumerable>)list)
654 IntPtr childList = GetListHandle(val);
655 ret = Interop.IoTConnectivity.Common.List.AddList(listHandle, childList, pos++);
656 if (ret != (int)IoTConnectivityError.None)
658 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
659 Interop.IoTConnectivity.Common.List.Destroy(childList);
660 throw IoTConnectivityErrorFactory.GetException(ret);
664 else if (list is IEnumerable<int>)
666 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Int, out listHandle);
668 foreach (int val in (IEnumerable<int>)list)
670 ret = Interop.IoTConnectivity.Common.List.AddInt(listHandle, val, pos++);
671 if (ret != (int)IoTConnectivityError.None)
673 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
674 throw IoTConnectivityErrorFactory.GetException(ret);
678 else if (list is IEnumerable<string>)
680 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.String, out listHandle);
682 foreach (string val in (IEnumerable<string>)list)
684 ret = Interop.IoTConnectivity.Common.List.AddStr(listHandle, val, pos++);
685 if (ret != (int)IoTConnectivityError.None)
687 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add str");
688 throw IoTConnectivityErrorFactory.GetException(ret);
692 else if (list is IEnumerable<double>)
694 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Double, out listHandle);
696 foreach (double val in (IEnumerable<double>)list)
698 ret = Interop.IoTConnectivity.Common.List.AddDouble(listHandle, val, pos++);
699 if (ret != (int)IoTConnectivityError.None)
701 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add double");
702 throw IoTConnectivityErrorFactory.GetException(ret);
706 else if (list is IEnumerable<bool>)
708 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Bool, out listHandle);
710 foreach (bool val in (IEnumerable<bool>)list)
712 ret = Interop.IoTConnectivity.Common.List.AddBool(listHandle, val, pos++);
713 if (ret != (int)IoTConnectivityError.None)
715 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool");
716 throw IoTConnectivityErrorFactory.GetException(ret);
720 else if (list is IEnumerable<Attributes>)
722 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Attributes, out listHandle);
724 foreach (Attributes val in (IEnumerable<Attributes>)list)
726 ret = Interop.IoTConnectivity.Common.List.AddAttributes(listHandle, val._resourceAttributesHandle, pos++);
727 if (ret != (int)IoTConnectivityError.None)
729 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
730 throw IoTConnectivityErrorFactory.GetException(ret);
734 else if (list is IEnumerable<byte[]>)
736 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.ByteStr, out listHandle);
738 foreach (byte[] val in (IEnumerable<byte[]>)list)
740 ret = Interop.IoTConnectivity.Common.List.AddByteStr(listHandle, val, val.Length, pos++);
741 if (ret != (int)IoTConnectivityError.None)
743 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add byte[]");
744 throw IoTConnectivityErrorFactory.GetException(ret);
750 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to GetListHandle");
751 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
756 internal static object GetList(IntPtr listHandle)
760 int ret = Interop.IoTConnectivity.Common.List.GetType(listHandle, out type);
761 if (ret != (int)IoTConnectivityError.None)
763 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
764 throw IoTConnectivityErrorFactory.GetException(ret);
766 switch ((Interop.IoTConnectivity.Common.DataType)type)
768 case Interop.IoTConnectivity.Common.DataType.Int:
770 list = new List<int>();
771 Interop.IoTConnectivity.Common.List.IntCallback cb = (int pos, int value, IntPtr userData) =>
776 ret = Interop.IoTConnectivity.Common.List.ForeachInt(listHandle, cb, IntPtr.Zero);
779 case Interop.IoTConnectivity.Common.DataType.Bool:
781 list = new List<bool>();
782 Interop.IoTConnectivity.Common.List.BoolCallback cb = (int pos, bool value, IntPtr userData) =>
787 ret = Interop.IoTConnectivity.Common.List.ForeachBool(listHandle, cb, IntPtr.Zero);
790 case Interop.IoTConnectivity.Common.DataType.Double:
792 list = new List<double>();
793 Interop.IoTConnectivity.Common.List.DoubleCallback cb = (int pos, double value, IntPtr userData) =>
798 ret = Interop.IoTConnectivity.Common.List.ForeachDouble(listHandle, cb, IntPtr.Zero);
801 case Interop.IoTConnectivity.Common.DataType.String:
803 list = new List<string>();
804 Interop.IoTConnectivity.Common.List.StrCallback cb = (int pos, string value, IntPtr userData) =>
809 ret = Interop.IoTConnectivity.Common.List.ForeachStr(listHandle, cb, IntPtr.Zero);
812 case Interop.IoTConnectivity.Common.DataType.Attributes:
814 list = new List<Attributes>();
815 Interop.IoTConnectivity.Common.List.AttribsCallback cb = (int pos, IntPtr value, IntPtr userData) =>
817 list.Add(new Attributes(value));
820 ret = Interop.IoTConnectivity.Common.List.ForeachAttributes(listHandle, cb, IntPtr.Zero);
823 case Interop.IoTConnectivity.Common.DataType.ByteStr:
825 list = new List<byte[]>();
826 Interop.IoTConnectivity.Common.List.ByteStrCallback cb = (int pos, byte[] value, int len, IntPtr userData) =>
831 ret = Interop.IoTConnectivity.Common.List.ForeachByteStr(listHandle, cb, IntPtr.Zero);
834 case Interop.IoTConnectivity.Common.DataType.List:
836 list = new List<List<object>>();
837 Interop.IoTConnectivity.Common.List.ListCallback cb = (int pos, IntPtr value, IntPtr userData) =>
839 object childList = GetList(value);
840 if (childList != null)
844 ret = Interop.IoTConnectivity.Common.List.ForeachList(listHandle, cb, IntPtr.Zero);
850 if (ret != (int)IoTConnectivityError.None)
852 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
853 throw IoTConnectivityErrorFactory.GetException(ret);