chirpstack / chirpstack-gateway-bridge

ChirpStack Gateway Bridge abstracts Packet Forwarder protocols into Protobuf or JSON over MQTT.
https://www.chirpstack.io
MIT License
415 stars 269 forks source link

Add Basic Station Time Synchronization #179

Closed vfylyk closed 3 years ago

vfylyk commented 3 years ago

Summary

It seems like the current Basic Station implementation does not include support for the LNS @timesync@ message type, so it would be great if it could be included.

This is what happens currently:

time="2020-09-21T04:21:24Z" level=debug msg="backend/basicstation: message received" gateway_id=d4adbdfffe9e2890 message="{\"msgtype\":\"timesync\",\"txtime\":4035744838}"
time="2020-09-21T04:21:24Z" level=info msg="backend/basicstation: raw packet-forwarder event received" gateway_id=d4adbdfffe9e2890 raw_id=d145516d-901f-412b-b65c-57bfa0a8d6bb
time="2020-09-21T04:21:24Z" level=info msg="integration/mqtt: publishing event" event=raw qos=1 raw_id=d145516d-901f-412b-b65c-57bfa0a8d6bb topic=gateway/d4adbdfffe9e2890/au/event/raw

What is the use-case?

The most recent Cisco IXM gateway comes with Basic Station support, using GPS and @timesync@ messages to provide timestamps. Whenever possible I prefer to have timestamps applied at the gateway as they more accurately tell when a payload has been received.

Implementation description

The LNS Protocol indicates how this is implemented.

Can you implement this by yourself and make a pull request?

I can give it a try.

brocaar commented 3 years ago

If you would like to create a PR for this, that would be much appreciated :+1:

Where the LNS Protocol docs mention that the LNS must answer, I think it makes sense to let the ChirpStack Gateway Bridge respond with the gps timestamp.

The github.com/brocaar/lorawan/gps package provides an utility to convert from a time.Time to GPS time (time.Duration): https://pkg.go.dev/github.com/brocaar/lorawan@v0.0.0-20200903101545-e07a1f3df89c/gps

E.g.

gpsTime  := gps.Time(time.Now()).TimeSinceGPSEpoch()
vfylyk commented 3 years ago

Thanks @brocaar, I'll give it a try when a get a moment.