2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
21 using System.Runtime.InteropServices;
23 namespace Tizen.Network.IoTConnectivity
26 /// This class provides API to manage representation.
27 /// A representation is a payload of a request or a response.
29 public class Representation : IDisposable
31 internal IntPtr _representationHandle = IntPtr.Zero;
33 private bool _disposed = false;
34 private ObservableCollection<Representation> _children = new ObservableCollection<Representation>();
37 /// The Representation constructor
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>
44 /// Representation repr = new Representation();
46 public Representation()
48 int ret = Interop.IoTConnectivity.Common.Representation.Create(out _representationHandle);
49 if (ret != (int)IoTConnectivityError.None)
51 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
52 throw IoTConnectivityErrorFactory.GetException(ret);
55 _children.CollectionChanged += ChildrenCollectionChanged;
58 // Constructor for cloning native representation object
59 internal Representation(IntPtr handleToClone)
61 int ret = (int)IoTConnectivityError.InvalidParameter;
62 if (handleToClone != IntPtr.Zero)
64 ret = Interop.IoTConnectivity.Common.Representation.Clone(handleToClone, out _representationHandle);
66 if (ret != (int)IoTConnectivityError.None)
68 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
69 throw IoTConnectivityErrorFactory.GetException(ret);
72 _children.CollectionChanged += ChildrenCollectionChanged;
76 /// Destructor of the Representation class.
84 /// The URI path of resource
87 /// Setter can throw exceptions
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>
93 /// Representation repr = new Representation();
94 /// repr.UriPath = "/a/light";
95 /// Console.WriteLine("URI is {0}", repr.UriPath); //Getter
102 int ret = Interop.IoTConnectivity.Common.Representation.GetUriPath(_representationHandle, out path);
103 if (ret != (int)IoTConnectivityError.None)
105 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get uri");
106 throw IoTConnectivityErrorFactory.GetException(ret);
108 return Marshal.PtrToStringAnsi(path);
112 int ret = (int)IoTConnectivityError.InvalidParameter;
114 ret = Interop.IoTConnectivity.Common.Representation.SetUriPath(_representationHandle, value);
115 if (ret != (int)IoTConnectivityError.None)
117 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set uri");
118 throw IoTConnectivityErrorFactory.GetException(ret);
124 /// The type of resource
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>
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)
137 /// Console.WriteLine("Type is {0}", item);
140 public ResourceTypes Type
145 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out typeHandle);
146 if (ret != (int)IoTConnectivityError.None)
148 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
151 return (typeHandle == IntPtr.Zero) ? null : new ResourceTypes(typeHandle);
155 int ret = (int)IoTConnectivityError.InvalidParameter;
157 ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value._resourceTypeHandle);
158 if (ret != (int)IoTConnectivityError.None)
160 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type");
161 throw IoTConnectivityErrorFactory.GetException(ret);
167 /// The interface of the resource
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>
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)
180 /// Console.WriteLine("Interface is {0}", iface);
183 public ResourceInterfaces Interface
187 IntPtr interfaceHandle;
188 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceInterfaces(_representationHandle, out interfaceHandle);
189 if (ret != (int)IoTConnectivityError.None)
191 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
194 return new ResourceInterfaces(interfaceHandle);
198 int ret = (int)IoTConnectivityError.InvalidParameter;
200 ret = Interop.IoTConnectivity.Common.Representation.SetResourceInterfaces(_representationHandle, value.ResourceInterfacesHandle);
201 if (ret != (int)IoTConnectivityError.None)
203 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
204 throw IoTConnectivityErrorFactory.GetException(ret);
210 /// Current attributes of the resource
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>
217 /// Representation repr = new Representation();
218 /// Attributes attributes = new Attributes() {
219 /// { "state", "ON" },
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);
228 public Attributes Attributes
232 IntPtr attributeHandle;
233 int ret = Interop.IoTConnectivity.Common.Representation.GetAttributes(_representationHandle, out attributeHandle);
234 if (ret != (int)IoTConnectivityError.None)
236 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
237 throw IoTConnectivityErrorFactory.GetException(ret);
239 return new Attributes(attributeHandle);
243 int ret = (int)IoTConnectivityError.InvalidParameter;
246 ret = Interop.IoTConnectivity.Common.Representation.SetAttributes(_representationHandle, value._resourceAttributesHandle);
247 if (ret != (int)IoTConnectivityError.None)
249 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set attributes");
250 throw IoTConnectivityErrorFactory.GetException(ret);
257 /// List of Child resource representation
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;
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)
273 /// Console.WriteLine("Exception caught : " + ex.Message);
276 public ICollection<Representation> Children
285 /// Releases any unmanaged resources used by this object.
287 public void Dispose()
290 GC.SuppressFinalize(this);
294 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
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)
304 // Free managed objects
306 Interface?.Dispose();
307 Attributes?.Dispose();
308 foreach(var child in Children)
314 Interop.IoTConnectivity.Common.Representation.Destroy(_representationHandle);
318 private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
320 if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
322 foreach (Representation r in e.NewItems)
324 int ret = Interop.IoTConnectivity.Common.Representation.AddChild(_representationHandle, r._representationHandle);
325 if (ret != (int)IoTConnectivityError.None)
327 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add child");
328 throw IoTConnectivityErrorFactory.GetException(ret);
332 else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
334 foreach (Representation r in e.NewItems)
336 int ret = Interop.IoTConnectivity.Common.Representation.RemoveChild(_representationHandle, r._representationHandle);
337 if (ret != (int)IoTConnectivityError.None)
339 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove child");
340 throw IoTConnectivityErrorFactory.GetException(ret);