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 APIs to manage representation.
27 /// A representation is a payload of a request or a response.
29 /// <since_tizen> 3 </since_tizen>
30 public class Representation : IDisposable
32 internal IntPtr _representationHandle = IntPtr.Zero;
34 private bool _disposed = false;
35 private ObservableCollection<Representation> _children = new ObservableCollection<Representation>();
38 /// The Representation constructor.
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>
46 /// Representation repr = new Representation();
48 public Representation()
50 int ret = Interop.IoTConnectivity.Common.Representation.Create(out _representationHandle);
51 if (ret != (int)IoTConnectivityError.None)
53 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
54 throw IoTConnectivityErrorFactory.GetException(ret);
57 _children.CollectionChanged += ChildrenCollectionChanged;
60 // Constructor for cloning native representation object
61 internal Representation(IntPtr handleToClone)
63 int ret = (int)IoTConnectivityError.InvalidParameter;
64 if (handleToClone != IntPtr.Zero)
66 ret = Interop.IoTConnectivity.Common.Representation.Clone(handleToClone, out _representationHandle);
68 if (ret != (int)IoTConnectivityError.None)
70 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
71 throw IoTConnectivityErrorFactory.GetException(ret);
74 _children.CollectionChanged += ChildrenCollectionChanged;
78 /// Destructor of the Representation class.
86 /// The URI path of resource.
88 /// <since_tizen> 3 </since_tizen>
90 /// The URI path of resource.
91 /// Setter can throw exceptions.
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>
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
106 int ret = Interop.IoTConnectivity.Common.Representation.GetUriPath(_representationHandle, out path);
107 if (ret != (int)IoTConnectivityError.None)
109 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get uri");
110 throw IoTConnectivityErrorFactory.GetException(ret);
112 return (path != IntPtr.Zero) ? Marshal.PtrToStringAnsi(path) : string.Empty;
116 int ret = (int)IoTConnectivityError.InvalidParameter;
118 ret = Interop.IoTConnectivity.Common.Representation.SetUriPath(_representationHandle, value);
119 if (ret != (int)IoTConnectivityError.None)
121 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set uri");
122 throw IoTConnectivityErrorFactory.GetException(ret);
128 /// The type of resource.
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)
143 /// Console.WriteLine("Type is {0}", item);
145 /// ]]></code></example>
146 public ResourceTypes Type
151 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out typeHandle);
152 if (ret != (int)IoTConnectivityError.None)
154 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
157 return (typeHandle == IntPtr.Zero) ? null : new ResourceTypes(typeHandle);
161 int ret = (int)IoTConnectivityError.InvalidParameter;
163 ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value._resourceTypeHandle);
164 if (ret != (int)IoTConnectivityError.None)
166 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type");
167 throw IoTConnectivityErrorFactory.GetException(ret);
173 /// The interface of the resource.
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)
188 /// Console.WriteLine("Interface is {0}", iface);
190 /// ]]></code></example>
191 public ResourceInterfaces Interface
195 IntPtr interfaceHandle;
196 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceInterfaces(_representationHandle, out interfaceHandle);
197 if (ret != (int)IoTConnectivityError.None)
199 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
202 return (interfaceHandle == IntPtr.Zero) ? null : new ResourceInterfaces(interfaceHandle);
206 int ret = (int)IoTConnectivityError.InvalidParameter;
208 ret = Interop.IoTConnectivity.Common.Representation.SetResourceInterfaces(_representationHandle, value.ResourceInterfacesHandle);
209 if (ret != (int)IoTConnectivityError.None)
211 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
212 throw IoTConnectivityErrorFactory.GetException(ret);
218 /// Current attributes of the resource.
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>
227 /// Representation repr = new Representation();
228 /// Attributes attributes = new Attributes() {
229 /// { "state", "ON" },
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
242 IntPtr attributeHandle;
243 int ret = Interop.IoTConnectivity.Common.Representation.GetAttributes(_representationHandle, out attributeHandle);
244 if (ret != (int)IoTConnectivityError.None)
246 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
247 throw IoTConnectivityErrorFactory.GetException(ret);
249 return (attributeHandle == IntPtr.Zero) ? null : new Attributes(attributeHandle);
253 int ret = (int)IoTConnectivityError.InvalidParameter;
256 ret = Interop.IoTConnectivity.Common.Representation.SetAttributes(_representationHandle, value._resourceAttributesHandle);
257 if (ret != (int)IoTConnectivityError.None)
259 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set attributes");
260 throw IoTConnectivityErrorFactory.GetException(ret);
267 /// List of Child resource representation.
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;
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)
285 /// Console.WriteLine("Exception caught : " + ex.Message);
287 /// ]]></code></example>
288 public ICollection<Representation> Children
297 /// Releases any unmanaged resources used by this object.
299 /// <since_tizen> 3 </since_tizen>
300 /// <feature>http://tizen.org/feature/iot.ocf</feature>
301 public void Dispose()
304 GC.SuppressFinalize(this);
308 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
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)
320 // Free managed objects
322 Interface?.Dispose();
323 Attributes?.Dispose();
324 foreach(var child in Children)
330 Interop.IoTConnectivity.Common.Representation.Destroy(_representationHandle);
334 private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
336 if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
338 foreach (Representation r in e.NewItems)
340 int ret = Interop.IoTConnectivity.Common.Representation.AddChild(_representationHandle, r._representationHandle);
341 if (ret != (int)IoTConnectivityError.None)
343 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add child");
344 throw IoTConnectivityErrorFactory.GetException(ret);
348 else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
350 foreach (Representation r in e.NewItems)
352 int ret = Interop.IoTConnectivity.Common.Representation.RemoveChild(_representationHandle, r._representationHandle);
353 if (ret != (int)IoTConnectivityError.None)
355 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove child");
356 throw IoTConnectivityErrorFactory.GetException(ret);