ListDevices.cpp

/**
 * ListDevices.cpp
 *
 * This example prints all the available devices to the screen.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

#include "PrintInfo.hpp"
#include <iostream>

#if defined(__linux) || defined(__unix)
#  include <cstdlib>
#endif

int main()
{
  int status = EXIT_SUCCESS;

  // Initialize library:
  TiePie::Hardware::Library::init();

  // Print library information:
  printLibraryInfo();

  // Enable network search:
  TiePie::Hardware::Network::setAutoDetectEnabled(true);

  // Update device list:
  TiePie::Hardware::DeviceList::update();

  // Get the number of connected devices:
  const uint32_t connectedDevices = TiePie::Hardware::DeviceList::count();

  if(connectedDevices != 0)
  {
    std::cout << "\nAvailable devices:\n";

    for(uint32_t index = 0; index < connectedDevices; index++)
    {
      try
      {
        const auto item = TiePie::Hardware::DeviceList::getItemByIndex(index);

        std::cout << "  Name: " << item->name() << '\n';
        std::cout << "    Serial Number  : " << item->serialNumber() << '\n';
        std::cout << "    Available types: " << deviceTypeToStr(item->types()) << '\n';

        if(item->hasServer())
        {
          const auto server = item->server();
          std::cout << "    Server         : " << server->url() << " (" << server->name() << ")\n";
        }
      }
      catch(const std::exception& e)
      {
        std::cerr << "Exception: " << e.what() << '\n';
        status = EXIT_FAILURE;
      }
    }
  }
  else
    std::cerr << "No devices found!\n";

  // Finalize library:
  TiePie::Hardware::Library::fini();

  return status;
}