BLE.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <core/CHIPCore.h>
  18. #include <lib/shell/Commands.h>
  19. #if CONFIG_DEVICE_LAYER
  20. #include <platform/CHIPDeviceLayer.h>
  21. #endif
  22. #include <lib/shell/Engine.h>
  23. #include <lib/shell/commands/Help.h>
  24. #include <support/CHIPArgParser.hpp>
  25. #include <support/CHIPMem.h>
  26. #include <support/CodeUtils.h>
  27. using chip::DeviceLayer::ConnectivityMgr;
  28. namespace chip {
  29. namespace Shell {
  30. static chip::Shell::Engine sShellDeviceSubcommands;
  31. int BLEHelpHandler(int argc, char ** argv)
  32. {
  33. sShellDeviceSubcommands.ForEachCommand(PrintCommandHelp, nullptr);
  34. return 0;
  35. }
  36. int BLEAdvertiseHandler(int argc, char ** argv)
  37. {
  38. CHIP_ERROR error = CHIP_NO_ERROR;
  39. streamer_t * sout = streamer_get();
  40. bool adv_enabled;
  41. VerifyOrReturnError(argc == 1, error = CHIP_ERROR_INVALID_ARGUMENT);
  42. adv_enabled = ConnectivityMgr().IsBLEAdvertisingEnabled();
  43. if (strcmp(argv[0], "start") == 0)
  44. {
  45. if (adv_enabled)
  46. {
  47. streamer_printf(sout, "BLE advertising already enabled\r\n");
  48. }
  49. else
  50. {
  51. streamer_printf(sout, "Starting BLE advertising\r\n");
  52. return ConnectivityMgr().SetBLEAdvertisingEnabled(true);
  53. }
  54. }
  55. else if (strcmp(argv[0], "stop") == 0)
  56. {
  57. if (adv_enabled)
  58. {
  59. streamer_printf(sout, "Stopping BLE advertising\r\n");
  60. return ConnectivityMgr().SetBLEAdvertisingEnabled(false);
  61. }
  62. else
  63. {
  64. streamer_printf(sout, "BLE advertising already stopped\r\n");
  65. }
  66. }
  67. else
  68. {
  69. return CHIP_ERROR_INVALID_ARGUMENT;
  70. }
  71. return CHIP_NO_ERROR;
  72. }
  73. int BLEDispatch(int argc, char ** argv)
  74. {
  75. if (argc == 0)
  76. {
  77. BLEHelpHandler(argc, argv);
  78. return CHIP_NO_ERROR;
  79. }
  80. return sShellDeviceSubcommands.ExecCommand(argc, argv);
  81. }
  82. void RegisterBLECommands()
  83. {
  84. static const shell_command_t sBLESubCommands[] = {
  85. { &BLEHelpHandler, "help", "Usage: ble <subcommand>" },
  86. { &BLEAdvertiseHandler, "adv", "Enable or disable advertisement. Usage: ble adv <start|stop>" },
  87. };
  88. static const shell_command_t sBLECommand = { &BLEDispatch, "ble", "BLE transport commands" };
  89. // Register `device` subcommands with the local shell dispatcher.
  90. sShellDeviceSubcommands.RegisterCommands(sBLESubCommands, ArraySize(sBLESubCommands));
  91. // Register the root `btp` command with the top-level shell.
  92. Engine::Root().RegisterCommands(&sBLECommand, 1);
  93. }
  94. } // namespace Shell
  95. } // namespace chip