ASX Version4.10.32

cobranet/main.c

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

/* $Header: /home/eliot/asi/repo/cvsrepo/Repository/apps/asx/examples/cobranet/main.c,v 1.6 2010/01/11 21:50:06 as-age Exp $ */
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"
#include "asx.h"
#include "asxstring.h"

int CheckError(ASX_HANDLE hObj, int nLine);
int CheckErrorNonTerminal(ASX_HANDLE hObj, int nLine);
void PrintNodeName(ASX_HANDLE hNode);
void PrintControlName(ASX_HANDLE hControl);
void PrintMeterReadings(ASX_HANDLE hMixer, ASX_HANDLE hControl);
ASX_HANDLE FindSnmpCobranetControl(ASX_HANDLE hMixer);

ASX_HANDLE hSystem;

int main(int argc, char* argv[])
{

    char *pszName;
    ASX_HANDLE hAdapter;
    ASX_HANDLE hMixer;
    ASX_HANDLE hNode;
    ASX_HANDLE hControl,hCobraNetControl,hMeterControl;
    ASX_ERROR asxError;
    int nAdapterToUse=0;
    int i,j,nLen,nNodes,nControls,nAdapters;
    unsigned int nBundle;
    unsigned int nMap[8];
    unsigned int nCount;

    // make sure ASX system handle is NULL
    hSystem=NULL;

    // set the address of the host PC network adapter
    ASX_System_SetHostNetworkInterface("192.168.1.106");

    // create the system
    ASX_System_CreateSubSystem(ASX_SYSTEM_TYPE_HPIUDP,&hSystem);
    CheckError(hSystem, __LINE__);

    // add SNMP so that we can control CobraNet devices
    ASX_System_CreateSubSystem(ASX_SYSTEM_TYPE_SNMP,&hSystem);
    CheckError(hSystem, __LINE__);

    // wait 4 seconds for CobraNet device discovery to complete
    printf("Waiting 2 seconds for device discovery to complete.\n");
    Sleep(2000);

    // list all the adapters
    ASX_System_GetAdapterCount(hSystem,&nAdapters);
    CheckError(hSystem, __LINE__);
    printf("Found %d adapters.\n",nAdapters);

    // dump adapter information
    for(i=0;i<nAdapters;i++) {
        char szIP[ASX_SHORT_STRING];
        char szInfo[ASX_LONG_STRING];
        int nIndex=0;

        // get the adapter
        asxError = ASX_System_GetAdapter(hSystem,i,&hAdapter);
        CheckError(hSystem, __LINE__);

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

        /* 
        The adapter index is never the same as the loop index.
        Network adapters have a fixed unique index that is independent of there
        IP address or order of discovery.
        */
        ASX_Adapter_GetIndex(hAdapter, &nIndex);
        CheckError(hAdapter, __LINE__);
        printf("\t Index is : %d \n", nIndex);

        ASX_Adapter_GetIpAddress(hAdapter, szIP);
        CheckError(hAdapter, __LINE__);
            printf("\t IP address is : %s \n", szIP);

        // get the mixer handle
        asxError = ASX_Adapter_GetMixer( hAdapter, &hMixer );
        CheckError(hAdapter, __LINE__);

        // get the base control that has some CobraNet stuff
        hCobraNetControl = FindSnmpCobranetControl(hMixer);
        if(hCobraNetControl) {
            asxError = ASX_Cobranet_GetDescription(hCobraNetControl,szInfo,sizeof(szInfo));
            CheckError(hCobraNetControl, __LINE__);
            if( !asxError )
                printf("\tsysDescription: %s\n",szInfo);
            else
                printf("Error %d\n",asxError);

            asxError = ASX_Cobranet_GetName(hCobraNetControl,szInfo,sizeof(szInfo));
            CheckError(hCobraNetControl, __LINE__);
            if( !asxError )
                printf("\tsysName: %s\n",szInfo);

            asxError = ASX_Cobranet_GetLocation(hCobraNetControl,szInfo,sizeof(szInfo));
            CheckError(hCobraNetControl, __LINE__);
            if( !asxError )
                printf("\tsysLocation: %s\n",szInfo);
        } else {
            printf("No CobraNet control on adapter node\n");
        }


    }

    printf("Enter adapter number to use : ");
    scanf("%d",&i);

    // get the selected adapter
    asxError = ASX_System_GetAdapter(hSystem,i,&hAdapter);
    CheckError(hSystem, __LINE__);

    // get the mixer handle
    asxError = ASX_Adapter_GetMixer( hAdapter, &hMixer );
    CheckError(hAdapter, __LINE__);

    // get the base control that has some CobraNet stuff
    asxError = ASX_Mixer_GetControlByNodeTypeAndIndex(
        hMixer,
        asxNODE_COBRANET_IN,0,
        asxNODE_COBRANET_OUT,0,
        asxCONTROL_COBRANET, &hCobraNetControl);
    CheckError(hMixer, __LINE__);

    // dump source lines
    ASX_Mixer_GetSourceNodeCount(hMixer,&nNodes);
    printf("Source nodes\n");
    for(j=0;j<nNodes;j++)
    {
        ASX_Mixer_GetSourceNode(hMixer,j,&hNode);
        PrintNodeName(hNode);
        printf("\n");
    }
    // dump destination lines
    ASX_Mixer_GetDestinationNodeCount(hMixer,&nNodes);
    printf("Destination nodes\n");
    for(j=0;j<nNodes;j++)
    {
        ASX_Mixer_GetDestinationNode(hMixer,j,&hNode);
        PrintNodeName(hNode);
        printf("\n");
    }

    // dump all controls
    asxError = ASX_Mixer_GetControlCount(hMixer,&nControls);
    CheckError(hMixer, __LINE__);

    printf("Retrieved controls (skipping volumes and sample rate converters)\n");
    for(i=0;i<nControls;i++)
    {
        enum asxCONTROL eControl;
        ASX_Mixer_GetControl(hMixer,i,&hControl);

        ASX_Control_GetType(hControl, &eControl);
        if((eControl==asxCONTROL_VOLUME)||(eControl==asxCONTROL_SRC))
            continue;

        PrintControlName(hControl);

        printf("On node(s) ");
        ASX_Control_GetSourceNode(hControl,&hNode);
        if( hNode )
            PrintNodeName(hNode);
        ASX_Control_GetDestinationNode(hControl,&hNode);
        if( hNode )
            PrintNodeName(hNode);
        printf("\n");

    }

    // get the details for the first CobraNet transmitter
    asxError = ASX_Mixer_GetControlByNodeTypeAndIndex(
        hMixer,
        0,0,
        asxNODE_COBRANET_TRANSMITTER,0,
        asxCONTROL_COBRANET_TRANSMITTER, &hCobraNetControl);
    CheckError(hMixer, __LINE__);
    if(!asxError) {
        ASX_CobranetTx_GetBundle(hCobraNetControl, &nBundle);
        ASX_CobranetTx_GetChannelCount(hCobraNetControl, &nCount);
        ASX_CobranetTx_GetChannelMap(hCobraNetControl, nMap);
        printf("CobraNet transmitter 0 details\n");
        printf("Bundle : %d\n",nBundle);
        printf("Channel count : %d\n",nCount);
        printf("Channel map : %d %d %d %d %d %d %d %d\n",
            nMap[0],
            nMap[1],
            nMap[2],
            nMap[3],
            nMap[4],
            nMap[5],
            nMap[6],
            nMap[7]);
    }

    // get the details for the first CobraNet receiver
    asxError = ASX_Mixer_GetControlByNodeTypeAndIndex(
        hMixer,
        asxNODE_COBRANET_RECEIVER,0,
        0,0,
        asxCONTROL_COBRANET_RECEIVER, &hCobraNetControl);
    CheckError(hMixer, __LINE__);
    if(!asxError) {
        ASX_CobranetRx_GetBundle(hCobraNetControl, &nBundle);
        ASX_CobranetRx_GetChannelMap(hCobraNetControl, nMap);
        printf("CobraNet receiver 0 details\n");
        printf("Bundle : %d\n",nBundle);
        printf("Channel map : %d %d %d %d %d %d %d %d\n",
            nMap[0],
            nMap[1],
            nMap[2],
            nMap[3],
            nMap[4],
            nMap[5],
            nMap[6],
            nMap[7]);
    }

    // look for some peak meters on CobraNet nodes

    // This is a meter on the audio path from a CobraNet Rx that is on the 
    // input side (source node) of an ASI2416.
    asxError = ASX_Mixer_GetControlByNodeTypeAndIndex(
            hMixer,
            asxNODE_COBRANET_IN,0,
            asxNODE_NONE,0,
            asxCONTROL_METER, &hMeterControl);
    CheckError(hMixer, __LINE__);

    if(!asxError) {
        printf("Found meter on first CobraNet input 0 from the network\n");
        PrintMeterReadings(hMixer, hMeterControl);
    }

    // This is a meter on the audio path from the ASI2416 to a CobraNet Tx.
    asxError = ASX_Mixer_GetControlByNodeTypeAndIndex(
            hMixer,
            asxNODE_NONE,0,
            asxNODE_COBRANET_OUT,0,
            asxCONTROL_METER, &hMeterControl);
    CheckError(hMixer, __LINE__);

    if(!asxError) {
        printf("Found meter on first CobraNet output 0 to the network\n");
        PrintMeterReadings(hMixer, hMeterControl);
    }

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

void PrintMeterReadings(ASX_HANDLE hMixer, ASX_HANDLE hControl)
{
    int i;
    int chans=0;
    float readings[2];
    ASX_ERROR asxError;

    asxError = ASX_Meter_GetChannels(hControl, (int *)&chans );
    CheckError(hMixer, __LINE__);

    asxError = ASX_Meter_GetPeak(hControl, readings, chans );
    CheckError(hMixer, __LINE__);
    if(!asxError)
        for(i=0;i<chans;i++)
            printf("Meter[%d] reads peak of %5.3f dB\n",i,readings[i]);

    asxError = ASX_Meter_GetRMS(hControl, readings, chans );
    CheckError(hMixer, __LINE__);
    if(!asxError)
        for(i=0;i<chans;i++)
            printf("Meter[%d] reads RMS of %5.3f dB\n",i,readings[i]);

}

ASX_HANDLE FindSnmpCobranetControl(ASX_HANDLE hMixer)
{
    ASX_HANDLE hSnmpCobranet = NULL;
    ASX_ERROR asxError;
    int nControl, nNumControls;
    enum asxCONTROL eControlType;
    int nSubSystem;

    asxError = ASX_Mixer_GetControlCount(
            hMixer,
            &nNumControls );

    for(nControl=0;nControl < nNumControls;nControl++){
        asxError = ASX_Mixer_GetControl(
            hMixer,
            nControl,
            &hSnmpCobranet);
        if(asxError == asxERROR_NO_ERROR){
            ASX_Control_GetType(hSnmpCobranet,&eControlType);
            ASX_Control_GetSubSystem(hSnmpCobranet,&nSubSystem);
            if(eControlType==asxCONTROL_COBRANET &&
               nSubSystem==ASX_SYSTEM_TYPE_SNMP){
                break;
            }
        }
    }
    if(nControl == nNumControls){
        hSnmpCobranet = NULL;
    }
    return hSnmpCobranet;
}

void PrintControlName(ASX_HANDLE hControl)
{
    char *pszName;
    int nLen;
    enum asxCONTROL eControl;

    ASX_Control_GetType(hControl, &eControl);
    ASXSTRING_EnumToString(eControl,0,0,&nLen);
    pszName=(char *)malloc(nLen);
    ASXSTRING_EnumToString(eControl,pszName,nLen,&nLen);
    printf("Control : %s ",pszName);

    free(pszName);
}

void PrintNodeName(ASX_HANDLE hNode)
{
    char *pszName;
    int nLen,nIndex;
    enum asxNODE eNode;

    ASX_Node_GetType(hNode, &eNode);
    ASX_Node_GetIndex(hNode, &nIndex);
    ASXSTRING_EnumToString(eNode,0,0,&nLen);
    pszName=(char *)malloc(nLen);
    ASXSTRING_EnumToString(eNode,pszName,nLen,&nLen);
    printf("Node : %s_%d ",pszName,nIndex);
    free(pszName);
}

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

    ASX_Error_GetLast( hObj, &asxError, &asxSubSystemErrorCode);
    if(!asxError)
        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",
        asxError,
        pszAsxErrorString,
        asxSubSystemErrorCode,
        pszAsxSubSystemErrorString );
    printf("When called from source %s line %d\n",__FILE__,nLine);

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

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

    ASX_Error_GetLast( hObj, &asxError, &asxSubSystemErrorCode);
    if(!asxError)
        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("WARNING: #%d, %s - Skipping.\n\n",
        asxError,
        pszAsxErrorString);
    ASX_Error_Clear( hObj );
    return 1;
}