AppOptions.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "AppOptions.h"
  19. using chip::ArgParser::OptionDef;
  20. using chip::ArgParser::OptionSet;
  21. using chip::ArgParser::PrintArgError;
  22. constexpr uint16_t kOptionDacProviderFilePath = 0xFF01;
  23. constexpr uint16_t kOptionInteractiveMode = 0xFF02;
  24. constexpr uint16_t kOptionInteractiveModePort = 0xFF03;
  25. static chip::Credentials::Examples::TestHarnessDACProvider mDacProvider;
  26. static bool gInteractiveMode = false;
  27. static chip::Optional<uint16_t> gInteractiveModePort;
  28. bool AppOptions::HandleOptions(const char * program, OptionSet * options, int identifier, const char * name, const char * value)
  29. {
  30. bool retval = true;
  31. switch (identifier)
  32. {
  33. case kOptionDacProviderFilePath:
  34. mDacProvider.Init(value);
  35. break;
  36. case kOptionInteractiveMode:
  37. gInteractiveMode = true;
  38. break;
  39. case kOptionInteractiveModePort:
  40. gInteractiveModePort = chip::MakeOptional(static_cast<uint16_t>(atoi(value)));
  41. break;
  42. default:
  43. PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", program, name);
  44. retval = false;
  45. break;
  46. }
  47. return retval;
  48. }
  49. OptionSet * AppOptions::GetOptions()
  50. {
  51. static OptionDef optionsDef[] = {
  52. { "dac_provider", chip::ArgParser::kArgumentRequired, kOptionDacProviderFilePath },
  53. { "interactive", chip::ArgParser::kNoArgument, kOptionInteractiveMode },
  54. { "port", chip::ArgParser::kArgumentRequired, kOptionInteractiveModePort },
  55. {},
  56. };
  57. static OptionSet options = {
  58. AppOptions::HandleOptions, optionsDef, "PROGRAM OPTIONS",
  59. " --dac_provider <filepath>\n"
  60. " A json file with data used by the example dac provider to validate device attestation procedure.\n"
  61. " --interactive\n"
  62. " Enable server interactive mode.\n"
  63. " --port <port>\n"
  64. " Specify the listening port for the server interactive mode.\n"
  65. };
  66. return &options;
  67. }
  68. chip::Credentials::DeviceAttestationCredentialsProvider * AppOptions::GetDACProvider()
  69. {
  70. return &mDacProvider;
  71. }
  72. bool AppOptions::GetInteractiveMode()
  73. {
  74. return gInteractiveMode;
  75. }
  76. chip::Optional<uint16_t> AppOptions::GetInteractiveModePort()
  77. {
  78. return gInteractiveModePort;
  79. }