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;
13 namespace Tizen.Network.IoTConnectivity
16 /// Class containing representation of a resource
18 public class Representation : IDisposable
20 internal IntPtr _representationHandle = IntPtr.Zero;
22 private bool _disposed = false;
23 private ObservableCollection<Representation> _children = new ObservableCollection<Representation>();
28 public Representation()
30 int ret = Interop.IoTConnectivity.Common.Representation.Create(out _representationHandle);
31 if (ret != (int)IoTConnectivityError.None)
33 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
34 throw IoTConnectivityErrorFactory.GetException(ret);
37 _children.CollectionChanged += ChildrenCollectionChanged;
40 // Constructor for cloning native representation object
41 internal Representation(IntPtr handleToClone)
43 int ret = (int)IoTConnectivityError.InvalidParameter;
44 if (handleToClone != IntPtr.Zero)
46 ret = Interop.IoTConnectivity.Common.Representation.Clone(handleToClone, out _representationHandle);
48 if (ret != (int)IoTConnectivityError.None)
50 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
51 throw IoTConnectivityErrorFactory.GetException(ret);
54 _children.CollectionChanged += ChildrenCollectionChanged;
63 /// The URI of resource
70 int ret = Interop.IoTConnectivity.Common.Representation.GetUriPath(_representationHandle, out path);
71 if (ret != (int)IoTConnectivityError.None)
73 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get uri");
74 throw IoTConnectivityErrorFactory.GetException(ret);
80 int ret = (int)IoTConnectivityError.InvalidParameter;
82 ret = Interop.IoTConnectivity.Common.Representation.SetUriPath(_representationHandle, value);
83 if (ret != (int)IoTConnectivityError.None)
85 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set uri");
86 throw IoTConnectivityErrorFactory.GetException(ret);
92 /// The type of resource
94 public ResourceTypes Type
99 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out typeHandle);
100 if (ret != (int)IoTConnectivityError.None)
102 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
103 throw IoTConnectivityErrorFactory.GetException(ret);
105 return new ResourceTypes(typeHandle);
109 int ret = (int)IoTConnectivityError.InvalidParameter;
111 ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value._resourceTypeHandle);
112 if (ret != (int)IoTConnectivityError.None)
114 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type");
115 throw IoTConnectivityErrorFactory.GetException(ret);
121 /// The interface of the resource
123 public ResourceInterfaces Interface
127 IntPtr interfaceHandle;
128 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out interfaceHandle);
129 if (ret != (int)IoTConnectivityError.None)
131 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
132 throw IoTConnectivityErrorFactory.GetException(ret);
134 return new ResourceInterfaces(interfaceHandle);
138 int ret = (int)IoTConnectivityError.InvalidParameter;
140 ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value.ResourceInterfacesHandle);
141 if (ret != (int)IoTConnectivityError.None)
143 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
144 throw IoTConnectivityErrorFactory.GetException(ret);
150 /// Current state of the resource
161 int ret = (int)IoTConnectivityError.InvalidParameter;
163 ret = Interop.IoTConnectivity.Common.Representation.SetState(_representationHandle, State._resourceStateHandle);
164 if (ret != (int)IoTConnectivityError.None)
166 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
167 throw IoTConnectivityErrorFactory.GetException(ret);
173 /// List of Child resource representation
175 public ICollection<Representation> Children
183 public void Dispose()
186 GC.SuppressFinalize(this);
189 protected virtual void Dispose(bool disposing)
196 // Free managed objects
202 Interop.IoTConnectivity.Common.Representation.Destroy(_representationHandle);
206 private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
208 if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
210 foreach (Representation r in e.NewItems)
212 int ret = Interop.IoTConnectivity.Common.Representation.AddChild(_representationHandle, r._representationHandle);
213 throw IoTConnectivityErrorFactory.GetException(ret);
216 else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
218 foreach (Representation r in e.NewItems)
220 int ret = Interop.IoTConnectivity.Common.Representation.RemoveChild(_representationHandle, r._representationHandle);
221 throw IoTConnectivityErrorFactory.GetException(ret);