{"action":"created","issue":{"url":"https://api.github.com/repos/mcci-catena/arduino-lmic/issues/147","repository_url":"https://api.github.com/repos/mcci-catena/arduino-lmic","labels_url":"https://api.github.com/repos/mcci-catena/arduino-lmic/issues/147/labels{/name}","comments_url":"https://api.github.com/repos/mcci-catena/arduino-lmic/issues/147/comments","events_url":"https://api.github.com/repos/mcci-catena/arduino-lmic/issues/147/events","html_url":"https://github.com/mcci-catena/arduino-lmic/pull/147","id":368532424,"node_id":"MDExOlB1bGxSZXF1ZXN0MjIxNjg3MDUz","number":147,"title":"Add support for MAC command 0x0D (DeviceTimeReq and DeviceTimeAns)","user":{"login":"frazar","id":914950,"node_id":"MDQ6VXNlcjkxNDk1MA==","avatar_url":"https://avatars0.githubusercontent.com/u/914950?v=4","gravatar_id":"","url":"https://api.github.com/users/frazar","html_url":"https://github.com/frazar","followers_url":"https://api.github.com/users/frazar/followers","following_url":"https://api.github.com/users/frazar/following{/other_user}","gists_url":"https://api.github.com/users/frazar/gists{/gist_id}","starred_url":"https://api.github.com/users/frazar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/frazar/subscriptions","organizations_url":"https://api.github.com/users/frazar/orgs","repos_url":"https://api.github.com/users/frazar/repos","events_url":"https://api.github.com/users/frazar/events{/privacy}","received_events_url":"https://api.github.com/users/frazar/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":13,"created_at":"2018-10-10T07:40:50Z","updated_at":"2018-11-06T21:51:55Z","closed_at":"2018-11-06T21:51:22Z","author_association":"NONE","pull_request":{"url":"https://api.github.com/repos/mcci-catena/arduino-lmic/pulls/147","html_url":"https://github.com/mcci-catena/arduino-lmic/pull/147","diff_url":"https://github.com/mcci-catena/arduino-lmic/pull/147.diff","patch_url":"https://github.com/mcci-catena/arduino-lmic/pull/147.patch"},"body":"###  Purpose\r\nThe `DeviceTimeReq` and `DeviceTimeAns` MAC commands are part of the [LoRaWAN v1.0.3 specifications](https://lora-alliance.org/resource-hub/lorawantm-specification-v103) and later.\r\nThey allow an end-device to request the current date and time.\r\n\r\n### Enabling the support for the new MAC commands\r\nTo enable the support for the `DeviceTimeReq` and `DeviceTimeAns` commands, the `LMIC_ENABLE_DeviceTimeReq` constant must be defined in the `project_config/lmic_project_config.h` file and set to 1, like this\r\n```C\r\n#define LMIC_ENABLE_DeviceTimeReq 1\r\n```\r\n\r\n### How it works\r\nThis PR introduces the `LMIC_requestNetworkTime()` function, which expects two arguments:\r\n1. a callback function supplied by the user, and\r\n2. an arbitrary pointer to user data that will be passed to the callback function (you can pass `NULL` if this is not needed).\r\n\r\nWhen the  `LMIC_requestNetworkTime()` is called, a `DeviceTimeReq` is scheduled on the next transmission. After receiving a `DeviceTimeAns` response, the user callback function will be called with two arguments:\r\n1. a pointer to user data, the same as the second argument to the `LMIC_requestNetworkTime()` function, and\r\n2. an integer different from 0 in case of success or equal to 0 in case of failure.\r\n\r\nTo get the network time, the user callback should call the `LMIC_getNetworkTimeReference()` function. This functions accepts a pointer to a struct of type `lmic_time_reference_t`, which it will try to populate when called, and return 1 on success, 0 on fail.\r\n\r\nA struct of type `lmic_time_reference_t` contains two fields:\r\n- a field named `tLocal` of type `ostime_t`, containig the time at which the `DeviceTimeReq` was sent as returned by `os_getTime()`, and \r\n- a field named `tNetwork` of type `lmic_gpstime_t` (aka `u4_t`), storing the number of seconds since the GPS epoch in the moment when the gateway received the `DeviceTimeReq`.\r\n\r\n### Example\r\nYou can check the `ttn-otaa-network-time.ino` example to see how `LMIC_requestNetworkTime()`  is used and for an example implementation of the user callback function which prints the current UTC time on the serial connection.\r\n\r\n### Testing\r\nThe code was tested on a The Things Network setup. \r\nNote that most `packet_forwarder` implementations don't send the corrent timing data unless there's a GPS module attached to the gateway. \r\nIf you don't have a GPS module, and are ok with using the OS clock for timing information (like me), you can use [my fork](https://github.com/frazar/packet_forwarder) of Lora-Net's `packet_forwarder` specifying the option \r\n```json\r\nfake_gps_time: true,\r\n``` \r\nin the JSON configuration."},"comment":{"url":"https://api.github.com/repos/mcci-catena/arduino-lmic/issues/comments/436421765","html_url":"https://github.com/mcci-catena/arduino-lmic/pull/147#issuecomment-436421765","issue_url":"https://api.github.com/repos/mcci-catena/arduino-lmic/issues/147","id":436421765,"node_id":"MDEyOklzc3VlQ29tbWVudDQzNjQyMTc2NQ==","user":{"login":"terrillmoore","id":19333103,"node_id":"MDQ6VXNlcjE5MzMzMTAz","avatar_url":"https://avatars3.githubusercontent.com/u/19333103?v=4","gravatar_id":"","url":"https://api.github.com/users/terrillmoore","html_url":"https://github.com/terrillmoore","followers_url":"https://api.github.com/users/terrillmoore/followers","following_url":"https://api.github.com/users/terrillmoore/following{/other_user}","gists_url":"https://api.github.com/users/terrillmoore/gists{/gist_id}","starred_url":"https://api.github.com/users/terrillmoore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/terrillmoore/subscriptions","organizations_url":"https://api.github.com/users/terrillmoore/orgs","repos_url":"https://api.github.com/users/terrillmoore/repos","events_url":"https://api.github.com/users/terrillmoore/events{/privacy}","received_events_url":"https://api.github.com/users/terrillmoore/received_events","type":"User","site_admin":false},"created_at":"2018-11-06T21:51:55Z","updated_at":"2018-11-06T21:51:55Z","author_association":"MEMBER","body":"Merged, thanks again!"}}
{"actor":{"display_login":"terrillmoore"}}