-
Notifications
You must be signed in to change notification settings - Fork 17
/
betwixt.go
243 lines (206 loc) · 6.55 KB
/
betwixt.go
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
package betwixt
import (
"time"
"github.com/zubairhamed/canopus"
)
// LWM2MObjectType represents a LWM2M Object Type
type LWM2MObjectType uint16
// LWM2MResourceType represents a LWM2M Resource Type
type LWM2MResourceType uint16
// LWM2MObjectInstances contains instances of LWM2M Objects by Type
type LWM2MObjectInstances map[LWM2MObjectType]Object
// Event Callback
type FnEvent func()
// Event Callback for OnStartup
type FnOnStartup func()
// Event Callback for a read request
type FnOnRead func()
// Event Callback for a write request
type FnOnWrite func()
// Event Callback for an execute request
type FnOnExecute func()
// Event Callback for a register request
type FnOnRegistered func(RegisteredClient)
// Event Callback for a deregister request
type FnOnDeregistered func()
// Event Callback when an error occurs
type FnOnError func()
type OperationType byte
type EventType int
const (
EVENT_START EventType = 0
)
// LWM2M Operation Types
type OperationCode int
const (
OPERATION_NONE OperationCode = 0
OPERATION_R OperationCode = 1
OPERATION_W OperationCode = 2
OPERATION_RW OperationCode = 3
OPERATION_E OperationCode = 4
OPERATION_RE OperationCode = 5
OPERATION_WE OperationCode = 6
OPERATION_RWE OperationCode = 7
)
// Type of LWM2M value
type IdentifierType byte
const (
IDENTIFIER_OBJECT_INSTANCE IdentifierType = 0
IDENTIFIER_RESOURCE_INSTANCE IdentifierType = 1
IDENTIFIER_RESOURCES IdentifierType = 2
IDENTIFIER_RESOURCE_WITH_VALUE IdentifierType = 3
)
// Binding Modes
type BindingMode string
const (
BINDINGMODE_UDP BindingMode = "U"
BINDINGMODE_UDP_WITH_QUEUE_MODE BindingMode = "UQ"
BINDINGMODE_SMS BindingMode = "S"
BINDINGMODE_SMS_WITH_QUEUE_MODE BindingMode = "SQ"
BINDINGMODE_UDP_AND_SMS BindingMode = "US"
BINDINGMODE_UDP_WITH_QUEUE_MODE_AND_SMS BindingMode = "UQS"
)
// Operation Types
const (
OPERATIONTYPE_REGISTER OperationType = 0
OPERATIONTYPE_UPDATE OperationType = 1
OPERATIONTYPE_DEREGISTER OperationType = 2
OPERATIONTYPE_READ OperationType = 3
OPERATIONTYPE_DISCOVER OperationType = 4
OPERATIONTYPE_WRITE OperationType = 5
OPERATIONTYPE_WRITE_ATTRIBUTES OperationType = 6
OPERATIONTYPE_EXECUTE OperationType = 7
OPERATIONTYPE_CREATE OperationType = 8
OPERATIONTYPE_DELETE OperationType = 9
OPERATIONTYPE_OBSERVE OperationType = 10
OPERATIONTYPE_NOTIFY OperationType = 11
OPERATIONTYPE_CANCEL_OBSERVE OperationType = 12
)
// ObjectEnabler interface to handler any incoming requests from a server for a given object
type ObjectEnabler interface {
OnRead(int, int, Lwm2mRequest) Lwm2mResponse
OnDelete(int, Lwm2mRequest) Lwm2mResponse
OnWrite(int, int, Lwm2mRequest) Lwm2mResponse
OnCreate(int, int, Lwm2mRequest) Lwm2mResponse
OnExecute(int, int, Lwm2mRequest) Lwm2mResponse
}
// ObjectSource interface representing a source consumed by a Registry to resolve and retrieve
// LWM2M object definitions
type ObjectSource interface {
Initialize()
GetObject(n LWM2MObjectType) ObjectDefinition
GetObjects() map[LWM2MObjectType]ObjectDefinition
AddObject(m ObjectDefinition, res []ResourceDefinition)
}
// Registry interface represents a source from which LWM2M object definitions can be looked up/resolved or
// stored
type Registry interface {
GetDefinition(LWM2MObjectType) ObjectDefinition
Register(ObjectSource)
GetMandatory() []ObjectDefinition
GetDefinitions() []ObjectDefinition
}
// ObjectDefinition interface defines a LWM2M Object
type ObjectDefinition interface {
GetName() string
GetType() LWM2MObjectType
GetDescription() string
SetResources([]ResourceDefinition)
GetResources() []ResourceDefinition
GetResource(n LWM2MResourceType) ResourceDefinition
AllowMultiple() bool
IsMandatory() bool
}
// ResourceDefinition interface defines a LWM2M Resource
type ResourceDefinition interface {
GetId() LWM2MResourceType
GetName() string
GetDescription() string
GetUnits() string
GetRangeOrEnums() string
IsMandatory() bool
MultipleValuesAllowed() bool
GetResourceType() ValueTypeCode
GetOperations() OperationCode
}
// LWM2MClient interface defining a LWM2M Client
type LWM2MClient interface {
AddObjectInstance(LWM2MObjectType, int) error
AddObjectInstances(LWM2MObjectType, ...int)
AddResource()
AddObject()
Register(string) (string, error)
Deregister()
Update()
UseRegistry(Registry)
EnableObject(LWM2MObjectType, ObjectEnabler) error
SetEnabler(LWM2MObjectType, ObjectEnabler)
GetRegistry() Registry
GetEnabledObjects() map[LWM2MObjectType]Object
GetObject(n LWM2MObjectType) Object
Start()
// Events
OnStartup(FnOnStartup)
OnRead(FnOnRead)
OnWrite(FnOnWrite)
OnExecute(FnOnExecute)
OnError(FnOnError)
}
// Lwm2mRequest interface represents an incoming request from a server
type Lwm2mRequest interface {
GetPath() string
GetMessage() *canopus.Message
GetOperationType() OperationType
GetCoapRequest() canopus.CoapRequest
}
// Lwm2mResponse interface represents an outgoing response to a server
type Lwm2mResponse interface {
GetResponseCode() canopus.CoapCode
GetResponseValue() Value
}
// Server interface defines a LWM2M Server
type Server interface {
UseRegistry(Registry)
On(EventType, FnEvent)
Start()
GetClients() map[string]RegisteredClient
GetClient(id string) RegisteredClient
GetStats() ServerStatistics
GetCoapServer() canopus.CoapServer
}
// RegisteredClient interface is an instance of a client registered on a server
type RegisteredClient interface {
GetId() string
GetName() string
GetLifetime() int
GetVersion() string
GetBindingMode() BindingMode
GetSmsNumber() string
GetRegistrationDate() time.Time
Update()
LastUpdate() time.Time
SetObjects(map[LWM2MObjectType]Object)
GetObjects() map[LWM2MObjectType]Object
GetObject(LWM2MObjectType) Object
GetAddress() string
ReadObject(uint16, uint16) (Value, error)
ReadResource(object uint16, instance uint16, resource uint16) (Value, error)
Delete(int, int)
Execute(int, int, int)
}
// An Object interface represents an Object used on a client or Objects supported by a Registered Client on a server
// Not to be confused with ObjectDefinition, which represents the definition of an Object
type Object interface {
AddInstance(int)
RemoveInstance(int)
GetInstances() []int
GetEnabler() ObjectEnabler
GetType() LWM2MObjectType
GetDefinition() ObjectDefinition
SetEnabler(ObjectEnabler)
}
// ServerStatistics Statistics recorded for the server
type ServerStatistics interface {
IncrementCoapRequestsCount()
GetRequestsCount() int
}