Enumeration#

#include <stdio.h>
#include <stdlib.h>
#include "ItalaApiC/ItalaC.h"

#define DEVICES_COUNT 5
#define STRING_LENGTH 200

ItalaError ErrorManager(ItalaError e) {
  size_t sizeMessage = 255;
  char* message = (char*)malloc(sizeof(char) * sizeMessage);
  ERR_GetLastErrorMessage(message, &sizeMessage);
  printf("\nITALA ERROR (%d):\t%s, %zu char", ERR_GetLastErrorCode(), message, sizeMessage);
  free(message);
  return e;
}

int main(int argc, char** argv) {
  printf("***** Enumeration example started. *****\n");
  ItalaError error = ItalaErrorSuccess;

  // Initialize the system instance to enables the usage of the entire API.
  // It is the root of Itala and only one system object can exist at a time. If
  // two systems are created simultaneously, an error occurs. When the system
  // instance is no longer needed (e.g. on application exit) it MUST be released
  // by calling its dedicated function Dispose().
  error = SYS_Initialize();
  if (error) return ErrorManager(error);

  // Look for available devices across all network interfaces on the host with a limit of 700ms to
  // show up.
  error = SYS_EnumerateDevices(700);
  if (error) return ErrorManager(error);

  // Retrive the amount of devices found with previous enumeration
  size_t numberOfDevices = 0;
  error = SYS_GetDeviceCount(&numberOfDevices);
  if (error) ErrorManager(error);

  if (numberOfDevices == 0) {
    printf("No devices found. Example canceled.\n");
    return ItalaErrorError;
  }

  // Let's print some data..
  printf("%zu devices discovered.\n", numberOfDevices);

  // For every device found some information is printed. Note that some devices
  // may be in a misconfigured state (e.g. a different subnet) thus impossible
  // to be reached or used, as eventually reported by DeviceInfo.
  for (size_t i = 0; i < numberOfDevices; i++) {
    // Each info object uniquely identifies a detected device and provides a set
    // of useful information about it. DeviceInfo objects are nothing more than
    // information containers with no effects on the device state.
    DeviceInfo currentDevice;
    error = SYS_GetDeviceByIndex(i, &currentDevice);
    if (error) return ErrorManager(error);
    printf("\n");
    printf("\tDevice: %zu is %s\n", i, currentDevice.SerialNumber);
    printf("\ttDisplay name: %s\n", currentDevice.DisplayName);

    char macAddressDevString[STRING_LENGTH];
    size_t macAddressDevStringSize = STRING_LENGTH;
    error = STR_MacAddressToString(currentDevice.MacAddress, macAddressDevString,
                                   &macAddressDevStringSize);
    if (error) return ErrorManager(error);
    printf("\tMAC Address: %s\n", macAddressDevString);

    char ipAddressDevString[STRING_LENGTH];
    size_t ipAddressDevStringSize = STRING_LENGTH;
    error =
        STR_AddressToString(currentDevice.IpAddress, ipAddressDevString, &ipAddressDevStringSize);
    if (error) return ErrorManager(error);
    printf("\tIP Address: %s\n", ipAddressDevString);

    printf("\tModel: %s\n", currentDevice.Model);
    printf("\tAccess status: %d\n", currentDevice.AccessStatus);
    printf("\n");
  }

  DeviceInfo deviceToOpen;
  error = SYS_GetDeviceByIndex(0, &deviceToOpen);
  if (error) return ErrorManager(error);
  H_DEVICE hDevice = NULL;
  error = SYS_CreateDevice(deviceToOpen, &hDevice);
  if (error) return ErrorManager(error);
  printf("%s device instance created.\n", deviceToOpen.SerialNumber);

  // Do something with the devices...

  // Never forget to dispose the used device when done! It's strongly suggested
  // to null the released pointer to avoid violations. The DEV_Dispose() method
  // and the "delete" operator (which is not available here) are the exact same
  // thing hence they must be used the same way.
  error = DEV_Dispose(hDevice);
  if (error) return ErrorManager(error);
  hDevice = NULL;

  // Never forget to dispose the system when done! It's strongly suggested
  // to null the released pointer to avoid violations. The SYS_Dispose() method
  // and the "delete" operator (which is not available here) are the exact same
  // thing hence they must be used the same way.
  error = SYS_Dispose();
  if (error) return ErrorManager(error);
  printf("System instance disposed.\n");
  return 0;
}