/
Resource.cs
executable file
·506 lines (470 loc) · 21 KB
/
Resource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
namespace Tizen.Network.IoTConnectivity
{
/// <summary>
/// Abstract class respresenting a resource.
/// All resources need to inherit from this class.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public abstract class Resource : IDisposable
{
private IntPtr _resourceHandle = IntPtr.Zero;
private bool _disposed = false;
private ObservableCollection<Resource> _children = new ObservableCollection<Resource>();
private IntPtr _observerHandle = IntPtr.Zero;
/// <summary>
/// The constructor.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <remarks>
/// <paramref name="uri" /> format would be relative URI path like '/a/light'
/// and its length must be less than 128.
/// </remarks>
/// <privilege>http://tizen.org/privilege/internet</privilege>
/// <privlevel>public</privlevel>
/// <param name="uri">The URI path of the resource.</param>
/// <param name="types">Resource types.</param>
/// <param name="interfaces">Resource interfaces.</param>
/// <param name="policy">The policies of the resoruce.</param>
/// <feature>http://tizen.org/feature/iot.ocf</feature>
/// <pre>
/// IoTConnectivityServerManager.Initialize() should be called to initialize.
/// </pre>
/// <seealso cref="ResourceTypes"/>
/// <seealso cref="ResourceInterfaces"/>
/// <seealso cref="ResourcePolicy"/>
/// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
/// <exception cref="OutOfMemoryException">Thrown when there is not enough memory.</exception>
/// <example><code><![CDATA[
/// // Create a class which inherits from Resource
/// public class DoorResource : Resource
/// {
/// public DoorResource(string uri, ResourceTypes types, ResourceInterfaces interfaces, ResourcePolicy policy)
/// : base(uri, types, interfaces, policy) {
/// }
/// protected override Response OnDelete(Request request) {
/// // Do something
/// }
/// protected override Response OnGet(Request request) {
/// // Do something
/// }
/// // Override other abstract methods of Resource class
/// }
///
/// // Use it like below
/// ResourceInterfaces ifaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.DefaultInterface });
/// ResourceTypes types = new ResourceTypes(new List<string>(){ "oic.iot.door.new" });
/// Resource resource = new DoorResource("/door/uri1", types, ifaces, ResourcePolicy.Discoverable | ResourcePolicy.Observable);
/// ]]></code></example>
protected Resource(string uri, ResourceTypes types, ResourceInterfaces interfaces, ResourcePolicy policy)
{
UriPath = uri;
Types = types;
Interfaces = interfaces;
Policy = policy;
_children.CollectionChanged += ChildrenCollectionChanged;
int ret = Interop.IoTConnectivity.Server.Observers.Create(out _observerHandle);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create obsever handle");
throw IoTConnectivityErrorFactory.GetException(ret);
}
}
/// <summary>
/// Destructor of the Resource class.
/// </summary>
~Resource()
{
Dispose(false);
}
/// <summary>
/// Type details of the resource.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>Type details of the resource.</value>
public ResourceTypes Types { get; internal set; }
/// <summary>
/// Interface details of the resource.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>Interface details of the resource.</value>
public ResourceInterfaces Interfaces { get; internal set; }
/// <summary>
/// The policies of the resource.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>The policies of the resource.</value>
public ResourcePolicy Policy { get; internal set; }
/// <summary>
/// URI path of the resource.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>URI path of the resource.</value>
public string UriPath { get; internal set; }
/// <summary>
/// List of Child resources.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>List of Child resources.</value>
public ICollection<Resource> Children
{
get
{
return _children;
}
}
internal IntPtr ResourceHandle
{
get
{
return _resourceHandle;
}
set
{
_resourceHandle = value;
}
}
/// <summary>
/// Notify the specified representation and qos.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/internet</privilege>
/// <privlevel>public</privlevel>
/// <param name="representation">Representation.</param>
/// <param name="qos">The quality of service for message transfer.</param>
/// <feature>http://tizen.org/feature/iot.ocf</feature>
/// <pre>
/// IoTConnectivityServerManager.Initialize() should be called to initialize.
/// </pre>
/// <seealso cref="Representation"/>
/// <seealso cref="QualityOfService"/>
/// <exception cref="NotSupportedException">Thrown when the iotcon is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when an application does not have privilege to access.</exception>
/// <exception cref="InvalidOperationException">Thrown when the operation is invalid.</exception>
/// <example><code><![CDATA[
/// ResourceInterfaces ifaces = new ResourceInterfaces(new List<string>(){ ResourceInterfaces.DefaultInterface });
/// ResourceTypes types = new ResourceTypes(new List<string>(){ "oic.iot.door.new.notify" });
/// Resource resource = new DoorResource("/door/uri/new/notify", types, ifaces, ResourcePolicy.Discoverable | ResourcePolicy.Observable);
/// IoTConnectivityServerManager.RegisterResource(resource);
///
/// Representation repr = new Representation();
/// repr.UriPath = "/door/uri/new/notify";
/// repr.Type = new ResourceTypes(new List<string>(){ "oic.iot.door.new.notify" });
/// repr.Attributes = new Attributes() {
/// _attribute, 1 }
/// };
/// resource.Notify(repr, QualityOfService.High);
/// ]]></code></example>
public void Notify(Representation representation, QualityOfService qos)
{
int ret = (int)IoTConnectivityError.None;
ret = Interop.IoTConnectivity.Server.Resource.Notify(_resourceHandle, representation._representationHandle, _observerHandle, (int)qos);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send notification");
throw IoTConnectivityErrorFactory.GetException(ret);
}
}
/// <summary>
/// This is called when the client performs get operation on this resource.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="request">A request from client.</param>
/// <returns>A response having the representation and the result.</returns>
protected abstract Response OnGet(Request request);
/// <summary>
/// This is called when the client performs put operation on this resource.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="request">A request from client.</param>
/// <returns>A response.</returns>
protected abstract Response OnPut(Request request);
/// <summary>
/// This is called when the client performs post operation on this resource.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="request">A request from client.</param>
/// <returns>A response having the representation and the result.</returns>
protected abstract Response OnPost(Request request);
/// <summary>
/// This is called when the client performs delete operation on this resource.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="request">A request from client.</param>
/// <returns>A response.</returns>
protected abstract Response OnDelete(Request request);
/// <summary>
/// Called on the observing event.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="request">A request from client.</param>
/// <param name="type">Observer type.</param>
/// <param name="observeId">Observe identifier.</param>
/// <returns>Returns true if it wants to be observed, else false.</returns>
protected abstract bool OnObserving(Request request, ObserveType type, int observeId);
/// <summary>
/// Releases any unmanaged resources used by this object.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <feature>http://tizen.org/feature/iot.ocf</feature>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
/// <feature>http://tizen.org/feature/iot.ocf</feature>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
Types?.Dispose();
Interfaces?.Dispose();
}
if (_resourceHandle != IntPtr.Zero)
Interop.IoTConnectivity.Server.Resource.Destroy(_resourceHandle);
if (_observerHandle != IntPtr.Zero)
Interop.IoTConnectivity.Server.Observers.Destroy(_observerHandle);
_disposed = true;
}
// This method is used as callback for Resource
internal void OnRequest(IntPtr resourceHandle, IntPtr requestHandle, IntPtr userData)
{
Request request = GetRequest(requestHandle);
Response response = null;
if (request == null)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get Request");
return;
}
try
{
int observeType;
int ret = Interop.IoTConnectivity.Server.Request.GetObserveType(requestHandle, out observeType);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get observe type");
return;
}
if ((ObserveType)observeType != ObserveType.NoType)
{
int observeId;
ret = Interop.IoTConnectivity.Server.Request.GetObserveId(requestHandle, out observeId);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get observe id");
return;
}
switch ((ObserveType)observeType)
{
case ObserveType.Register:
{
if (OnObserving(request, ObserveType.Register, observeId))
{
ret = Interop.IoTConnectivity.Server.Observers.Add(_observerHandle, observeId);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add observer id");
return;
}
break;
}
else
{
// If OnObserving for ObserveType.Register returns false, do not operate for Get operation after Observe operation.
return;
}
}
case ObserveType.Deregister:
{
if (OnObserving(request, ObserveType.Deregister, observeId))
{
ret = Interop.IoTConnectivity.Server.Observers.Remove(_observerHandle, observeId);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove observer id");
return;
}
break;
}
else
{
// If OnObserving for ObserveType.Deregister returns false, do not operate for Get operation after Observe operation.
return;
}
}
}
}
int requestType;
ret = Interop.IoTConnectivity.Server.Request.GetRequestType(requestHandle, out requestType);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get request type");
return;
}
switch ((Interop.IoTConnectivity.Server.RequestType)requestType)
{
case Interop.IoTConnectivity.Server.RequestType.Put:
{
response = OnPut(request);
break;
}
case Interop.IoTConnectivity.Server.RequestType.Get:
{
response = OnGet(request);
break;
}
case Interop.IoTConnectivity.Server.RequestType.Post:
{
response = OnPost(request);
break;
}
case Interop.IoTConnectivity.Server.RequestType.Delete:
{
response = OnDelete(request);
break;
}
default:
break;
}
if (response == null)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send Response");
return;
}
if (!response.Send(requestHandle))
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send Response");
return;
}
}
finally
{
request?.Dispose();
response?.Dispose();
}
}
private Request GetRequest(IntPtr requestHandle)
{
IntPtr hostAddressPtr;
int ret = Interop.IoTConnectivity.Server.Request.GetHostAddress(requestHandle, out hostAddressPtr);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get host address");
return null;
}
IntPtr optionsHandle = IntPtr.Zero;
ret = Interop.IoTConnectivity.Server.Request.GetOptions(requestHandle, out optionsHandle);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get options");
return null;
}
IntPtr queryHandle = IntPtr.Zero;
ret = Interop.IoTConnectivity.Server.Request.GetQuery(requestHandle, out queryHandle);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get Query");
return null;
}
IntPtr representationHandle = IntPtr.Zero;
ret = Interop.IoTConnectivity.Server.Request.GetRepresentation(requestHandle, out representationHandle);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get representation");
return null;
}
ResourceOptions opts = null;
ResourceQuery query = null;
Representation representation = null;
try
{
opts = (optionsHandle == IntPtr.Zero) ? null : new ResourceOptions(optionsHandle);
}
catch (Exception exp)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new ResourceOptions: " + exp.Message);
return null;
}
try
{
query = (queryHandle == IntPtr.Zero) ? null : new ResourceQuery(queryHandle);
}
catch (Exception exp)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new ResourceQuery: " + exp.Message);
opts?.Dispose();
return null;
}
try
{
representation = (representationHandle == IntPtr.Zero) ? null : new Representation(representationHandle);
}
catch (Exception exp)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new Representation: " + exp.Message);
opts?.Dispose();
query?.Dispose();
return null;
}
return new Request()
{
HostAddress = (hostAddressPtr != IntPtr.Zero) ? Marshal.PtrToStringAnsi(hostAddressPtr) : string.Empty,
Options = opts,
Query = query,
Representation = representation
};
}
private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs eventArgs)
{
if (eventArgs.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
foreach (Resource r in eventArgs.NewItems)
{
int ret = Interop.IoTConnectivity.Server.Resource.BindChildResource(_resourceHandle, r._resourceHandle);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to bind resource ");
throw IoTConnectivityErrorFactory.GetException(ret);
}
}
}
else if (eventArgs.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
foreach (Resource r in eventArgs.NewItems)
{
int ret = Interop.IoTConnectivity.Server.Resource.UnbindChildResource(_resourceHandle, r._resourceHandle);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to unbind resource");
throw IoTConnectivityErrorFactory.GetException(ret);
}
}
}
}
}
}