{"action":"created","comment":{"url":"https://api.github.com/repos/openthread/openthread/pulls/comments/231066814","pull_request_review_id":171946293,"id":231066814,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIzMTA2NjgxNA==","diff_hunk":"@@ -151,6 +154,82 @@ void TimeSync::ProcessTimeSync(void)\n }\n #endif // OPENTHREAD_FTD\n \n+void TimeSync::HandleStateChanged(otChangedFlags aFlags)\n+{\n+    if ((aFlags & OT_CHANGED_THREAD_ROLE) != 0)\n+    {\n+        CheckAndHandleChanges(false);\n+    }\n+}\n+\n+void TimeSync::HandleTimeout()\n+{\n+    CheckAndHandleChanges(false);\n+}\n+\n+void TimeSync::HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags)\n+{\n+    aCallback.GetOwner<TimeSync>().HandleStateChanged(aFlags);\n+}\n+\n+void TimeSync::HandleTimeout(Timer &aTimer)\n+{\n+    aTimer.GetOwner<TimeSync>().HandleTimeout();\n+}\n+\n+void TimeSync::CheckAndHandleChanges(bool aTimeUpdated)\n+{\n+    otNetworkTimeStatus networkTimeStatus       = OT_NETWORK_TIME_SYNCHRONIZED;\n+    const otDeviceRole  role                    = GetInstance().GetThreadNetif().GetMle().GetRole();\n+    const uint32_t      resyncNeededThresholdMs = 2 * TimerMilli::SecToMsec(mTimeSyncPeriod);\n+    const uint32_t      timeSyncLastSyncMs      = TimerMilli::GetNow() - mLastTimeSyncReceived;\n+    uint32_t            timerTimeoutMs;\n+\n+    switch (role)\n+    {\n+    case OT_DEVICE_ROLE_DISABLED:\n+    case OT_DEVICE_ROLE_DETACHED:\n+        networkTimeStatus = OT_NETWORK_TIME_UNSYNCHRONIZED;\n+        break;\n+\n+    case OT_DEVICE_ROLE_CHILD:\n+    case OT_DEVICE_ROLE_ROUTER:\n+        if (timeSyncLastSyncMs > resyncNeededThresholdMs)\n+        {\n+            // The device hasn’t received time sync for more than two periods time.\n+            networkTimeStatus = OT_NETWORK_TIME_RESYNC_NEEDED;\n+        }\n+        break;\n+\n+    case OT_DEVICE_ROLE_LEADER:\n+        break;\n+    }\n+\n+    mTimer.Stop();\n+\n+    if (networkTimeStatus == OT_NETWORK_TIME_SYNCHRONIZED)\n+    {\n+        // Schedule a check 1 millisecond after two periods of time\n+        if (resyncNeededThresholdMs >= timeSyncLastSyncMs)\n+        {\n+            timerTimeoutMs = resyncNeededThresholdMs - timeSyncLastSyncMs + 1;\n+        }\n+        else\n+        {\n+            timerTimeoutMs = resyncNeededThresholdMs + 1;","path":"src/core/thread/time_sync_service.cpp","position":null,"original_position":156,"commit_id":"0413de5f994c7d688896bf46814a83143938e8ba","original_commit_id":"5d452bfb1a18633bacf17f6d56704d37b7afc7a3","user":{"login":"josephn123","id":28803255,"node_id":"MDQ6VXNlcjI4ODAzMjU1","avatar_url":"https://avatars3.githubusercontent.com/u/28803255?v=4","gravatar_id":"","url":"https://api.github.com/users/josephn123","html_url":"https://github.com/josephn123","followers_url":"https://api.github.com/users/josephn123/followers","following_url":"https://api.github.com/users/josephn123/following{/other_user}","gists_url":"https://api.github.com/users/josephn123/gists{/gist_id}","starred_url":"https://api.github.com/users/josephn123/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josephn123/subscriptions","organizations_url":"https://api.github.com/users/josephn123/orgs","repos_url":"https://api.github.com/users/josephn123/repos","events_url":"https://api.github.com/users/josephn123/events{/privacy}","received_events_url":"https://api.github.com/users/josephn123/received_events","type":"User","site_admin":false},"body":"Yes it was because of the leader case leaving `mLastTimeSyncReceived` as 0 but you are right in pointing out we don't need the timer for the leader anyway. We also don't need the timer for disabled and detached, so I've combined the logic in to the switch statement. Furthermore, I added an assert `assert(resyncNeededThresholdMs >= timeSyncLastSyncMs)` because the conditions for preventing this condition are quite subtle and may sting a contributor if they make modifications to the behaviour of time sync in the future.  ","created_at":"2018-11-06T10:17:19Z","updated_at":"2018-11-06T10:17:19Z","html_url":"https://github.com/openthread/openthread/pull/3238#discussion_r231066814","pull_request_url":"https://api.github.com/repos/openthread/openthread/pulls/3238","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/openthread/openthread/pulls/comments/231066814"},"html":{"href":"https://github.com/openthread/openthread/pull/3238#discussion_r231066814"},"pull_request":{"href":"https://api.github.com/repos/openthread/openthread/pulls/3238"}},"in_reply_to_id":230985683},"pull_request":{"url":"https://api.github.com/repos/openthread/openthread/pulls/3238","id":227007957,"node_id":"MDExOlB1bGxSZXF1ZXN0MjI3MDA3OTU3","html_url":"https://github.com/openthread/openthread/pull/3238","diff_url":"https://github.com/openthread/openthread/pull/3238.diff","patch_url":"https://github.com/openthread/openthread/pull/3238.patch","issue_url":"https://api.github.com/repos/openthread/openthread/issues/3238","number":3238,"state":"open","locked":false,"title":"[ncp] Notify host asynchronously upon a significant thread network time change","user":{"login":"josephn123","id":28803255,"node_id":"MDQ6VXNlcjI4ODAzMjU1","avatar_url":"https://avatars3.githubusercontent.com/u/28803255?v=4","gravatar_id":"","url":"https://api.github.com/users/josephn123","html_url":"https://github.com/josephn123","followers_url":"https://api.github.com/users/josephn123/followers","following_url":"https://api.github.com/users/josephn123/following{/other_user}","gists_url":"https://api.github.com/users/josephn123/gists{/gist_id}","starred_url":"https://api.github.com/users/josephn123/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josephn123/subscriptions","organizations_url":"https://api.github.com/users/josephn123/orgs","repos_url":"https://api.github.com/users/josephn123/repos","events_url":"https://api.github.com/users/josephn123/events{/privacy}","received_events_url":"https://api.github.com/users/josephn123/received_events","type":"User","site_admin":false},"body":"This pull request is to add asynchronously notifying the host of a significant (>10ms by default) network time change.\r\n\r\nThis is useful if the host is maintaining a local copy of the thread network time (additionally by polling periodically).\r\n\r\nTested with wpantund. It safely ignores these unsolicited messages.\r\n\r\nWill be following up later with additional changes to wpantund to forward via dbus.\r\n\r\nThe definition of \"significant\" is up for debate. In my case, in normal running operation, I observed the network time offset to deviate within 10ms on my efr32 at each network time sync.","created_at":"2018-10-30T17:10:13Z","updated_at":"2018-11-06T10:17:19Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4df7ae78a6ac6d8b88fb5cfcca784f6fbd3f34c0","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":380297891,"node_id":"MDU6TGFiZWwzODAyOTc4OTE=","url":"https://api.github.com/repos/openthread/openthread/labels/NCP","name":"NCP","color":"c2e0c6","default":false},{"id":397568980,"node_id":"MDU6TGFiZWwzOTc1Njg5ODA=","url":"https://api.github.com/repos/openthread/openthread/labels/P4","name":"P4","color":"AFDF00","default":false},{"id":355043113,"node_id":"MDU6TGFiZWwzNTUwNDMxMTM=","url":"https://api.github.com/repos/openthread/openthread/labels/enhancement","name":"enhancement","color":"84b6eb","default":true}],"milestone":null,"commits_url":"https://api.github.com/repos/openthread/openthread/pulls/3238/commits","review_comments_url":"https://api.github.com/repos/openthread/openthread/pulls/3238/comments","review_comment_url":"https://api.github.com/repos/openthread/openthread/pulls/comments{/number}","comments_url":"https://api.github.com/repos/openthread/openthread/issues/3238/comments","statuses_url":"https://api.github.com/repos/openthread/openthread/statuses/0413de5f994c7d688896bf46814a83143938e8ba","head":{"label":"josephn123:time-notif-ncp","ref":"time-notif-ncp","sha":"0413de5f994c7d688896bf46814a83143938e8ba","user":{"login":"josephn123","id":28803255,"node_id":"MDQ6VXNlcjI4ODAzMjU1","avatar_url":"https://avatars3.githubusercontent.com/u/28803255?v=4","gravatar_id":"","url":"https://api.github.com/users/josephn123","html_url":"https://github.com/josephn123","followers_url":"https://api.github.com/users/josephn123/followers","following_url":"https://api.github.com/users/josephn123/following{/other_user}","gists_url":"https://api.github.com/users/josephn123/gists{/gist_id}","starred_url":"https://api.github.com/users/josephn123/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josephn123/subscriptions","organizations_url":"https://api.github.com/users/josephn123/orgs","repos_url":"https://api.github.com/users/josephn123/repos","events_url":"https://api.github.com/users/josephn123/events{/privacy}","received_events_url":"https://api.github.com/users/josephn123/received_events","type":"User","site_admin":false},"repo":{"id":154652059,"node_id":"MDEwOlJlcG9zaXRvcnkxNTQ2NTIwNTk=","name":"openthread","full_name":"josephn123/openthread","private":false,"owner":{"login":"josephn123","id":28803255,"node_id":"MDQ6VXNlcjI4ODAzMjU1","avatar_url":"https://avatars3.githubusercontent.com/u/28803255?v=4","gravatar_id":"","url":"https://api.github.com/users/josephn123","html_url":"https://github.com/josephn123","followers_url":"https://api.github.com/users/josephn123/followers","following_url":"https://api.github.com/users/josephn123/following{/other_user}","gists_url":"https://api.github.com/users/josephn123/gists{/gist_id}","starred_url":"https://api.github.com/users/josephn123/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josephn123/subscriptions","organizations_url":"https://api.github.com/users/josephn123/orgs","repos_url":"https://api.github.com/users/josephn123/repos","events_url":"https://api.github.com/users/josephn123/events{/privacy}","received_events_url":"https://api.github.com/users/josephn123/received_events","type":"User","site_admin":false},"html_url":"https://github.com/josephn123/openthread","description":"OpenThread released by Nest is an open-source implementation of the Thread networking protocol","fork":true,"url":"https://api.github.com/repos/josephn123/openthread","forks_url":"https://api.github.com/repos/josephn123/openthread/forks","keys_url":"https://api.github.com/repos/josephn123/openthread/keys{/key_id}","collaborators_url":"https://api.github.com/repos/josephn123/openthread/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/josephn123/openthread/teams","hooks_url":"https://api.github.com/repos/josephn123/openthread/hooks","issue_events_url":"https://api.github.com/repos/josephn123/openthread/issues/events{/number}","events_url":"https://api.github.com/repos/josephn123/openthread/events","assignees_url":"https://api.github.com/repos/josephn123/openthread/assignees{/user}","branches_url":"https://api.github.com/repos/josephn123/openthread/branches{/branch}","tags_url":"https://api.github.com/repos/josephn123/openthread/tags","blobs_url":"https://api.github.com/repos/josephn123/openthread/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/josephn123/openthread/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/josephn123/openthread/git/refs{/sha}","trees_url":"https://api.github.com/repos/josephn123/openthread/git/trees{/sha}","statuses_url":"https://api.github.com/repos/josephn123/openthread/statuses/{sha}","languages_url":"https://api.github.com/repos/josephn123/openthread/languages","stargazers_url":"https://api.github.com/repos/josephn123/openthread/stargazers","contributors_url":"https://api.github.com/repos/josephn123/openthread/contributors","subscribers_url":"https://api.github.com/repos/josephn123/openthread/subscribers","subscription_url":"https://api.github.com/repos/josephn123/openthread/subscription","commits_url":"https://api.github.com/repos/josephn123/openthread/commits{/sha}","git_commits_url":"https://api.github.com/repos/josephn123/openthread/git/commits{/sha}","comments_url":"https://api.github.com/repos/josephn123/openthread/comments{/number}","issue_comment_url":"https://api.github.com/repos/josephn123/openthread/issues/comments{/number}","contents_url":"https://api.github.com/repos/josephn123/openthread/contents/{+path}","compare_url":"https://api.github.com/repos/josephn123/openthread/compare/{base}...{head}","merges_url":"https://api.github.com/repos/josephn123/openthread/merges","archive_url":"https://api.github.com/repos/josephn123/openthread/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/josephn123/openthread/downloads","issues_url":"https://api.github.com/repos/josephn123/openthread/issues{/number}","pulls_url":"https://api.github.com/repos/josephn123/openthread/pulls{/number}","milestones_url":"https://api.github.com/repos/josephn123/openthread/milestones{/number}","notifications_url":"https://api.github.com/repos/josephn123/openthread/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/josephn123/openthread/labels{/name}","releases_url":"https://api.github.com/repos/josephn123/openthread/releases{/id}","deployments_url":"https://api.github.com/repos/josephn123/openthread/deployments","created_at":"2018-10-25T10:26:12Z","updated_at":"2018-10-31T10:30:09Z","pushed_at":"2018-11-06T10:12:06Z","git_url":"git://github.com/josephn123/openthread.git","ssh_url":"git@github.com:josephn123/openthread.git","clone_url":"https://github.com/josephn123/openthread.git","svn_url":"https://github.com/josephn123/openthread","homepage":"","size":52910,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"bsd-3-clause","name":"BSD 3-Clause \"New\" or \"Revised\" License","spdx_id":"BSD-3-Clause","url":"https://api.github.com/licenses/bsd-3-clause","node_id":"MDc6TGljZW5zZTU="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"openthread:master","ref":"master","sha":"728bd3e5a835428eac72c262a1d2f267196282ad","user":{"login":"openthread","id":16142269,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE2MTQyMjY5","avatar_url":"https://avatars2.githubusercontent.com/u/16142269?v=4","gravatar_id":"","url":"https://api.github.com/users/openthread","html_url":"https://github.com/openthread","followers_url":"https://api.github.com/users/openthread/followers","following_url":"https://api.github.com/users/openthread/following{/other_user}","gists_url":"https://api.github.com/users/openthread/gists{/gist_id}","starred_url":"https://api.github.com/users/openthread/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openthread/subscriptions","organizations_url":"https://api.github.com/users/openthread/orgs","repos_url":"https://api.github.com/users/openthread/repos","events_url":"https://api.github.com/users/openthread/events{/privacy}","received_events_url":"https://api.github.com/users/openthread/received_events","type":"Organization","site_admin":false},"repo":{"id":55808787,"node_id":"MDEwOlJlcG9zaXRvcnk1NTgwODc4Nw==","name":"openthread","full_name":"openthread/openthread","private":false,"owner":{"login":"openthread","id":16142269,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE2MTQyMjY5","avatar_url":"https://avatars2.githubusercontent.com/u/16142269?v=4","gravatar_id":"","url":"https://api.github.com/users/openthread","html_url":"https://github.com/openthread","followers_url":"https://api.github.com/users/openthread/followers","following_url":"https://api.github.com/users/openthread/following{/other_user}","gists_url":"https://api.github.com/users/openthread/gists{/gist_id}","starred_url":"https://api.github.com/users/openthread/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openthread/subscriptions","organizations_url":"https://api.github.com/users/openthread/orgs","repos_url":"https://api.github.com/users/openthread/repos","events_url":"https://api.github.com/users/openthread/events{/privacy}","received_events_url":"https://api.github.com/users/openthread/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/openthread/openthread","description":"OpenThread released by Nest is an open-source implementation of the Thread networking protocol","fork":false,"url":"https://api.github.com/repos/openthread/openthread","forks_url":"https://api.github.com/repos/openthread/openthread/forks","keys_url":"https://api.github.com/repos/openthread/openthread/keys{/key_id}","collaborators_url":"https://api.github.com/repos/openthread/openthread/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/openthread/openthread/teams","hooks_url":"https://api.github.com/repos/openthread/openthread/hooks","issue_events_url":"https://api.github.com/repos/openthread/openthread/issues/events{/number}","events_url":"https://api.github.com/repos/openthread/openthread/events","assignees_url":"https://api.github.com/repos/openthread/openthread/assignees{/user}","branches_url":"https://api.github.com/repos/openthread/openthread/branches{/branch}","tags_url":"https://api.github.com/repos/openthread/openthread/tags","blobs_url":"https://api.github.com/repos/openthread/openthread/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/openthread/openthread/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/openthread/openthread/git/refs{/sha}","trees_url":"https://api.github.com/repos/openthread/openthread/git/trees{/sha}","statuses_url":"https://api.github.com/repos/openthread/openthread/statuses/{sha}","languages_url":"https://api.github.com/repos/openthread/openthread/languages","stargazers_url":"https://api.github.com/repos/openthread/openthread/stargazers","contributors_url":"https://api.github.com/repos/openthread/openthread/contributors","subscribers_url":"https://api.github.com/repos/openthread/openthread/subscribers","subscription_url":"https://api.github.com/repos/openthread/openthread/subscription","commits_url":"https://api.github.com/repos/openthread/openthread/commits{/sha}","git_commits_url":"https://api.github.com/repos/openthread/openthread/git/commits{/sha}","comments_url":"https://api.github.com/repos/openthread/openthread/comments{/number}","issue_comment_url":"https://api.github.com/repos/openthread/openthread/issues/comments{/number}","contents_url":"https://api.github.com/repos/openthread/openthread/contents/{+path}","compare_url":"https://api.github.com/repos/openthread/openthread/compare/{base}...{head}","merges_url":"https://api.github.com/repos/openthread/openthread/merges","archive_url":"https://api.github.com/repos/openthread/openthread/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/openthread/openthread/downloads","issues_url":"https://api.github.com/repos/openthread/openthread/issues{/number}","pulls_url":"https://api.github.com/repos/openthread/openthread/pulls{/number}","milestones_url":"https://api.github.com/repos/openthread/openthread/milestones{/number}","notifications_url":"https://api.github.com/repos/openthread/openthread/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/openthread/openthread/labels{/name}","releases_url":"https://api.github.com/repos/openthread/openthread/releases{/id}","deployments_url":"https://api.github.com/repos/openthread/openthread/deployments","created_at":"2016-04-08T20:47:41Z","updated_at":"2018-11-06T04:48:53Z","pushed_at":"2018-11-06T10:12:08Z","git_url":"git://github.com/openthread/openthread.git","ssh_url":"git@github.com:openthread/openthread.git","clone_url":"https://github.com/openthread/openthread.git","svn_url":"https://github.com/openthread/openthread","homepage":"","size":53519,"stargazers_count":1698,"watchers_count":1698,"language":"C++","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":506,"mirror_url":null,"archived":false,"open_issues_count":54,"license":{"key":"bsd-3-clause","name":"BSD 3-Clause \"New\" or \"Revised\" License","spdx_id":"BSD-3-Clause","url":"https://api.github.com/licenses/bsd-3-clause","node_id":"MDc6TGljZW5zZTU="},"forks":506,"open_issues":54,"watchers":1698,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/openthread/openthread/pulls/3238"},"html":{"href":"https://github.com/openthread/openthread/pull/3238"},"issue":{"href":"https://api.github.com/repos/openthread/openthread/issues/3238"},"comments":{"href":"https://api.github.com/repos/openthread/openthread/issues/3238/comments"},"review_comments":{"href":"https://api.github.com/repos/openthread/openthread/pulls/3238/comments"},"review_comment":{"href":"https://api.github.com/repos/openthread/openthread/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/openthread/openthread/pulls/3238/commits"},"statuses":{"href":"https://api.github.com/repos/openthread/openthread/statuses/0413de5f994c7d688896bf46814a83143938e8ba"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"josephn123"}}