init_Matter.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "AppConfig.h"
  19. #include <DeviceInfoProviderImpl.h>
  20. #include <app/server/Dnssd.h>
  21. #include <app/server/OnboardingCodesUtil.h>
  22. #include <app/server/Server.h>
  23. #include <credentials/DeviceAttestationCredsProvider.h>
  24. #include <credentials/examples/DeviceAttestationCredsExample.h>
  25. #include <init_Matter.h>
  26. #include <mbedtls/platform.h>
  27. #if CONFIG_ENABLE_CHIP_SHELL
  28. #include <shell/launch_shell.h>
  29. #endif
  30. #include <platform/ASR/ASRFactoryDataProvider.h>
  31. using namespace ::chip;
  32. using namespace ::chip::Inet;
  33. using namespace ::chip::DeviceLayer;
  34. using namespace ::chip::Credentials;
  35. using namespace ::chip::System;
  36. namespace {
  37. ASRFactoryDataProvider sFactoryDataProvider;
  38. } // namespace
  39. chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
  40. CHIP_ERROR MatterInitializer::Matter_Task_Config(lega_task_config_t * cfg)
  41. {
  42. lega_rtos_get_chip_task_cfg(cfg);
  43. return CHIP_NO_ERROR;
  44. }
  45. CHIP_ERROR MatterInitializer::Init_Matter_Stack(const char * appName)
  46. {
  47. // mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);
  48. ASR_LOG("==================================================\r\n");
  49. ASR_LOG("%s starting\r\n", appName);
  50. ASR_LOG("==================================================\r\n");
  51. CHIP_ERROR err = CHIP_NO_ERROR;
  52. err = chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init();
  53. if (err != CHIP_NO_ERROR)
  54. {
  55. ASR_LOG("PersistedStorage::KeyValueStoreMgrImpl().Init() failed");
  56. appError(err);
  57. }
  58. SetCommissionableDataProvider(&sFactoryDataProvider);
  59. //==============================================
  60. // Init Matter Stack
  61. //==============================================
  62. ASR_LOG("Init CHIP Stack");
  63. // Init Chip memory management before the stack
  64. err = chip::Platform::MemoryInit();
  65. ReturnErrorOnFailure(err);
  66. err = PlatformMgr().InitChipStack();
  67. ReturnErrorOnFailure(err);
  68. err = sFactoryDataProvider.Init();
  69. ReturnErrorOnFailure(err);
  70. SetDeviceInstanceInfoProvider(&sFactoryDataProvider);
  71. chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName(appName);
  72. if (CONFIG_NETWORK_LAYER_BLE)
  73. {
  74. ConnectivityMgr().SetBLEAdvertisingEnabled(true);
  75. }
  76. return CHIP_NO_ERROR;
  77. }
  78. CHIP_ERROR MatterInitializer::Init_Matter_Server(void)
  79. {
  80. CHIP_ERROR err = CHIP_NO_ERROR;
  81. // Init Matter Server and Start Event Loop
  82. chip::DeviceLayer::PlatformMgr().LockChipStack();
  83. static chip::CommonCaseDeviceServerInitParams initParams;
  84. (void) initParams.InitializeStaticResourcesBeforeServerInit();
  85. chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
  86. chip::Server::GetInstance().Init(initParams);
  87. chip::DeviceLayer::PlatformMgr().UnlockChipStack();
  88. // Initialize device attestation config
  89. SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider);
  90. ASR_LOG("Starting Platform Manager Event Loop");
  91. // // Start a task to run the CHIP Device event loop.
  92. err = PlatformMgr().StartEventLoopTask();
  93. ReturnErrorOnFailure(err);
  94. #if CONFIG_ENABLE_CHIP_SHELL
  95. chip::LaunchShell();
  96. #endif
  97. return CHIP_NO_ERROR;
  98. }