Skip to content

Commit ba5c7ac

Browse files
Merge pull request #8 from TheThingsNetwork/fix/negative-coordinates
Fix for negative coordinates
2 parents ace5f9b + b93f1e6 commit ba5c7ac

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

cayennelpp/decoder.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,15 @@ func (d *decoder) decodeGPS(channel uint8, target UplinkTarget) error {
226226
if _, err := io.ReadFull(d.r, buf); err != nil {
227227
return err
228228
}
229-
var latitude, longitude, altitude int32
230-
latitude = int32(buf[0])<<16 | int32(buf[1])<<8 | int32(buf[2])
231-
longitude = int32(buf[3])<<16 | int32(buf[4])<<8 | int32(buf[5])
232-
altitude = int32(buf[6])<<16 | int32(buf[7])<<8 | int32(buf[8])
233-
target.GPS(channel, float32(latitude)/10000, float32(longitude)/10000, float32(altitude)/100)
229+
latitude := make([]byte, 4)
230+
copy(latitude, buf[0:3])
231+
longitude := make([]byte, 4)
232+
copy(longitude, buf[3:6])
233+
altitude := make([]byte, 4)
234+
copy(altitude, buf[6:9])
235+
target.GPS(channel,
236+
float32(int32(binary.BigEndian.Uint32(latitude))>>8)/10000,
237+
float32(int32(binary.BigEndian.Uint32(longitude))>>8)/10000,
238+
float32(int32(binary.BigEndian.Uint32(altitude))>>8)/100)
234239
return nil
235240
}

cayennelpp/decoder_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,15 @@ func TestDecode(t *testing.T) {
157157
err := decoder.DecodeDownlink(target)
158158
a.So(err, ShouldEqual, io.ErrUnexpectedEOF)
159159
}
160+
161+
// Negative coordinates
162+
{
163+
buf := []byte{0x01, GPS, 0x06, 0x76, 0x5f, 0xf2, 0x96, 0x0a, 0x00, 0x03, 0xe8}
164+
decoder := NewDecoder(bytes.NewBuffer(buf))
165+
target := &target{make(map[uint8]interface{})}
166+
167+
err := decoder.DecodeUplink(target)
168+
a.So(err, ShouldBeNil)
169+
a.So(target.values[1], ShouldResemble, []float32{42.3519, -87.9094, 10})
170+
}
160171
}

0 commit comments

Comments
 (0)