-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathLwm2mAccessControlObject.cs
More file actions
91 lines (82 loc) · 2.21 KB
/
Lwm2mAccessControlObject.cs
File metadata and controls
91 lines (82 loc) · 2.21 KB
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
using System;
using System.Threading.Tasks;
using Waher.Networking.CoAP;
using Waher.Persistence;
using Waher.Persistence.Filters;
namespace Waher.Networking.LWM2M
{
/// <summary>
/// LWM2M Access Control object.
/// </summary>
public class Lwm2mAccessControlObject : Lwm2mObject
{
/// <summary>
/// LWM2M Access Control object.
/// </summary>
public Lwm2mAccessControlObject()
: base(2)
{
}
/// <summary>
/// Loads any Bootstrap information.
/// </summary>
public override async Task LoadBootstrapInfo()
{
this.ClearInstances();
foreach (Lwm2mAccessControlObjectInstance Instance in await Database.Find<Lwm2mAccessControlObjectInstance>(
new FilterFieldEqualTo("Id", this.Id), "InstanceId"))
{
try
{
this.Add(Instance);
}
catch (Exception)
{
await Database.Delete(Instance);
}
}
await base.LoadBootstrapInfo();
}
/// <summary>
/// Deletes any Bootstrap information.
/// </summary>
public override async Task DeleteBootstrapInfo()
{
await base.DeleteBootstrapInfo();
this.ClearInstances();
}
/// <summary>
/// If the resource handles subpaths.
/// </summary>
public override bool HandlesSubPaths => true;
/// <summary>
/// If the PUT method is allowed.
/// </summary>
public bool AllowsPUT => true;
/// <summary>
/// Executes the PUT method on the resource.
/// </summary>
/// <param name="Request">CoAP Request</param>
/// <param name="Response">CoAP Response</param>
/// <exception cref="CoapException">If an error occurred when processing the method.</exception>
public async Task PUT(CoapMessage Request, CoapResponse Response)
{
if (this.Client.State == Lwm2mState.Bootstrap)
{
if (!string.IsNullOrEmpty(Request.SubPath) &&
ushort.TryParse(Request.SubPath.Substring(1), out ushort InstanceId))
{
Lwm2mAccessControlObjectInstance Instance = new Lwm2mAccessControlObjectInstance(InstanceId);
this.Add(Instance);
this.Client.Endpoint.Register(Instance);
await Instance.AfterRegister(this.Client);
await Instance.PUT(Request, Response);
}
else
await Response.RST(CoapCode.BadRequest);
}
else
await Response.RST(CoapCode.Unauthorized);
}
}
}