Device list example

View on GitHub

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

#include 
#include "libtiepie++.h"
#include "PrintInfo.h"

#if defined(__linux) || defined(__unix)
  #include 
#endif

using namespace std;
using namespace LibTiePie;

int main()
{
  int status = EXIT_SUCCESS;

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

  // Print library information:
  printLibraryInfo();

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

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

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

  if(connectedDevices != 0)
  {
    cout << endl;
    cout << "Available devices:" << endl;

    for(uint32_t index = 0; index < connectedDevices; index++)
    {
      DeviceListItem* item = 0;

      try
      {
        item = DeviceList::getItemByIndex(index);

        cout << "  Name: " << item->name() << endl;
        cout << "    Serial Number  : " << item->serialNumber() << endl;
        cout << "    Available types: " << deviceTypeToStr(item->types()) << endl;

        if(item->hasServer())
        {
          Server* server = item->server();
          cout << "    Server         : " << server->url() << " (" << server->name() << ")" << endl;
          delete server;
        }
      }
      catch(const exception& e)
      {
        cerr << "Exception: " << e.what() << endl;
        status = EXIT_FAILURE;
      }

      delete item;
    }
  }
  else
  {
    cerr << "No devices found!" << endl;
  }

  // Exit library:
  Library::exit();

  return status;
}