1 /// Copyright 2016 by Samsung Electronics, Inc.,
4 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 using System.Collections;
22 using System.Collections.Generic;
23 using System.Runtime.InteropServices;
25 namespace Tizen.Network.IoTConnectivity
28 /// This class represents current attributes of a resource.
29 /// It provides API to manage attributes.
30 /// This class is accessed by using a constructor to create a new instance of this object.
32 /// <since_tizen> 3 </since_tizen>
33 public class Attributes : IDictionary<string, object>, IDisposable
35 internal IntPtr _resourceAttributesHandle = IntPtr.Zero;
36 private readonly IDictionary<string, object> _attributes = new Dictionary<string, object>();
37 private bool _disposed = false;
40 /// The Attributes constructor.
42 /// <since_tizen> 3 </since_tizen>
43 /// <feature>http://tizen.org/feature/iot.ocf</feature>
44 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
45 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory.</exception>
47 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
51 int ret = Interop.IoTConnectivity.Common.Attributes.Create(out _resourceAttributesHandle);
52 if (ret != (int)IoTConnectivityError.None)
54 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create attributes handle");
55 throw IoTConnectivityErrorFactory.GetException(ret);
59 internal Attributes(IntPtr attributesHandleToClone)
61 int ret = Interop.IoTConnectivity.Common.Attributes.Clone(attributesHandleToClone, out _resourceAttributesHandle);
62 if (ret != (int)IoTConnectivityError.None)
64 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create attributes handle");
65 throw IoTConnectivityErrorFactory.GetException(ret);
68 SetAttributes(_resourceAttributesHandle);
72 /// Destructor of the Attributes class.
80 /// Gets the number of keys.
82 /// <since_tizen> 3 </since_tizen>
83 /// <value>The number of keys.</value>
85 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
86 /// attributes.Add("brightness", 50);
87 /// var count = attributes.Count;
88 /// Console.WriteLine("There are {0} keys in the attribute object", count);
94 return _attributes.Count;
99 /// Represents whether an attribute is readonly.
101 /// <since_tizen> 3 </since_tizen>
102 /// <value>Whether an attribute is readonly.</value>
104 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
105 /// { "state", "ON" },
108 /// if (attributes.IsReadOnly)
109 /// Console.WriteLine("Read only attribute");
111 public bool IsReadOnly
115 return _attributes.IsReadOnly;
120 /// Contains all the attribute keys.
122 /// <since_tizen> 3 </since_tizen>
123 /// <value>All the attribute keys.</value>
125 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
126 /// { "state", "ON" },
129 /// var keys = attributes.Keys;
130 /// Console.WriteLine("Attribute contains keys {0} and {1}", keys.ElementAt(0), keys.ElementAt(1));
132 public ICollection<string> Keys
136 return _attributes.Keys;
141 /// Contains all the attribute values.
143 /// <since_tizen> 3 </since_tizen>
144 /// <value>All the attribute values.</value>
146 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
147 /// { "state", "ON" },
150 /// var values = attributes.Values;
151 /// Console.WriteLine("Attribute contains values {0} and {1}", values.ElementAt(0), values.ElementAt(1));
153 public ICollection<object> Values
157 return _attributes.Values;
162 /// Gets or sets the attribute with the specified key.
164 /// <since_tizen> 3 </since_tizen>
165 /// <value>The attribute with the specified key.</value>
166 /// <param name="key">The key of the attribute to get or set.</param>
167 /// <returns>The element with the specified key.</returns>
169 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
170 /// attributes["state"] = "ON";
171 /// Console.WriteLine("Attribute value for key 'state' : {0}", attributes["state"]);
173 public object this[string key]
177 if (_attributes.ContainsKey(key))
178 return _attributes[key];
190 /// Adds the attribute key and a value as a key value pair.
192 /// <since_tizen> 3 </since_tizen>
193 /// <param name="item">The key value pair to add.</param>
194 /// <feature>http://tizen.org/feature/iot.ocf</feature>
196 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
197 /// attributes.Add(new KeyValuePair<string, object> ("state", "ON"));
199 public void Add(KeyValuePair<string, object> item)
201 Add(item.Key, item.Value);
205 /// Adds an attribute.
207 /// <since_tizen> 3 </since_tizen>
208 /// <param name="key">The key representing the attribute.</param>
209 /// <param name="value">The value representing the attribute.</param>
210 /// <feature>http://tizen.org/feature/iot.ocf</feature>
212 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
213 /// attributes.Add("brightness", 50);
215 public void Add(string key, object value)
220 ret = Interop.IoTConnectivity.Common.Attributes.AddInt(_resourceAttributesHandle, key, (int)value);
221 if (ret != (int)IoTConnectivityError.None)
223 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add int");
224 throw IoTConnectivityErrorFactory.GetException(ret);
227 else if (value is Attributes)
229 Attributes attribs = (Attributes)value;
230 ret = Interop.IoTConnectivity.Common.Attributes.AddAttributes(_resourceAttributesHandle, key, attribs._resourceAttributesHandle);
231 if (ret != (int)IoTConnectivityError.None)
233 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
234 throw IoTConnectivityErrorFactory.GetException(ret);
237 else if (value is string)
239 ret = Interop.IoTConnectivity.Common.Attributes.AddStr(_resourceAttributesHandle, key, (string)value);
240 if (ret != (int)IoTConnectivityError.None)
242 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add string");
243 throw IoTConnectivityErrorFactory.GetException(ret);
246 else if (value is double)
248 ret = Interop.IoTConnectivity.Common.Attributes.AddDouble(_resourceAttributesHandle, key, (double)value);
249 if (ret != (int)IoTConnectivityError.None)
251 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add double");
252 throw IoTConnectivityErrorFactory.GetException(ret);
255 else if (value is bool)
257 ret = Interop.IoTConnectivity.Common.Attributes.AddBool(_resourceAttributesHandle, key, (bool)value);
258 if (ret != (int)IoTConnectivityError.None)
260 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool");
261 throw IoTConnectivityErrorFactory.GetException(ret);
264 else if (value is byte[])
266 byte[] val = value as byte[];
269 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get byte[] val");
270 throw new ArgumentException("Invalid Parameter");
272 ret = Interop.IoTConnectivity.Common.Attributes.AddByteStr(_resourceAttributesHandle, key, val, val.Length);
273 if (ret != (int)IoTConnectivityError.None)
275 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool");
276 throw IoTConnectivityErrorFactory.GetException(ret);
279 else if (value is IEnumerable)
281 IntPtr listHandle = List.GetListHandle(value);
282 ret = Interop.IoTConnectivity.Common.Attributes.AddList(_resourceAttributesHandle, key, listHandle);
283 if (ret != (int)IoTConnectivityError.None)
285 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add list");
286 throw IoTConnectivityErrorFactory.GetException(ret);
291 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Add");
292 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
294 _attributes.Add(key, value);
298 /// Clears attributes collection.
300 /// <since_tizen> 3 </since_tizen>
301 /// <feature>http://tizen.org/feature/iot.ocf</feature>
302 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
303 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
305 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
306 /// attributes.Add("brightness", 50);
307 /// attributes.Clear();
311 foreach (string key in _attributes.Keys)
313 int ret = Interop.IoTConnectivity.Common.Attributes.Remove(_resourceAttributesHandle, key);
314 if (ret != (int)IoTConnectivityError.None)
316 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to clear attributes");
317 throw IoTConnectivityErrorFactory.GetException(ret);
324 /// Checks whether the given key value pair exists in attributes collection.
326 /// <since_tizen> 3 </since_tizen>
327 /// <param name="item">The status key value pair.</param>
328 /// <returns>true if exists. Otherwise, false.</returns>
330 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
331 /// { "state", "ON" },
334 /// if (attributes.Contains(new KeyValuePair<string, object> ("dim", 10))
335 /// Console.WriteLine("Attribute conatins pair ('dim', 10)");
337 public bool Contains(KeyValuePair<string, object> item)
339 return _attributes.Contains(item);
343 /// Checks whether the given key exists in attributes collection.
345 /// <since_tizen> 3 </since_tizen>
346 /// <param name="key">The status key to look for.</param>
347 /// <returns>true if exists. Otherwise, false.</returns>
349 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
350 /// { "state", "ON" },
353 /// if (attributes.ContainsKey("dim"))
354 /// Console.WriteLine("Attribute conatins key : dim");
356 public bool ContainsKey(string key)
358 return _attributes.ContainsKey(key);
362 /// Copies the elements of the attributes to an array, starting at a particular index.
364 /// <since_tizen> 3 </since_tizen>
365 /// <param name="array">The destination array.</param>
366 /// <param name="arrayIndex">The zero-based index in an array at which copying begins.</param>
368 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
369 /// { "state", "ON" },
372 /// KeyValuePair<string, object>[] dest = new KeyValuePair<string, object>[attributes.Count];
374 /// attributes.CopyTo(dest, index);
375 /// Console.WriteLine("Dest conatins ({0}, {1})", dest[0].Key, dest[0].Value);
377 public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
379 _attributes.CopyTo(array, arrayIndex);
383 /// Returns an enumerator that iterates through the collection.
385 /// <since_tizen> 3 </since_tizen>
386 /// <returns> An enumerator that can be used to iterate through the collection.</returns>
388 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
389 /// { "state", "ON" },
392 /// foreach (KeyValuePair<string, object> pair in attributes)
394 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
397 public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
399 return _attributes.GetEnumerator();
403 /// Removes an attribute from collection.
405 /// <since_tizen> 3 </since_tizen>
406 /// <param name="item">The attributes element to remove.</param>
407 /// <returns>true if operation is successful, otherwise, false.</returns>
408 /// <feature>http://tizen.org/feature/iot.ocf</feature>
409 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
410 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
411 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
413 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
414 /// { "state", "ON" },
417 /// if (attributes.Remove(new KeyValuePair<string, object>("dim", 10)))
418 /// Console.WriteLine("Remove was successful");
420 public bool Remove(KeyValuePair<string, object> item)
422 return Remove(item.Key);
426 /// Removes an attribute from collection using a key.
428 /// <since_tizen> 3 </since_tizen>
429 /// <param name="key">The attributes element to remove.</param>
430 /// <returns>true if operation is successful, otherwise, false.</returns>
431 /// <feature>http://tizen.org/feature/iot.ocf</feature>
432 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
433 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
434 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
436 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
437 /// { "state", "ON" },
440 /// if (attributes.Remove("state"))
441 /// Console.WriteLine("Remove was successful");
443 public bool Remove(string key)
445 int ret = Interop.IoTConnectivity.Common.Attributes.Remove(_resourceAttributesHandle, key);
446 if (ret != (int)IoTConnectivityError.None)
448 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove attributes");
449 throw IoTConnectivityErrorFactory.GetException(ret);
452 bool isRemoved = _attributes.Remove(key);
458 /// Gets the value associated with the specified key.
460 /// <since_tizen> 3 </since_tizen>
461 /// <param name="key">The key whose value to get.</param>
462 /// <param name="value"> The value associated with the specified key.</param>
463 /// <returns> true if the attributes collection contains an element with the specified key, otherwise, false.</returns>
465 /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
466 /// { "state", "ON" }
469 /// var isPresent = attributes.TryGetValue("state", out value);
471 /// Console.WriteLine("value : {0}", value);
473 public bool TryGetValue(string key, out object value)
475 return _attributes.TryGetValue(key, out value);
479 /// Returns an enumerator that iterates through the collection.
481 /// <since_tizen> 3 </since_tizen>
482 IEnumerator IEnumerable.GetEnumerator()
484 return _attributes.GetEnumerator();
488 /// Releases any unmanaged resources used by this object.
490 /// <since_tizen> 3 </since_tizen>
491 /// <feature>http://tizen.org/feature/iot.ocf</feature>
492 public void Dispose()
495 GC.SuppressFinalize(this);
499 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
501 /// <since_tizen> 3 </since_tizen>
502 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
503 /// <feature>http://tizen.org/feature/iot.ocf</feature>
504 protected virtual void Dispose(bool disposing)
511 // Free managed objects
514 Interop.IoTConnectivity.Common.Attributes.Destroy(_resourceAttributesHandle);
518 private void SetAttributes(IntPtr attributesHandle)
520 Interop.IoTConnectivity.Common.Attributes.AttributesCallback cb = (IntPtr attributes, string key, IntPtr userData) =>
522 Interop.IoTConnectivity.Common.DataType dataType;
523 int ret = Interop.IoTConnectivity.Common.Attributes.GetType(attributes, key, out dataType);
524 if (ret != (int)IoTConnectivityError.None)
526 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
527 throw IoTConnectivityErrorFactory.GetException(ret);
530 switch ((Interop.IoTConnectivity.Common.DataType)dataType)
532 case Interop.IoTConnectivity.Common.DataType.Int:
535 ret = Interop.IoTConnectivity.Common.Attributes.GetInt(attributes, key, out value);
536 if (ret != (int)IoTConnectivityError.None)
538 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
539 throw IoTConnectivityErrorFactory.GetException(ret);
541 _attributes.Add(key, value);
544 case Interop.IoTConnectivity.Common.DataType.Bool:
547 ret = Interop.IoTConnectivity.Common.Attributes.GetBool(attributes, key, out value);
548 if (ret != (int)IoTConnectivityError.None)
550 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
551 throw IoTConnectivityErrorFactory.GetException(ret);
553 _attributes.Add(key, value);
556 case Interop.IoTConnectivity.Common.DataType.Double:
559 ret = Interop.IoTConnectivity.Common.Attributes.GetDouble(attributes, key, out value);
560 if (ret != (int)IoTConnectivityError.None)
562 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
563 throw IoTConnectivityErrorFactory.GetException(ret);
565 _attributes.Add(key, value);
568 case Interop.IoTConnectivity.Common.DataType.String:
572 ret = Interop.IoTConnectivity.Common.Attributes.GetStr(attributes, key, out value);
573 if (ret != (int)IoTConnectivityError.None)
575 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
576 throw IoTConnectivityErrorFactory.GetException(ret);
578 Str = (value != IntPtr.Zero) ? Marshal.PtrToStringAnsi(value) : string.Empty;
579 _attributes.Add(key, Str);
582 case Interop.IoTConnectivity.Common.DataType.ByteStr:
586 ret = Interop.IoTConnectivity.Common.Attributes.GetByteStr(attributes, key, out byteStrPtr, out byteStrSize);
587 if (ret != (int)IoTConnectivityError.None)
589 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
590 throw IoTConnectivityErrorFactory.GetException(ret);
592 byte[] byteStr = new byte[byteStrSize];
593 Marshal.Copy(byteStrPtr, byteStr, 0, byteStrSize);
594 _attributes.Add(key, byteStr);
597 case Interop.IoTConnectivity.Common.DataType.Null:
599 _attributes.Add(key, null);
602 case Interop.IoTConnectivity.Common.DataType.List:
605 ret = Interop.IoTConnectivity.Common.Attributes.GetList(attributes, key, out listHandle);
606 if (ret != (int)IoTConnectivityError.None)
608 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
609 throw IoTConnectivityErrorFactory.GetException(ret);
611 _attributes.Add(key, List.GetList(listHandle));
614 case Interop.IoTConnectivity.Common.DataType.Attributes:
616 IntPtr attribsHandle;
617 ret = Interop.IoTConnectivity.Common.Attributes.GetAttributes(attributes, key, out attribsHandle);
618 if (ret != (int)IoTConnectivityError.None)
620 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
621 throw IoTConnectivityErrorFactory.GetException(ret);
623 _attributes.Add(key, new Attributes(attribsHandle));
633 int res = Interop.IoTConnectivity.Common.Attributes.Foreach(attributesHandle, cb, IntPtr.Zero);
634 if (res != (int)IoTConnectivityError.None)
636 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove attributes");
637 throw IoTConnectivityErrorFactory.GetException(res);
642 internal static class List
644 internal static IntPtr GetListHandle(object list)
646 IntPtr listHandle = IntPtr.Zero;
650 if (list is IEnumerable<IEnumerable>)
652 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.List, out listHandle);
654 foreach (IEnumerable val in (IEnumerable<IEnumerable>)list)
656 IntPtr childList = GetListHandle(val);
657 ret = Interop.IoTConnectivity.Common.List.AddList(listHandle, childList, pos++);
658 if (ret != (int)IoTConnectivityError.None)
660 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
661 Interop.IoTConnectivity.Common.List.Destroy(childList);
662 throw IoTConnectivityErrorFactory.GetException(ret);
666 else if (list is IEnumerable<int>)
668 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Int, out listHandle);
670 foreach (int val in (IEnumerable<int>)list)
672 ret = Interop.IoTConnectivity.Common.List.AddInt(listHandle, val, pos++);
673 if (ret != (int)IoTConnectivityError.None)
675 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
676 throw IoTConnectivityErrorFactory.GetException(ret);
680 else if (list is IEnumerable<string>)
682 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.String, out listHandle);
684 foreach (string val in (IEnumerable<string>)list)
686 ret = Interop.IoTConnectivity.Common.List.AddStr(listHandle, val, pos++);
687 if (ret != (int)IoTConnectivityError.None)
689 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add str");
690 throw IoTConnectivityErrorFactory.GetException(ret);
694 else if (list is IEnumerable<double>)
696 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Double, out listHandle);
698 foreach (double val in (IEnumerable<double>)list)
700 ret = Interop.IoTConnectivity.Common.List.AddDouble(listHandle, val, pos++);
701 if (ret != (int)IoTConnectivityError.None)
703 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add double");
704 throw IoTConnectivityErrorFactory.GetException(ret);
708 else if (list is IEnumerable<bool>)
710 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Bool, out listHandle);
712 foreach (bool val in (IEnumerable<bool>)list)
714 ret = Interop.IoTConnectivity.Common.List.AddBool(listHandle, val, pos++);
715 if (ret != (int)IoTConnectivityError.None)
717 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool");
718 throw IoTConnectivityErrorFactory.GetException(ret);
722 else if (list is IEnumerable<Attributes>)
724 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Attributes, out listHandle);
726 foreach (Attributes val in (IEnumerable<Attributes>)list)
728 ret = Interop.IoTConnectivity.Common.List.AddAttributes(listHandle, val._resourceAttributesHandle, pos++);
729 if (ret != (int)IoTConnectivityError.None)
731 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
732 throw IoTConnectivityErrorFactory.GetException(ret);
736 else if (list is IEnumerable<byte[]>)
738 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.ByteStr, out listHandle);
740 foreach (byte[] val in (IEnumerable<byte[]>)list)
742 ret = Interop.IoTConnectivity.Common.List.AddByteStr(listHandle, val, val.Length, pos++);
743 if (ret != (int)IoTConnectivityError.None)
745 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add byte[]");
746 throw IoTConnectivityErrorFactory.GetException(ret);
752 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to GetListHandle");
753 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
758 internal static object GetList(IntPtr listHandle)
762 int ret = Interop.IoTConnectivity.Common.List.GetType(listHandle, out type);
763 if (ret != (int)IoTConnectivityError.None)
765 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
766 throw IoTConnectivityErrorFactory.GetException(ret);
768 switch ((Interop.IoTConnectivity.Common.DataType)type)
770 case Interop.IoTConnectivity.Common.DataType.Int:
772 list = new List<int>();
773 Interop.IoTConnectivity.Common.List.IntCallback cb = (int pos, int value, IntPtr userData) =>
778 ret = Interop.IoTConnectivity.Common.List.ForeachInt(listHandle, cb, IntPtr.Zero);
781 case Interop.IoTConnectivity.Common.DataType.Bool:
783 list = new List<bool>();
784 Interop.IoTConnectivity.Common.List.BoolCallback cb = (int pos, bool value, IntPtr userData) =>
789 ret = Interop.IoTConnectivity.Common.List.ForeachBool(listHandle, cb, IntPtr.Zero);
792 case Interop.IoTConnectivity.Common.DataType.Double:
794 list = new List<double>();
795 Interop.IoTConnectivity.Common.List.DoubleCallback cb = (int pos, double value, IntPtr userData) =>
800 ret = Interop.IoTConnectivity.Common.List.ForeachDouble(listHandle, cb, IntPtr.Zero);
803 case Interop.IoTConnectivity.Common.DataType.String:
805 list = new List<string>();
806 Interop.IoTConnectivity.Common.List.StrCallback cb = (int pos, string value, IntPtr userData) =>
811 ret = Interop.IoTConnectivity.Common.List.ForeachStr(listHandle, cb, IntPtr.Zero);
814 case Interop.IoTConnectivity.Common.DataType.Attributes:
816 list = new List<Attributes>();
817 Interop.IoTConnectivity.Common.List.AttribsCallback cb = (int pos, IntPtr value, IntPtr userData) =>
819 list.Add(new Attributes(value));
822 ret = Interop.IoTConnectivity.Common.List.ForeachAttributes(listHandle, cb, IntPtr.Zero);
825 case Interop.IoTConnectivity.Common.DataType.ByteStr:
827 list = new List<byte[]>();
828 Interop.IoTConnectivity.Common.List.ByteStrCallback cb = (int pos, byte[] value, int len, IntPtr userData) =>
833 ret = Interop.IoTConnectivity.Common.List.ForeachByteStr(listHandle, cb, IntPtr.Zero);
836 case Interop.IoTConnectivity.Common.DataType.List:
838 list = new List<List<object>>();
839 Interop.IoTConnectivity.Common.List.ListCallback cb = (int pos, IntPtr value, IntPtr userData) =>
841 object childList = GetList(value);
842 if (childList != null)
846 ret = Interop.IoTConnectivity.Common.List.ForeachList(listHandle, cb, IntPtr.Zero);
852 if (ret != (int)IoTConnectivityError.None)
854 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
855 throw IoTConnectivityErrorFactory.GetException(ret);