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