ASX Version4.20.14

adapter/main.c

This is an example of how to use the ASX Adapter functions.

/*
 * \file main.c
 *
 * File to get adapter information from the ASX library.
 */
#include "stdio.h"
#include "stdlib.h"
#include "asx.h"

#ifdef _WIN32
#include "windows.h"
#include "Iphlpapi.h"
#pragma comment(lib, "IPHLPAPI.lib")
#endif

#define TEST_SUBSYSTEM_UDP    (1 << 1)
#define TEST_SUBSYSTEM_AVDECC (1 << 2)

#define TEST_SUBSYSTEMS (TEST_SUBSYSTEM_UDP | TEST_SUBSYSTEM_AVDECC)

int CheckError(ASX_HANDLE hObj, int nLine);

ASX_HANDLE hSystem=0;

void asx_avdecc_errror_callback(ASX_HANDLE hASX_Object, const char *pszCallingFunction, void *pUser1, void *pUser2)
{
    /* can print AVDECC errors here if required */
}

#ifdef _WIN32
void get_network_interface_selection(char *szName, size_t lenName,
    char *szGUIDname, size_t lenGUID,
    char *IPaddr, size_t lenIP)
{
    PIP_ADAPTER_INFO pIPAI = NULL;
    ULONG dwBufferSize = 0;
    int adapter_count = 0;
    int selection = 0;

    /* list the adapters */
    if (ERROR_BUFFER_OVERFLOW == GetAdaptersInfo(pIPAI, &dwBufferSize)){
        pIPAI = (PIP_ADAPTER_INFO)malloc(dwBufferSize);
        GetAdaptersInfo(pIPAI, &dwBufferSize);

        PIP_ADAPTER_INFO pInfo = pIPAI;
        while (pInfo) {
            if ( strcmp(pInfo->IpAddressList.IpAddress.String, "0.0.0.0") != 0) {
                printf("[%d] %s (IP %s)\n", adapter_count, pInfo->Description, pInfo->IpAddressList.IpAddress.String);
                adapter_count++;
            }
            pInfo = pInfo->Next;
        }
        free(pIPAI);
    }

    /* select an adapter */
    selection = 0;

    /* get information from the selected adapter */
    pIPAI = NULL;
    dwBufferSize = 0;
    adapter_count = 0;
    /* set strings to empty */
    *szName = 0;
    *szGUIDname = 0;
    *IPaddr = 0;
    if (ERROR_BUFFER_OVERFLOW == GetAdaptersInfo(pIPAI, &dwBufferSize)){
        pIPAI = (PIP_ADAPTER_INFO)malloc(dwBufferSize);
        GetAdaptersInfo(pIPAI, &dwBufferSize);

        PIP_ADAPTER_INFO pInfo = pIPAI;
        while (pInfo) {
            if (strcmp(pInfo->IpAddressList.IpAddress.String, "0.0.0.0") != 0) {
                if (adapter_count == selection) {
                    strncpy(szName, pInfo->Description, lenName);
                    strncpy(szGUIDname, pInfo->AdapterName, lenGUID);
                    strncpy(IPaddr, pInfo->IpAddressList.IpAddress.String, lenIP);
                    break;
                }
                adapter_count++;
            }
            pInfo = pInfo->Next;
        }
        free(pIPAI);
    }
}
#endif

int main(int argc, char* argv[])
{
    unsigned int nSubSystemMask = 0, nSubSystemOkMask = 0;
    ASX_ERROR asxError;
    char szNetworkInterfaceName[256];
    char szNetworkInterfaceGUID[256];
    char szNetworkInterfaceIP[64];
    int nAdapters = 0;
    int i;


#if TEST_SUBSYSTEMS & TEST_SUBSYSTEM_AVDECC
#ifdef _WIN32
    get_network_interface_selection(
        szNetworkInterfaceName, sizeof(szNetworkInterfaceName),
        szNetworkInterfaceGUID, sizeof(szNetworkInterfaceName),
        szNetworkInterfaceIP, sizeof(szNetworkInterfaceIP));
    printf("Using network interface: %s (IP %s)\n", szNetworkInterfaceName, szNetworkInterfaceIP);
#else
    /* future linux code goes here */
#endif
#endif

#if TEST_SUBSYSTEMS & (TEST_SUBSYSTEM_AVDECC | TEST_SUBSYSTEM_UDP)
    ASX_System_SetHostNetworkInterface(szNetworkInterfaceIP);
#endif

    // create the subsystem(s)
#if TEST_SUBSYSTEMS & TEST_SUBSYSTEM_UDP
    ASX_System_CreateSubSystem(ASX_SYSTEM_TYPE_HPIUDP, &hSystem);
#else
    ASX_System_CreateSubSystem(ASX_SYSTEM_TYPE_HPI, &hSystem);
#endif
    CheckError(hSystem, __LINE__);

#if TEST_SUBSYSTEMS & TEST_SUBSYSTEM_AVDECC
    ASX_System_RegisterErrorCallback(hSystem, asx_avdecc_errror_callback, NULL, NULL);
    ASX_System_SetAvdeccInterface(szNetworkInterfaceGUID);
    ASX_System_CreateSubSystem(ASX_SYSTEM_TYPE_AVB_1722_1, &hSystem);
#endif

    // allow background network discovery to complete
    Sleep(1000);

    // find out how many adapters there are
    asxError = ASX_System_GetAdapterCount(hSystem,&nAdapters);
    if (asxError) {
        CheckError(hSystem, __LINE__);
    }
    printf("There are %d audio devices that have been discovered.\n", nAdapters);

    // loop over the adapters
    for(i=0;i<nAdapters;i++)
    {
        char *pszAdapterName;
        ASX_HANDLE hAdapter;
        unsigned long lSerial;
        char *pszRevision;
        int nLen;
        int nIndex;
        int nDspUtilization;
        ASX_HANDLE hMixer;


        ASX_System_GetAdapter(hSystem,i,&hAdapter);
        CheckError(hSystem, __LINE__);

        ASX_Adapter_GetName(hAdapter,0,0,&nLen);
        CheckError(hAdapter, __LINE__);
        pszAdapterName = (char *)malloc(nLen);
        ASX_Adapter_GetName(hAdapter,pszAdapterName,nLen,&nLen);
        CheckError(hAdapter, __LINE__);
        printf("Adapter [%d] is %s \n", i,pszAdapterName);

        /* the adapter index is not the same as the loop index */
        ASX_Adapter_GetIndex(hAdapter, &nIndex);
        CheckError(hAdapter, __LINE__);
        printf("Index is %ld \n", nIndex);
        
        ASX_Adapter_GetSerialNumber(hAdapter,&lSerial);
        CheckError(hAdapter, __LINE__);
        printf("Serial is %ld \n", lSerial);

        pszRevision = (char *)malloc(ASX_SHORT_STRING);
        ASX_Adapter_GetHardwareRevision(hAdapter,pszRevision);
        CheckError(hAdapter, __LINE__);
        printf("Revision is %s \n", pszRevision);

        ASX_Adapter_GetDspUtilization(hAdapter,1,&nDspUtilization);
        CheckError(hAdapter, __LINE__);
        printf("Utilization is %d percent \n", nDspUtilization);

        free(pszAdapterName);
        free(pszRevision);
    }

    ASX_System_Delete(hSystem);
    printf("Press ENTER to exit\n");
    getchar();
    return 0;
}

int CheckError(ASX_HANDLE hObj, int nLine)
{
    ASX_ERROR nError;
    int asxSubSystemErrorCode=0;
    char *pszAsxErrorString;
    char *pszAsxSubSystemErrorString;
    int nLen1,nLen2;

    ASX_Error_GetLast( hObj, &nError, &asxSubSystemErrorCode);
    if(!nError)
        return 0;
    ASX_Error_GetLastString( hObj, 0,0,&nLen1,0,0,&nLen2);
    pszAsxErrorString = (char *)malloc(nLen1);
    pszAsxSubSystemErrorString = (char *)malloc(nLen2);
    ASX_Error_GetLastString( hObj, pszAsxErrorString,nLen1,&nLen1,pszAsxSubSystemErrorString,nLen2,&nLen2);
    printf("Error: #%d, %s - Subsystem Error: #%ld, %s \n", 
        nError,  
        pszAsxErrorString, 
        asxSubSystemErrorCode, 
        pszAsxSubSystemErrorString );
    printf("When called from source %s line %d\n",__FILE__,nLine);

    printf("Press ENTER to exit\n");
    getchar();
    free(pszAsxErrorString);
    free(pszAsxSubSystemErrorString);
    ASX_System_Delete(hSystem);
    exit(1);
    return 1;
}