From 1ae483d5d91737f76820a1b4186b96b2158f17b8 Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Wed, 2 Feb 2022 21:59:00 +0530 Subject: [PATCH] OTA-Provider: Implementation for obtaining user consent (#13816) * ota-provider: Implementation for obtaining user consent * ota-provider: Support for user consent in linux app * Added an API SourceNodeId() in CommandHandler Using PRIu16 for logging endpoint and ChipLogFormatX64/ChipLogValueX64 for logging Node Id. Use Clock::Seconds32 insetead of Clock::Milliseconds32 * Modified the UserConsent delegate and added default implementation UserConsent is not a SHALL so, using it when its available. Default implementation returns true by default, also added APIs to grant and revoke the UserConsent. * Do not crash if it's a group session * Keep supporting query behaviour option and set DelayedActionTime in Busy and NotAvailable Case * restyled * Fixing error after resolving merge conflict * Remove unnecessary chip:: resolution usage * Use GetSubjectDescriptor() to get SourceNodeId * Addressed review comments * minor changes * Addressed review comments and removed per subject handling, for now supporting only global user consent poliy. --- examples/ota-provider-app/linux/main.cpp | 45 +++- .../ota-provider-common/BUILD.gn | 3 + .../DefaultUserConsentProvider.cpp | 51 +++++ .../DefaultUserConsentProvider.h | 62 ++++++ .../OTAProviderExample.cpp | 192 +++++++++++------- .../ota-provider-common/OTAProviderExample.h | 9 + .../ota-provider-common/UserConsentDelegate.h | 93 +++++++++ src/app/CommandHandler.h | 2 + 8 files changed, 380 insertions(+), 77 deletions(-) create mode 100644 examples/ota-provider-app/ota-provider-common/DefaultUserConsentProvider.cpp create mode 100644 examples/ota-provider-app/ota-provider-common/DefaultUserConsentProvider.h create mode 100644 examples/ota-provider-app/ota-provider-common/UserConsentDelegate.h diff --git a/examples/ota-provider-app/linux/main.cpp b/examples/ota-provider-app/linux/main.cpp index 641c06ed0..349acd5bd 100644 --- a/examples/ota-provider-app/linux/main.cpp +++ b/examples/ota-provider-app/linux/main.cpp @@ -30,8 +30,8 @@ #include #include #include - #include +#include #include #include @@ -54,12 +54,14 @@ constexpr uint16_t kOptionFilepath = 'f'; constexpr uint16_t kOptionOtaImageList = 'o'; constexpr uint16_t kOptionQueryImageBehavior = 'q'; constexpr uint16_t kOptionDelayedActionTimeSec = 'd'; +constexpr uint16_t kOptionUserConsent = 'u'; // Global variables used for passing the CLI arguments to the OTAProviderExample object -static OTAProviderExample::QueryImageBehaviorType gQueryImageBehavior = OTAProviderExample::kRespondWithUpdateAvailable; +static OTAProviderExample::QueryImageBehaviorType gQueryImageBehavior = OTAProviderExample::kRespondWithUnknown; static uint32_t gDelayedActionTimeSec = 0; static const char * gOtaFilepath = nullptr; static const char * gOtaImageListFilepath = nullptr; +static chip::ota::UserConsentState gUserConsentState = chip::ota::UserConsentState::kUnknown; // Parses the JSON filepath and extracts DeviceSoftwareVersionModel parameters static bool ParseJsonFileAndPopulateCandidates(const char * filepath, @@ -183,6 +185,30 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, case kOptionDelayedActionTimeSec: gDelayedActionTimeSec = static_cast(strtol(aValue, NULL, 0)); break; + case kOptionUserConsent: + if (aValue == NULL) + { + PrintArgError("%s: ERROR: NULL UserConsent parameter\n", aProgram); + retval = false; + } + else if (strcmp(aValue, "granted") == 0) + { + gUserConsentState = chip::ota::UserConsentState::kGranted; + } + else if (strcmp(aValue, "denied") == 0) + { + gUserConsentState = chip::ota::UserConsentState::kDenied; + } + else if (strcmp(aValue, "deferred") == 0) + { + gUserConsentState = chip::ota::UserConsentState::kObtaining; + } + else + { + PrintArgError("%s: ERROR: Invalid UserConsent parameter: %s\n", aProgram, aValue); + retval = false; + } + break; default: PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName); retval = false; @@ -197,6 +223,7 @@ OptionDef cmdLineOptionsDef[] = { { "otaImageList", chip::ArgParser::kArgumentRequired, kOptionOtaImageList }, { "QueryImageBehavior", chip::ArgParser::kArgumentRequired, kOptionQueryImageBehavior }, { "DelayedActionTimeSec", chip::ArgParser::kArgumentRequired, kOptionDelayedActionTimeSec }, + { "UserConsent", chip::ArgParser::kArgumentRequired, kOptionUserConsent }, {}, }; @@ -209,7 +236,12 @@ OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS" " Status value in the Query Image Response\n" " -d/--DelayedActionTimeSec