init_OTARequestor.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. *
  3. * Copyright (c) 2022 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "init_OTARequestor.h"
  19. #include "app/clusters/ota-requestor/DefaultOTARequestorStorage.h"
  20. #include <app/clusters/ota-requestor/BDXDownloader.h>
  21. #include <app/clusters/ota-requestor/DefaultOTARequestor.h>
  22. #include <app/clusters/ota-requestor/DefaultOTARequestorDriver.h>
  23. #include <app/clusters/ota-requestor/DefaultOTARequestorUserConsent.h>
  24. #include <app/clusters/ota-requestor/ExtendedOTARequestorDriver.h>
  25. #include <platform/ASR/ASROTAImageProcessor.h>
  26. using namespace chip;
  27. using namespace chip::DeviceLayer;
  28. namespace {
  29. DefaultOTARequestor gRequestorCore;
  30. DefaultOTARequestorStorage gRequestorStorage;
  31. ExtendedOTARequestorDriver gRequestorUser;
  32. BDXDownloader gDownloader;
  33. ASROTAImageProcessor gImageProcessor;
  34. chip::ota::DefaultOTARequestorUserConsent gUserConsentProvider;
  35. static chip::ota::UserConsentState gUserConsentState = chip::ota::UserConsentState::kGranted;
  36. } // namespace
  37. #define OTA_PERIODIC_TIMEOUT 86400 // 24 * 60 * 60
  38. extern "C" void asrQueryImageCmdHandler()
  39. {
  40. ChipLogProgress(DeviceLayer, "Calling asrQueryImageCmdHandler");
  41. PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->TriggerImmediateQuery(); });
  42. }
  43. extern "C" void asrApplyUpdateCmdHandler()
  44. {
  45. ChipLogProgress(DeviceLayer, "Calling asrApplyUpdateCmdHandler");
  46. PlatformMgr().ScheduleWork([](intptr_t) { GetRequestorInstance()->ApplyUpdate(); });
  47. }
  48. void OTAInitializer::InitOTARequestor(void)
  49. {
  50. SetRequestorInstance(&gRequestorCore);
  51. ConfigurationMgr().StoreSoftwareVersion(CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION);
  52. gRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage());
  53. // Set server instance used for session establishment
  54. gRequestorCore.Init(chip::Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader);
  55. gImageProcessor.SetOTADownloader(&gDownloader);
  56. // Connect the Downloader and Image Processor objects
  57. gDownloader.SetImageProcessorDelegate(&gImageProcessor);
  58. gRequestorUser.SetPeriodicQueryTimeout(OTA_PERIODIC_TIMEOUT);
  59. gRequestorUser.Init(&gRequestorCore, &gImageProcessor);
  60. if (gUserConsentState != chip::ota::UserConsentState::kUnknown)
  61. {
  62. gUserConsentProvider.SetUserConsentState(gUserConsentState);
  63. gRequestorUser.SetUserConsentDelegate(&gUserConsentProvider);
  64. }
  65. ChipLogProgress(DeviceLayer, "Current Software Version: %u", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION);
  66. ChipLogProgress(DeviceLayer, "Current Software Version String: %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);
  67. }
  68. void OTAInitializer::ReloadQueryTimeout(uint32_t timeout)
  69. {
  70. if (timeout > 0)
  71. {
  72. gRequestorUser.SetPeriodicQueryTimeout(timeout);
  73. gRequestorUser.RekickPeriodicQueryTimer();
  74. }
  75. }