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
41 /// Representation repr = new Representation();
43 public Representation()
45 int ret = Interop.IoTConnectivity.Common.Representation.Create(out _representationHandle);
46 if (ret != (int)IoTConnectivityError.None)
48 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
49 throw IoTConnectivityErrorFactory.GetException(ret);
52 _children.CollectionChanged += ChildrenCollectionChanged;
55 // Constructor for cloning native representation object
56 internal Representation(IntPtr handleToClone)
58 int ret = (int)IoTConnectivityError.InvalidParameter;
59 if (handleToClone != IntPtr.Zero)
61 ret = Interop.IoTConnectivity.Common.Representation.Clone(handleToClone, out _representationHandle);
63 if (ret != (int)IoTConnectivityError.None)
65 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
66 throw IoTConnectivityErrorFactory.GetException(ret);
69 _children.CollectionChanged += ChildrenCollectionChanged;
73 /// Destructor of the Representation class.
81 /// The URI path of resource
84 /// Setter can throw exceptions
87 /// Representation repr = new Representation();
88 /// repr.UriPath = "/a/light";
89 /// Console.WriteLine("URI is {0}", repr.UriPath); //Getter
96 int ret = Interop.IoTConnectivity.Common.Representation.GetUriPath(_representationHandle, out path);
97 if (ret != (int)IoTConnectivityError.None)
99 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get uri");
100 throw IoTConnectivityErrorFactory.GetException(ret);
102 return Marshal.PtrToStringAnsi(path);
106 int ret = (int)IoTConnectivityError.InvalidParameter;
108 ret = Interop.IoTConnectivity.Common.Representation.SetUriPath(_representationHandle, value);
109 if (ret != (int)IoTConnectivityError.None)
111 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set uri");
112 throw IoTConnectivityErrorFactory.GetException(ret);
118 /// The type of resource
120 /// <seealso cref="ResourceTypes"/>
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)
128 /// Console.WriteLine("Type is {0}", item);
131 public ResourceTypes Type
136 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out typeHandle);
137 if (ret != (int)IoTConnectivityError.None)
139 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
140 throw IoTConnectivityErrorFactory.GetException(ret);
142 if (typeHandle == IntPtr.Zero)
146 return new ResourceTypes(typeHandle);
150 int ret = (int)IoTConnectivityError.InvalidParameter;
152 ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value._resourceTypeHandle);
153 if (ret != (int)IoTConnectivityError.None)
155 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type");
156 throw IoTConnectivityErrorFactory.GetException(ret);
162 /// The interface of the resource
164 /// <seealso cref="ResourceInterfaces"/>
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)
172 /// Console.WriteLine("Interface is {0}", iface);
175 public ResourceInterfaces Interface
179 IntPtr interfaceHandle;
180 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceInterfaces(_representationHandle, out interfaceHandle);
181 if (ret != (int)IoTConnectivityError.None)
183 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
184 throw IoTConnectivityErrorFactory.GetException(ret);
186 if (interfaceHandle == IntPtr.Zero)
190 return new ResourceInterfaces(interfaceHandle);
194 int ret = (int)IoTConnectivityError.InvalidParameter;
196 ret = Interop.IoTConnectivity.Common.Representation.SetResourceInterfaces(_representationHandle, value.ResourceInterfacesHandle);
197 if (ret != (int)IoTConnectivityError.None)
199 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
200 throw IoTConnectivityErrorFactory.GetException(ret);
206 /// Current attributes of the resource
208 /// <seealso cref="Attributes"/>
210 /// Representation repr = new Representation();
211 /// Attributes attributes = new Attributes() {
212 /// { "state", "ON" },
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);
221 public Attributes Attributes
225 IntPtr attributeHandle;
226 int ret = Interop.IoTConnectivity.Common.Representation.GetAttributes(_representationHandle, out attributeHandle);
227 if (ret != (int)IoTConnectivityError.None)
229 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
230 throw IoTConnectivityErrorFactory.GetException(ret);
232 return new Attributes(attributeHandle);
236 int ret = (int)IoTConnectivityError.InvalidParameter;
239 ret = Interop.IoTConnectivity.Common.Representation.SetAttributes(_representationHandle, value._resourceAttributesHandle);
240 if (ret != (int)IoTConnectivityError.None)
242 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set attributes");
243 throw IoTConnectivityErrorFactory.GetException(ret);
250 /// List of Child resource representation
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;
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)
266 /// Console.WriteLine("Exception caught : " + ex.Message);
269 public ICollection<Representation> Children
278 /// Releases any unmanaged resources used by this object.
280 public void Dispose()
283 GC.SuppressFinalize(this);
287 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
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)
297 // Free managed objects
299 Interface?.Dispose();
300 Attributes?.Dispose();
301 foreach(var child in Children)
307 Interop.IoTConnectivity.Common.Representation.Destroy(_representationHandle);
311 private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
313 if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
315 foreach (Representation r in e.NewItems)
317 int ret = Interop.IoTConnectivity.Common.Representation.AddChild(_representationHandle, r._representationHandle);
318 if (ret != (int)IoTConnectivityError.None)
320 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add child");
321 throw IoTConnectivityErrorFactory.GetException(ret);
325 else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
327 foreach (Representation r in e.NewItems)
329 int ret = Interop.IoTConnectivity.Common.Representation.RemoveChild(_representationHandle, r._representationHandle);
330 if (ret != (int)IoTConnectivityError.None)
332 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove child");
333 throw IoTConnectivityErrorFactory.GetException(ret);