Skip to content

Commit

Permalink
[Telink]: Updated lock-app to fully support NVS storage. (#31744)
Browse files Browse the repository at this point in the history
* [Telink]: Updated lock-app to fully support NVS storage.

Reduced Lock-app RAM usage from 12kb to 4kb. Refactored credential storage.

Signed-off-by: Misha Tkachenko <[email protected]>

* Restyled by whitespace

* Restyled by clang-format

* [Telink]: Added PWM configuration to project file

Added PWM configuration to project file

---------

Signed-off-by: Misha Tkachenko <[email protected]>
Co-authored-by: Serhii Salamakha <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Alex Tsitsiura <[email protected]>
  • Loading branch information
4 people authored Feb 2, 2024
1 parent 2823126 commit 8da05d0
Show file tree
Hide file tree
Showing 19 changed files with 664 additions and 204 deletions.
4 changes: 4 additions & 0 deletions config/telink/chip-module/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ config CHIP_ENABLE_POWER_ON_FACTORY_RESET
first 5 seconds after power on and this sequence repeated 5 times - factory
reset will be involved.

config CHIP_TASK_STACK_SIZE
int
default 8192

config CHIP_USE_MARS_SENSOR
bool "Use Mars board sensor"
depends on SOC_SERIES_RISCV_TELINK_B9X && (BOARD_TLSR9518ADK80D || BOARD_TLSR9518ADK80D_RETENTION)
Expand Down
5 changes: 4 additions & 1 deletion config/telink/chip-module/Kconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ config SETTINGS_NVS_SECTOR_SIZE_MULT

# Set sector counter of NVS
config SETTINGS_NVS_SECTOR_COUNT
default 10
default 12

# Enable OpenThread

Expand Down Expand Up @@ -329,4 +329,7 @@ config PWM_SHELL
config OPENTHREAD_SHELL
default n

config CHIP_TASK_STACK_SIZE
default 4864 if PM

endif
3 changes: 3 additions & 0 deletions examples/lighting-app/telink/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ CONFIG_CHIP_FACTORY_DATA_BUILD=n
CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n
CONFIG_CHIP_CERTIFICATION_DECLARATION_STORAGE=n

#Enable PWM
CONFIG_PWM=y

# Enable Power Management
CONFIG_PM=n
11 changes: 11 additions & 0 deletions examples/lock-app/telink/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@
#
mainmenu "Matter Telink Lock Example Application"

if BOARD_TLSR9528A_RETENTION || BOARD_TLSR9518ADK80D_RETENTION
config COMMON_LIBC_MALLOC_ARENA_SIZE
default 9216
endif

config SETTINGS_NVS_NAME_CACHE
default y

config SETTINGS_NVS_NAME_CACHE_SIZE
default 256

rsource "../../../config/telink/chip-module/Kconfig.defaults"
source "Kconfig.zephyr"
16 changes: 16 additions & 0 deletions examples/lock-app/telink/boards/tlsr9518adk80d.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/ {
keys {
compatible = "gpio-keys";
key_5: button_5 {
gpios = <&gpioe 0 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
};
key_6: button_6 {
gpios = <&gpioe 1 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
};
};
};

&gpioe {
interrupts = <38 1>;
status = "okay";
};
16 changes: 16 additions & 0 deletions examples/lock-app/telink/boards/tlsr9528a.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/ {
keys {
compatible = "gpio-keys";
key_5: button_5 {
gpios = <&gpiob 6 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
};
key_6: button_6 {
gpios = <&gpiob 7 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
};
};
};

&gpiob {
interrupts = <39 1>;
status = "okay";
};
16 changes: 16 additions & 0 deletions examples/lock-app/telink/boards/tlsr9528a_retention.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/ {
keys {
compatible = "gpio-keys";
key_5: button_5 {
gpios = <&gpiob 6 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
};
key_6: button_6 {
gpios = <&gpiob 7 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
};
};
};

&gpiob {
interrupts = <39 1>;
status = "okay";
};
8 changes: 8 additions & 0 deletions examples/lock-app/telink/include/AppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
#define LOCK_MANAGER_CONFIG_USE_NVM_CREDENTIAL_STORAGE 1
#define LOCK_MANAGER_ACTUATOR_MOVEMENT_TIME_MS 2000

#define APP_MAX_USERS 10
#define APP_MAX_CREDENTIAL 10
#define APP_MAX_WEEKDAY_SCHEDULE_PER_USER 10
#define APP_MAX_YEARDAY_SCHEDULE_PER_USER 10
#define APP_MAX_HOLYDAY_SCHEDULE_PER_USER 10

#define APP_MAX_SCHEDULES_TOTAL 100

#define APP_DEFAULT_USERS_COUNT 5
#define APP_DEFAULT_CREDENTIAL_COUNT 5
#define APP_DEFAULT_WEEKDAY_SCHEDULE_PER_USER_COUNT 5
Expand Down
4 changes: 4 additions & 0 deletions examples/lock-app/telink/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class AppTask : public AppTaskCommon

static void LockActionEventHandler(AppEvent * event);
static void LockStateChanged(LockManager::State_t state);
static void LockJammedEventHandler(void);
static void LockJammedActionHandler(AppEvent * aEvent);
static void LockStateEventHandler(void);
static void LockStateActionHandler(AppEvent * aEvent);

static AppTask sAppTask;
};
Expand Down
37 changes: 14 additions & 23 deletions examples/lock-app/telink/include/LockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,27 @@

struct WeekDaysScheduleInfo
{
DlScheduleStatus status;
DlScheduleStatus status = DlScheduleStatus::kAvailable;
EmberAfPluginDoorLockWeekDaySchedule schedule;
};

struct YearDayScheduleInfo
{
DlScheduleStatus status;
DlScheduleStatus status = DlScheduleStatus::kAvailable;
EmberAfPluginDoorLockYearDaySchedule schedule;
};

struct HolidayScheduleInfo
{
DlScheduleStatus status;
DlScheduleStatus status = DlScheduleStatus::kAvailable;
EmberAfPluginDoorLockHolidaySchedule schedule;
};

namespace TelinkDoorLock {
namespace ResourceRanges {
// Used to size arrays
static constexpr uint16_t kMaxUsers = 10;
static constexpr uint8_t kMaxCredentialsPerUser = 10;
static constexpr uint8_t kMaxWeekdaySchedulesPerUser = 10;
static constexpr uint8_t kMaxYeardaySchedulesPerUser = 10;
static constexpr uint8_t kMaxHolidaySchedules = 10;
static constexpr uint8_t kMaxCredentialSize = 20;
static constexpr uint8_t kNumCredentialTypes = 6;

static constexpr uint8_t kMaxCredentials = kMaxUsers * kMaxCredentialsPerUser;
static constexpr uint8_t kMaxCredentialSize = 20;
static constexpr uint8_t kNumCredentialTypes = 6;

} // namespace ResourceRanges

Expand Down Expand Up @@ -197,26 +190,24 @@ class LockManager
OperationSource mActuatorOperationSource = OperationSource::kButton;
k_timer mActuatorTimer = {};

#if LOCK_MANAGER_CONFIG_USE_NVM_CREDENTIAL_STORAGE
bool ReadConfigValues();
#endif

bool setLockState(chip::EndpointId endpointId, DlLockState lockState, OperationSource source, OperationErrorEnum & err,
const Nullable<chip::FabricIndex> & fabricIdx, const Nullable<chip::NodeId> & nodeId,
const Optional<chip::ByteSpan> & pin);

static void ActuatorTimerEventHandler(k_timer * timer);
static void ActuatorAppEventHandler(const AppEvent & event);

EmberAfPluginDoorLockUserInfo mLockUsers[kMaxUsers];
EmberAfPluginDoorLockCredentialInfo mLockCredentials[kNumCredentialTypes][kMaxCredentials];
WeekDaysScheduleInfo mWeekdaySchedule[kMaxUsers][kMaxWeekdaySchedulesPerUser];
YearDayScheduleInfo mYeardaySchedule[kMaxUsers][kMaxYeardaySchedulesPerUser];
HolidayScheduleInfo mHolidaySchedule[kMaxHolidaySchedules];
#if !LOCK_MANAGER_CONFIG_USE_NVM_CREDENTIAL_STORAGE
EmberAfPluginDoorLockUserInfo mLockUsers[APP_MAX_USERS];
EmberAfPluginDoorLockCredentialInfo mLockCredentials[kNumCredentialTypes][APP_MAX_CREDENTIAL];
WeekDaysScheduleInfo mWeekdaySchedule[APP_MAX_USERS][APP_MAX_WEEKDAY_SCHEDULE_PER_USER];
YearDayScheduleInfo mYeardaySchedule[APP_MAX_USERS][APP_MAX_YEARDAY_SCHEDULE_PER_USER];
HolidayScheduleInfo mHolidaySchedule[APP_MAX_HOLYDAY_SCHEDULE_PER_USER];

char mUserNames[ArraySize(mLockUsers)][DOOR_LOCK_MAX_USER_NAME_SIZE];
uint8_t mCredentialData[kNumCredentialTypes][kMaxCredentials][kMaxCredentialSize];
CredentialStruct mCredentials[kMaxUsers][kMaxCredentials];
uint8_t mCredentialData[kNumCredentialTypes][APP_MAX_CREDENTIAL][kMaxCredentialSize];
CredentialStruct mCredentials[APP_MAX_USERS][APP_MAX_CREDENTIAL];
#endif

static LockManager sLock;
TelinkDoorLock::LockInitParams::LockParam LockParams;
Expand Down
27 changes: 19 additions & 8 deletions examples/lock-app/telink/include/LockSettingsStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ namespace Internal {
class LockSettingsStorage : ZephyrConfig
{
public:
static const ZephyrConfig::Key kConfigKey_LockUser;
static const ZephyrConfig::Key kConfigKey_LockUserName;
static const ZephyrConfig::Key kConfigKey_UserCredentials;
static const ZephyrConfig::Key kConfigKey_WeekDaySchedules;
static const ZephyrConfig::Key kConfigKey_YearDaySchedules;
static const ZephyrConfig::Key kConfigKey_HolidaySchedules;
static const char * kConfigKey_Credential[kNumCredentialTypes];
static const char * kConfigKey_CredentialData[kNumCredentialTypes];
static const char * kConfigKey_LockUser[APP_MAX_USERS];
static const char * kConfigKey_LockUserName[APP_MAX_USERS];
static const char * kConfigKey_UserCredentials[APP_MAX_CREDENTIAL];
static const char * kConfigKey_WeekDaySchedules[APP_MAX_USERS][APP_MAX_WEEKDAY_SCHEDULE_PER_USER];
static const char * kConfigKey_YearDaySchedules[APP_MAX_USERS][APP_MAX_YEARDAY_SCHEDULE_PER_USER];
static const char * kConfigKey_HolidaySchedules[APP_MAX_HOLYDAY_SCHEDULE_PER_USER];

static const char * kConfigKey_CredentialPin[APP_MAX_CREDENTIAL];
static const char * kConfigKey_CredentialFace[APP_MAX_CREDENTIAL];
static const char * kConfigKey_CredentialFingerprint[APP_MAX_CREDENTIAL];
static const char * kConfigKey_CredentialFingervein[APP_MAX_CREDENTIAL];
static const char * kConfigKey_CredentialRfid[APP_MAX_CREDENTIAL];

static const char * kConfigKey_CredentialData[APP_MAX_CREDENTIAL];
static const char * kConfigKey_CredentialDataPin[APP_MAX_CREDENTIAL];
static const char * kConfigKey_CredentialDataFace[APP_MAX_CREDENTIAL];
static const char * kConfigKey_CredentialDataFingerprint[APP_MAX_CREDENTIAL];
static const char * kConfigKey_CredentialDataFingerVein[APP_MAX_CREDENTIAL];
static const char * kConfigKey_CredentialDataRfid[APP_MAX_CREDENTIAL];
};
} // namespace Internal
} // namespace DeviceLayer
Expand Down
57 changes: 57 additions & 0 deletions examples/lock-app/telink/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "AppTask.h"
#include "ButtonManager.h"
#include <LockManager.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/data-model/Nullable.h>
Expand All @@ -39,12 +40,20 @@ LEDWidget sLockLED;
} // namespace

AppTask AppTask::sAppTask;
static const struct gpio_dt_spec sLockJammedInputDt = GPIO_DT_SPEC_GET(DT_NODELABEL(key_5), gpios);
static const struct gpio_dt_spec sLockStatusInputDt = GPIO_DT_SPEC_GET(DT_NODELABEL(key_6), gpios);
Button sLockJammedAction;
Button sLockStatusChangedAction;

CHIP_ERROR AppTask::Init(void)
{
#if APP_USE_EXAMPLE_START_BUTTON
SetExampleButtonCallbacks(LockActionEventHandler);
#endif
sLockJammedAction.Configure(&sLockJammedInputDt, LockJammedEventHandler);
sLockStatusChangedAction.Configure(&sLockStatusInputDt, LockStateEventHandler);
ButtonManagerInst().AddButton(sLockJammedAction);
ButtonManagerInst().AddButton(sLockStatusChangedAction);
InitCommonParts();

#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
Expand Down Expand Up @@ -200,3 +209,51 @@ void AppTask::LockStateChanged(LockManager::State_t state)
break;
}
}

void AppTask::LockJammedEventHandler(void)
{
AppEvent event;

event.Type = AppEvent::kEventType_Button;
event.ButtonEvent.Action = kButtonPushEvent;
event.Handler = LockJammedActionHandler;
GetAppTask().PostEvent(&event);
}

void AppTask::LockJammedActionHandler(AppEvent * aEvent)
{
LOG_INF("Sending a lock jammed event");

/* Generating Door Lock Jammed event */
DoorLockServer::Instance().SendLockAlarmEvent(kExampleEndpointId, AlarmCodeEnum::kLockJammed);
}

void AppTask::LockStateEventHandler(void)
{
AppEvent event;

event.Type = AppEvent::kEventType_Button;
event.ButtonEvent.Action = kButtonPushEvent;
event.Handler = LockStateActionHandler;
GetAppTask().PostEvent(&event);
}

void AppTask::LockStateActionHandler(AppEvent * aEvent)
{
LOG_INF("Sending a lock state event");

// This code was written for testing purpose only
// For real door status the level detection may be used instead of pulse
static DoorStateEnum mDoorState = DoorStateEnum::kDoorOpen;
if (mDoorState == DoorStateEnum::kDoorOpen)
{
mDoorState = DoorStateEnum::kDoorClosed;
}
else
{
mDoorState = DoorStateEnum::kDoorOpen;
}

/* Generating Door Lock Status event */
DoorLockServer::Instance().SetDoorState(kExampleEndpointId, mDoorState);
}
Loading

0 comments on commit 8da05d0

Please sign in to comment.