[IoTConnectivity] Added Base implementation
[platform/core/csapi/iotcon.git] / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / Representation.cs
1 /// Copyright 2016 by Samsung Electronics, Inc.,
2 ///
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.
8
9 using System;
10 using System.Collections.Generic;
11 using System.Collections.ObjectModel;
12
13 namespace Tizen.Network.IoTConnectivity
14 {
15     /// <summary>
16     /// Class containing representation of a resource
17     /// </summary>
18     public class Representation : IDisposable
19     {
20         internal IntPtr _representationHandle = IntPtr.Zero;
21
22         private bool _disposed = false;
23         private ObservableCollection<Representation> _children = new ObservableCollection<Representation>();
24
25         /// <summary>
26         /// Constructor
27         /// </summary>
28         public Representation()
29         {
30             int ret = Interop.IoTConnectivity.Common.Representation.Create(out _representationHandle);
31             if (ret != (int)IoTConnectivityError.None)
32             {
33                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
34                 throw IoTConnectivityErrorFactory.GetException(ret);
35             }
36
37             _children.CollectionChanged += ChildrenCollectionChanged;
38         }
39
40         // Constructor for cloning native representation object
41         internal Representation(IntPtr handleToClone)
42         {
43             int ret = (int)IoTConnectivityError.InvalidParameter;
44             if (handleToClone != IntPtr.Zero)
45             {
46                 ret = Interop.IoTConnectivity.Common.Representation.Clone(handleToClone, out _representationHandle);
47             }
48             if (ret != (int)IoTConnectivityError.None)
49             {
50                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation");
51                 throw IoTConnectivityErrorFactory.GetException(ret);
52             }
53
54             _children.CollectionChanged += ChildrenCollectionChanged;
55         }
56
57         ~Representation()
58         {
59             Dispose(false);
60         }
61
62         /// <summary>
63         /// The URI of resource
64         /// </summary>
65         public string UriPath
66         {
67             get
68             {
69                 string path;
70                 int ret = Interop.IoTConnectivity.Common.Representation.GetUriPath(_representationHandle, out path);
71                 if (ret != (int)IoTConnectivityError.None)
72                 {
73                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get uri");
74                     throw IoTConnectivityErrorFactory.GetException(ret);
75                 }
76                 return path;
77             }
78             set
79             {
80                 int ret = (int)IoTConnectivityError.InvalidParameter;
81                 if (value != null)
82                     ret = Interop.IoTConnectivity.Common.Representation.SetUriPath(_representationHandle, value);
83                 if (ret != (int)IoTConnectivityError.None)
84                 {
85                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set uri");
86                     throw IoTConnectivityErrorFactory.GetException(ret);
87                 }
88             }
89         }
90
91         /// <summary>
92         /// The type of resource
93         /// </summary>
94         public ResourceTypes Type
95         {
96             get
97             {
98                 IntPtr typeHandle;
99                 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out typeHandle);
100                 if (ret != (int)IoTConnectivityError.None)
101                 {
102                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
103                     throw IoTConnectivityErrorFactory.GetException(ret);
104                 }
105                 return new ResourceTypes(typeHandle);
106             }
107             set
108             {
109                 int ret = (int)IoTConnectivityError.InvalidParameter;
110                 if (value != null)
111                     ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value._resourceTypeHandle);
112                 if (ret != (int)IoTConnectivityError.None)
113                 {
114                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type");
115                     throw IoTConnectivityErrorFactory.GetException(ret);
116                 }
117             }
118         }
119
120         /// <summary>
121         /// The interface of the resource
122         /// </summary>
123         public ResourceInterfaces Interface
124         {
125             get
126             {
127                 IntPtr interfaceHandle;
128                 int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out interfaceHandle);
129                 if (ret != (int)IoTConnectivityError.None)
130                 {
131                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
132                     throw IoTConnectivityErrorFactory.GetException(ret);
133                 }
134                 return new ResourceInterfaces(interfaceHandle);
135             }
136             set
137             {
138                 int ret = (int)IoTConnectivityError.InvalidParameter;
139                 if (value != null)
140                     ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value.ResourceInterfacesHandle);
141                 if (ret != (int)IoTConnectivityError.None)
142                 {
143                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
144                     throw IoTConnectivityErrorFactory.GetException(ret);
145                 }
146             }
147         }
148
149         /// <summary>
150         /// Current state of the resource
151         /// </summary>
152         public State State
153         {
154             get
155             {
156                 return State;
157             }
158             set
159             {
160                 State = value;
161                 int ret = (int)IoTConnectivityError.InvalidParameter;
162                 if (State != null)
163                     ret = Interop.IoTConnectivity.Common.Representation.SetState(_representationHandle, State._resourceStateHandle);
164                 if (ret != (int)IoTConnectivityError.None)
165                 {
166                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
167                     throw IoTConnectivityErrorFactory.GetException(ret);
168                 }
169             }
170         }
171
172         /// <summary>
173         /// List of Child resource representation
174         /// </summary>
175         public ICollection<Representation> Children
176         {
177             get
178             {
179                 return _children;
180             }
181         }
182
183         public void Dispose()
184         {
185             Dispose(true);
186             GC.SuppressFinalize(this);
187         }
188
189         protected virtual void Dispose(bool disposing)
190         {
191             if (_disposed)
192                 return;
193
194             if (disposing)
195             {
196                 // Free managed objects
197                 Type.Dispose();
198                 Interface.Dispose();
199                 State?.Dispose();
200             }
201
202             Interop.IoTConnectivity.Common.Representation.Destroy(_representationHandle);
203             _disposed = true;
204         }
205
206         private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
207         {
208             if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
209             {
210                 foreach (Representation r in e.NewItems)
211                 {
212                     int ret = Interop.IoTConnectivity.Common.Representation.AddChild(_representationHandle, r._representationHandle);
213                     throw IoTConnectivityErrorFactory.GetException(ret);
214                 }
215             }
216             else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
217             {
218                 foreach (Representation r in e.NewItems)
219                 {
220                     int ret = Interop.IoTConnectivity.Common.Representation.RemoveChild(_representationHandle, r._representationHandle);
221                     throw IoTConnectivityErrorFactory.GetException(ret);
222                 }
223             }
224         }
225     }
226 }