From d3e3d975f6b86c181aa6103e81348d26b590948c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Kr=C3=B3lik?= <66667989+Damian-Nordic@users.noreply.github.com> Date: Tue, 2 Apr 2024 16:51:48 +0200 Subject: [PATCH] [shell] Clean OTA commands & remove base64 commands (#32396) * [shell] Clean OTA commands 1. Remove obsolete "apply" and "notify" commands. These actions are invoked automatically by OTA requestor. 2. Do not post tasks onto CHIP thread - shell commands are already run on CHIP thread. 3. Do not print error codes. They are already printed by shell core. * [shell] Remove base64 commands Base64 encoding/decoding is not specific to Matter so base64 shell commands are not useful on actual Matter devices. --- docs/guides/silabs_cli_guide.md | 2 - src/lib/shell/Commands.h | 6 -- src/lib/shell/Engine.cpp | 1 - src/lib/shell/commands/BUILD.gn | 1 - src/lib/shell/commands/Base64.cpp | 113 ------------------------- src/lib/shell/commands/Ota.cpp | 136 +++++++++--------------------- 6 files changed, 38 insertions(+), 221 deletions(-) delete mode 100644 src/lib/shell/commands/Base64.cpp diff --git a/docs/guides/silabs_cli_guide.md b/docs/guides/silabs_cli_guide.md index 8a368f093cfd38..5a795399217487 100644 --- a/docs/guides/silabs_cli_guide.md +++ b/docs/guides/silabs_cli_guide.md @@ -197,8 +197,6 @@ OTA commands ```bash matterCli> ota query Query for a new image. Usage: ota query - apply Apply the current update. Usage: ota apply - notify Notify the new image has been applied. Usage: ota notify state Gets state of a current image update process. Usage: ota state progress Gets progress of a current image update process. Usage: ota progress ``` diff --git a/src/lib/shell/Commands.h b/src/lib/shell/Commands.h index 59fb07c1a21906..9cd4c88bbdf281 100644 --- a/src/lib/shell/Commands.h +++ b/src/lib/shell/Commands.h @@ -20,12 +20,6 @@ namespace chip { namespace Shell { -/** - * This function registers the base64 encode/decode commands. - * - */ -void RegisterBase64Commands(); - /** * This function registers the BLE commands. * diff --git a/src/lib/shell/Engine.cpp b/src/lib/shell/Engine.cpp index b83b6cae00f53e..809d44cc3c2bd4 100644 --- a/src/lib/shell/Engine.cpp +++ b/src/lib/shell/Engine.cpp @@ -105,7 +105,6 @@ CHIP_ERROR Engine::ExecCommand(int argc, char * argv[]) void Engine::RegisterDefaultCommands() { - RegisterBase64Commands(); RegisterMetaCommands(); #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE RegisterBLECommands(); diff --git a/src/lib/shell/commands/BUILD.gn b/src/lib/shell/commands/BUILD.gn index d6190feb9ae41f..d4dd5820ad7f7a 100644 --- a/src/lib/shell/commands/BUILD.gn +++ b/src/lib/shell/commands/BUILD.gn @@ -20,7 +20,6 @@ import("${chip_root}/src/system/system.gni") source_set("commands") { sources = [ - "Base64.cpp", "Help.cpp", "Help.h", "Meta.cpp", diff --git a/src/lib/shell/commands/Base64.cpp b/src/lib/shell/commands/Base64.cpp deleted file mode 100644 index 0e64bd5959bbdc..00000000000000 --- a/src/lib/shell/commands/Base64.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/* - * - * Copyright (c) 2021 Project CHIP Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * 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. - */ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -chip::Shell::Engine sShellBase64Commands; - -namespace chip { -namespace Shell { - -static CHIP_ERROR Base64HelpHandler(int argc, char ** argv) -{ - sShellBase64Commands.ForEachCommand(PrintCommandHelp, nullptr); - return CHIP_NO_ERROR; -} - -static CHIP_ERROR Base64DecodeHandler(int argc, char ** argv) -{ - streamer_t * sout = streamer_get(); - uint32_t binarySize; - uint8_t binary[256]; - - VerifyOrReturnError(argc > 0, CHIP_ERROR_INVALID_ARGUMENT); - auto argLen = strlen(argv[0]); - VerifyOrReturnError(CanCastTo(argLen), CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(BASE64_MAX_DECODED_LEN(argLen) <= sizeof(binary), CHIP_ERROR_INVALID_ARGUMENT); - - binarySize = Base64Decode(argv[0], static_cast(argLen), binary); - VerifyOrReturnError(binarySize != UINT16_MAX, CHIP_ERROR_INVALID_ARGUMENT); - streamer_print_hex(sout, binary, static_cast(binarySize)); - streamer_printf(sout, "\r\n"); - return CHIP_NO_ERROR; -} - -static CHIP_ERROR Base64EncodeHandler(int argc, char ** argv) -{ - streamer_t * sout = streamer_get(); - char base64[256]; - uint8_t binary[256]; - uint32_t binarySize, base64Size; - - VerifyOrReturnError(argc > 0, CHIP_ERROR_INVALID_ARGUMENT); - - auto argLen = strlen(argv[0]); - VerifyOrReturnError(CanCastTo(argLen), CHIP_ERROR_INVALID_ARGUMENT); - - ArgParser::ParseHexString(argv[0], static_cast(argLen), binary, sizeof(binary), binarySize); - if (!CanCastTo(binarySize)) - { - return CHIP_ERROR_INVALID_ARGUMENT; - } - base64Size = Base64Encode(binary, static_cast(binarySize), base64); - streamer_printf(sout, "%.*s\r\n", base64Size, base64); - return CHIP_NO_ERROR; -} - -static CHIP_ERROR Base64Dispatch(int argc, char ** argv) -{ - if (argc == 0) - { - return Base64HelpHandler(argc, argv); - } - return sShellBase64Commands.ExecCommand(argc, argv); -} - -void RegisterBase64Commands() -{ - /// Subcommands for root command: `base64 ` - static const shell_command_t sBase64SubCommands[] = { - { &Base64HelpHandler, "help", "Usage: base64 " }, - { &Base64EncodeHandler, "encode", "Encode a hex sting as base64. Usage: base64 encode " }, - { &Base64DecodeHandler, "decode", "Decode a base64 sting as hex. Usage: base64 decode " }, - }; - - static const shell_command_t sBase64Command = { &Base64Dispatch, "base64", "Base64 encode / decode utilities" }; - - // Register `base64` subcommands with the local shell dispatcher. - sShellBase64Commands.RegisterCommands(sBase64SubCommands, ArraySize(sBase64SubCommands)); - - // Register the root `base64` command with the top-level shell. - Engine::Root().RegisterCommands(&sBase64Command, 1); -} - -} // namespace Shell -} // namespace chip diff --git a/src/lib/shell/commands/Ota.cpp b/src/lib/shell/commands/Ota.cpp index 4cc005c5d423bf..729d1b9b059466 100644 --- a/src/lib/shell/commands/Ota.cpp +++ b/src/lib/shell/commands/Ota.cpp @@ -16,12 +16,11 @@ */ #include +#include #include #include #include #include -#include -#include using namespace chip::DeviceLayer; @@ -32,98 +31,35 @@ namespace { Shell::Engine sSubShell; CHIP_ERROR QueryImageHandler(int argc, char ** argv) -{ - VerifyOrReturnError(GetRequestorInstance() != nullptr, CHIP_ERROR_INCORRECT_STATE); - VerifyOrReturnError(argc == 0, CHIP_ERROR_INVALID_ARGUMENT); - PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->TriggerImmediateQuery(); }); - return CHIP_NO_ERROR; -} - -CHIP_ERROR ApplyImageHandler(int argc, char ** argv) -{ - VerifyOrReturnError(GetRequestorInstance() != nullptr, CHIP_ERROR_INCORRECT_STATE); - VerifyOrReturnError(argc == 0, CHIP_ERROR_INVALID_ARGUMENT); - PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->ApplyUpdate(); }); - return CHIP_NO_ERROR; -} - -CHIP_ERROR NotifyImageHandler(int argc, char ** argv) { VerifyOrReturnError(GetRequestorInstance() != nullptr, CHIP_ERROR_INCORRECT_STATE); VerifyOrReturnError(argc == 0, CHIP_ERROR_INVALID_ARGUMENT); - PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->NotifyUpdateApplied(); }); - return CHIP_NO_ERROR; + return GetRequestorInstance()->TriggerImmediateQuery(); } -static void HandleState(intptr_t context) +const char * UpdateStateToString(app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum state) { - app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum state; - CHIP_ERROR err = GetRequestorInstance()->GetUpdateStateAttribute(0, state); - - if (err == CHIP_NO_ERROR) + switch (state) { - streamer_printf(streamer_get(), "Update state: "); - switch (state) - { - case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kUnknown: - streamer_printf(streamer_get(), "unknown"); - break; - case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kIdle: - streamer_printf(streamer_get(), "idle"); - break; - case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kQuerying: - streamer_printf(streamer_get(), "querying"); - break; - case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kDelayedOnQuery: - streamer_printf(streamer_get(), "delayed on query"); - break; - case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kDownloading: - streamer_printf(streamer_get(), "downloading"); - break; - case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kApplying: - streamer_printf(streamer_get(), "applying"); - break; - case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kDelayedOnApply: - streamer_printf(streamer_get(), "delayed on apply"); - break; - case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kRollingBack: - streamer_printf(streamer_get(), "rolling back"); - break; - case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kDelayedOnUserConsent: - streamer_printf(streamer_get(), "delayed on user consent"); - break; - default: - streamer_printf(streamer_get(), "invalid"); - break; - } - streamer_printf(streamer_get(), "\r\n"); - } - else - { - streamer_printf(streamer_get(), "Error: %" CHIP_ERROR_FORMAT "\r\n", err.Format()); - } -} - -static void HandleProgress(intptr_t context) -{ - chip::app::DataModel::Nullable progress; - CHIP_ERROR err = GetRequestorInstance()->GetUpdateStateProgressAttribute(0, progress); - - if (err == CHIP_NO_ERROR) - { - if (progress.IsNull()) - { - streamer_printf(streamer_get(), "Update progress: NULL\r\n"); - } - else - { - streamer_printf(streamer_get(), "Update progress: %d %%\r\n", progress.Value()); - } - } - else - { - streamer_printf(streamer_get(), "Error: %" CHIP_ERROR_FORMAT "\r\n", err.Format()); + case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kIdle: + return "idle"; + case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kQuerying: + return "querying"; + case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kDelayedOnQuery: + return "delayed on query"; + case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kDownloading: + return "downloading"; + case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kApplying: + return "applying"; + case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kDelayedOnApply: + return "delayed on apply"; + case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kRollingBack: + return "rolling back"; + case app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum::kDelayedOnUserConsent: + return "delayed on user consent"; + default: + return "unknown"; } } @@ -132,7 +68,10 @@ CHIP_ERROR StateHandler(int argc, char ** argv) VerifyOrReturnError(GetRequestorInstance() != nullptr, CHIP_ERROR_INCORRECT_STATE); VerifyOrReturnError(argc == 0, CHIP_ERROR_INVALID_ARGUMENT); - PlatformMgr().ScheduleWork(HandleState, reinterpret_cast(nullptr)); + app::Clusters::OtaSoftwareUpdateRequestor::OTAUpdateStateEnum state; + ReturnErrorOnFailure(GetRequestorInstance()->GetUpdateStateAttribute(kRootEndpointId, state)); + + streamer_printf(streamer_get(), "Update state: %s\r\n", UpdateStateToString(state)); return CHIP_NO_ERROR; } @@ -142,7 +81,17 @@ CHIP_ERROR ProgressHandler(int argc, char ** argv) VerifyOrReturnError(GetRequestorInstance() != nullptr, CHIP_ERROR_INCORRECT_STATE); VerifyOrReturnError(argc == 0, CHIP_ERROR_INVALID_ARGUMENT); - PlatformMgr().ScheduleWork(HandleProgress, reinterpret_cast(nullptr)); + app::DataModel::Nullable progress; + ReturnErrorOnFailure(GetRequestorInstance()->GetUpdateStateProgressAttribute(kRootEndpointId, progress)); + + if (progress.IsNull()) + { + streamer_printf(streamer_get(), "Update progress: unknown\r\n"); + } + else + { + streamer_printf(streamer_get(), "Update progress: %d %%\r\n", progress.Value()); + } return CHIP_NO_ERROR; } @@ -155,14 +104,7 @@ CHIP_ERROR OtaHandler(int argc, char ** argv) 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; + return sSubShell.ExecCommand(argc, argv); } } // namespace @@ -171,8 +113,6 @@ void RegisterOtaCommands() // Register subcommands of the `ota` commands. static const shell_command_t subCommands[] = { { &QueryImageHandler, "query", "Query for a new image. Usage: ota query" }, - { &ApplyImageHandler, "apply", "Apply the current update. Usage: ota apply" }, - { &NotifyImageHandler, "notify", "Notify the new image has been applied. Usage: ota notify " }, { &StateHandler, "state", "Gets state of a current image update process. Usage: ota state" }, { &ProgressHandler, "progress", "Gets progress of a current image update process. Usage: ota progress" } };