8b1ddb19d6402eae085484806dd84a2ce871405d
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / Representation.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.Generic;
11 using System.Collections.ObjectModel;
12 using System.Runtime.InteropServices;
13
14 namespace Tizen.Network.IoTConnectivity
15 {
16     /// <summary>
17     /// This class provides API to manage representation.
18     /// A representation is a payload of a request or a response.
19     /// </summary>
20     public class Representation : IDisposable
21     {
22         internal IntPtr _representationHandle = IntPtr.Zero;
23
24         private bool _disposed = false;
25         private ObservableCollection<Representation> _children = new ObservableCollection<Representation>();
26
27         /// <summary>
28         /// The Representation constructor
29         /// </summary>
30         /// </summary>
31         /// <code>
32         /// Representation repr = new Representation();
33         /// </code>
34         public Representation()
35         {
36             int ret = Interop.IoTConnectivity.Common.Representation.Create(out _representationHandle);
37             if (ret != (int)IoTConnectivityError.None)
38             {
39                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
40                 throw IoTConnectivityErrorFactory.GetException(ret);
41             }
42
43             _children.CollectionChanged += ChildrenCollectionChanged;
44         }
45
46         // Constructor for cloning native representation object
47         internal Representation(IntPtr handleToClone)
48         {
49             int ret = (int)IoTConnectivityError.InvalidParameter;
50             if (handleToClone != IntPtr.Zero)
51             {
52                 ret = Interop.IoTConnectivity.Common.Representation.Clone(handleToClone, out _representationHandle);
53             }
54             if (ret != (int)IoTConnectivityError.None)
55             {
56                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
57                 throw IoTConnectivityErrorFactory.GetException(ret);
58             }
59
60             _children.CollectionChanged += ChildrenCollectionChanged;
61         }
62
63         /// <summary>
64         /// Destructor of the Representation class.
65         /// </summary>
66         ~Representation()
67         {
68             Dispose(false);
69         }
70
71         /// <summary>
72         /// The URI path of resource
73         /// </summary>
74         /// <remarks>
75         /// Setter can throw exceptions
76         /// </remarks>
77         /// <code>
78         /// Representation repr = new Representation();
79         /// repr.UriPath = "/a/light";
80         /// Console.WriteLine("URI is {0}", repr.UriPath);  //Getter
81         /// </code>
82         public string UriPath
83         {
84             get
85             {
86                 IntPtr path;
87                 int ret = Interop.IoTConnectivity.Common.Representation.GetUriPath(_representationHandle, out path);
88                 if (ret != (int)IoTConnectivityError.None)
89                 {
90                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get uri");
91                     throw IoTConnectivityErrorFactory.GetException(ret);
92                 }
93                 return Marshal.PtrToStringAnsi(path);
94             }
95             set
96             {
97                 int ret = (int)IoTConnectivityError.InvalidParameter;
98                 if (value != null)
99                     ret = Interop.IoTConnectivity.Common.Representation.SetUriPath(_representationHandle, value);
100                 if (ret != (int)IoTConnectivityError.None)
101                 {
102                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set uri");
103                     throw IoTConnectivityErrorFactory.GetException(ret);
104                 }
105             }
106         }
107
108         /// <summary>
109         /// The type of resource
110         /// </summary>
111         /// <seealso cref="ResourceTypes"/>
112         /// <code>
113         /// Representation repr = new Representation();
114         /// ResourceTypes types = new ResourceTypes (new List<string>(){ "org.tizen.light" });
115         /// repr.Type = types;
116         /// var type = repr.Type;   // Getter
117         /// foreach (string item in type)
118         /// {
119         ///     Console.WriteLine("Type is {0}", item);
120         /// }
121         /// </code>
122         public ResourceTypes Type
123         {
124             get
125             {
126                 IntPtr typeHandle;
127                 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out typeHandle);
128                 if (ret != (int)IoTConnectivityError.None)
129                 {
130                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
131                     throw IoTConnectivityErrorFactory.GetException(ret);
132                 }
133                 if (typeHandle == IntPtr.Zero)
134                 {
135                     return null;
136                 }
137                 return new ResourceTypes(typeHandle);
138             }
139             set
140             {
141                 int ret = (int)IoTConnectivityError.InvalidParameter;
142                 if (value != null)
143                     ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value._resourceTypeHandle);
144                 if (ret != (int)IoTConnectivityError.None)
145                 {
146                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type");
147                     throw IoTConnectivityErrorFactory.GetException(ret);
148                 }
149             }
150         }
151
152         /// <summary>
153         /// The interface of the resource
154         /// </summary>
155         /// <seealso cref="ResourceInterfaces"/>
156         /// <code>
157         /// Representation repr = new Representation();
158         /// ResourceInterfaces ifaces = new ResourceInterfaces (new List<string>(){ ResourceInterfaces.DefaultInterface });
159         /// repr.Interface = ifaces;
160         /// var iface = repr.Interface;   // Getter
161         /// foreach (string item in iface)
162         /// {
163         ///     Console.WriteLine("Interface is {0}", iface);
164         /// }
165         /// </code>
166         public ResourceInterfaces Interface
167         {
168             get
169             {
170                 IntPtr interfaceHandle;
171                 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceInterfaces(_representationHandle, out interfaceHandle);
172                 if (ret != (int)IoTConnectivityError.None)
173                 {
174                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
175                     throw IoTConnectivityErrorFactory.GetException(ret);
176                 }
177                 if (interfaceHandle == IntPtr.Zero)
178                 {
179                     return null;
180                 }
181                 return new ResourceInterfaces(interfaceHandle);
182             }
183             set
184             {
185                 int ret = (int)IoTConnectivityError.InvalidParameter;
186                 if (value != null)
187                     ret = Interop.IoTConnectivity.Common.Representation.SetResourceInterfaces(_representationHandle, value.ResourceInterfacesHandle);
188                 if (ret != (int)IoTConnectivityError.None)
189                 {
190                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
191                     throw IoTConnectivityErrorFactory.GetException(ret);
192                 }
193             }
194         }
195
196         /// <summary>
197         /// Current attributes of the resource
198         /// </summary>
199         /// <seealso cref="Attributes"/>
200         /// <code>
201         /// Representation repr = new Representation();
202         /// Attributes attributes = new Attributes() {
203         ///     { "state", "ON" },
204         ///     { "dim", 10 }
205         /// };
206         /// repr.Attributes = attributes;
207         /// var newAttributes = repr.Attributes;   // Getter
208         /// string strval = newAttributes["state"] as string;
209         /// int intval = (int)newAttributes["dim"];
210         /// Console.WriteLine("attributes are {0} and {1}", strval, intval);
211         /// </code>
212         public Attributes Attributes
213         {
214             get
215             {
216                 IntPtr attributeHandle;
217                 int ret = Interop.IoTConnectivity.Common.Representation.GetAttributes(_representationHandle, out attributeHandle);
218                 if (ret != (int)IoTConnectivityError.None)
219                 {
220                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
221                     throw IoTConnectivityErrorFactory.GetException(ret);
222                 }
223                 return new Attributes(attributeHandle);
224             }
225             set
226             {
227                 int ret = (int)IoTConnectivityError.InvalidParameter;
228                 if (value != null)
229                 {
230                     ret = Interop.IoTConnectivity.Common.Representation.SetAttributes(_representationHandle, value._resourceAttributesHandle);
231                     if (ret != (int)IoTConnectivityError.None)
232                     {
233                         Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set attributes");
234                         throw IoTConnectivityErrorFactory.GetException(ret);
235                     }
236                 }
237             }
238         }
239
240         /// <summary>
241         /// List of Child resource representation
242         /// </summary>
243         /// <code>
244         /// Representation repr = new Representation();
245         /// Representation child1 = new Representation();
246         /// ResourceTypes types1 = new ResourceTypes(new List<string>() { "org.tizen.light" });
247         /// child1.Type = types1;
248         /// ResourceInterfaces ifaces1 = new ResourceInterfaces(new List<string>() { ResourceInterfaces.DefaultInterface });
249         /// child1.Interface = ifaces1;
250         /// try
251         /// {
252         ///     repr.Children.Add(child1);
253         ///     Console.WriteLine("Number of children : {0}", repr.Children.Count);
254         ///     Representation firstChild = repr.Children.ElementAt(0);
255         /// } catch(Exception ex)
256         /// {
257         ///     Console.WriteLine("Exception caught : " + ex.Message);
258         /// }
259         /// </code>
260         public ICollection<Representation> Children
261         {
262             get
263             {
264                 return _children;
265             }
266         }
267
268         /// <summary>
269         /// Releases any unmanaged resources used by this object.
270         /// </summary>
271         public void Dispose()
272         {
273             Dispose(true);
274             GC.SuppressFinalize(this);
275         }
276
277         /// <summary>
278         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
279         /// </summary>
280         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
281         protected virtual void Dispose(bool disposing)
282         {
283             if (_disposed)
284                 return;
285
286             if (disposing)
287             {
288                 // Free managed objects
289                 Type?.Dispose();
290                 Interface?.Dispose();
291                 Attributes?.Dispose();
292                 foreach(var child in Children)
293                 {
294                     child.Dispose();
295                 }
296             }
297
298             Interop.IoTConnectivity.Common.Representation.Destroy(_representationHandle);
299             _disposed = true;
300         }
301
302         private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
303         {
304             if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
305             {
306                 foreach (Representation r in e.NewItems)
307                 {
308                     int ret = Interop.IoTConnectivity.Common.Representation.AddChild(_representationHandle, r._representationHandle);
309                     if (ret != (int)IoTConnectivityError.None)
310                     {
311                         Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add child");
312                         throw IoTConnectivityErrorFactory.GetException(ret);
313                     }
314                 }
315             }
316             else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
317             {
318                 foreach (Representation r in e.NewItems)
319                 {
320                     int ret = Interop.IoTConnectivity.Common.Representation.RemoveChild(_representationHandle, r._representationHandle);
321                     if (ret != (int)IoTConnectivityError.None)
322                     {
323                         Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove child");
324                         throw IoTConnectivityErrorFactory.GetException(ret);
325                     }
326                 }
327             }
328         }
329     }
330 }