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