@@ -37,12 +37,20 @@ var (
37
37
ErrInvalidProtocolVersion = errors .New ("gateway: invalid protocol version" )
38
38
)
39
39
40
+ func protocolSupported (p uint8 ) bool {
41
+ if p == ProtocolVersion1 || p == ProtocolVersion2 {
42
+ return true
43
+ }
44
+ return false
45
+ }
46
+
40
47
// PushDataPacket type is used by the gateway mainly to forward the RF packets
41
48
// received, and associated metadata, to the server.
42
49
type PushDataPacket struct {
43
- RandomToken uint16
44
- GatewayMAC lorawan.EUI64
45
- Payload PushDataPayload
50
+ ProtocolVersion uint8
51
+ RandomToken uint16
52
+ GatewayMAC lorawan.EUI64
53
+ Payload PushDataPayload
46
54
}
47
55
48
56
// MarshalBinary marshals the object in binary form.
@@ -53,7 +61,7 @@ func (p PushDataPacket) MarshalBinary() ([]byte, error) {
53
61
}
54
62
55
63
out := make ([]byte , 4 , len (pb )+ 12 )
56
- out [0 ] = ProtocolVersion2
64
+ out [0 ] = p . ProtocolVersion
57
65
binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
58
66
out [3 ] = byte (PushData )
59
67
out = append (out , p .GatewayMAC [0 :len (p .GatewayMAC )]... )
@@ -69,10 +77,12 @@ func (p *PushDataPacket) UnmarshalBinary(data []byte) error {
69
77
if data [3 ] != byte (PushData ) {
70
78
return errors .New ("gateway: identifier mismatch (PUSH_DATA expected)" )
71
79
}
72
- if data [0 ] != ProtocolVersion2 {
80
+
81
+ if ! protocolSupported (data [0 ]) {
73
82
return ErrInvalidProtocolVersion
74
83
}
75
84
85
+ p .ProtocolVersion = data [0 ]
76
86
p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
77
87
for i := 0 ; i < 8 ; i ++ {
78
88
p .GatewayMAC [i ] = data [4 + i ]
@@ -84,13 +94,14 @@ func (p *PushDataPacket) UnmarshalBinary(data []byte) error {
84
94
// PushACKPacket is used by the server to acknowledge immediately all the
85
95
// PUSH_DATA packets received.
86
96
type PushACKPacket struct {
87
- RandomToken uint16
97
+ ProtocolVersion uint8
98
+ RandomToken uint16
88
99
}
89
100
90
101
// MarshalBinary marshals the object in binary form.
91
102
func (p PushACKPacket ) MarshalBinary () ([]byte , error ) {
92
103
out := make ([]byte , 4 )
93
- out [0 ] = ProtocolVersion2
104
+ out [0 ] = p . ProtocolVersion
94
105
binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
95
106
out [3 ] = byte (PushACK )
96
107
return out , nil
@@ -104,23 +115,26 @@ func (p *PushACKPacket) UnmarshalBinary(data []byte) error {
104
115
if data [3 ] != byte (PushACK ) {
105
116
return errors .New ("gateway: identifier mismatch (PUSH_ACK expected)" )
106
117
}
107
- if data [0 ] != ProtocolVersion2 {
118
+
119
+ if ! protocolSupported (data [0 ]) {
108
120
return ErrInvalidProtocolVersion
109
121
}
122
+ p .ProtocolVersion = data [0 ]
110
123
p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
111
124
return nil
112
125
}
113
126
114
127
// PullDataPacket is used by the gateway to poll data from the server.
115
128
type PullDataPacket struct {
116
- RandomToken uint16
117
- GatewayMAC [8 ]byte
129
+ ProtocolVersion uint8
130
+ RandomToken uint16
131
+ GatewayMAC [8 ]byte
118
132
}
119
133
120
134
// MarshalBinary marshals the object in binary form.
121
135
func (p PullDataPacket ) MarshalBinary () ([]byte , error ) {
122
136
out := make ([]byte , 4 , 12 )
123
- out [0 ] = ProtocolVersion2
137
+ out [0 ] = p . ProtocolVersion
124
138
binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
125
139
out [3 ] = byte (PullData )
126
140
out = append (out , p .GatewayMAC [0 :len (p .GatewayMAC )]... )
@@ -135,9 +149,11 @@ func (p *PullDataPacket) UnmarshalBinary(data []byte) error {
135
149
if data [3 ] != byte (PullData ) {
136
150
return errors .New ("gateway: identifier mismatch (PULL_DATA expected)" )
137
151
}
138
- if data [0 ] != ProtocolVersion2 {
152
+
153
+ if ! protocolSupported (data [0 ]) {
139
154
return ErrInvalidProtocolVersion
140
155
}
156
+ p .ProtocolVersion = data [0 ]
141
157
p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
142
158
for i := 0 ; i < 8 ; i ++ {
143
159
p .GatewayMAC [i ] = data [4 + i ]
@@ -148,13 +164,14 @@ func (p *PullDataPacket) UnmarshalBinary(data []byte) error {
148
164
// PullACKPacket is used by the server to confirm that the network route is
149
165
// open and that the server can send PULL_RESP packets at any time.
150
166
type PullACKPacket struct {
151
- RandomToken uint16
167
+ ProtocolVersion uint8
168
+ RandomToken uint16
152
169
}
153
170
154
171
// MarshalBinary marshals the object in binary form.
155
172
func (p PullACKPacket ) MarshalBinary () ([]byte , error ) {
156
173
out := make ([]byte , 4 )
157
- out [0 ] = ProtocolVersion2
174
+ out [0 ] = p . ProtocolVersion
158
175
binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
159
176
out [3 ] = byte (PullACK )
160
177
return out , nil
@@ -168,18 +185,20 @@ func (p *PullACKPacket) UnmarshalBinary(data []byte) error {
168
185
if data [3 ] != byte (PullACK ) {
169
186
return errors .New ("gateway: identifier mismatch (PULL_ACK expected)" )
170
187
}
171
- if data [0 ] != ProtocolVersion2 {
188
+ if ! protocolSupported ( data [0 ]) {
172
189
return ErrInvalidProtocolVersion
173
190
}
191
+ p .ProtocolVersion = data [0 ]
174
192
p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
175
193
return nil
176
194
}
177
195
178
196
// PullRespPacket is used by the server to send RF packets and associated
179
197
// metadata that will have to be emitted by the gateway.
180
198
type PullRespPacket struct {
181
- RandomToken uint16
182
- Payload PullRespPayload
199
+ ProtocolVersion uint8
200
+ RandomToken uint16
201
+ Payload PullRespPayload
183
202
}
184
203
185
204
// MarshalBinary marshals the object in binary form.
@@ -189,8 +208,12 @@ func (p PullRespPacket) MarshalBinary() ([]byte, error) {
189
208
return nil , err
190
209
}
191
210
out := make ([]byte , 4 , 4 + len (pb ))
192
- out [0 ] = ProtocolVersion2
193
- binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
211
+ out [0 ] = p .ProtocolVersion
212
+
213
+ if p .ProtocolVersion != ProtocolVersion1 {
214
+ // these two bytes are unused in ProtocolVersion1
215
+ binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
216
+ }
194
217
out [3 ] = byte (PullResp )
195
218
out = append (out , pb ... )
196
219
return out , nil
@@ -204,9 +227,10 @@ func (p *PullRespPacket) UnmarshalBinary(data []byte) error {
204
227
if data [3 ] != byte (PullResp ) {
205
228
return errors .New ("gateway: identifier mismatch (PULL_RESP expected)" )
206
229
}
207
- if data [0 ] != ProtocolVersion2 {
230
+ if ! protocolSupported ( data [0 ]) {
208
231
return ErrInvalidProtocolVersion
209
232
}
233
+ p .ProtocolVersion = data [0 ]
210
234
p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
211
235
return json .Unmarshal (data [4 :], & p .Payload )
212
236
}
@@ -215,9 +239,10 @@ func (p *PullRespPacket) UnmarshalBinary(data []byte) error {
215
239
// to inform if a downlink request has been accepted or rejected by the
216
240
// gateway.
217
241
type TXACKPacket struct {
218
- RandomToken uint16
219
- GatewayMAC lorawan.EUI64
220
- Payload * TXACKPayload
242
+ ProtocolVersion uint8
243
+ RandomToken uint16
244
+ GatewayMAC lorawan.EUI64
245
+ Payload * TXACKPayload
221
246
}
222
247
223
248
// MarshalBinary marshals the object into binary form.
@@ -232,7 +257,7 @@ func (p TXACKPacket) MarshalBinary() ([]byte, error) {
232
257
}
233
258
234
259
out := make ([]byte , 4 , len (pb )+ 12 )
235
- out [0 ] = ProtocolVersion2
260
+ out [0 ] = p . ProtocolVersion
236
261
binary .LittleEndian .PutUint16 (out [1 :3 ], p .RandomToken )
237
262
out [3 ] = byte (TXACK )
238
263
out = append (out , p .GatewayMAC [:]... )
@@ -248,9 +273,10 @@ func (p *TXACKPacket) UnmarshalBinary(data []byte) error {
248
273
if data [3 ] != byte (TXACK ) {
249
274
return errors .New ("gateway: identifier mismatch (TXACK expected)" )
250
275
}
251
- if data [0 ] != ProtocolVersion2 {
276
+ if ! protocolSupported ( data [0 ]) {
252
277
return ErrInvalidProtocolVersion
253
278
}
279
+ p .ProtocolVersion = data [0 ]
254
280
p .RandomToken = binary .LittleEndian .Uint16 (data [1 :3 ])
255
281
for i := 0 ; i < 8 ; i ++ {
256
282
p .GatewayMAC [i ] = data [4 + i ]
@@ -403,7 +429,7 @@ func GetPacketType(data []byte) (PacketType, error) {
403
429
return PacketType (0 ), errors .New ("gateway: at least 4 bytes of data are expected" )
404
430
}
405
431
406
- if data [0 ] != ProtocolVersion2 {
432
+ if ! protocolSupported ( data [0 ]) {
407
433
return PacketType (0 ), ErrInvalidProtocolVersion
408
434
}
409
435
0 commit comments