Skip to content

Commit

Permalink
Addressing review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin-crossman committed Dec 14, 2021
1 parent b45caa4 commit 8405ffd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 62 deletions.
17 changes: 6 additions & 11 deletions examples/lock-app/linux/include/LockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,18 @@

#include <functional>

#include <app/util/af.h>

class LockManager
{
public:
void Init();

bool Lock(const char* pin);
bool Unlock(const char* pin);
bool Lock(chip::Optional<chip::ByteSpan> pin);
bool Unlock(chip::Optional<chip::ByteSpan> pin);

static LockManager & Instance();
static LockManager instance;
private:
friend LockManager & LockMgr(void);

bool mLocked;

static LockManager sLock;
};

inline LockManager & LockMgr(void)
{
return LockManager::sLock;
}
10 changes: 5 additions & 5 deletions examples/lock-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ using namespace chip::app::Clusters::DoorLock;
// should wait for door to be locked on lock command and return success) but
// door lock server should check pin before even calling the lock-door
// callback.
bool emberAfPluginDoorLockOnDoorLockCommand(chip::EndpointId endpointId, const char * PINCode)
bool emberAfPluginDoorLockOnDoorLockCommand(chip::EndpointId endpointId, chip::Optional<chip::ByteSpan> pinCode)
{
return LockMgr().Lock(PINCode);
return LockManager::Instance().Lock(pinCode);
}

bool emberAfPluginDoorLockOnDoorUnlockCommand(chip::EndpointId endpointId, const char * PINCode)
bool emberAfPluginDoorLockOnDoorUnlockCommand(chip::EndpointId endpointId, chip::Optional<chip::ByteSpan> pinCode)
{
return LockMgr().Unlock(PINCode);
return LockManager::Instance().Unlock(pinCode);
}

void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t mask, uint8_t type, uint16_t size, uint8_t * value)
Expand All @@ -59,7 +59,7 @@ void emberAfDoorLockClusterInitCallback(EndpointId endpoint)
int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv) == 0);
LockMgr().Init();
LockManager::Instance().Init();
ChipLinuxAppMainLoop();
return 0;
}
11 changes: 8 additions & 3 deletions examples/lock-app/linux/src/LockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@
#include <cstring>
#include <lib/support/logging/CHIPLogging.h>

LockManager LockManager::sLock;
LockManager LockManager::instance;

LockManager & LockManager::Instance()
{
return instance;
}

void LockManager::Init()
{
mLocked = false;
}

bool LockManager::Lock(const char* pin)
bool LockManager::Lock(chip::Optional<chip::ByteSpan> pin)
{
mLocked = true;
return true;
}

bool LockManager::Unlock(const char* pin)
bool LockManager::Unlock(chip::Optional<chip::ByteSpan> pin)
{
mLocked = false;
return true;
Expand Down
70 changes: 32 additions & 38 deletions src/app/clusters/door-lock-server/door-lock-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ EmberEventControl emberAfPluginDoorLockServerRelockEventControl;

DoorLockServer DoorLockServer::instance;

// TODO: Remove hardcoded pin when SetCredential command is implemented.
chip::ByteSpan mPin({1,2,3,4});

/**********************************************************
* DoorLockServer Implementation
*********************************************************/
Expand All @@ -61,8 +64,7 @@ DoorLockServer & DoorLockServer::Instance()
void DoorLockServer::InitServer(chip::EndpointId endpointId)
{
emberAfDoorLockClusterPrintln("Door Lock cluster initialized at %d", endpointId);
// TODO: Remove hardcode
strcpy(mPin, "1234");

SetLockState(endpointId, DlLockState::kLocked);;
SetActuatorEnabled(endpointId, true);
}
Expand All @@ -88,7 +90,7 @@ bool DoorLockServer::SetActuatorEnabled(chip::EndpointId endpointId, bool newAct

emberAfDoorLockClusterPrintln("Setting Actuator Enabled State to '%hhu'", actuatorState);

bool status = Attributes::ActuatorEnabled::Set(endpointId, actuatorState);
auto status = Attributes::ActuatorEnabled::Set(endpointId, actuatorState);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(Zcl, "Unable to set the Actuator Enabled State to %hhu: internal error", actuatorState);
Expand All @@ -102,7 +104,7 @@ bool DoorLockServer::SetDoorState(chip::EndpointId endpointId, DlLockState newDo
auto doorState = static_cast<uint8_t>(newDoorState);

emberAfDoorLockClusterPrintln("Setting Door State to '%hhu'", doorState);
bool status = Attributes::DoorState::Set(endpointId, doorState);
auto status = Attributes::DoorState::Set(endpointId, doorState);

if (status != EMBER_ZCL_STATUS_SUCCESS)
{
Expand Down Expand Up @@ -151,32 +153,28 @@ bool emberAfDoorLockClusterLockDoorCallback(chip::app::CommandHandler * commandO
bool require_pin = false;
Attributes::RequirePINforRemoteOperation::Get(endpoint, &require_pin);

if(require_pin)
if(commandData.pinCode.HasValue())
{
if(!commandData.pinCode.HasValue())
// TODO: Search through list of stored PINs and check each.
if(mPin.data_equal(commandData.pinCode.Value()))
{
success = false; // Just to be explicit. success == false at this point anywyay
success = emberAfPluginDoorLockOnDoorLockCommand(endpoint, commandData.pinCode);
}
else
{
const char *cmd_pin = reinterpret_cast<const char*>(commandData.pinCode.Value().data());
char *mPin = DoorLockServer::Instance().mPin;
if(strncmp(mPin, cmd_pin, 4) == 0)
{
success = emberAfPluginDoorLockOnDoorLockCommand(endpoint, cmd_pin);
}
else
{
success = false;
}
success = false; // Just to be explicit. success == false at this point anyway
}
}
else
{
const char *cmd_pin = commandData.pinCode.HasValue() ?
reinterpret_cast<const char*>(commandData.pinCode.Value().data()) :
NULL;
success = emberAfPluginDoorLockOnDoorLockCommand(endpoint, cmd_pin);
if(!require_pin)
{
success = emberAfPluginDoorLockOnDoorLockCommand(endpoint, commandData.pinCode);
}
else
{
success = false;
}
}

emberAfSendImmediateDefaultResponse(success ? EMBER_ZCL_STATUS_SUCCESS : EMBER_ZCL_STATUS_FAILURE);
Expand All @@ -201,32 +199,28 @@ bool emberAfDoorLockClusterUnlockDoorCallback(
bool require_pin = false;
Attributes::RequirePINforRemoteOperation::Get(endpoint, &require_pin);

if(require_pin)
if(commandData.pinCode.HasValue())
{
if(!commandData.pinCode.HasValue())
// TODO: Search through list of stored PINs and check each.
if(mPin.data_equal(commandData.pinCode.Value()))
{
success = false; // Just to be explicit. success == false at this point anywyay
success = emberAfPluginDoorLockOnDoorUnlockCommand(endpoint, commandData.pinCode);
}
else
{
const char *cmd_pin = reinterpret_cast<const char*>(commandData.pinCode.Value().data());
char *mPin = DoorLockServer::Instance().mPin;
if(strncmp(mPin, cmd_pin, 4) == 0)
{
success = emberAfPluginDoorLockOnDoorUnlockCommand(endpoint, cmd_pin);
}
else
{
success = false;
}
success = false; // Just to be explicit. success == false at this point anyway
}
}
else
{
const char *cmd_pin = commandData.pinCode.HasValue() ?
reinterpret_cast<const char*>(commandData.pinCode.Value().data()) :
NULL;
success = emberAfPluginDoorLockOnDoorUnlockCommand(endpoint, cmd_pin);
if(!require_pin)
{
success = emberAfPluginDoorLockOnDoorUnlockCommand(endpoint, commandData.pinCode);
}
else
{
success = false;
}
}

emberAfSendImmediateDefaultResponse(success ? EMBER_ZCL_STATUS_SUCCESS : EMBER_ZCL_STATUS_FAILURE);
Expand Down
7 changes: 2 additions & 5 deletions src/app/clusters/door-lock-server/door-lock-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
class DoorLockServer
{
public:
// Where should this actually live?
char mPin[5];

static DoorLockServer & Instance();
static DoorLockServer instance;

Expand All @@ -56,5 +53,5 @@ class DoorLockServer

};

bool emberAfPluginDoorLockOnDoorLockCommand(chip::EndpointId endpointId, const char * PINCOde);
bool emberAfPluginDoorLockOnDoorUnlockCommand(chip::EndpointId endpointId, const char * PINCode);
bool emberAfPluginDoorLockOnDoorLockCommand(chip::EndpointId endpointId, chip::Optional<chip::ByteSpan> pinCode);
bool emberAfPluginDoorLockOnDoorUnlockCommand(chip::EndpointId endpointId, chip::Optional<chip::ByteSpan> pinCode);

0 comments on commit 8405ffd

Please sign in to comment.