main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "../common/common.hpp"
  2. #include <fstream>
  3. int main(int argc, char* argv[])
  4. {
  5. bool save = false;
  6. for(int i = 1; i < argc; i++) {
  7. if(strcmp(argv[i], "-s") == 0){
  8. save = true;
  9. }
  10. }
  11. // Init lib
  12. ASSERT_OK(TYInitLib());
  13. TY_VERSION_INFO ver;
  14. ASSERT_OK(TYLibVersion(&ver));
  15. LOGD("=== lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  16. LOGD("Update interface list");
  17. ASSERT_OK(TYUpdateInterfaceList());
  18. uint32_t n = 0;
  19. ASSERT_OK(TYGetInterfaceNumber(&n));
  20. LOGD("Got %u interfaces", n);
  21. if (n == 0) {
  22. LOGE("interface number incorrect");
  23. return TY_STATUS_ERROR;
  24. }
  25. std::string device_list = "device_list";
  26. std::ofstream* output;
  27. if (save){
  28. output = new std::ofstream(device_list.data());
  29. }
  30. std::vector<TY_INTERFACE_INFO> ifaces(n);
  31. ASSERT_OK(TYGetInterfaceList(&ifaces[0], n, &n));
  32. ASSERT(n == ifaces.size());
  33. std::vector<TY_INTERFACE_HANDLE> hIfaces;
  34. for (uint32_t i = 0; i < n; i++) {
  35. TY_INTERFACE_HANDLE hIface;
  36. ASSERT_OK(TYOpenInterface(ifaces[i].id, &hIface));
  37. hIfaces.push_back(hIface);
  38. }
  39. updateDevicesParallel(hIfaces);
  40. for (uint32_t i = 0; i < n; i++) {
  41. if (save) {
  42. *output << "Interface: " << i <<std::endl;
  43. }
  44. LOGI("Found interface %u:", i);
  45. LOGI(" name: %s", ifaces[i].name);
  46. LOGI(" id: %s", ifaces[i].id);
  47. LOGI(" type: 0x%x", ifaces[i].type);
  48. if (TYIsNetworkInterface(ifaces[i].type)) {
  49. LOGI(" MAC: %s", ifaces[i].netInfo.mac);
  50. LOGI(" ip: %s", ifaces[i].netInfo.ip);
  51. LOGI(" netmask: %s", ifaces[i].netInfo.netmask);
  52. LOGI(" gateway: %s", ifaces[i].netInfo.gateway);
  53. LOGI(" broadcast: %s", ifaces[i].netInfo.broadcast);
  54. }
  55. TY_INTERFACE_HANDLE hIface = hIfaces[i];
  56. uint32_t n = 0;
  57. TYGetDeviceNumber(hIface, &n);
  58. if (n == 0) continue;
  59. std::vector<TY_DEVICE_BASE_INFO> devs(n);
  60. TYGetDeviceList(hIface, &devs[0], n, &n);
  61. ASSERT(n == devs.size());
  62. for (uint32_t j = 0; j < n; j++) {
  63. if (TYIsNetworkInterface(devs[j].iface.type)) {
  64. LOGD(" - device %s:", devs[j].id);
  65. if (strlen(devs[j].userDefinedName) != 0) {
  66. LOGD(" vendor : %s", devs[j].userDefinedName);
  67. } else {
  68. LOGD(" vendor : %s", devs[j].vendorName);
  69. }
  70. LOGD(" model : %s", devs[j].modelName);
  71. LOGD(" device MAC : %s", devs[j].netInfo.mac);
  72. LOGD(" device IP : %s", devs[j].netInfo.ip);
  73. if (save) {
  74. *output << devs[j].id <<" ";
  75. *output << devs[j].netInfo.ip <<" "<<std::endl;
  76. }
  77. } else {
  78. TY_DEV_HANDLE handle;
  79. int32_t ret = TYOpenDevice(hIface, devs[j].id, &handle);
  80. if (ret == 0) {
  81. TYGetDeviceInfo(handle, &devs[j]);
  82. TYCloseDevice(handle);
  83. LOGD(" - device %s:", devs[j].id);
  84. } else {
  85. LOGD(" - device %s(open failed, error: %d)", devs[j].id, ret);
  86. }
  87. if (strlen(devs[j].userDefinedName) != 0) {
  88. LOGD(" vendor : %s", devs[j].userDefinedName);
  89. } else {
  90. LOGD(" vendor : %s", devs[j].vendorName);
  91. }
  92. LOGD(" model : %s", devs[j].modelName);
  93. if (save) {
  94. *output << devs[j].id <<" "<<std::endl;
  95. }
  96. }
  97. }
  98. TYCloseInterface(hIface);
  99. if (save) {
  100. *output << " " <<std::endl;
  101. }
  102. }
  103. ASSERT_OK(TYDeinitLib());
  104. LOGD("Done!");
  105. return 0;
  106. }