Engine.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. *
  3. * Copyright (c) 2020 Project CHIP Authors
  4. * Copyright (c) 2017 Google LLC
  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. * Source implementation for a generic shell API for CHIP examples.
  21. */
  22. #include <lib/shell/Engine.h>
  23. #include <core/CHIPError.h>
  24. #include <lib/shell/Commands.h>
  25. #include <lib/support/CHIPMem.h>
  26. #include <platform/CHIPDeviceLayer.h>
  27. #include <support/CodeUtils.h>
  28. #include <support/logging/CHIPLogging.h>
  29. #include <assert.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <stdbool.h>
  33. #include <stdint.h>
  34. #include <string.h>
  35. using namespace chip::Logging;
  36. namespace chip {
  37. namespace Shell {
  38. Engine Engine::theEngineRoot;
  39. void Engine::ForEachCommand(shell_command_iterator_t * on_command, void * arg)
  40. {
  41. for (unsigned i = 0; i < _commandSetCount; i++)
  42. {
  43. for (unsigned j = 0; j < _commandSetSize[i]; j++)
  44. {
  45. if (on_command(&_commandSet[i][j], arg))
  46. {
  47. return;
  48. }
  49. }
  50. }
  51. }
  52. void Engine::RegisterCommands(shell_command_t * command_set, unsigned count)
  53. {
  54. if (_commandSetCount >= CHIP_SHELL_MAX_MODULES)
  55. {
  56. ChipLogError(Shell, "Max number of modules reached\n");
  57. assert(0);
  58. }
  59. _commandSet[_commandSetCount] = command_set;
  60. _commandSetSize[_commandSetCount] = count;
  61. ++_commandSetCount;
  62. }
  63. int Engine::ExecCommand(int argc, char * argv[])
  64. {
  65. int retval = CHIP_ERROR_INVALID_ARGUMENT;
  66. VerifyOrReturnError(argc > 0, retval);
  67. // Find the command
  68. for (unsigned i = 0; i < _commandSetCount; i++)
  69. {
  70. for (unsigned j = 0; j < _commandSetSize[i]; j++)
  71. {
  72. if (strcmp(argv[0], _commandSet[i][j].cmd_name) == 0)
  73. {
  74. // Execute the command!
  75. retval = _commandSet[i][j].cmd_func(argc - 1, argv + 1);
  76. break;
  77. }
  78. }
  79. }
  80. return retval;
  81. }
  82. void Engine::RegisterDefaultCommands()
  83. {
  84. RegisterBase64Commands();
  85. RegisterMetaCommands();
  86. #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
  87. RegisterBLECommands();
  88. #endif
  89. #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION || CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  90. RegisterWiFiCommands();
  91. #endif
  92. #if CONFIG_DEVICE_LAYER
  93. RegisterConfigCommands();
  94. #endif
  95. }
  96. } // namespace Shell
  97. } // namespace chip