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