Skip to content

Commit

Permalink
[ESP32] Added cli to set requestoreCanConsent and UserConsentState at…
Browse files Browse the repository at this point in the history
… run-time (#19677)
  • Loading branch information
jadhavrohit924 authored and pull[bot] committed Jun 26, 2023
1 parent cea578e commit 3346769
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/ota-requestor-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-requestor"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
)
Expand Down
9 changes: 8 additions & 1 deletion examples/ota-requestor-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
#include "nvs_flash.h"
#include <common/CHIPDeviceManager.h>
#include <common/Esp32AppServer.h>

#include <lib/support/ErrorStr.h>
#include <ota/OTAHelper.h>
#include <shell_extension/launch.h>

#include "OTAImageProcessorImpl.h"

Expand All @@ -41,6 +42,7 @@
using namespace ::chip;
using namespace ::chip::System;
using namespace ::chip::DeviceManager;
using namespace chip::Shell;

namespace {
const char * TAG = "ota-requester-app";
Expand Down Expand Up @@ -80,6 +82,11 @@ extern "C" void app_main()
return;
}

#if CONFIG_ENABLE_CHIP_SHELL
chip::LaunchShell();
OTARequestorCommands::GetInstance().Register();
#endif // CONFIG_ENABLE_CHIP_SHELL

CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance();

CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks);
Expand Down
3 changes: 3 additions & 0 deletions examples/ota-requestor-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
# Enable OTA Requestor
CONFIG_ENABLE_OTA_REQUESTOR=y
CONFIG_DEVICE_SOFTWARE_VERSION_NUMBER=2

# Enable Chip Shell
CONFIG_ENABLE_CHIP_SHELL=y
114 changes: 112 additions & 2 deletions examples/platform/esp32/ota/OTAHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,43 @@

#include <app/clusters/ota-requestor/BDXDownloader.h>
#include <app/clusters/ota-requestor/DefaultOTARequestor.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorDriver.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
#include <app/clusters/ota-requestor/ExtendedOTARequestorDriver.h>
#include <platform/ESP32/OTAImageProcessorImpl.h>
#include <system/SystemEvent.h>

#include <app/clusters/ota-requestor/DefaultOTARequestorUserConsent.h>
#include <lib/shell/Commands.h>
#include <lib/shell/Engine.h>
#include <lib/shell/commands/Help.h>
#include <lib/support/logging/CHIPLogging.h>

using namespace chip::DeviceLayer;
using namespace chip;

class CustomOTARequestorDriver : public DeviceLayer::ExtendedOTARequestorDriver
{
public:
bool CanConsent() override;
};

namespace {
DefaultOTARequestor gRequestorCore;
DefaultOTARequestorStorage gRequestorStorage;
DefaultOTARequestorDriver gRequestorUser;
CustomOTARequestorDriver gRequestorUser;
BDXDownloader gDownloader;
OTAImageProcessorImpl gImageProcessor;
chip::Optional<bool> gRequestorCanConsent;
static chip::ota::UserConsentState gUserConsentState = chip::ota::UserConsentState::kUnknown;
chip::ota::DefaultOTARequestorUserConsent gUserConsentProvider;

} // namespace

bool CustomOTARequestorDriver::CanConsent()
{
return gRequestorCanConsent.ValueOr(DeviceLayer::ExtendedOTARequestorDriver::CanConsent());
}

static void InitOTARequestorHandler(System::Layer * systemLayer, void * appState)
{
SetRequestorInstance(&gRequestorCore);
Expand All @@ -43,10 +64,99 @@ static void InitOTARequestorHandler(System::Layer * systemLayer, void * appState
gImageProcessor.SetOTADownloader(&gDownloader);
gDownloader.SetImageProcessorDelegate(&gImageProcessor);
gRequestorUser.Init(&gRequestorCore, &gImageProcessor);

if (gUserConsentState != chip::ota::UserConsentState::kUnknown)
{
gUserConsentProvider.SetUserConsentState(gUserConsentState);
gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider);
}
}

void OTAHelpers::InitOTARequestor()
{
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), InitOTARequestorHandler,
nullptr);
}

namespace chip {
namespace Shell {
namespace {

Shell::Engine sSubShell;

CHIP_ERROR UserConsentStateHandler(int argc, char ** argv)
{
VerifyOrReturnError(argc == 1, CHIP_ERROR_INVALID_ARGUMENT);

if (strcmp(argv[0], "granted") == 0)
{
gUserConsentState = chip::ota::UserConsentState::kGranted;
}
else if (strcmp(argv[0], "denied") == 0)
{
gUserConsentState = chip::ota::UserConsentState::kDenied;
}
else if (strcmp(argv[0], "deferred") == 0)
{
gUserConsentState = chip::ota::UserConsentState::kObtaining;
}
return CHIP_NO_ERROR;
}

CHIP_ERROR RequestorCanConsentHandler(int argc, char ** argv)
{
VerifyOrReturnError(argc == 1, CHIP_ERROR_INVALID_ARGUMENT);

if (strcmp(argv[0], "true") == 0)
{
gRequestorCanConsent.SetValue(true);
}
else if (strcmp(argv[0], "false") == 0)
{
gRequestorCanConsent.SetValue(false);
}
return CHIP_NO_ERROR;
}

CHIP_ERROR OTARequestorHandler(int argc, char ** argv)
{
if (argc == 0)
{
sSubShell.ForEachCommand(PrintCommandHelp, nullptr);
return CHIP_NO_ERROR;
}

CHIP_ERROR error = sSubShell.ExecCommand(argc, argv);

if (error != CHIP_NO_ERROR)
{
streamer_printf(streamer_get(), "Error: %" CHIP_ERROR_FORMAT "\r\n", error.Format());
}

return error;
}
} // namespace

void OTARequestorCommands::Register()
{
// Register subcommands of the `OTARequestor` commands.
static const shell_command_t subCommands[] = {
{ &UserConsentStateHandler, "userConsentState",
"Set UserConsentState for QueryImageCommand\n"
"Usage: OTARequestor userConsentState <granted/denied/deferred>" },
{ &RequestorCanConsentHandler, "requestorCanConsent",
"Set requestorCanConsent for QueryImageCommand\n"
"Usage: OTARequestor requestorCanConsent <true/false>" },

};

sSubShell.RegisterCommands(subCommands, ArraySize(subCommands));

// Register the root `OTA Requestor` command in the top-level shell.
static const shell_command_t otaRequestorCommand = { &OTARequestorHandler, "OTARequestor", "OTA Requestor commands" };

Engine::Root().RegisterCommands(&otaRequestorCommand, 1);
}

} // namespace Shell
} // namespace chip
31 changes: 31 additions & 0 deletions examples/platform/esp32/ota/OTAHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <stdint.h>
class OTAHelpers
{
Expand All @@ -26,3 +28,32 @@ class OTAHelpers
static constexpr uint32_t kInitOTARequestorDelaySec = 3;
void InitOTARequestor(void);
};

namespace chip {
namespace Shell {

class OTARequestorCommands
{
public:
// delete the copy constructor
OTARequestorCommands(const OTARequestorCommands &) = delete;
// delete the move constructor
OTARequestorCommands(OTARequestorCommands &&) = delete;
// delete the assignment operator
OTARequestorCommands & operator=(const OTARequestorCommands &) = delete;

static OTARequestorCommands & GetInstance()
{
static OTARequestorCommands instance;
return instance;
}

// Register the OTA requestor commands
void Register();

private:
OTARequestorCommands() {}
};

} // namespace Shell
} // namespace chip

0 comments on commit 3346769

Please sign in to comment.