@@ -23,11 +23,13 @@ const (
23
23
PullData
24
24
PullResp
25
25
PullACK
26
+ TXACK
26
27
)
27
28
28
29
// Protocol versions
29
30
const (
30
31
ProtocolVersion1 uint8 = 0x01
32
+ ProtocolVersion2 uint8 = 0x02
31
33
)
32
34
33
35
// Errors
@@ -51,7 +53,7 @@ func (p PushDataPacket) MarshalBinary() ([]byte, error) {
51
53
}
52
54
53
55
out := make ([]byte , 4 , len (pb )+ 12 )
54
- out [0 ] = ProtocolVersion1
56
+ out [0 ] = ProtocolVersion2
55
57
binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
56
58
out [3 ] = byte (PushData )
57
59
out = append (out , p .GatewayMAC [0 :len (p .GatewayMAC )]... )
@@ -67,7 +69,7 @@ func (p *PushDataPacket) UnmarshalBinary(data []byte) error {
67
69
if data [3 ] != byte (PushData ) {
68
70
return errors .New ("gateway: identifier mismatch (PUSH_DATA expected)" )
69
71
}
70
- if data [0 ] != ProtocolVersion1 {
72
+ if data [0 ] != ProtocolVersion2 {
71
73
return ErrInvalidProtocolVersion
72
74
}
73
75
@@ -88,7 +90,7 @@ type PushACKPacket struct {
88
90
// MarshalBinary marshals the object in binary form.
89
91
func (p PushACKPacket ) MarshalBinary () ([]byte , error ) {
90
92
out := make ([]byte , 4 )
91
- out [0 ] = ProtocolVersion1
93
+ out [0 ] = ProtocolVersion2
92
94
binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
93
95
out [3 ] = byte (PushACK )
94
96
return out , nil
@@ -102,7 +104,7 @@ func (p *PushACKPacket) UnmarshalBinary(data []byte) error {
102
104
if data [3 ] != byte (PushACK ) {
103
105
return errors .New ("gateway: identifier mismatch (PUSH_ACK expected)" )
104
106
}
105
- if data [0 ] != ProtocolVersion1 {
107
+ if data [0 ] != ProtocolVersion2 {
106
108
return ErrInvalidProtocolVersion
107
109
}
108
110
p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
@@ -118,7 +120,7 @@ type PullDataPacket struct {
118
120
// MarshalBinary marshals the object in binary form.
119
121
func (p PullDataPacket ) MarshalBinary () ([]byte , error ) {
120
122
out := make ([]byte , 4 , 12 )
121
- out [0 ] = ProtocolVersion1
123
+ out [0 ] = ProtocolVersion2
122
124
binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
123
125
out [3 ] = byte (PullData )
124
126
out = append (out , p .GatewayMAC [0 :len (p .GatewayMAC )]... )
@@ -133,7 +135,7 @@ func (p *PullDataPacket) UnmarshalBinary(data []byte) error {
133
135
if data [3 ] != byte (PullData ) {
134
136
return errors .New ("gateway: identifier mismatch (PULL_DATA expected)" )
135
137
}
136
- if data [0 ] != ProtocolVersion1 {
138
+ if data [0 ] != ProtocolVersion2 {
137
139
return ErrInvalidProtocolVersion
138
140
}
139
141
p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
@@ -152,7 +154,7 @@ type PullACKPacket struct {
152
154
// MarshalBinary marshals the object in binary form.
153
155
func (p PullACKPacket ) MarshalBinary () ([]byte , error ) {
154
156
out := make ([]byte , 4 )
155
- out [0 ] = ProtocolVersion1
157
+ out [0 ] = ProtocolVersion2
156
158
binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
157
159
out [3 ] = byte (PullACK )
158
160
return out , nil
@@ -166,7 +168,7 @@ func (p *PullACKPacket) UnmarshalBinary(data []byte) error {
166
168
if data [3 ] != byte (PullACK ) {
167
169
return errors .New ("gateway: identifier mismatch (PULL_ACK expected)" )
168
170
}
169
- if data [0 ] != ProtocolVersion1 {
171
+ if data [0 ] != ProtocolVersion2 {
170
172
return ErrInvalidProtocolVersion
171
173
}
172
174
p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
@@ -187,7 +189,7 @@ func (p PullRespPacket) MarshalBinary() ([]byte, error) {
187
189
return nil , err
188
190
}
189
191
out := make ([]byte , 4 , 4 + len (pb ))
190
- out [0 ] = ProtocolVersion1
192
+ out [0 ] = ProtocolVersion2
191
193
binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
192
194
out [3 ] = byte (PullResp )
193
195
out = append (out , pb ... )
@@ -202,13 +204,70 @@ func (p *PullRespPacket) UnmarshalBinary(data []byte) error {
202
204
if data [3 ] != byte (PullResp ) {
203
205
return errors .New ("gateway: identifier mismatch (PULL_RESP expected)" )
204
206
}
205
- if data [0 ] != ProtocolVersion1 {
207
+ if data [0 ] != ProtocolVersion2 {
206
208
return ErrInvalidProtocolVersion
207
209
}
208
210
p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
209
211
return json .Unmarshal (data [4 :], & p .Payload )
210
212
}
211
213
214
+ // TXACKPacket is used by the gateway to send a feedback to the server
215
+ // to inform if a downlink request has been accepted or rejected by the
216
+ // gateway.
217
+ type TXACKPacket struct {
218
+ RandomToken uint16
219
+ Payload * TXACKPayload
220
+ }
221
+
222
+ // MarshalBinary marshals the object into binary form.
223
+ func (p TXACKPacket ) MarshalBinary () ([]byte , error ) {
224
+ var pb []byte
225
+ if p .Payload != nil {
226
+ var err error
227
+ pb , err = json .Marshal (p .Payload )
228
+ if err != nil {
229
+ return nil , err
230
+ }
231
+ }
232
+
233
+ out := make ([]byte , 4 , 4 + len (pb ))
234
+ out [0 ] = ProtocolVersion2
235
+ binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
236
+ out [3 ] = byte (TXACK )
237
+ out = append (out , pb ... )
238
+ return out , nil
239
+ }
240
+
241
+ // UnmarshalBinary decodes the object from binary form.
242
+ func (p * TXACKPacket ) UnmarshalBinary (data []byte ) error {
243
+ if len (data ) < 4 {
244
+ return errors .New ("gateway: at least 4 bytes of data are expected" )
245
+ }
246
+ if data [3 ] != byte (TXACK ) {
247
+ return errors .New ("gateway: identifier mismatch (TXACK expected)" )
248
+ }
249
+ if data [0 ] != ProtocolVersion2 {
250
+ return ErrInvalidProtocolVersion
251
+ }
252
+ p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
253
+ if len (data ) > 4 {
254
+ p .Payload = & TXACKPayload {}
255
+ return json .Unmarshal (data [4 :], p .Payload )
256
+ }
257
+ return nil
258
+ }
259
+
260
+ // TXACKPayload contains the TXACKPacket payload.
261
+ type TXACKPayload struct {
262
+ TXPKACK TXPKACK `json:"txpk_ack"`
263
+ }
264
+
265
+ // TXPKACK contains the status information of the associated PULL_RESP
266
+ // packet.
267
+ type TXPKACK struct {
268
+ Error string `json:"error"`
269
+ }
270
+
212
271
// PushDataPayload represents the upstream JSON data structure.
213
272
type PushDataPayload struct {
214
273
RXPK []RXPK `json:"rxpk,omitempty"`
@@ -339,7 +398,7 @@ func GetPacketType(data []byte) (PacketType, error) {
339
398
return PacketType (0 ), errors .New ("gateway: at least 4 bytes of data are expected" )
340
399
}
341
400
342
- if data [0 ] != ProtocolVersion1 {
401
+ if data [0 ] != ProtocolVersion2 {
343
402
return PacketType (0 ), ErrInvalidProtocolVersion
344
403
}
345
404
0 commit comments