-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathLwm2mServerObjectInstance.cs
231 lines (204 loc) · 7.86 KB
/
Lwm2mServerObjectInstance.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
using System;
using System.Threading.Tasks;
using Waher.Networking.CoAP;
using Waher.Persistence;
using Waher.Persistence.Attributes;
namespace Waher.Networking.LWM2M
{
/// <summary>
/// LWM2M Server object instance.
/// </summary>
public class Lwm2mServerObjectInstance : Lwm2mObjectInstance, ICoapDeleteMethod, ICoapPutMethod, ICoapPostMethod
{
/// <summary>
/// Used as link to associate server Object Instance.
/// </summary>
private readonly Lwm2mResourceInteger shortServerId;
/// <summary>
/// Specify the lifetime of the registration in seconds (see Section 5.3 Registration).
/// </summary>
private readonly Lwm2mResourceInteger lifetimeSeconds;
/// <summary>
/// If true, the LwM2M Client stores “Notify” operations to the LwM2M Server while the
/// LwM2M Server account is disabled or the LwM2M Client is offline. After the LwM2M Server
/// account is enabled or the LwM2M Client is online, the LwM2M Client reports the stored
/// “Notify” operations to the Server.
///
/// If false, the LwM2M Client discards all the “Notify” operations or temporarily disables
/// the Observe function while the LwM2M Server is disabled or the LwM2M Client is offline.
///
/// The default value is true. The maximum number of storing Notifications per Server is up
/// to the implementation.
/// </summary>
private readonly Lwm2mResourceBoolean notificationStoring = null;
/// <summary>
/// This Resource defines the transport binding configured for the LwM2M Client.
///
/// If the LwM2M Client supports the binding specified in this Resource, the LwM2M Client
/// MUST use that transport for the Current Binding Mode.
/// </summary>
private readonly Lwm2mResourceString binding = null;
/// <summary>
/// If this Resource is executed the LwM2M Client MUST perform an “Update” operation
/// with this LwM2M Server using that transport for the Current Binding Mode.
/// </summary>
private readonly Lwm2mResourceCommand registrationUpdateTrigger = null;
/// <summary>
/// LWM2M Server object instance.
/// </summary>
public Lwm2mServerObjectInstance()
: this(0)
{
}
/// <summary>
/// LWM2M Server object instance.
/// </summary>
/// <param name="InstanceId">ID of object instance.</param>
public Lwm2mServerObjectInstance(ushort InstanceId)
: base(1, InstanceId)
{
// E.2 LwM2M Object: LwM2M Server
// http://www.openmobilealliance.org/release/LightweightM2M/V1_0-20170208-A/OMA-TS-LightweightM2M-V1_0-20170208-A.pdf
this.shortServerId = new Lwm2mResourceInteger("ShortServerId", 1, InstanceId, 0, false, false, null, false);
this.lifetimeSeconds = new Lwm2mResourceInteger("LifetimeSeconds", 1, InstanceId, 1, true, false, null, false);
this.notificationStoring = new Lwm2mResourceBoolean("NotificationStoring", 1, InstanceId, 6, true, false, null);
this.binding = new Lwm2mResourceString("Binding", 1, InstanceId, 7, true, false, null);
this.registrationUpdateTrigger = new Lwm2mResourceCommand("RegistrationUpdateTrigger", 1, InstanceId, 8);
this.registrationUpdateTrigger.OnExecute += this.RegistrationUpdateTrigger_OnExecute;
this.Add(this.shortServerId);
this.Add(this.lifetimeSeconds);
this.Add(new Lwm2mResourceNotSupported(1, InstanceId, 2)); // Default Minimum Period
this.Add(new Lwm2mResourceNotSupported(1, InstanceId, 3)); // Default Maximum Period
this.Add(new Lwm2mResourceNotSupported(1, InstanceId, 4)); // Disable
this.Add(new Lwm2mResourceNotSupported(1, InstanceId, 5)); // Disable Timeout
this.Add(this.notificationStoring);
this.Add(this.binding);
this.Add(this.registrationUpdateTrigger);
}
private Task RegistrationUpdateTrigger_OnExecute(object Sender, EventArgs e)
{
return this.Object.Client.RegisterUpdate();
}
/// <summary>
/// Used as link to associate server Object Instance.
/// </summary>
[DefaultValueNull]
public ushort? ShortServerId
{
get { return (ushort?)this.shortServerId.IntegerValue; }
set { this.shortServerId.IntegerValue = value; }
}
/// <summary>
/// Specify the lifetime of the registration in seconds (see Section 5.3 Registration).
/// </summary>
[DefaultValueNull]
public long? LifetimeSeconds
{
get { return this.lifetimeSeconds.IntegerValue; }
set { this.lifetimeSeconds.IntegerValue = value; }
}
/// <summary>
/// If true, the LwM2M Client stores “Notify” operations to the LwM2M Server while the
/// LwM2M Server account is disabled or the LwM2M Client is offline. After the LwM2M Server
/// account is enabled or the LwM2M Client is online, the LwM2M Client reports the stored
/// “Notify” operations to the Server.
///
/// If false, the LwM2M Client discards all the “Notify” operations or temporarily disables
/// the Observe function while the LwM2M Server is disabled or the LwM2M Client is offline.
///
/// The default value is true. The maximum number of storing Notifications per Server is up
/// to the implementation.
/// </summary>
[DefaultValueNull]
public bool? NotificationStoring
{
get { return this.notificationStoring.BooleanValue; }
set { this.notificationStoring.BooleanValue = value; }
}
/// <summary>
/// This Resource defines the transport binding configured for the LwM2M Client.
///
/// If the LwM2M Client supports the binding specified in this Resource, the LwM2M Client
/// MUST use that transport for the Current Binding Mode.
/// </summary>
[DefaultValueNull]
public string Binding
{
get { return this.binding.StringValue; }
set { this.binding.StringValue = value; }
}
/// <summary>
/// If the DELETE method is allowed.
/// </summary>
public bool AllowsDELETE => true;
/// <summary>
/// Executes the DELETE 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 DELETE(CoapMessage Request, CoapResponse Response)
{
if (this.Object.Client.State == Lwm2mState.Bootstrap &&
this.Object.Client.IsFromBootstrapServer(Request))
{
await this.DeleteBootstrapInfo();
await Response.ACK(CoapCode.Deleted);
}
else
await Response.RST(CoapCode.Unauthorized);
}
/// <summary>
/// Deletes any Bootstrap information.
/// </summary>
public override async Task DeleteBootstrapInfo()
{
if (!(this.ObjectId is null))
await Database.Delete(this);
}
/// <summary>
/// Applies any Bootstrap information.
/// </summary>
public override async Task ApplyBootstrapInfo()
{
if (this.ObjectId is null)
await Database.Insert(this);
else
await Database.Update(this);
}
/// <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 override Task PUT(CoapMessage Request, CoapResponse Response)
{
if (this.Object.Client.State == Lwm2mState.Bootstrap &&
this.Object.Client.IsFromBootstrapServer(Request))
{
return base.PUT(Request, Response);
}
else
{
Response.RST(CoapCode.Unauthorized);
return Task.CompletedTask;
}
}
internal async Task<bool> Register(Lwm2mClient Client)
{
if (!this.shortServerId.IntegerValue.HasValue)
return false;
Lwm2mSecurityObjectInstance SecurityInfo = Client.GetSecurityInfo(
(ushort)this.shortServerId.IntegerValue.Value);
if (SecurityInfo is null)
return false;
Lwm2mServerReference Ref = SecurityInfo.GetServerReference(false);
if (Ref is null)
return false;
await Client.Register(this.lifetimeSeconds.IntegerValue.HasValue ?
(int)this.lifetimeSeconds.IntegerValue.Value : 86400, Ref);
return true;
}
}
}