|
24 | 24 | #include <lib/core/DataModelTypes.h>
|
25 | 25 |
|
26 | 26 | using namespace std;
|
| 27 | +using namespace chip::app::Clusters; |
27 | 28 | using namespace chip::app::Clusters::AccountLogin;
|
28 | 29 | using Status = chip::Protocols::InteractionModel::Status;
|
29 | 30 |
|
| 31 | +namespace { |
| 32 | + |
| 33 | +const auto loginTempAccountIdentifierFieldId = |
| 34 | + to_string(chip::to_underlying(AccountLogin::Commands::Login::Fields::kTempAccountIdentifier)); |
| 35 | +const auto loginSetupPINFieldId = to_string(chip::to_underlying(AccountLogin::Commands::Login::Fields::kSetupPIN)); |
| 36 | +const auto loginNodeFieldId = to_string(chip::to_underlying(AccountLogin::Commands::Login::Fields::kNode)); |
| 37 | +const auto logoutNodeFieldId = to_string(chip::to_underlying(AccountLogin::Commands::Logout::Fields::kNode)); |
| 38 | + |
| 39 | +string charSpanToString(const CharSpan & charSpan) |
| 40 | +{ |
| 41 | + return { charSpan.data(), charSpan.size() }; |
| 42 | +} |
| 43 | + |
| 44 | +std::string serializeLoginCommand(AccountLogin::Commands::Login::Type cmd) |
| 45 | +{ |
| 46 | + return R"({")" + loginTempAccountIdentifierFieldId + R"(":")" + charSpanToString(cmd.tempAccountIdentifier) + R"(",)" + R"(")" + |
| 47 | + loginSetupPINFieldId + R"(":")" + charSpanToString(cmd.setupPIN) + R"(",)" + R"(")" + loginNodeFieldId + R"(":")" + |
| 48 | + to_string(cmd.node.Value()) + R"("})"; |
| 49 | +} |
| 50 | + |
| 51 | +std::string serializeLogoutCommand(AccountLogin::Commands::Logout::Type cmd) |
| 52 | +{ |
| 53 | + return R"({")" + logoutNodeFieldId + R"(":")" + to_string(cmd.node.Value()) + R"("})"; |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace |
| 57 | + |
30 | 58 | AccountLoginManager::AccountLoginManager(ContentAppCommandDelegate * commandDelegate, const char * setupPin) :
|
31 | 59 | mCommandDelegate(commandDelegate)
|
32 | 60 | {
|
33 | 61 | CopyString(mSetupPin, sizeof(mSetupPin), setupPin);
|
34 | 62 | }
|
35 | 63 |
|
36 |
| -bool AccountLoginManager::HandleLogin(const CharSpan & tempAccountIdentifier, const CharSpan & setupPin, |
| 64 | +bool AccountLoginManager::HandleLogin(const CharSpan & tempAccountIdentifier, const CharSpan & setupPIN, |
37 | 65 | const chip::Optional<chip::NodeId> & nodeId)
|
38 | 66 | {
|
39 | 67 | ChipLogProgress(DeviceLayer, "AccountLoginManager::HandleLogin called for endpoint %d", mEndpointId);
|
40 |
| - string tempAccountIdentifierString(tempAccountIdentifier.data(), tempAccountIdentifier.size()); |
41 |
| - string setupPinString(setupPin.data(), setupPin.size()); |
42 | 68 |
|
43 |
| - if (strcmp(mSetupPin, setupPinString.c_str()) == 0) |
| 69 | + if (mCommandDelegate == nullptr) |
44 | 70 | {
|
45 |
| - ChipLogProgress(Zcl, "AccountLoginManager::HandleLogin success"); |
46 |
| - return true; |
| 71 | + ChipLogError(Zcl, "CommandDelegate not found"); |
| 72 | + return false; |
47 | 73 | }
|
48 |
| - else |
| 74 | + |
| 75 | + if (tempAccountIdentifier.empty() || setupPIN.empty() || !nodeId.HasValue()) |
49 | 76 | {
|
50 |
| - ChipLogProgress(Zcl, "AccountLoginManager::HandleLogin failed expected pin %s", mSetupPin); |
| 77 | + ChipLogError(Zcl, "Invalid parameters"); |
51 | 78 | return false;
|
52 | 79 | }
|
| 80 | + |
| 81 | + Json::Value response; |
| 82 | + bool commandHandled = true; |
| 83 | + AccountLogin::Commands::Login::Type cmd = { tempAccountIdentifier, setupPIN, nodeId }; |
| 84 | + |
| 85 | + auto status = mCommandDelegate->InvokeCommand(mEndpointId, AccountLogin::Id, AccountLogin::Commands::Login::Id, |
| 86 | + serializeLoginCommand(cmd), commandHandled, response); |
| 87 | + if (status == Status::Success) |
| 88 | + { |
| 89 | + // Format status response to verify that response is non-failure. |
| 90 | + status = mCommandDelegate->FormatStatusResponse(response); |
| 91 | + } |
| 92 | + ChipLogProgress(Zcl, "AccountLoginManager::HandleLogin command returned with status: %d", chip::to_underlying(status)); |
| 93 | + return status == chip::Protocols::InteractionModel::Status::Success; |
53 | 94 | }
|
54 | 95 |
|
55 | 96 | bool AccountLoginManager::HandleLogout(const chip::Optional<chip::NodeId> & nodeId)
|
56 | 97 | {
|
57 |
| - // TODO: Insert your code here to send logout request |
58 |
| - return true; |
| 98 | + ChipLogProgress(DeviceLayer, "AccountLoginManager::HandleLogout called for endpoint %d", mEndpointId); |
| 99 | + |
| 100 | + if (mCommandDelegate == nullptr) |
| 101 | + { |
| 102 | + ChipLogError(Zcl, "CommandDelegate not found"); |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + if (!nodeId.HasValue()) |
| 107 | + { |
| 108 | + ChipLogError(Zcl, "Invalid parameters"); |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + Json::Value response; |
| 113 | + bool commandHandled = true; |
| 114 | + AccountLogin::Commands::Logout::Type cmd = { nodeId }; |
| 115 | + |
| 116 | + auto status = mCommandDelegate->InvokeCommand(mEndpointId, AccountLogin::Id, AccountLogin::Commands::Logout::Id, |
| 117 | + serializeLogoutCommand(cmd), commandHandled, response); |
| 118 | + |
| 119 | + if (status == Status::Success) |
| 120 | + { |
| 121 | + // Format status response to verify that response is non-failure. |
| 122 | + status = mCommandDelegate->FormatStatusResponse(response); |
| 123 | + } |
| 124 | + ChipLogProgress(Zcl, "AccountLoginManager::HandleLogout command returned with status: %d", chip::to_underlying(status)); |
| 125 | + return status == chip::Protocols::InteractionModel::Status::Success; |
59 | 126 | }
|
60 | 127 |
|
61 | 128 | void AccountLoginManager::HandleGetSetupPin(CommandResponseHelper<GetSetupPINResponse> & helper,
|
|
0 commit comments