a23bb3fb420e43af862bab9c6ff99585b20e18d7
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / Attributes.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.Runtime.InteropServices;
13
14 namespace Tizen.Network.IoTConnectivity
15 {
16     /// <summary>
17     /// This class represents current attributes of a resource.
18     /// It provides API to manage attributes.
19     /// This class is accessed by using a constructor to create a new instance of this object.
20     /// </summary>
21     public class Attributes : IDictionary<string, object>, IDisposable
22     {
23         internal IntPtr _resourceAttributesHandle = IntPtr.Zero;
24         private readonly IDictionary<string, object> _attributes = new Dictionary<string, object>();
25         private bool _disposed = false;
26
27         /// <summary>
28         /// The Attributes constructor
29         /// </summary>
30         /// <code>
31         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
32         /// </code>
33         public Attributes()
34         {
35             int ret = Interop.IoTConnectivity.Common.Attributes.Create(out _resourceAttributesHandle);
36             if (ret != (int)IoTConnectivityError.None)
37             {
38                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create attributes handle");
39                 throw IoTConnectivityErrorFactory.GetException(ret);
40             }
41         }
42
43         internal Attributes(IntPtr attributesHandleToClone)
44         {
45             int ret = Interop.IoTConnectivity.Common.Attributes.Clone(attributesHandleToClone, out _resourceAttributesHandle);
46             if (ret != (int)IoTConnectivityError.None)
47             {
48                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create attributes handle");
49                 throw IoTConnectivityErrorFactory.GetException(ret);
50             }
51
52             SetAttributes(attributesHandleToClone);
53         }
54
55         /// <summary>
56         /// Destructor of the Attributes class.
57         /// </summary>
58         ~Attributes()
59         {
60             Dispose(false);
61         }
62
63         /// <summary>
64         /// Gets the number of keys
65         /// </summary>
66         /// <code>
67         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
68         /// attributes.Add("brightness", 50);
69         /// var count = attributes.Count;
70         /// Console.WriteLine("There are {0} keys in the attribute object", count);
71         /// </code>
72         public int Count
73         {
74             get
75             {
76                 return _attributes.Count;
77             }
78         }
79
80         /// <summary>
81         /// Represents whether attribute is readonly
82         /// </summary>
83         /// <code>
84         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
85         ///     { "state", "ON" },
86         ///     { "dim", 10 }
87         /// };
88         /// if (attributes.IsReadOnly)
89         ///     Console.WriteLine("Read only attribute");
90         /// </code>
91         public bool IsReadOnly
92         {
93             get
94             {
95                 return _attributes.IsReadOnly;
96             }
97         }
98
99         /// <summary>
100         /// Contains all the attribute keys
101         /// </summary>
102         /// <code>
103         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
104         ///     { "state", "ON" },
105         ///     { "dim", 10 }
106         /// };
107         /// var keys = attributes.Keys;
108         /// Console.WriteLine("Attribute contains keys {0} and {1}", keys.ElementAt(0), keys.ElementAt(1));
109         /// </code>
110         public ICollection<string> Keys
111         {
112             get
113             {
114                 return _attributes.Keys;
115             }
116         }
117
118         /// <summary>
119         /// Contains all the attribute values
120         /// </summary>
121         /// <code>
122         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
123         ///     { "state", "ON" },
124         ///     { "dim", 10 }
125         /// };
126         /// var values = attributes.Values;
127         /// Console.WriteLine("Attribute contains values {0} and {1}", values.ElementAt(0), values.ElementAt(1));
128         /// </code>
129         public ICollection<object> Values
130         {
131             get
132             {
133                 return _attributes.Values;
134             }
135         }
136
137         /// <summary>
138         /// Gets or sets the attribute with the specified key.
139         /// </summary>
140         /// <param name="key">The key of the attribute to get or set.</param>
141         /// <returns>The element with the specified key.</returns>
142         /// <code>
143         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
144         /// attributes["state"] = "ON";
145         /// Console.WriteLine("Attribute value for key 'state' : {0}", attributes["state"]);
146         /// </code>
147         public object this[string key]
148         {
149             get
150             {
151                 if (_attributes.ContainsKey(key))
152                     return _attributes[key];
153                 else
154                     return null;
155             }
156
157             set
158             {
159                 Add(key, value);
160             }
161         }
162
163         /// <summary>
164         /// Adds attribute key and value as a key value pair
165         /// </summary>
166         /// <param name="item">The key value pair to add</param>
167         /// <code>
168         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
169         /// attributes.Add(new KeyValuePair<string, object> ("state", "ON"));
170         /// </code>
171         public void Add(KeyValuePair<string, object> item)
172         {
173             Add(item.Key, item.Value);
174         }
175
176         /// <summary>
177         /// Adds an attribute
178         /// </summary>
179         /// <param name="key">The key representing the attribute</param>
180         /// <param name="value">The value representing the attribute</param>
181         /// <code>
182         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
183         /// attributes.Add("brightness", 50);
184         /// </code>
185         public void Add(string key, object value)
186         {
187             int ret = 0;
188             if (value is int)
189             {
190                 ret = Interop.IoTConnectivity.Common.Attributes.AddInt(_resourceAttributesHandle, key, (int)value);
191                 if (ret != (int)IoTConnectivityError.None)
192                 {
193                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add int");
194                     throw IoTConnectivityErrorFactory.GetException(ret);
195                 }
196             }
197             else if (value is Attributes)
198             {
199                 Attributes attribs = (Attributes)value;
200                 ret = Interop.IoTConnectivity.Common.Attributes.AddAttributes(_resourceAttributesHandle, key, attribs._resourceAttributesHandle);
201                 if (ret != (int)IoTConnectivityError.None)
202                 {
203                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
204                     throw IoTConnectivityErrorFactory.GetException(ret);
205                 }
206             }
207             else if (value is string)
208             {
209                 ret = Interop.IoTConnectivity.Common.Attributes.AddStr(_resourceAttributesHandle, key, (string)value);
210                 if (ret != (int)IoTConnectivityError.None)
211                 {
212                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add string");
213                     throw IoTConnectivityErrorFactory.GetException(ret);
214                 }
215             }
216             else if (value is double)
217             {
218                 ret = Interop.IoTConnectivity.Common.Attributes.AddDouble(_resourceAttributesHandle, key, (double)value);
219                 if (ret != (int)IoTConnectivityError.None)
220                 {
221                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add double");
222                     throw IoTConnectivityErrorFactory.GetException(ret);
223                 }
224             }
225             else if (value is bool)
226             {
227                 ret = Interop.IoTConnectivity.Common.Attributes.AddBool(_resourceAttributesHandle, key, (bool)value);
228                 if (ret != (int)IoTConnectivityError.None)
229                 {
230                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool");
231                     throw IoTConnectivityErrorFactory.GetException(ret);
232                 }
233             }
234             else if (value is byte[])
235             {
236                 byte[] val = value as byte[];
237                 if (val == null)
238                 {
239                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get byte[] val");
240                     throw new ArgumentException("Invalid Parameter");
241                 }
242                 ret = Interop.IoTConnectivity.Common.Attributes.AddByteStr(_resourceAttributesHandle, key, val, val.Length);
243                 if (ret != (int)IoTConnectivityError.None)
244                 {
245                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool");
246                     throw IoTConnectivityErrorFactory.GetException(ret);
247                 }
248             }
249             else if (value is IEnumerable)
250             {
251                 IntPtr listHandle = List.GetListHandle(value);
252                 ret = Interop.IoTConnectivity.Common.Attributes.AddList(_resourceAttributesHandle, key, listHandle);
253                 if (ret != (int)IoTConnectivityError.None)
254                 {
255                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add list");
256                     throw IoTConnectivityErrorFactory.GetException(ret);
257                 }
258             }
259             else
260             {
261                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Add");
262                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
263             }
264             _attributes.Add(key, value);
265         }
266
267         /// <summary>
268         /// Clears attributes collection
269         /// </summary>
270         /// <code>
271         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes();
272         /// attributes.Add("brightness", 50);
273         /// attributes.Clear();
274         /// </code>
275         public void Clear()
276         {
277             foreach (string key in _attributes.Keys)
278             {
279                 int ret = Interop.IoTConnectivity.Common.Attributes.Remove(_resourceAttributesHandle, key);
280                 if (ret != (int)IoTConnectivityError.None)
281                 {
282                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to clear attributes");
283                     throw IoTConnectivityErrorFactory.GetException(ret);
284                 }
285             }
286             _attributes.Clear();
287         }
288
289         /// <summary>
290         /// Checks whether the given key value pair exists in attributes collection
291         /// </summary>
292         /// <param name="item">The status key value pair</param>
293         /// <returns>true if exists. Otherwise, false</returns>
294         /// <code>
295         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
296         ///     { "state", "ON" },
297         ///     { "dim", 10 }
298         /// };
299         /// if (attributes.Contains(new KeyValuePair<string, object> ("dim", 10))
300         ///     Console.WriteLine("Attribute conatins pair ('dim', 10)");
301         /// </code>
302         public bool Contains(KeyValuePair<string, object> item)
303         {
304             return _attributes.Contains(item);
305         }
306
307         /// <summary>
308         /// Checks whether the given key exists in attributes collection
309         /// </summary>
310         /// <param name="key">The status key to look for</param>
311         /// <returns>true if exists. Otherwise, false</returns>
312         /// <code>
313         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
314         ///     { "state", "ON" },
315         ///     { "dim", 10 }
316         /// };
317         /// if (attributes.ContainsKey("dim"))
318         ///     Console.WriteLine("Attribute conatins key : dim");
319         /// </code>
320         public bool ContainsKey(string key)
321         {
322             return _attributes.ContainsKey(key);
323         }
324
325         /// <summary>
326         /// Copies the elements of the attributes to an array, starting at a particular index.
327         /// </summary>
328         /// <param name="array">The destination array</param>
329         /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
330         /// <code>
331         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
332         ///     { "state", "ON" },
333         ///     { "dim", 10 }
334         /// };
335         /// KeyValuePair<string, object>[] dest = new KeyValuePair<string, object>[attributes.Count];
336         /// int index = 0;
337         /// attributes.CopyTo(dest, index);
338         /// Console.WriteLine("Dest conatins ({0}, {1})", dest[0].Key, dest[0].Value);
339         /// </code>
340         public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
341         {
342             _attributes.CopyTo(array, arrayIndex);
343         }
344
345         /// <summary>
346         ///   Returns an enumerator that iterates through the collection.
347         /// </summary>
348         /// <returns> An enumerator that can be used to iterate through the collection.</returns>
349         /// <code>
350         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
351         ///     { "state", "ON" },
352         ///     { "dim", 10 }
353         /// };
354         /// foreach (KeyValuePair<string, object> pair in attributes)
355         /// {
356         ///     Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
357         /// }
358         /// </code>
359         public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
360         {
361             return _attributes.GetEnumerator();
362         }
363
364         /// <summary>
365         /// Removes an attribute from collection
366         /// </summary>
367         /// <param name="item">The attributes element to remove</param>
368         /// <returns>true if operation is success. Otherwise, false</returns>
369         /// <code>
370         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
371         ///     { "state", "ON" },
372         ///     { "dim", 10 }
373         /// };
374         /// if (attributes.Remove(new KeyValuePair<string, object>("dim", 10)))
375         ///     Console.WriteLine("Remove was successful");
376         /// </code>
377         public bool Remove(KeyValuePair<string, object> item)
378         {
379             return Remove(item.Key);
380         }
381
382         /// <summary>
383         ///  Removes an attribute from collection using key
384         /// </summary>
385         /// <param name="key">The attributes element to remove</param>
386         /// <returns>true if operation is successful, Otherwise, false</returns>
387         /// <code>
388         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
389         ///     { "state", "ON" },
390         ///     { "dim", 10 }
391         /// };
392         /// if (attributes.Remove("state"))
393         ///     Console.WriteLine("Remove was successful");
394         /// </code>
395         public bool Remove(string key)
396         {
397             bool isRemoved = _attributes.Remove(key);
398             if (isRemoved)
399             {
400                 int ret = Interop.IoTConnectivity.Common.Attributes.Remove(_resourceAttributesHandle, key);
401                 if (ret != (int)IoTConnectivityError.None)
402                 {
403                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove attributes");
404                     throw IoTConnectivityErrorFactory.GetException(ret);
405                 }
406             }
407
408             return isRemoved;
409         }
410
411         /// <summary>
412         /// Gets the value associated with the specified key.
413         /// </summary>
414         /// <param name="key">The key whose value to get.</param>
415         /// <param name="value"> The value associated with the specified key</param>
416         /// <returns> true if the attributes collection contains an element with the specified key; otherwise, false.</returns>
417         /// <code>
418         /// Tizen.Network.IoTConnectivity.Attributes attributes = new Tizen.Network.IoTConnectivity.Attributes() {
419         ///     { "state", "ON" }
420         /// };
421         /// object value;
422         /// var isPresent = attributes.TryGetValue("state", out value);
423         /// if (isPresent)
424         ///     Console.WriteLine("value : {0}", value);
425         /// </code>
426         public bool TryGetValue(string key, out object value)
427         {
428             return _attributes.TryGetValue(key, out value);
429         }
430
431         /// <summary>
432         ///   Returns an enumerator that iterates through the collection.
433         /// </summary>
434         IEnumerator IEnumerable.GetEnumerator()
435         {
436             return _attributes.GetEnumerator();
437         }
438
439         /// <summary>
440         /// Releases any unmanaged resources used by this object.
441         /// </summary>
442         public void Dispose()
443         {
444             Dispose(true);
445             GC.SuppressFinalize(this);
446         }
447
448         /// <summary>
449         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
450         /// </summary>
451         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
452         protected virtual void Dispose(bool disposing)
453         {
454             if (_disposed)
455                 return;
456
457             if (disposing)
458             {
459                 // Free managed objects
460             }
461
462             Interop.IoTConnectivity.Common.Attributes.Destroy(_resourceAttributesHandle);
463             _disposed = true;
464         }
465
466         private void SetAttributes(IntPtr attributesHandle)
467         {
468             Interop.IoTConnectivity.Common.Attributes.AttributesCallback cb = (IntPtr attributes, string key, IntPtr userData) =>
469             {
470                 Interop.IoTConnectivity.Common.DataType dataType;
471                 int ret = Interop.IoTConnectivity.Common.Attributes.GetType(attributes, key, out dataType);
472                 if (ret != (int)IoTConnectivityError.None)
473                 {
474                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
475                     throw IoTConnectivityErrorFactory.GetException(ret);
476                 }
477
478                 switch ((Interop.IoTConnectivity.Common.DataType)dataType)
479                 {
480                     case Interop.IoTConnectivity.Common.DataType.Int:
481                         {
482                             int value;
483                             ret = Interop.IoTConnectivity.Common.Attributes.GetInt(attributes, key, out value);
484                             if (ret != (int)IoTConnectivityError.None)
485                             {
486                                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
487                                 throw IoTConnectivityErrorFactory.GetException(ret);
488                             }
489                             Add(key, value);
490                             break;
491                         }
492                     case Interop.IoTConnectivity.Common.DataType.Bool:
493                         {
494                             bool value;
495                             ret = Interop.IoTConnectivity.Common.Attributes.GetBool(attributes, key, out value);
496                             if (ret != (int)IoTConnectivityError.None)
497                             {
498                                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
499                                 throw IoTConnectivityErrorFactory.GetException(ret);
500                             }
501                             Add(key, value);
502                             break;
503                         }
504                     case Interop.IoTConnectivity.Common.DataType.Double:
505                         {
506                             double value;
507                             ret = Interop.IoTConnectivity.Common.Attributes.GetDouble(attributes, key, out value);
508                             if (ret != (int)IoTConnectivityError.None)
509                             {
510                                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
511                                 throw IoTConnectivityErrorFactory.GetException(ret);
512                             }
513                             Add(key, value);
514                             break;
515                         }
516                     case Interop.IoTConnectivity.Common.DataType.String:
517                         {
518                             string value;
519                             ret = Interop.IoTConnectivity.Common.Attributes.GetStr(attributes, key, out value);
520                             if (ret != (int)IoTConnectivityError.None)
521                             {
522                                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
523                                 throw IoTConnectivityErrorFactory.GetException(ret);
524                             }
525                             Add(key, value);
526                             break;
527                         }
528                     case Interop.IoTConnectivity.Common.DataType.ByteStr:
529                         {
530                             IntPtr byteStrPtr;
531                             int byteStrSize;
532                             ret = Interop.IoTConnectivity.Common.Attributes.GetByteStr(attributes, key, out byteStrPtr, out byteStrSize);
533                             if (ret != (int)IoTConnectivityError.None)
534                             {
535                                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
536                                 throw IoTConnectivityErrorFactory.GetException(ret);
537                             }
538                             byte[] byteStr = new byte[byteStrSize];
539                             Marshal.Copy(byteStrPtr, byteStr, 0, byteStrSize);
540                             Add(key, byteStr);
541                             break;
542                         }
543                     case Interop.IoTConnectivity.Common.DataType.Null:
544                         {
545                             Add(key, null);
546                             break;
547                         }
548                     case Interop.IoTConnectivity.Common.DataType.List:
549                         {
550                             IntPtr listHandle;
551                             ret = Interop.IoTConnectivity.Common.Attributes.GetList(attributes, key, out listHandle);
552                             if (ret != (int)IoTConnectivityError.None)
553                             {
554                                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
555                                 throw IoTConnectivityErrorFactory.GetException(ret);
556                             }
557                             Add(key, List.GetList(listHandle));
558                             break;
559                         }
560                     case Interop.IoTConnectivity.Common.DataType.Attributes:
561                         {
562                             IntPtr attribsHandle;
563                             ret = Interop.IoTConnectivity.Common.Attributes.GetAttributes(attributes, key, out attribsHandle);
564                             if (ret != (int)IoTConnectivityError.None)
565                             {
566                                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
567                                 throw IoTConnectivityErrorFactory.GetException(ret);
568                             }
569                             Add(key, new Attributes(attribsHandle));
570                             break;
571                         }
572                     default:
573                         break;
574                 }
575
576                 return true;
577             };
578
579             int res = Interop.IoTConnectivity.Common.Attributes.Foreach(attributesHandle, cb, IntPtr.Zero);
580             if (res != (int)IoTConnectivityError.None)
581             {
582                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove attributes");
583                 throw IoTConnectivityErrorFactory.GetException(res);
584             }
585         }
586     }
587
588     internal static class List
589     {
590         internal static IntPtr GetListHandle(object list)
591         {
592             IntPtr listHandle = IntPtr.Zero;
593             int ret;
594             int pos = 0;
595
596             if (list is IEnumerable<IEnumerable>)
597             {
598                 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.List, out listHandle);
599                 pos = 0;
600                 foreach (IEnumerable val in (IEnumerable<IEnumerable>)list)
601                 {
602                     IntPtr childList = GetListHandle(val);
603                     ret = Interop.IoTConnectivity.Common.List.AddList(listHandle, childList, pos++);
604                     if (ret != (int)IoTConnectivityError.None)
605                     {
606                         Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
607                         Interop.IoTConnectivity.Common.List.Destroy(childList);
608                         throw IoTConnectivityErrorFactory.GetException(ret);
609                     }
610                 }
611             }
612             else if (list is IEnumerable<int>)
613             {
614                 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Int, out listHandle);
615                 pos = 0;
616                 foreach (int val in (IEnumerable<int>)list)
617                 {
618                     ret = Interop.IoTConnectivity.Common.List.AddInt(listHandle, val, pos++);
619                     if (ret != (int)IoTConnectivityError.None)
620                     {
621                         Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
622                         throw IoTConnectivityErrorFactory.GetException(ret);
623                     }
624                 }
625             }
626             else if (list is IEnumerable<string>)
627             {
628                 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.String, out listHandle);
629                 pos = 0;
630                 foreach (string val in (IEnumerable<string>)list)
631                 {
632                     ret = Interop.IoTConnectivity.Common.List.AddStr(listHandle, val, pos++);
633                     if (ret != (int)IoTConnectivityError.None)
634                     {
635                         Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add str");
636                         throw IoTConnectivityErrorFactory.GetException(ret);
637                     }
638                 }
639             }
640             else if (list is IEnumerable<double>)
641             {
642                 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Double, out listHandle);
643                 pos = 0;
644                 foreach (double val in (IEnumerable<double>)list)
645                 {
646                     ret = Interop.IoTConnectivity.Common.List.AddDouble(listHandle, val, pos++);
647                     if (ret != (int)IoTConnectivityError.None)
648                     {
649                         Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add double");
650                         throw IoTConnectivityErrorFactory.GetException(ret);
651                     }
652                 }
653             }
654             else if (list is IEnumerable<bool>)
655             {
656                 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Bool, out listHandle);
657                 pos = 0;
658                 foreach (bool val in (IEnumerable<bool>)list)
659                 {
660                     ret = Interop.IoTConnectivity.Common.List.AddBool(listHandle, val, pos++);
661                     if (ret != (int)IoTConnectivityError.None)
662                     {
663                         Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool");
664                         throw IoTConnectivityErrorFactory.GetException(ret);
665                     }
666                 }
667             }
668             else if (list is IEnumerable<Attributes>)
669             {
670                 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Attributes, out listHandle);
671                 pos = 0;
672                 foreach (Attributes val in (IEnumerable<Attributes>)list)
673                 {
674                     ret = Interop.IoTConnectivity.Common.List.AddAttributes(listHandle, val._resourceAttributesHandle, pos++);
675                     if (ret != (int)IoTConnectivityError.None)
676                     {
677                         Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add attributes");
678                         throw IoTConnectivityErrorFactory.GetException(ret);
679                     }
680                 }
681             }
682             else if (list is IEnumerable<byte[]>)
683             {
684                 ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.ByteStr, out listHandle);
685                 pos = 0;
686                 foreach (byte[] val in (IEnumerable<byte[]>)list)
687                 {
688                     ret = Interop.IoTConnectivity.Common.List.AddByteStr(listHandle, val, val.Length, pos++);
689                     if (ret != (int)IoTConnectivityError.None)
690                     {
691                         Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add byte[]");
692                         throw IoTConnectivityErrorFactory.GetException(ret);
693                     }
694                 }
695             }
696             else
697             {
698                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to GetListHandle");
699                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
700             }
701             return listHandle;
702         }
703
704         internal static object GetList(IntPtr listHandle)
705         {
706             IList list = null;
707             int type;
708             int ret = Interop.IoTConnectivity.Common.List.GetType(listHandle, out type);
709             if (ret != (int)IoTConnectivityError.None)
710             {
711                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
712                 throw IoTConnectivityErrorFactory.GetException(ret);
713             }
714             switch ((Interop.IoTConnectivity.Common.DataType)type)
715             {
716                 case Interop.IoTConnectivity.Common.DataType.Int:
717                     {
718                         list = new List<int>();
719                         Interop.IoTConnectivity.Common.List.IntCallback cb = (int pos, int value, IntPtr userData) =>
720                         {
721                             list.Add(value);
722                             return true;
723                         };
724                         ret = Interop.IoTConnectivity.Common.List.ForeachInt(listHandle, cb, IntPtr.Zero);
725                         break;
726                     }
727                 case Interop.IoTConnectivity.Common.DataType.Bool:
728                     {
729                         list = new List<bool>();
730                         Interop.IoTConnectivity.Common.List.BoolCallback cb = (int pos, bool value, IntPtr userData) =>
731                         {
732                             list.Add(value);
733                             return true;
734                         };
735                         ret = Interop.IoTConnectivity.Common.List.ForeachBool(listHandle, cb, IntPtr.Zero);
736                         break;
737                     }
738                 case Interop.IoTConnectivity.Common.DataType.Double:
739                     {
740                         list = new List<double>();
741                         Interop.IoTConnectivity.Common.List.DoubleCallback cb = (int pos, double value, IntPtr userData) =>
742                         {
743                             list.Add(value);
744                             return true;
745                         };
746                         ret = Interop.IoTConnectivity.Common.List.ForeachDouble(listHandle, cb, IntPtr.Zero);
747                         break;
748                     }
749                 case Interop.IoTConnectivity.Common.DataType.String:
750                     {
751                         list = new List<string>();
752                         Interop.IoTConnectivity.Common.List.StrCallback cb = (int pos, string value, IntPtr userData) =>
753                         {
754                             list.Add(value);
755                             return true;
756                         };
757                         ret = Interop.IoTConnectivity.Common.List.ForeachStr(listHandle, cb, IntPtr.Zero);
758                         break;
759                     }
760                 case Interop.IoTConnectivity.Common.DataType.Attributes:
761                     {
762                         list = new List<Attributes>();
763                         Interop.IoTConnectivity.Common.List.AttribsCallback cb = (int pos, IntPtr value, IntPtr userData) =>
764                         {
765                             list.Add(new Attributes(value));
766                             return true;
767                         };
768                         ret = Interop.IoTConnectivity.Common.List.ForeachAttributes(listHandle, cb, IntPtr.Zero);
769                         break;
770                     }
771                 case Interop.IoTConnectivity.Common.DataType.ByteStr:
772                     {
773                         list = new List<byte[]>();
774                         Interop.IoTConnectivity.Common.List.ByteStrCallback cb = (int pos, byte[] value, int len, IntPtr userData) =>
775                         {
776                             list.Add(value);
777                             return true;
778                         };
779                         ret = Interop.IoTConnectivity.Common.List.ForeachByteStr(listHandle, cb, IntPtr.Zero);
780                         break;
781                     }
782                 case Interop.IoTConnectivity.Common.DataType.List:
783                     {
784                         list = new List<List<object>>();
785                         Interop.IoTConnectivity.Common.List.ListCallback cb = (int pos, IntPtr value, IntPtr userData) =>
786                         {
787                             object childList = GetList(value);
788                             list.Add(childList);
789                             return true;
790                         };
791                         ret = Interop.IoTConnectivity.Common.List.ForeachList(listHandle, cb, IntPtr.Zero);
792                         break;
793                     }
794                 default:
795                     break;
796             }
797             if (ret != (int)IoTConnectivityError.None)
798             {
799                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
800                 throw IoTConnectivityErrorFactory.GetException(ret);
801             }
802             return list;
803         }
804     }
805 }