Skip to content

Commit 528c7fb

Browse files
committed
improve updateCredential handling
1 parent 59e8967 commit 528c7fb

3 files changed

Lines changed: 75 additions & 17 deletions

File tree

‎drivers/SmartThings/zigbee-lock/src/lock_handlers/zigbee_responses.lua‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function ZigbeeHandlers.set_pin_code_response(driver, device, zb_rx)
3838
-- apply result based on response and identify command result status
3939
local result_status, user_index
4040
if set_pin_code_status == SetCodeStatus.SUCCESS then
41-
result_status, user_index = lock_utils.add_commanded_credential(device, command_in_progress, credential_args_in_use)
41+
result_status, user_index = lock_utils.apply_commanded_credential(device, command_in_progress, credential_args_in_use)
4242
elseif RESPONSE_RESULT_MAP[set_pin_code_status] then
4343
result_status = RESPONSE_RESULT_MAP[set_pin_code_status]
4444
else
@@ -200,7 +200,7 @@ function ZigbeeHandlers.programming_event_notification(driver, device, zb_rx)
200200
local result_status
201201
local user_index = credential_args_in_use.userIndex
202202
if event_code == ProgramEventCode.PIN_CODE_ADDED then -- note: some devices emit PIN_CODE_ADDED even for UPDATE events
203-
result_status, user_index = lock_utils.add_commanded_credential(device, command_in_progress, credential_args_in_use)
203+
result_status, user_index = lock_utils.apply_commanded_credential(device, command_in_progress, credential_args_in_use)
204204
elseif event_code == ProgramEventCode.PIN_CODE_DELETED and (command_in_progress == consts.LOCK_CREDENTIALS.DELETE or command_in_progress == consts.LOCK_USERS.DELETE) then
205205
result_status = tables.delete_entry(device, "credentials", credential_args_in_use.credentialIndex)
206206
-- the deleteUser command injects a deleteCredential command, so ensure the lockUsers command results is emitted in this case.
@@ -213,7 +213,7 @@ function ZigbeeHandlers.programming_event_notification(driver, device, zb_rx)
213213
end
214214
end
215215
elseif event_code == ProgramEventCode.PIN_CODE_CHANGED and command_in_progress == consts.LOCK_CREDENTIALS.UPDATE then
216-
result_status = consts.COMMAND_RESULT.SUCCESS
216+
result_status, user_index = lock_utils.apply_commanded_credential(device, command_in_progress, credential_args_in_use)
217217
end
218218

219219
if result_status then

‎drivers/SmartThings/zigbee-lock/src/lock_utils/utils.lua‎

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ end
5555

5656
-- [[ CREDENTIAL TABLE MANAGEMENT ]] --
5757

58-
-- Add a user and its credential together. Existing entries at either index are left untouched
59-
-- by the underlying table add. Returns the result status of the credential add.
60-
-- A user is only created if the credential add succeeded, so a failed or colliding credential never leaves an orphan user.
58+
-- Add a user and its credential together, so a user never exists without a credential.
59+
-- The credential is added first; the user is only added if that succeeded, so a failed or
60+
-- colliding credential add leaves no user behind. Returns the result status of the credential add.
6161
function lock_utils.add_user_and_credential(device, args)
6262
local name = "Guest " .. args.userIndex
6363
local user_type = "guest"
@@ -77,10 +77,29 @@ function lock_utils.add_user_and_credential(device, args)
7777
return credential_status
7878
end
7979

80+
-- Apply the arguments of an updateCredential command to the credentials table.
81+
-- Returns the command result status and the credential's userIndex.
82+
function lock_utils.update_commanded_credential(device, credential_args)
83+
local existing = tables.find_entry(device, "credentials", credential_args.credentialIndex)
84+
if not existing then return consts.COMMAND_RESULT.FAILURE end
85+
local updates = {}
86+
if credential_args.credentialName ~= nil and credential_args.credentialName ~= existing.credentialName then
87+
updates.credentialName = credential_args.credentialName
88+
end
89+
if credential_args.userIndex ~= nil and credential_args.userIndex ~= existing.userIndex then
90+
updates.userIndex = credential_args.userIndex
91+
end
92+
if next(updates) then
93+
local status = tables.update_entry(device, "credentials", credential_args.credentialIndex, updates)
94+
if status ~= consts.COMMAND_RESULT.SUCCESS then return status end
95+
end
96+
return consts.COMMAND_RESULT.SUCCESS, updates.userIndex or existing.userIndex
97+
end
98+
8099
-- Per command: update the tables after the lock confirmed a set-credential command we sent.
81-
-- Returns the command result status, or nil for any other command. The user is always new,
82-
-- and its index is chosen here. Also returns the newly chosen user's userIndex
83-
function lock_utils.add_commanded_credential(device, command_in_progress, credential_args)
100+
-- Returns the command result status, or nil for any other command, plus the affected userIndex.
101+
-- For addCredential the user is always new, and its index is chosen here.
102+
function lock_utils.apply_commanded_credential(device, command_in_progress, credential_args)
84103
if command_in_progress == consts.LOCK_CREDENTIALS.ADD then
85104
local user_index = tables.next_index(device, "users")
86105
local status = lock_utils.add_user_and_credential(device, {
@@ -89,20 +108,19 @@ function lock_utils.add_commanded_credential(device, command_in_progress, creden
89108
credentialType = credential_args.credentialType,
90109
credentialName = credential_args.credentialName,
91110
})
92-
return status, user_index
111+
return status, status == consts.COMMAND_RESULT.SUCCESS and user_index or nil
93112
elseif command_in_progress == consts.LOCK_CREDENTIALS.UPDATE then
94-
return consts.COMMAND_RESULT.SUCCESS, credential_args.userIndex
113+
return lock_utils.update_commanded_credential(device, credential_args)
95114
end
96115
end
97116

98-
-- Per report: add a user and PIN credential for a code the lock reports at `credential_index` without any command
99-
-- from us (sync, or a programming event). If a credential already exists there it is left in place.
117+
-- Per report: ensure a user and PIN credential exist for a code the lock reports at `credential_index`
118+
-- without any command from us (sync, or a programming event). If a credential already exists there it
119+
-- is left in place
100120
function lock_utils.add_reported_credential(device, credential_index)
101-
local found_cred = tables.find_entry(device, "credentials", credential_index)
102-
if found_cred then return end
103-
local user_index = tables.next_index(device, "users")
121+
if tables.find_entry(device, "credentials", credential_index) then return end
104122
lock_utils.add_user_and_credential(device, {
105-
userIndex = user_index,
123+
userIndex = tables.next_index(device, "users"),
106124
credentialIndex = credential_index,
107125
})
108126
end

‎drivers/SmartThings/zigbee-lock/src/test/test_lock_programming_events.lua‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,23 @@ test.register_coroutine_test(
198198
"ProgrammingEventNotification PIN_CODE_CHANGED while updateCredential is in flight acts as failsafe",
199199
function()
200200
mock_device:set_field(constants.DRIVER_STATE.SLGA_MIGRATED, true, {})
201+
-- Seed the credential that the in-flight updateCredential refers to.
202+
test.socket.zigbee:__queue_receive(build_programming_event(ProgrammingEventCode.PIN_CODE_ADDED, 1))
203+
test.socket.capability:__expect_send(
204+
mock_device:generate_test_message("main",
205+
capabilities.lockCredentials.credentials(
206+
{ { userIndex = 1, credentialIndex = 1, credentialType = "pin", credentialName = "Guest 1" } },
207+
{ visibility = { displayed = false } }
208+
))
209+
)
210+
test.socket.capability:__expect_send(
211+
mock_device:generate_test_message("main",
212+
capabilities.lockUsers.users(
213+
{ { userIndex = 1, userName = "Guest 1", userType = "guest" } },
214+
{ visibility = { displayed = false } }
215+
))
216+
)
217+
test.wait_for_events()
201218
-- Simulate an updateCredential command being in flight with matching user ID.
202219
mock_device:set_field(constants.DRIVER_STATE.BUSY, os.time(), {})
203220
mock_device:set_field(constants.DRIVER_STATE.COMMAND_IN_PROGRESS, constants.LOCK_CREDENTIALS.UPDATE, {})
@@ -219,6 +236,29 @@ test.register_coroutine_test(
219236
end
220237
)
221238

239+
test.register_coroutine_test(
240+
"ProgrammingEventNotification PIN_CODE_CHANGED while updateCredential is in flight fails if the credential is gone",
241+
function()
242+
mock_device:set_field(constants.DRIVER_STATE.SLGA_MIGRATED, true, {})
243+
mock_device:set_field(constants.DRIVER_STATE.BUSY, os.time(), {})
244+
mock_device:set_field(constants.DRIVER_STATE.COMMAND_IN_PROGRESS, constants.LOCK_CREDENTIALS.UPDATE, {})
245+
mock_device:set_field(constants.DRIVER_STATE.CREDENTIAL_ARGS_IN_USE, {
246+
userIndex = 1, credentialIndex = 1, credentialType = "pin"
247+
}, {})
248+
249+
test.socket.zigbee:__queue_receive(build_programming_event(ProgrammingEventCode.PIN_CODE_CHANGED, 1))
250+
test.socket.capability:__expect_send(
251+
mock_device:generate_test_message("main",
252+
capabilities.lockCredentials.commandResult(
253+
{ commandName = "updateCredential", statusCode = "failure", userIndex = nil, credentialIndex = 1 },
254+
{ state_change = true, visibility = { displayed = false } }
255+
)
256+
)
257+
)
258+
test.wait_for_events()
259+
end
260+
)
261+
222262
test.register_coroutine_test(
223263
"ProgrammingEventNotification PIN_CODE_DELETED while deleteCredential is in flight acts as failsafe",
224264
function()

0 commit comments

Comments
 (0)