Skip to content

Commit

Permalink
Working on door-lock implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin-crossman committed Dec 16, 2021
1 parent 7c02d19 commit 3169044
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 38 deletions.
5 changes: 3 additions & 2 deletions examples/lock-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import("//build_overrides/chip.gni")

executable("chip-lock-app") {
sources = [
#"include/tv-callbacks.cpp",
"src/LockManager.cpp",
"main.cpp",
]

Expand All @@ -28,7 +28,8 @@ executable("chip-lock-app") {
]

include_dirs =
[ "${chip_root}/examples/lock-app/lock-common/include" ]
[ "${chip_root}/examples/lock-app/lock-common/include",
"include" ]

cflags = [ "-Wconversion" ]

Expand Down
19 changes: 12 additions & 7 deletions examples/lock-app/linux/include/LockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,24 @@ class LockManager
kState_Unlocked,
} State;

int Init();
bool InitiateAction(Action_t aAction);
void Init();
//bool InitiateAction(Action_t aAction);

using LockCallback_fn = std::function<void(Action_t)>;
bool CheckPin(const char* pin);

void SetCallbacks(LockCallback_fn aActionInitiated_CB, LockCallback_fn aActionCompleted_CB);
//using LockCallback_fn = std::function<void(Action_t)>;

//void SetCallbacks(LockCallback_fn aActionInitiated_CB, LockCallback_fn aActionCompleted_CB);

private:
friend LockManager & LockMgr(void);
State_t mState;

LockCallback_fn mActionInitiated_CB;
LockCallback_fn mActionCompleted_CB;
//State_t mState;

bool mLocked;

//LockCallback_fn mActionInitiated_CB;
//LockCallback_fn mActionCompleted_CB;

static LockManager sLock;
};
Expand Down
48 changes: 35 additions & 13 deletions examples/lock-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <app/clusters/door-lock-server/door-lock-server.h>
#include <app/util/af.h>

#include "LockManager.h"

#include "AppMain.h"

/* Current proposed DoorLockServer API:
Expand All @@ -29,7 +31,7 @@ class DoorLockServer
void InitServer();
bool SetLockState(chip::EndpointId endpointId, chip::app::Clusters::DoorLock::DlLockState newLockState);
bool SetLockState(chip::EndpointId endpointId, chip::app::Clusters::DoorLock::DlLockState newLockState);
bool SetActuatorState(chip::EndpointId endpointId, bool actuatorState);
bool SetDoorState(chip::EndpointId endpointId, chip::app::Clusters::DoorLock::DlLockState doorState);
Expand Down Expand Up @@ -57,24 +59,31 @@ bool emberAfPluginDoorLockSetCredential(chip::EndpointId endpointId, ...);
bool emberAfPluginDoorLockClearCredential(chip::EndpointId endpointId, ...);
*/

// Many of these should just be implemented in src/app/clusters/door-lock-server/*.
using namespace chip;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::DoorLock;

bool emberAfPluginDoorLockOnDoorLockCommand(chip::EndpointId endpointId, const char * PINCode)
{
// TODO: Set LockState, ActuatorEnabled
// call SetLockState
// call SetActuatorState
return true;
if(LockMgr().CheckPin(PINCode))
{
return DoorLockServer::Instance().SetLockState(endpointId, DlLockState::kLocked);
}

return false;
}

bool emberAfPluginDoorLockOnDoorUnlockCommand(chip::EndpointId endpointId, const char * PINCode)
{
// TODO: Set LockState, ActuatorEnabled
// call SetLockState
// call SetActuatorState
return true;
}
if(LockMgr().CheckPin(PINCode))
{
return DoorLockServer::Instance().SetLockState(endpointId, DlLockState::kUnlocked);
}

return false;
}

/*
bool emberAfPluginDoorLockGetUsers(chip::EndpointId endpointId, ...)
{
// TODO: Get (how to get? send out msg somehow?)
Expand Down Expand Up @@ -111,23 +120,36 @@ bool emberAfPluginDoorLockClearCredential(chip::EndpointId endpointId, ...)
// TODO:
return true;
}
*/


/* TODO: Don't think we need this
bool emberAfBasicClusterMfgSpecificPingCallback(chip::app::Command * commandObj)
{
emberAfSendDefaultResponse(emberAfCurrentCommand(), EMBER_ZCL_STATUS_SUCCESS);
return true;
}
*/

void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t mask, uint8_t type, uint16_t size, uint8_t * value)
{
// Watch for LockState, DoorState, Mode, etc changes and trigger appropriate action
// TODO: Watch for LockState, DoorState, Mode, etc changes and trigger appropriate action

if (attributePath.mClusterId == DoorLock::Id)
{
emberAfDoorLockClusterPrintln("Door Lock attribute changed");
}
}

void emberAfDoorLockClusterInitCallback(EndpointId endpoint)
{
// TODO: Implement if needed
}


int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv) == 0);
LockMgr().Init();
ChipLinuxAppMainLoop();
return 0;
}
20 changes: 20 additions & 0 deletions examples/lock-app/linux/src/LockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,27 @@
#include "LockManager.h"

#include <iostream>
#include <cstring>
#include <lib/support/logging/CHIPLogging.h>

LockManager LockManager::sLock;

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

bool LockManager::CheckPin(const char* pin)
{
// TODO: Remove pin hardcode
if(pin != NULL && std::strncmp(pin, "1234", 4) == 0)
{
mLocked = true;

return true;
}
else
{
return false;
}
}
52 changes: 38 additions & 14 deletions src/app/clusters/door-lock-server/door-lock-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* limitations under the License. */

/****************************************************************************
* @file
Expand Down Expand Up @@ -70,8 +69,8 @@ bool DoorLockServer::SetLockState(chip::EndpointId endpointId, DlLockState newLo

emberAfDoorLockClusterPrintln("Setting Lock State to '%hhu'", lockState);

bool status = Attributes::LockState::Set(endpointId, lockState);
if (!status)
auto status = Attributes::LockState::Set(endpointId, lockState);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(Zcl, "Unable to set the Lock State to %hhu: internal error", lockState);
}
Expand All @@ -86,7 +85,7 @@ bool DoorLockServer::SetActuatorState(chip::EndpointId endpointId, bool newActua
emberAfDoorLockClusterPrintln("Setting Actuator State to '%hhu'", actuatorState);

bool status = Attributes::LockState::Set(endpointId, actuatorState);
if (!status)
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(Zcl, "Unable to set the Actuator State to %hhu: internal error", actuatorState);
}
Expand All @@ -101,7 +100,7 @@ bool DoorLockServer::SetDoorState(chip::EndpointId endpointId, DlLockState newDo
emberAfDoorLockClusterPrintln("Setting Door State to '%hhu'", doorState);
bool status = Attributes::DoorState::Set(endpointId, doorState);

if (!status)
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(Zcl, "Unable to set the Door State to %hhu: internal error", doorState);
}
Expand Down Expand Up @@ -140,23 +139,48 @@ bool emberAfDoorLockClusterLockDoorCallback(chip::app::CommandHandler * commandO
const chip::app::ConcreteCommandPath & commandPath,
const chip::app::Clusters::DoorLock::Commands::LockDoor::DecodableType & commandData)
{
emberAfDoorLockClusterPrintln("Received Lock Door command (not implemented)");
emberAfDoorLockClusterPrintln("Received Lock Door command (implementing currently!)");
bool success = false;

// TODO: Implement door locking by calling emberAfPluginDoorLockOnDoorLockCommand
// TODO: Get actual endpoint id
chip::EndpointId endpoint = 1;

emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
return true;
if(commandData.pinCode.HasValue())
{
success = emberAfPluginDoorLockOnDoorLockCommand(endpoint,
reinterpret_cast<const char*>(commandData.pinCode.Value().data()));
}
else
{
success = emberAfPluginDoorLockOnDoorLockCommand(endpoint, NULL);
}

emberAfSendImmediateDefaultResponse(success ? EMBER_ZCL_STATUS_SUCCESS : EMBER_ZCL_STATUS_FAILURE);
return success;
}

bool emberAfDoorLockClusterUnlockDoorCallback(
chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
const chip::app::Clusters::DoorLock::Commands::UnlockDoor::DecodableType & commandData)
{
emberAfDoorLockClusterPrintln("Received Unlock Door command (not implemented)");
emberAfDoorLockClusterPrintln("Received Unlock Door command (implementing currently!)");
bool success = false;

// TODO: Implement door unlocking by calling emberAfPluginDoorLockOnDoorUnlockCommand
emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
return true;
// TODO: Get actual endpoint id
chip::EndpointId endpoint = 1;

if(commandData.pinCode.HasValue())
{
success = emberAfPluginDoorLockOnDoorUnlockCommand(endpoint,
reinterpret_cast<const char*>(commandData.pinCode.Value().data()));
}
else
{
success = emberAfPluginDoorLockOnDoorUnlockCommand(endpoint, NULL);
}

emberAfSendImmediateDefaultResponse(success ? EMBER_ZCL_STATUS_SUCCESS : EMBER_ZCL_STATUS_FAILURE);
return success;
}

bool emberAfDoorLockClusterSetUserCallback(chip::app::CommandHandler * commandObj,
Expand Down
14 changes: 12 additions & 2 deletions src/app/clusters/door-lock-server/door-lock-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@

class DoorLockServer
{
public:
enum LockState
{
LOCKED,
UNLOCKED,
UNKNOWN
};

// Where should this actually live?
char *pin;

static DoorLockServer & Instance();
static DoorLockServer instance;

void InitServer(chip::EndpointId endpointId);

Expand All @@ -49,8 +61,6 @@ class DoorLockServer
bool SetOneTouchLocking(chip::EndpointId endpointId, bool isEnabled);
bool SetPrivacyModeButton(chip::EndpointId endpointId, bool isEnabled);

private:
static DoorLockServer instance;
};

bool emberAfPluginDoorLockOnDoorLockCommand(chip::EndpointId endpointId, const char * PINCOde);
Expand Down

0 comments on commit 3169044

Please sign in to comment.