Options.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 <app/server/OnboardingCodesUtil.h>
  20. #include <platform/CHIPDeviceLayer.h>
  21. #include <core/CHIPError.h>
  22. #include <support/CHIPArgParser.hpp>
  23. using namespace chip;
  24. using namespace chip::ArgParser;
  25. namespace {
  26. LinuxDeviceOptions gDeviceOptions;
  27. // Follow the code style of command line arguments in case we need to add more options in the future.
  28. enum
  29. {
  30. kDeviceOption_BleDevice = 0x1000,
  31. kDeviceOption_WiFi = 0x1001,
  32. kDeviceOption_Thread = 0x1002,
  33. kDeviceOption_Version = 0x1003,
  34. kDeviceOption_VendorID = 0x1004,
  35. kDeviceOption_ProductID = 0x1005,
  36. kDeviceOption_CustomFlow = 0x1006,
  37. kDeviceOption_Capabilities = 0x1007,
  38. kDeviceOption_Discriminator = 0x1008,
  39. kDeviceOption_Passcode = 0x1009,
  40. kDeviceOption_SecuredDevicePort = 0x100a,
  41. kDeviceOption_SecuredCommissionerPort = 0x100b,
  42. kDeviceOption_UnsecuredCommissionerPort = 0x100c
  43. };
  44. constexpr unsigned kAppUsageLength = 64;
  45. OptionDef sDeviceOptionDefs[] = { { "ble-device", kArgumentRequired, kDeviceOption_BleDevice },
  46. #if CHIP_DEVICE_CONFIG_ENABLE_WPA
  47. { "wifi", kNoArgument, kDeviceOption_WiFi },
  48. #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA
  49. #if CHIP_ENABLE_OPENTHREAD
  50. { "thread", kNoArgument, kDeviceOption_Thread },
  51. #endif // CHIP_ENABLE_OPENTHREAD
  52. { "version", kArgumentRequired, kDeviceOption_Version },
  53. { "vendor-id", kArgumentRequired, kDeviceOption_VendorID },
  54. { "product-id", kArgumentRequired, kDeviceOption_ProductID },
  55. { "custom-flow", kArgumentRequired, kDeviceOption_CustomFlow },
  56. { "capabilities", kArgumentRequired, kDeviceOption_Capabilities },
  57. { "discriminator", kArgumentRequired, kDeviceOption_Discriminator },
  58. { "passcode", kArgumentRequired, kDeviceOption_Passcode },
  59. { "secured-device-port", kArgumentRequired, kDeviceOption_SecuredDevicePort },
  60. { "secured-commissioner-port", kArgumentRequired, kDeviceOption_SecuredCommissionerPort },
  61. { "unsecured-commissioner-port", kArgumentRequired, kDeviceOption_UnsecuredCommissionerPort },
  62. {} };
  63. const char * sDeviceOptionHelp =
  64. " --ble-device <number>\n"
  65. " The device number for CHIPoBLE, without 'hci' prefix, can be found by hciconfig.\n"
  66. #if CHIP_DEVICE_CONFIG_ENABLE_WPA
  67. "\n"
  68. " --wifi\n"
  69. " Enable WiFi management via wpa_supplicant.\n"
  70. #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA
  71. #if CHIP_ENABLE_OPENTHREAD
  72. "\n"
  73. " --thread\n"
  74. " Enable Thread management via ot-agent.\n"
  75. #endif // CHIP_ENABLE_OPENTHREAD
  76. "\n"
  77. " --version <version>\n"
  78. " The version indication provides versioning of the setup payload.\n"
  79. "\n"
  80. " --vendor-id <id>\n"
  81. " The Vendor ID is assigned by the Connectivity Standards Alliance.\n"
  82. "\n"
  83. " --product-id <id>\n"
  84. " The Product ID is specified by vendor.\n"
  85. "\n"
  86. " --custom-flow <Standard = 0 | UserActionRequired = 1 | Custom = 2>\n"
  87. " A 2-bit unsigned enumeration specifying manufacturer-specific custom flow options.\n"
  88. "\n"
  89. " --capabilities <None = 0, SoftAP = 1 << 0, BLE = 1 << 1, OnNetwork = 1 << 2>\n"
  90. " Discovery Capabilities Bitmask which contains information about Device’s available technologies for device discovery.\n"
  91. "\n"
  92. " --discriminator <discriminator>\n"
  93. " A 12-bit unsigned integer match the value which a device advertises during commissioning.\n"
  94. "\n"
  95. " --passcode <passcode>\n"
  96. " A 27-bit unsigned integer, which serves as proof of possession during commissioning.\n"
  97. "\n"
  98. " --secured-device-port <port>\n"
  99. " A 16-bit unsigned integer specifying the listen port to use for secure device messages (default is 5540).\n"
  100. "\n"
  101. " --secured-commissioner-port <port>\n"
  102. " A 16-bit unsigned integer specifying the listen port to use for secure commissioner messages (default is 5542). Only "
  103. "valid when app is both device and commissioner\n"
  104. "\n"
  105. " --unsecured-commissioner-port <port>\n"
  106. " A 16-bit unsigned integer specifying the port to use for unsecured commissioner messages (default is 5550).\n"
  107. "\n";
  108. bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue)
  109. {
  110. bool retval = true;
  111. switch (aIdentifier)
  112. {
  113. case kDeviceOption_BleDevice:
  114. if (!ParseInt(aValue, LinuxDeviceOptions::GetInstance().mBleDevice))
  115. {
  116. PrintArgError("%s: invalid value specified for ble device number: %s\n", aProgram, aValue);
  117. retval = false;
  118. }
  119. break;
  120. case kDeviceOption_WiFi:
  121. LinuxDeviceOptions::GetInstance().mWiFi = true;
  122. break;
  123. case kDeviceOption_Thread:
  124. LinuxDeviceOptions::GetInstance().mThread = true;
  125. break;
  126. case kDeviceOption_Version:
  127. LinuxDeviceOptions::GetInstance().payload.version = static_cast<uint8_t>(atoi(aValue));
  128. break;
  129. case kDeviceOption_VendorID:
  130. LinuxDeviceOptions::GetInstance().payload.vendorID = static_cast<uint16_t>(atoi(aValue));
  131. break;
  132. case kDeviceOption_ProductID:
  133. LinuxDeviceOptions::GetInstance().payload.productID = static_cast<uint16_t>(atoi(aValue));
  134. break;
  135. case kDeviceOption_CustomFlow:
  136. LinuxDeviceOptions::GetInstance().payload.commissioningFlow = static_cast<CommissioningFlow>(atoi(aValue));
  137. break;
  138. case kDeviceOption_Capabilities:
  139. LinuxDeviceOptions::GetInstance().payload.rendezvousInformation.SetRaw(static_cast<uint8_t>(atoi(aValue)));
  140. break;
  141. case kDeviceOption_Discriminator:
  142. LinuxDeviceOptions::GetInstance().payload.discriminator = static_cast<uint16_t>(atoi(aValue));
  143. break;
  144. case kDeviceOption_Passcode:
  145. LinuxDeviceOptions::GetInstance().payload.setUpPINCode = static_cast<uint32_t>(atoi(aValue));
  146. break;
  147. case kDeviceOption_SecuredDevicePort:
  148. LinuxDeviceOptions::GetInstance().securedDevicePort = static_cast<uint16_t>(atoi(aValue));
  149. break;
  150. case kDeviceOption_SecuredCommissionerPort:
  151. LinuxDeviceOptions::GetInstance().securedCommissionerPort = static_cast<uint16_t>(atoi(aValue));
  152. break;
  153. case kDeviceOption_UnsecuredCommissionerPort:
  154. LinuxDeviceOptions::GetInstance().unsecuredCommissionerPort = static_cast<uint16_t>(atoi(aValue));
  155. break;
  156. default:
  157. PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName);
  158. retval = false;
  159. break;
  160. }
  161. return (retval);
  162. }
  163. OptionSet sDeviceOptions = { HandleOption, sDeviceOptionDefs, "GENERAL OPTIONS", sDeviceOptionHelp };
  164. OptionSet * sLinuxDeviceOptionSets[] = { &sDeviceOptions, nullptr, nullptr };
  165. } // namespace
  166. CHIP_ERROR ParseArguments(int argc, char * argv[])
  167. {
  168. char usage[kAppUsageLength];
  169. snprintf(usage, kAppUsageLength, "Usage: %s [options]", argv[0]);
  170. HelpOptions helpOptions(argv[0], usage, "1.0");
  171. sLinuxDeviceOptionSets[1] = &helpOptions;
  172. if (!ParseArgs(argv[0], argc, argv, sLinuxDeviceOptionSets))
  173. {
  174. return CHIP_ERROR_INVALID_ARGUMENT;
  175. }
  176. return CHIP_NO_ERROR;
  177. }
  178. LinuxDeviceOptions & LinuxDeviceOptions::GetInstance()
  179. {
  180. return gDeviceOptions;
  181. }