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