matter_shell.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /**
  19. * @file
  20. * This file provides the Shell implementation of Matter.
  21. * It can be also used in ASR AT Command implementation.
  22. */
  23. #include "matter_shell.h"
  24. #include "AppConfig.h"
  25. #include <app-common/zap-generated/attributes/Accessors.h>
  26. #include <app-common/zap-generated/ids/Attributes.h>
  27. #include <app-common/zap-generated/ids/Clusters.h>
  28. #include <app/server/Dnssd.h>
  29. #include <app/server/OnboardingCodesUtil.h>
  30. #include <app/server/Server.h>
  31. #include <lega_rtos_api.h>
  32. #include <lib/core/CHIPError.h>
  33. #include <string>
  34. #include <vector>
  35. #if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
  36. #include "init_OTARequestor.h"
  37. #endif
  38. using namespace ::chip;
  39. using namespace ::chip::Credentials;
  40. using namespace ::chip::DeviceLayer;
  41. #if CONFIG_ENABLE_CHIP_SHELL
  42. #include "lib/shell/Engine.h"
  43. using chip::Shell::Engine;
  44. using chip::Shell::shell_command_t;
  45. using chip::Shell::streamer_get;
  46. using chip::Shell::streamer_printf;
  47. #endif
  48. void asr_matter_reset(Reset_t type)
  49. {
  50. if (type == WIFI_RESET)
  51. {
  52. ConnectivityMgr().ClearWiFiStationProvision();
  53. chip::Server::GetInstance().GetFabricTable().DeleteAllFabrics();
  54. chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow();
  55. }
  56. else if (type == FACTORY_RESET)
  57. {
  58. chip::Server::GetInstance().ScheduleFactoryReset();
  59. }
  60. else if (type == COMMISSIONING_RESET)
  61. {
  62. chip::Server::GetInstance().GetFabricTable().DeleteAllFabrics();
  63. auto & commissionMgr = chip::Server::GetInstance().GetCommissioningWindowManager();
  64. commissionMgr.OpenBasicCommissioningWindow(commissionMgr.MaxCommissioningTimeout(),
  65. CommissioningWindowAdvertisement::kDnssdOnly);
  66. }
  67. }
  68. void ShutdownChip()
  69. {
  70. Server::GetInstance().Shutdown();
  71. PlatformMgr().StopEventLoopTask();
  72. PlatformMgr().Shutdown();
  73. }
  74. void asr_matter_onoff(int value)
  75. {
  76. ChipLogProgress(Zcl, "updating on/off = %d", value);
  77. EmberAfStatus status = chip::app::Clusters::OnOff::Attributes::OnOff::Set(
  78. /* endpoint ID */ 1, (uint8_t *) &value);
  79. if (status != EMBER_ZCL_STATUS_SUCCESS)
  80. {
  81. ChipLogProgress(Zcl, "ERR: updating on/off %x", status);
  82. }
  83. }
  84. void asr_matter_sensors(bool enable, int temp, int humi, int pressure)
  85. {
  86. #if ASR_BOARD_ENABLE_SENSORS
  87. if (enable)
  88. {
  89. chip::app::Clusters::TemperatureMeasurement::Attributes::MeasuredValue::Set(
  90. /* endpoint ID */ 1, static_cast<int16_t>(temp));
  91. chip::app::Clusters::RelativeHumidityMeasurement::Attributes::MeasuredValue::Set(
  92. /* endpoint ID */ 1, static_cast<int16_t>(humi));
  93. chip::app::Clusters::PressureMeasurement::Attributes::MeasuredValue::Set(
  94. /* endpoint ID */ 1, static_cast<int16_t>(pressure));
  95. }
  96. #else
  97. ChipLogProgress(Zcl, "Sensor is not supported!");
  98. #endif
  99. }
  100. void asr_matter_ota(uint32_t timeout)
  101. {
  102. #if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
  103. OTAInitializer::Instance().ReloadQueryTimeout(timeout);
  104. #else
  105. ChipLogProgress(Zcl, "OTA is not supported!");
  106. #endif
  107. }
  108. #if CONFIG_ENABLE_CHIP_SHELL
  109. static CHIP_ERROR sLightCommandHandler(int argc, char ** argv)
  110. {
  111. if (argc == 1 && strcmp(argv[0], "on") == 0)
  112. {
  113. asr_matter_onoff(1);
  114. return CHIP_NO_ERROR;
  115. }
  116. if (argc == 1 && strcmp(argv[0], "off") == 0)
  117. {
  118. asr_matter_onoff(0);
  119. return CHIP_NO_ERROR;
  120. }
  121. streamer_printf(streamer_get(), "Usage: OnOff [on|off]");
  122. return CHIP_NO_ERROR;
  123. }
  124. void RegisterLightCommands()
  125. {
  126. static const shell_command_t sLightCommand = { sLightCommandHandler, "OnOff", "OnOff commands. Usage: OnOff [on|off]" };
  127. Engine::Root().RegisterCommands(&sLightCommand, 1);
  128. }
  129. #endif