OTAConfig.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. *
  3. * Copyright (c) 2021 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 "OTAConfig.h"
  19. #include <app/clusters/ota-requestor/BDXDownloader.h>
  20. #include <app/clusters/ota-requestor/DefaultOTARequestor.h>
  21. #include <app/clusters/ota-requestor/DefaultOTARequestorDriver.h>
  22. #include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
  23. #include <app/server/Server.h>
  24. #include <platform/mt793x/OTAImageProcessorImpl.h>
  25. using namespace ::chip;
  26. using namespace ::chip::DeviceLayer;
  27. namespace OTAConfig {
  28. constexpr uint32_t kInitOTARequestorDelaySec = 3;
  29. // Global OTA objects
  30. chip::DefaultOTARequestor gRequestorCore;
  31. chip::DefaultOTARequestorStorage gRequestorStorage;
  32. chip::DeviceLayer::DefaultOTARequestorDriver gRequestorUser;
  33. chip::BDXDownloader gDownloader;
  34. chip::OTAImageProcessorImpl gImageProcessor;
  35. static void PlatformEventHandler(const ChipDeviceEvent * event, intptr_t arg);
  36. static void InitRequestor(void);
  37. void Init()
  38. {
  39. ChipLogProgress(DeviceLayer, "Register OTA Requestor init handler");
  40. PlatformMgrImpl().AddEventHandler(PlatformEventHandler, 0);
  41. }
  42. void PlatformEventHandler(const ChipDeviceEvent * event, intptr_t arg)
  43. {
  44. ChipLogProgress(SoftwareUpdate, "OTA Config handler enter !!");
  45. switch (event->Type)
  46. {
  47. case DeviceEventType::kInternetConnectivityChange:
  48. if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established ||
  49. event->InternetConnectivityChange.IPv6 == kConnectivity_Established)
  50. {
  51. ChipLogProgress(SoftwareUpdate, "Internet connect established!!");
  52. InitRequestor();
  53. // SystemLayer().StartTimer(System::Clock::Seconds32(kInitOTARequestorDelaySec), InitRequestor, nullptr);
  54. }
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. void InitRequestor(void)
  61. {
  62. ChipLogProgress(SoftwareUpdate, "Init Requestor enter");
  63. if (GetRequestorInstance() != nullptr)
  64. return;
  65. ChipLogProgress(SoftwareUpdate, "Initializing requestor");
  66. // Initialize and interconnect the Requestor and Image Processor objects -- START
  67. SetRequestorInstance(&gRequestorCore);
  68. gRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage());
  69. gRequestorCore.Init(chip::Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader);
  70. gRequestorUser.Init(&gRequestorCore, &gImageProcessor);
  71. gImageProcessor.SetOTADownloader(&gDownloader);
  72. // Connect the Downloader and Image Processor objects
  73. gDownloader.SetImageProcessorDelegate(&gImageProcessor);
  74. // Initialize and interconnect the Requestor and Image Processor objects -- END
  75. }
  76. } // namespace OTAConfig