Skip to content

Commit 59e8967

Browse files
committed
ensure used indices are always valid
1 parent b8ee72c commit 59e8967

5 files changed

Lines changed: 21 additions & 23 deletions

File tree

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@ function CapabilityHandlers.add_credential(driver, device, command)
128128
end
129129

130130
-- addCredential has no credentialIndex argument, so its userIndex argument selects the credential slot. A value
131-
-- of 0 means "auto-assign the next available slot". The user is always new, and takes the next available user slot.
131+
-- of 0 means "auto-assign the next available slot". The user is always new; its index is chosen when the lock confirms the code
132132
local cred_index = command.args.userIndex == 0 and tables.next_index(device, "credentials", 1) or command.args.userIndex
133-
local user_index = tables.next_index(device, "users")
134133

135134
if #(tables.get_state(device, "credentials") or {}) >= tables.get_max_entries(device, "credentials") then
136135
lock_utils.emit_command_result(device, capabilities.lockCredentials, consts.LOCK_CREDENTIALS.ADD, consts.COMMAND_RESULT.RESOURCE_EXHAUSTED)
@@ -145,7 +144,6 @@ function CapabilityHandlers.add_credential(driver, device, command)
145144

146145
-- Set busy state and attempt to set the PIN on-device.
147146
lock_utils.set_busy_state(device, consts.LOCK_CREDENTIALS.ADD, {
148-
userIndex = user_index,
149147
credentialIndex = cred_index,
150148
credentialType = command.args.credentialType,
151149
credentialName = command.args.credentialName,

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ function ZigbeeHandlers.set_pin_code_response(driver, device, zb_rx)
3636
}
3737

3838
-- apply result based on response and identify command result status
39-
local result_status
39+
local result_status, user_index
4040
if set_pin_code_status == SetCodeStatus.SUCCESS then
41-
result_status = lock_utils.add_commanded_credential(device, command_in_progress, credential_args_in_use)
41+
result_status, user_index = lock_utils.add_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
@@ -48,7 +48,7 @@ function ZigbeeHandlers.set_pin_code_response(driver, device, zb_rx)
4848
-- emit command result
4949
if command_in_progress then
5050
local additional_info = result_status == consts.COMMAND_RESULT.SUCCESS and {
51-
userIndex = credential_args_in_use.userIndex,
51+
userIndex = user_index,
5252
credentialIndex = credential_args_in_use.credentialIndex,
5353
} or nil
5454
lock_utils.emit_command_result(device, capabilities.lockCredentials, command_in_progress, result_status, additional_info)
@@ -198,8 +198,9 @@ function ZigbeeHandlers.programming_event_notification(driver, device, zb_rx)
198198
local credential_args_in_use = device:get_field(consts.DRIVER_STATE.CREDENTIAL_ARGS_IN_USE) or {}
199199
if command_in_progress and credential_args_in_use.credentialIndex == user_id then
200200
local result_status
201+
local user_index = credential_args_in_use.userIndex
201202
if event_code == ProgramEventCode.PIN_CODE_ADDED then -- note: some devices emit PIN_CODE_ADDED even for UPDATE events
202-
result_status = lock_utils.add_commanded_credential(device, command_in_progress, credential_args_in_use)
203+
result_status, user_index = lock_utils.add_commanded_credential(device, command_in_progress, credential_args_in_use)
203204
elseif event_code == ProgramEventCode.PIN_CODE_DELETED and (command_in_progress == consts.LOCK_CREDENTIALS.DELETE or command_in_progress == consts.LOCK_USERS.DELETE) then
204205
result_status = tables.delete_entry(device, "credentials", credential_args_in_use.credentialIndex)
205206
-- the deleteUser command injects a deleteCredential command, so ensure the lockUsers command results is emitted in this case.
@@ -220,7 +221,7 @@ function ZigbeeHandlers.programming_event_notification(driver, device, zb_rx)
220221
capabilities.lockCredentials,
221222
command_in_progress,
222223
result_status,
223-
{ userIndex = credential_args_in_use.userIndex, credentialIndex = credential_args_in_use.credentialIndex }
224+
{ userIndex = user_index, credentialIndex = credential_args_in_use.credentialIndex }
224225
)
225226
lock_utils.clear_busy_state(device)
226227
return

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ end
136136
-- An optional `start` overrides the table's minimum, e.g. to skip a reserved slot.
137137
function table_utils.next_index(device, table_name, start)
138138
local def = resolve_table_def(device, table_name)
139-
assert(def, string.format("table_helpers: unknown table %q", tostring(table_name)))
139+
if not def then return 1 end
140140
local t = table_utils.get_state(device, def) or {}
141141
local occupied = {}
142142
for _, entry in ipairs(t) do occupied[entry[def.match_key]] = true end

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ end
5757

5858
-- Add a user and its credential together. Existing entries at either index are left untouched
5959
-- by the underlying table add. Returns the result status of the credential add.
60-
-- Only creates a user if the credential create succeeded, or if the credential already existed
60+
-- A user is only created if the credential add succeeded, so a failed or colliding credential never leaves an orphan user.
6161
function lock_utils.add_user_and_credential(device, args)
6262
local name = "Guest " .. args.userIndex
6363
local user_type = "guest"
@@ -67,7 +67,7 @@ function lock_utils.add_user_and_credential(device, args)
6767
credentialType = args.credentialType or consts.CRED_TYPE_PIN,
6868
credentialName = args.credentialName or name,
6969
})
70-
if credential_status == consts.COMMAND_RESULT.SUCCESS or credential_status == consts.COMMAND_RESULT.OCCUPIED then
70+
if credential_status == consts.COMMAND_RESULT.SUCCESS then
7171
tables.add_entry(device, "users", {
7272
userIndex = args.userIndex,
7373
userName = name,
@@ -78,12 +78,20 @@ function lock_utils.add_user_and_credential(device, args)
7878
end
7979

8080
-- 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.
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
8283
function lock_utils.add_commanded_credential(device, command_in_progress, credential_args)
8384
if command_in_progress == consts.LOCK_CREDENTIALS.ADD then
84-
return lock_utils.add_user_and_credential(device, credential_args)
85+
local user_index = tables.next_index(device, "users")
86+
local status = lock_utils.add_user_and_credential(device, {
87+
userIndex = user_index,
88+
credentialIndex = credential_args.credentialIndex,
89+
credentialType = credential_args.credentialType,
90+
credentialName = credential_args.credentialName,
91+
})
92+
return status, user_index
8593
elseif command_in_progress == consts.LOCK_CREDENTIALS.UPDATE then
86-
return consts.COMMAND_RESULT.SUCCESS
94+
return consts.COMMAND_RESULT.SUCCESS, credential_args.userIndex
8795
end
8896
end
8997

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ local function seed_credentials(entries)
6262
test.wait_for_events()
6363
end
6464

65-
-- Set totalUsersSupported on the mock device and consume the resulting event.
66-
local function set_total_users_supported(n)
67-
test.socket.capability:__expect_send(
68-
mock_device:generate_test_message("main",
69-
capabilities.lockUsers.totalUsersSupported(n, { visibility = { displayed = false } }))
70-
)
71-
mock_device:emit_event(capabilities.lockUsers.totalUsersSupported(n, { visibility = { displayed = false } }))
72-
test.wait_for_events()
73-
end
7465

7566
-- ============================================================================
7667
-- addUser

0 commit comments

Comments
 (0)