Options.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. *
  3. * Copyright (c) 2020 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 "Options.h"
  19. #include <platform/CHIPDeviceLayer.h>
  20. #include <core/CHIPError.h>
  21. #include <support/CHIPArgParser.hpp>
  22. using namespace chip;
  23. using namespace chip::ArgParser;
  24. namespace {
  25. LinuxDeviceOptions gDeviceOptions;
  26. // Follow the code style of command line arguments in case we need to add more options in the future.
  27. enum
  28. {
  29. kDeviceOption_BleDevice = 0x1000,
  30. kDeviceOption_WiFi = 0x1001,
  31. kDeviceOption_Thread = 0x1002,
  32. };
  33. OptionDef sDeviceOptionDefs[] = { { "ble-device", kArgumentRequired, kDeviceOption_BleDevice },
  34. #if CHIP_DEVICE_CONFIG_ENABLE_WPA
  35. { "wifi", kNoArgument, kDeviceOption_WiFi },
  36. #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA
  37. #if CHIP_ENABLE_OPENTHREAD
  38. { "thread", kNoArgument, kDeviceOption_Thread },
  39. #endif // CHIP_ENABLE_OPENTHREAD
  40. {} };
  41. const char * sDeviceOptionHelp = " --ble-device <number>\n"
  42. " The device number for CHIPoBLE, without 'hci' prefix, can be found by hciconfig.\n"
  43. #if CHIP_DEVICE_CONFIG_ENABLE_WPA
  44. "\n"
  45. " --wifi\n"
  46. " Enable WiFi management via wpa_supplicant.\n"
  47. #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA
  48. #if CHIP_ENABLE_OPENTHREAD
  49. "\n"
  50. " --thread\n"
  51. " Enable Thread management via ot-agent.\n"
  52. #endif // CHIP_ENABLE_OPENTHREAD
  53. "\n";
  54. bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue)
  55. {
  56. bool retval = true;
  57. switch (aIdentifier)
  58. {
  59. case kDeviceOption_BleDevice:
  60. if (!ParseInt(aValue, LinuxDeviceOptions::GetInstance().mBleDevice))
  61. {
  62. PrintArgError("%s: invalid value specified for ble device number: %s\n", aProgram, aValue);
  63. retval = false;
  64. }
  65. break;
  66. case kDeviceOption_WiFi:
  67. LinuxDeviceOptions::GetInstance().mWiFi = true;
  68. break;
  69. case kDeviceOption_Thread:
  70. LinuxDeviceOptions::GetInstance().mThread = true;
  71. break;
  72. default:
  73. PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName);
  74. retval = false;
  75. break;
  76. }
  77. return (retval);
  78. }
  79. OptionSet sDeviceOptions = { HandleOption, sDeviceOptionDefs, "GENERAL OPTIONS", sDeviceOptionHelp };
  80. OptionSet * sLinuxDeviceOptionSets[] = { &sDeviceOptions, nullptr };
  81. } // namespace
  82. CHIP_ERROR ParseArguments(int argc, char * argv[])
  83. {
  84. if (!ParseArgs(argv[0], argc, argv, sLinuxDeviceOptionSets))
  85. {
  86. return CHIP_ERROR_INVALID_ARGUMENT;
  87. }
  88. return CHIP_NO_ERROR;
  89. }
  90. LinuxDeviceOptions & LinuxDeviceOptions::GetInstance()
  91. {
  92. return gDeviceOptions;
  93. }