CommandLineFeatureHelper.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef CMDLINE_FEATURE_HELPER__H_
  2. #define CMDLINE_FEATURE_HELPER__H_
  3. #include "CommandLineParser.hpp"
  4. #include "TYApi.h"
  5. #include "Utils.hpp"
  6. /// @brief command line sgb feature param id
  7. struct ty_fetaure_options
  8. {
  9. int component_id;
  10. int feature_id;
  11. ty_fetaure_options(int comp_id = 0, int _f_id = 0)
  12. {
  13. component_id = comp_id;
  14. feature_id = _f_id;
  15. }
  16. };
  17. /// @brief command line feature helper for set device feature by command line args
  18. class CommandLineFeatureHelper
  19. {
  20. public:
  21. TyCommandlineParser<ty_fetaure_options> cmd_parser; ///< command line parser
  22. /// @brief add feature param to command line parser
  23. /// @param param command line param name
  24. /// @param comp_id component id
  25. /// @param feat_id feature id
  26. /// @param val default value
  27. /// @param desc describe
  28. /// @param is_flag is a flag only , no value
  29. void add_feature(const std::string &param, int comp_id, int feat_id, int val, const std::string &desc, bool is_flag = false)
  30. {
  31. cmd_parser.addItem(param, desc, is_flag, std::to_string(val), ty_fetaure_options(comp_id, feat_id));
  32. }
  33. /// @brief add feature param to command line parser
  34. /// @param param command line param name
  35. /// @param comp_id component id
  36. /// @param feat_id feature id
  37. /// @param val default value
  38. /// @param desc describe
  39. /// @param is_flag is a flag only , no value
  40. void add_feature(const std::string &param, int comp_id, int feat_id, std::string val, const std::string &desc, bool is_flag = false)
  41. {
  42. cmd_parser.addItem(param, desc, is_flag, val, ty_fetaure_options(comp_id, feat_id));
  43. }
  44. /// @brief add feature param to command line parser
  45. /// @param name command line param name
  46. /// @return command line item
  47. const TyCommandlineItem<ty_fetaure_options> *get_feature(const std::string &name) const
  48. {
  49. auto res = cmd_parser.get(name);
  50. return res;
  51. }
  52. /// @brief get command line param describe
  53. /// @return describe string
  54. std::string usage_describe() const
  55. {
  56. return cmd_parser.getUsage();
  57. }
  58. /// @brief parse command line args
  59. void parse_argv(int argc, char *argv[])
  60. {
  61. cmd_parser.parse(argc, argv);
  62. }
  63. /// @brief set command line param to device
  64. /// @param hDevice device handle
  65. void set_device_feature(TY_DEV_HANDLE hDevice)
  66. {
  67. // loop for all command line argv items and set to device
  68. for (auto &kv : cmd_parser.cmd_items)
  69. {
  70. auto &p = kv.second;
  71. int res = TY_STATUS_OK;
  72. if (!p.has_set)
  73. {
  74. continue;
  75. }
  76. int feature_id = p.ctx.feature_id;
  77. int comp_id = p.ctx.component_id;
  78. if (comp_id == 0 && feature_id == 0)
  79. {
  80. // param is not a feature setting
  81. continue;
  82. }
  83. // set feature by type
  84. int type = feature_id & 0xf000;
  85. if (type == TY_FEATURE_INT)
  86. {
  87. int val = p.get_int_val();
  88. LOGD("set feature %s (compId 0x%x featId 0x%x) to %d", p.name.c_str(), comp_id, feature_id, val);
  89. res = TYSetInt(hDevice, comp_id, feature_id, val);
  90. }
  91. else if (type == TY_FEATURE_BOOL)
  92. {
  93. bool val = p.get_bool_val();
  94. LOGD("set feature %s (compId 0x%x featId 0x%x) to %d", p.name.c_str(), comp_id, feature_id, val);
  95. res = TYSetBool(hDevice, comp_id, feature_id, val);
  96. }
  97. else if (type == TY_FEATURE_FLOAT)
  98. {
  99. float val = p.get_float_val();
  100. LOGD("set feature %s (compId 0x%x featId 0x%x) to %f", p.name.c_str(), comp_id, feature_id, val);
  101. res = TYSetFloat(hDevice, comp_id, feature_id, val);
  102. }
  103. else
  104. {
  105. LOGE("unknow feature type %d for %s", type, p.name.c_str());
  106. continue;
  107. }
  108. if (res != TY_STATUS_OK)
  109. {
  110. LOGE("set feature %s (%s) FAILED with return status code %d", p.name.c_str(), p.describe.c_str(), res);
  111. }
  112. }
  113. }
  114. };
  115. #endif // CMDLINE_FEATURE_HELPER__H_