1 /// Copyright 2016 by Samsung Electronics, Inc.,
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
10 using System.Collections.Generic;
11 using System.Collections.ObjectModel;
12 using System.Runtime.InteropServices;
14 namespace Tizen.Network.IoTConnectivity
17 /// This class provides API to manage representation.
18 /// A representation is a payload of a request or a response.
20 public class Representation : IDisposable
22 internal IntPtr _representationHandle = IntPtr.Zero;
24 private bool _disposed = false;
25 private ObservableCollection<Representation> _children = new ObservableCollection<Representation>();
28 /// The Representation constructor
32 /// Representation repr = new Representation();
34 public Representation()
36 int ret = Interop.IoTConnectivity.Common.Representation.Create(out _representationHandle);
37 if (ret != (int)IoTConnectivityError.None)
39 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
40 throw IoTConnectivityErrorFactory.GetException(ret);
43 _children.CollectionChanged += ChildrenCollectionChanged;
46 // Constructor for cloning native representation object
47 internal Representation(IntPtr handleToClone)
49 int ret = (int)IoTConnectivityError.InvalidParameter;
50 if (handleToClone != IntPtr.Zero)
52 ret = Interop.IoTConnectivity.Common.Representation.Clone(handleToClone, out _representationHandle);
54 if (ret != (int)IoTConnectivityError.None)
56 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
57 throw IoTConnectivityErrorFactory.GetException(ret);
60 _children.CollectionChanged += ChildrenCollectionChanged;
64 /// Destructor of the Representation class.
72 /// The URI path of resource
75 /// Setter can throw exceptions
78 /// Representation repr = new Representation();
79 /// repr.UriPath = "/a/light";
80 /// Console.WriteLine("URI is {0}", repr.UriPath); //Getter
87 int ret = Interop.IoTConnectivity.Common.Representation.GetUriPath(_representationHandle, out path);
88 if (ret != (int)IoTConnectivityError.None)
90 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get uri");
91 throw IoTConnectivityErrorFactory.GetException(ret);
93 return Marshal.PtrToStringAnsi(path);
97 int ret = (int)IoTConnectivityError.InvalidParameter;
99 ret = Interop.IoTConnectivity.Common.Representation.SetUriPath(_representationHandle, value);
100 if (ret != (int)IoTConnectivityError.None)
102 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set uri");
103 throw IoTConnectivityErrorFactory.GetException(ret);
109 /// The type of resource
111 /// <seealso cref="ResourceTypes"/>
113 /// Representation repr = new Representation();
114 /// ResourceTypes types = new ResourceTypes (new List<string>(){ "org.tizen.light" });
115 /// repr.Type = types;
116 /// var type = repr.Type; // Getter
117 /// foreach (string item in type)
119 /// Console.WriteLine("Type is {0}", item);
122 public ResourceTypes Type
127 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out typeHandle);
128 if (ret != (int)IoTConnectivityError.None)
130 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
131 throw IoTConnectivityErrorFactory.GetException(ret);
133 if (typeHandle == IntPtr.Zero)
137 return new ResourceTypes(typeHandle);
141 int ret = (int)IoTConnectivityError.InvalidParameter;
143 ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value._resourceTypeHandle);
144 if (ret != (int)IoTConnectivityError.None)
146 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type");
147 throw IoTConnectivityErrorFactory.GetException(ret);
153 /// The interface of the resource
155 /// <seealso cref="ResourceInterfaces"/>
157 /// Representation repr = new Representation();
158 /// ResourceInterfaces ifaces = new ResourceInterfaces (new List<string>(){ ResourceInterfaces.DefaultInterface });
159 /// repr.Interface = ifaces;
160 /// var iface = repr.Interface; // Getter
161 /// foreach (string item in iface)
163 /// Console.WriteLine("Interface is {0}", iface);
166 public ResourceInterfaces Interface
170 IntPtr interfaceHandle;
171 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceInterfaces(_representationHandle, out interfaceHandle);
172 if (ret != (int)IoTConnectivityError.None)
174 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
175 throw IoTConnectivityErrorFactory.GetException(ret);
177 if (interfaceHandle == IntPtr.Zero)
181 return new ResourceInterfaces(interfaceHandle);
185 int ret = (int)IoTConnectivityError.InvalidParameter;
187 ret = Interop.IoTConnectivity.Common.Representation.SetResourceInterfaces(_representationHandle, value.ResourceInterfacesHandle);
188 if (ret != (int)IoTConnectivityError.None)
190 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
191 throw IoTConnectivityErrorFactory.GetException(ret);
197 /// Current attributes of the resource
199 /// <seealso cref="Attributes"/>
201 /// Representation repr = new Representation();
202 /// Attributes attributes = new Attributes() {
203 /// { "state", "ON" },
206 /// repr.Attributes = attributes;
207 /// var newAttributes = repr.Attributes; // Getter
208 /// string strval = newAttributes["state"] as string;
209 /// int intval = (int)newAttributes["dim"];
210 /// Console.WriteLine("attributes are {0} and {1}", strval, intval);
212 public Attributes Attributes
216 IntPtr attributeHandle;
217 int ret = Interop.IoTConnectivity.Common.Representation.GetAttributes(_representationHandle, out attributeHandle);
218 if (ret != (int)IoTConnectivityError.None)
220 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get attributes");
221 throw IoTConnectivityErrorFactory.GetException(ret);
223 return new Attributes(attributeHandle);
227 int ret = (int)IoTConnectivityError.InvalidParameter;
230 ret = Interop.IoTConnectivity.Common.Representation.SetAttributes(_representationHandle, value._resourceAttributesHandle);
231 if (ret != (int)IoTConnectivityError.None)
233 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set attributes");
234 throw IoTConnectivityErrorFactory.GetException(ret);
241 /// List of Child resource representation
244 /// Representation repr = new Representation();
245 /// Representation child1 = new Representation();
246 /// ResourceTypes types1 = new ResourceTypes(new List<string>() { "org.tizen.light" });
247 /// child1.Type = types1;
248 /// ResourceInterfaces ifaces1 = new ResourceInterfaces(new List<string>() { ResourceInterfaces.DefaultInterface });
249 /// child1.Interface = ifaces1;
252 /// repr.Children.Add(child1);
253 /// Console.WriteLine("Number of children : {0}", repr.Children.Count);
254 /// Representation firstChild = repr.Children.ElementAt(0);
255 /// } catch(Exception ex)
257 /// Console.WriteLine("Exception caught : " + ex.Message);
260 public ICollection<Representation> Children
269 /// Releases any unmanaged resources used by this object.
271 public void Dispose()
274 GC.SuppressFinalize(this);
278 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
280 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
281 protected virtual void Dispose(bool disposing)
288 // Free managed objects
290 Interface?.Dispose();
291 Attributes?.Dispose();
292 foreach(var child in Children)
298 Interop.IoTConnectivity.Common.Representation.Destroy(_representationHandle);
302 private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
304 if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
306 foreach (Representation r in e.NewItems)
308 int ret = Interop.IoTConnectivity.Common.Representation.AddChild(_representationHandle, r._representationHandle);
309 if (ret != (int)IoTConnectivityError.None)
311 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add child");
312 throw IoTConnectivityErrorFactory.GetException(ret);
316 else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
318 foreach (Representation r in e.NewItems)
320 int ret = Interop.IoTConnectivity.Common.Representation.RemoveChild(_representationHandle, r._representationHandle);
321 if (ret != (int)IoTConnectivityError.None)
323 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove child");
324 throw IoTConnectivityErrorFactory.GetException(ret);