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