The following is some example HPI code that can be compiled under Windows or Linux.

Windows: In your project/makefile, you will need to define HPI_OS_WIN32_USER and HPIDLL_IMPORTS and link with ASIHPI32.LIB that comes in the Windows HPI SDK.

Linux: In your project/makefile, you will need to define HPI_OS_LINUX.

// $Header: /Repository/apps/thpi/thpif.c,v 1.11 2008/10/15 13:13:27 as-age Exp $
// THPIF.C
// Test Hardware Programming Interface (HPI)  using HPI functions
//
//

//#include <stdafx.h>   // for Microsoft apps only

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "hpi.h"

// local protos
void HandleError( HW16 wHE );

// file read/write
void THPI_WavFileOpen( short nIndex, char *pszFile);
short THPI_WavFileRead( short nIndex, unsigned char *pbData, long lLength);
void THPI_WavFileClose( short nIndex );


void main()
{

        HW16                    wHE=0;  // HPI error
        HW32                    dwVersion=0;
        HPI_HSUBSYS             *phSubSys;      // handle to audio subsystem
        HPI_FORMAT              Format;
        HW16                    wAdapterIndex=0;
        HW16                    wNumAdapters=0;
        HW16                    awAdapterList[20];
        HW16                    wListLength=20;
        HPI_HOSTREAM            hOutStream0=0;
        HPI_HOSTREAM            hOutStream1=0;
        HW32                    dwStrBufferSize=0;
        HW32                    dwDataToPlay=0;
        HW16                    wState=0;
        HPI_HMIXER              hMixer=0;
        HPI_HCONTROL            hControl=0;
        short                   anGain0_01dB[HPI_MAX_CHANNELS];

        #define BLOCK_SIZE 8192
        unsigned char   abBuffer[BLOCK_SIZE];
        HW32    i=0;
        short   nPeakLeft=0, nPeakRight=0;

        printf("\n** Test HPI using Functions **\n");

        // open subsystem and find adapters
        phSubSys = HPI_SubSysCreate();
        HandleError( wHE );
        printf("HPI_SubSys_Create\n");

        wHE = HPI_SubSysGetVersion( phSubSys, &dwVersion );
        HandleError( wHE );
        printf("HPI_SubSys_Version=%lx\n", dwVersion );

        wHE = HPI_SubSysFindAdapters(
                phSubSys,
                &wNumAdapters,
                awAdapterList,
                wListLength
                );
        HandleError( wHE );
        printf("HPI_SubSysFindAdapters NumberAdapters=%d ", wNumAdapters);
        for(i=0; i<4; i++)
                printf("%ld=%X ", i, awAdapterList[i] );
        printf("\n\n");

        // open adapter
        {
        HW16    wNumOutStreams;
        HW16    wNumInStreams;
        HW32    dwSerialNumber;
        HW16    wAdapterType;
        HW16    wVersion;

        wHE = HPI_AdapterOpen(
                phSubSys,
                wAdapterIndex
                );
        HandleError( wHE );
        printf("HPI Open_Adapter\n");

        wHE = HPI_AdapterGetInfo(
                phSubSys,
                wAdapterIndex,
                &wNumOutStreams,
                &wNumInStreams,
                &wVersion,
                &dwSerialNumber,
                &wAdapterType
                );
        HandleError( wHE );
        printf("HPI Get_Adapter_Info\n");
        printf("Adapter Index=%d NumOutStreams=%d NumInStreams=%d Version=%d S/N=%ld Adapter=ASI%X\n",
                wAdapterIndex, wNumOutStreams, wNumInStreams, wVersion, dwSerialNumber, wAdapterType );
        }

        // open the out_streams 0 and 1

        wHE = HPI_OutStreamOpen(
                phSubSys,
                wAdapterIndex,
                0,
                &hOutStream0
                );
        printf("HPI_OutStreamOpen 0: handle=%lX\n", hOutStream0);
        HandleError( wHE );

        wHE = HPI_OutStreamOpen(
                phSubSys,
                wAdapterIndex,
                1,
                &hOutStream1
                );
        printf("HPI_OutStreamOpen 1: handle=%lX\n", hOutStream1);
        HandleError( wHE );

        // find out some info about the device
        wHE = HPI_OutStreamGetInfo(
                phSubSys,
                hOutStream0,
                NULL,
                &dwStrBufferSize,
                NULL
                );
        printf("HPI_OutStreamGetInfo 0: BufferSize=%ld\n", dwStrBufferSize);
        HandleError( wHE );

        // open the mixer of this adapter
        wHE = HPI_MixerOpen(
                phSubSys,
                wAdapterIndex,
                &hMixer
                );
        printf("HPI_MixerOpen: handle=%lX\n", hMixer);
        HandleError( wHE );

        // get mixer volume control on the connection
        // between out_stream 0 and lineOut 0
        wHE = HPI_MixerGetControl(
                        phSubSys,
                        hMixer,
                        HPI_SOURCENODE_OSTREAM,
                        0,
                        HPI_DESTNODE_LINEOUT,
                        0,
                        HPI_CONTROL_VOLUME,
                        &hControl
                        );
        HandleError( wHE );

        // set the level on LineOut 1 to +19 dBu
        anGain0_01dB[0] = 19 * HPI_UNITS_PER_dB;
        anGain0_01dB[1] = 19 * HPI_UNITS_PER_dB;
        wHE = HPI_MixerGetControl( phSubSys, hMixer,
                        0, 0,                           // source "none", index "none"
                        HPI_DESTNODE_LINEOUT, 0,        // destination line out, index 0
                        HPI_CONTROL_LEVEL, &hControl
                        );
        HandleError( wHE );
        wHE = HPI_LevelSetGain(phSubSys,hControl,anGain0_01dB);
        printf("Set line out 1 output level to +19 dBu\n");
        HandleError( wHE );
        // set the level on LineIn 1 to +19 dBu
        wHE = HPI_MixerGetControl(phSubSys,hMixer,
                        HPI_SOURCENODE_LINEIN,  0,      // source line in, index 0
                        0, 0,                           // destination "none", index "none"
                        HPI_CONTROL_LEVEL, &hControl
                        );
        HandleError( wHE );
        wHE = HPI_LevelSetGain(phSubSys,hControl,anGain0_01dB);
        printf("Set line in 1 input level to +19 dBu\n");
        HandleError( wHE );

        // count the number of line outs
        wHE = 0;
        i=0;
        while(!wHE)
        {
                wHE = HPI_MixerGetControl(
                                phSubSys,
                                hMixer,
                                0,
                                0,
                                HPI_DESTNODE_LINEOUT,
                                i,
                                HPI_CONTROL_METER,
                                &hControl
                                );
                if(!wHE)
                        i++;
        }
        printf("Number of line outs found - %d\n",i);


        // open some audio files to play
        THPI_WavFileOpen( 0, "c:\\asi\\audio\\mp2\\BETTY.WAV" );
        THPI_WavFileOpen( 1, "c:\\asi\\audio\\mp2\\LONELY.WAV" );

        // setup format and size of data block
        // we will use to send audio data to out_stream
        // we would normally get the format directly from the file
        // or audio data
        wHE = HPI_FormatCreate(
                &Format,
                1,          // mono channel
                HPI_FORMAT_MPEG_L2,
                44100,          //sample rate
                128000,     //128k bits/sec
                0                       // no attributes
                );
        HandleError( wHE );

        // preload buffer of device
        printf("Preload ");
        for(i=0; i<(dwStrBufferSize/BLOCK_SIZE); i++)
        {
                // out_stream #1
                THPI_WavFileRead( 0, abBuffer, BLOCK_SIZE );

                wHE = HPI_OutStreamWriteBuf(
                        phSubSys,
                        hOutStream0,
                        abBuffer,
                        BLOCK_SIZE,
                        &Format
                        );

                printf(".");

        }
        printf("\n");


        // start play back
        wHE = HPI_OutStreamStart(
                                phSubSys,
                                hOutStream0
                                );
        HandleError( wHE );
        printf("OutStream - start\n");

        // keep buffers full of data
        // while playing
        while(1)
        {
                char szPeakL[40];
                char szPeakR[40];
                short anPeakLog[2] = {0,0};
                short j=0;

                // get the meter level value
                wHE = HPI_MeterGetPeak(
                        phSubSys,
                        hControl,
                        anPeakLog
                );

                // create level bar
                #define BARLEN 10
                nPeakLeft = anPeakLog[0]/3;
                if(nPeakLeft < -BARLEN) nPeakLeft = -BARLEN;
                if(nPeakLeft > 0 ) nPeakLeft = 0;
                nPeakLeft = BARLEN+nPeakLeft;
                strcpy(szPeakL,"");
                for(j=0; j<nPeakLeft; j++)
                        strcat(szPeakL, "*");
                nPeakRight = anPeakLog[1]/3;
                if(nPeakRight < -BARLEN) nPeakRight = -BARLEN;
                if(nPeakRight > 0 ) nPeakRight = 0;
                nPeakRight = BARLEN+nPeakRight;
                strcpy(szPeakR,"");
                for(j=0; j<nPeakRight; j++)
                        strcat(szPeakR, "*");

                //get state of device
                wHE = HPI_OutStreamGetInfo(
                        phSubSys,
                        hOutStream0,
                        &wState,
                        NULL,
                        &dwDataToPlay
                );

                printf("DataToPlay=%06ld State=%d PeakMeter:L=%11s R=%11s\r", dwDataToPlay, wState, szPeakL, szPeakR );

                if( wState != HPI_STATE_PLAYING )
                        break;

                if( dwDataToPlay < dwStrBufferSize/2)
                {
                        // read a block of audio data from the file
                        if( ! THPI_WavFileRead( 0, abBuffer, BLOCK_SIZE ))
                        {
                                // write it to the device
                                wHE = HPI_OutStreamWriteBuf(
                                        phSubSys,
                                        hOutStream0,
                                        abBuffer,
                                        BLOCK_SIZE,
                                        &Format
                                        );
                }
                }

                // stop if the user presses a key
                if(kbhit())
                        break;
        }
        printf("\n");

        wHE = HPI_OutStreamStop(
                        phSubSys,
                        hOutStream0 );
        printf("HPI OStream_Stop\n" );
        HandleError( wHE );

        wHE = HPI_OutStreamClose(
                        phSubSys,
                        hOutStream0 );
        printf("HPI OStream_Close_0\n" );
        HandleError( wHE );

        wHE = HPI_OutStreamClose(
                        phSubSys,
                        hOutStream1 );
        printf("HPI OStream_Close_1 \n" );
        HandleError( wHE );

        wHE = HPI_MixerClose(
                        phSubSys,
                        hMixer );
        printf("HPI Mixer_Close\n" );
        HandleError( wHE );

        wHE = HPI_AdapterClose(
                        phSubSys,
                        wAdapterIndex );
        printf("HPI Adapter_Close\n" );
        HandleError( wHE );

        THPI_WavFileClose(0);

        getch();
}

FILE *gpFile[4];

void THPI_WavFileOpen( short nIndex, char *pszFile)
{
        gpFile[nIndex] = fopen(pszFile,"rb");
        if(!gpFile[nIndex])
        {
                printf("****ERROR**** - can't open file\n");
                getch();
                exit(0);
        }
        fseek(gpFile[nIndex], 0x50, SEEK_SET);

}

short THPI_WavFileRead( short nIndex, unsigned char *pbData, long lLength)
{
        long lNumRead;
        lNumRead = fread( pbData, 1, lLength, gpFile[nIndex] );                 //read WAV file
        if( lNumRead != lLength)
                return(1);
        else
                return(0);
}

void THPI_WavFileClose(short nIndex )
{
        fclose(gpFile[nIndex]);
}


void    HandleError( HW16 wHE )
{
        char szError[256];

        if(wHE)
        {
                HPI_GetErrorText( wHE, szError );
                printf("ERROR %d %s\n", wHE, szError);
                printf("press a key...\n");
                getch();
        }
}

/* END_OF_CODE */

// $Header: /Repository/apps/thpi/thpifGPIO.c,v 1.6 2008/10/15 13:13:27 as-age Exp $
//
// Test Hardware Programming Interface (HPI) for GPIO.
// Currently works for a single GPIO module on an ASI2416.
//

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "hpi.h"

// local protos
void HandleError( HW16 wHE );

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

        HW16            wHE=0;  // HPI error
        HW32            dwVersion=0;
        HPI_HSUBSYS     *phSubSys;      // handle to audio subsystem
        HPI_HGPIO       hGpio=0;
        HW16            wGpioInputCount;
        HW16            wGpioOutputCount;
        HW16            wGpioOptoData[4];
        HW16            wAdapterIndex=0;
        HW16            wNumAdapters=0;
        HW32            i=0,j=0;
        HW32            dwIndex=0;
        int             nAdapters=0;

        printf("\n** Test GPIO interface using HPI Functions **\n");
        printf("Usage: thpifGPIO ip_address_of_computer_nic \n");
        printf(">thpifGPIO 192.168.1.103\n");

        if(argc<2)
        {
                printf("ERROR need to input the IP address of the NIC to use.\n");
                exit(-1);
        }

        // open subsystem and find adapters
        phSubSys = HPI_SubSysCreate();
        HandleError( wHE );
        printf("HPI_SubSys_Create\n");

        wHE = HPI_SubSysGetVersion( phSubSys, &dwVersion );
        HandleError( wHE );
        printf("HPI_SubSys_Version=%lx\n", dwVersion );

        wHE = HPI_SubSysSetHostNetworkInterface( phSubSys, argv[1] );
        HandleError( wHE );
        printf("Pause 2 seconds for IP address assignment and ASI2416 discovery.\n");
        Sleep(2000);    // 2000ms = 2 seconds


        wHE = HPI_SubSysGetNumAdapters( phSubSys, &nAdapters);
        HandleError( wHE );

        printf("Adapters:\n");
        printf("Index\tType\tMAC\n");
        for(i=0;i<nAdapters;i++)
        {
                HW16 wAdapterType;
                wHE = HPI_SubSysGetAdapter( phSubSys, i, &dwIndex, &wAdapterType );
                HandleError( wHE );

                /* indexes >1000 are network adapters */
                if(dwIndex>1000)
                //if(0)
                {
                        HW16 macWord2, macWord1, macWord0, wDummy;

                        wHE = HPI_AdapterOpen( phSubSys, dwIndex );
                        HandleError( wHE );

                        wHE = HPI_AdapterGetProperty( phSubSys, dwIndex,
                                                HPI_ADAPTER_PROPERTY_MAC_ADDRESS_MSB,
                                                &macWord2,
                                                &macWord1 );
                        HandleError( wHE );
                        wHE = HPI_AdapterGetProperty( phSubSys, dwIndex,
                                                HPI_ADAPTER_PROPERTY_MAC_ADDRESS_LSB,
                                                &macWord0,
                                                &wDummy );
                        HandleError( wHE );

                        printf("%d\tASI%x\t%02X.%02X.%02X.%02X.%02X.%02X\n",dwIndex,wAdapterType,
                                (macWord2>>8) & 0xff,
                                macWord2 & 0xff,
                                (macWord1>>8) & 0xff,
                                macWord1 & 0xff,
                                (macWord0>>8) & 0xff,
                                macWord0 & 0xff
                        );

                        wHE = HPI_AdapterClose( phSubSys, dwIndex );
                        HandleError( wHE );

                }
                else
                        printf("%d\tASI%x\tnone\n",dwIndex,wAdapterType);

        }

        printf("\nType the index of adapter (from above list) to open and press <ENTER>\n>");
        scanf("%d",&dwIndex);

        // open adapter
        wHE = HPI_AdapterOpen(
                phSubSys,
                dwIndex
                );
        HandleError( wHE );
        printf("HPI Open_Adapter index %d\n",dwIndex);

        // look for GPIO
        wHE = HPI_GpioOpen(
                phSubSys,
                dwIndex,
                &hGpio,
                &wGpioInputCount,
                &wGpioOutputCount);
        HandleError( wHE );

        printf("GPIO: input Optos %d, output relays %d\n",wGpioInputCount,wGpioOutputCount);

        /*
        if(wGpioInputCount>16)
        {
                printf("WARNING - this software only tests up to 16 bits of GPIO\n");
                wGpioInputCount = 16;
        }
        */

        // test GPIO
        if(wGpioInputCount)
        {
                for(i=0;i<wGpioInputCount;i++)
                {
                        HW16 wBit;
                        HW16 wMask=0x8000;

                        // set a bit
                        printf("GPIO, set RELAY bit %d\n",i);
                        wHE = HPI_GpioWriteBit(
                                phSubSys,
                                hGpio,
                                i,
                                1);
                        HandleError( wHE );
                        // wait 100 ms
                        Sleep(100);
                        // read back single bit
                        wHE = HPI_GpioReadBit(
                                phSubSys,
                                hGpio,
                                i,
                                &wBit);
                        HandleError( wHE );
                        if(wBit)
                                printf("GPIO, get OPTO bit %d - read %d, OK\n",i,wBit);
                        else
                                printf("GPIO, get OPTO bit %d - read %d, ERROR\n",i,wBit);
                        // read back all bits
                        wHE = HPI_GpioReadAllBits(
                                phSubSys,
                                hGpio,
                                &wGpioOptoData);
                        HandleError( wHE );
                        printf("GPIO, get all OPTO bits ");
                        for(j=0;j<16;j++)
                        {
                                if(wGpioOptoData[0]&wMask)
                                        printf("1");
                                else
                                        printf("0");
                                wMask >>= 1;
                        }
                        printf("\n");

                        // reset it
                        printf("GPIO, reset RELAY bit %d\n",i);
                        wHE = HPI_GpioWriteBit(
                                phSubSys,
                                hGpio,
                                i,
                                0);
                }
        }
        // GPIO object does not need to be closed !

        // close adapter
        wHE = HPI_AdapterClose(
                        phSubSys,
                        wAdapterIndex );
        printf("HPI Adapter_Close\n" );
        HandleError( wHE );
}



void    HandleError( HW16 wHE )
{
        char szError[256];

        if(wHE)
        {
                HPI_GetErrorText( wHE, szError );
                printf("ERROR %d %s\n", wHE, szError);
                printf("press a key...\n");
                getch();
        }
}
/* END_OF_CODE */

/******************************************************************************
$Header: /Repository/drv/linux/test/asihpitune.c,v 1.37 2009/03/06 00:03:46 as-ewb Exp $

  Control tuners on ASI87xx and ASI892x cards

  Usage: asihpitune --help

******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#ifdef HPI_OS_LINUX
#include <unistd.h>
#endif
#include <getopt.h>
#include <hpi.h>
#include <hpirds.h>
#include <hpidebug.h>

#define verbose_printf if (verbose) printf

typedef struct {
        HW16 wControlType;      // HPI_CONTROL_METER, _VOLUME etc
        HW16 wSrcNodeType;
        HW16 wSrcNodeIndex;
        HW16 wDstNodeType;
        HW16 wDstNodeIndex;
} hpi_control_t;

HPI_HSUBSYS *hSubSys;           // handle to audio subsystem

static char *hpi_control_strings[] = HPI_CONTROL_TYPE_STRINGS;
static char *hpi_src_strings[] = HPI_SOURCENODE_STRINGS;
static char *hpi_dst_strings[] = HPI_DESTNODE_STRINGS;

#define nanosec 1000000

// local protos
void HandleError(
        HW16 wHE
);
void HandleErrorComment(
        char *comment,
        HW16 wHE
);
void HandleErrorCommentContinue(
        char *comment,
        HW16 wHE
);
int getch(
        void
);
static void asi_sleep(
        unsigned int milliseconds
);

/* NOTE: indices must match HPI defines for each band */
char *band_names[HPI_TUNER_BAND_LAST + 1] = {
        "invalid",
        "AM",
        "FM",
        "NTSC_M",
        "FM2",
        "Aux",
        "PAL_BG",
        "PAL_I",
        "PAL_DK",
        "Secam_L",
};

/* Option variables */
unsigned int dump_mixer = 0;
HW16 wAdapterIndex = 0;
HW16 nTunerIndex = 0;
int nReadLevel = 1;
HW16 nBand = 0;
HW32 nTunerFrequency = 0;
short wRssMode = -1;
short wRdsMode = -1;
int query_tuner_caps = 0;
int query_tuner_settings = 0;
int verbose = 0;
int rds = 0;
int rds_poll_ms = 1000;
int rds_poll_loops = 100;
short nMux = -1;

static struct option long_options[] = {
        {"adapter", required_argument, 0, 'a'},
        {"band", required_argument, 0, 'b'},
        {"query-caps", no_argument, 0, 'c'},
        {"dump-mixer", no_argument, 0, 'd'},
        {"frequency", required_argument, 0, 'f'},
        {"rss-mode", required_argument, 0, 'm'},
        {"rds-mode", required_argument, 0, 'n'},
        {"rds_poll_ms", required_argument, 0, 'p'},
        {"rds", optional_argument, 0, 'r'},
        {"settings", no_argument, 0, 's'},
        {"tuner", required_argument, 0, 't'},
        {"verbose", no_argument, 0, 'v'},
        {"mux", required_argument, 0, 'x'},
        {"help", no_argument, 0, 'h'},
        {0, 0, 0, 0}
};

const char *short_options = "a:b:cdf:m:n:p:r::t:st:vx:h";

const char *option_help[] = {
        "<adapter number> to test, default is 0.",
        "<band> see below for valid options",
        "Query tuner capabilities.",
        "Dump list of mixer controls.",
        "<freq in kHz> tuner frequency",
        "<mode> Set tuner RSS mode.",
        "<mode> Set tuner RDS mode.",
        "<milliseconds> Set RDS polling interval. Default 1000",
        "[optional loop count] Reads RDS data from the tuner. Default 100 loops",
        "Read back tuner settings.",
        "<tuner index> to query or set, default is 0.",
        "Enable verbose output.",
        "<n> Set audio out mux to tuner",
        "Show this text."
};
void help(
        void
)
{
        int i = 0;
        printf("\nUsage - asihpitune [options]\n");
        while (long_options[i].name != 0) {
                printf("--%s -%c %s\n",
                        long_options[i].name,
                        (char)(long_options[i].val), option_help[i]);
                i++;
        }
        printf("Valid values for -b option are ");
        for (i = 1; i <= HPI_TUNER_BAND_LAST; i++)
                printf("%s, ", band_names[i]);
        printf("\n");
        exit(0);
}

void parse_options(
        int argc,
        char *argv[]
)
{
        int c;
    /*********** Parse the command line options ***************/
        while (1) {
                // int this_option_optind = optind ? optind : 1;
                int option_index = 0;

                c = getopt_long(argc, argv, short_options,
                        long_options, &option_index);
                if (c == -1)
                        break;

                switch (c) {
                case 0:
                        printf("option %s", long_options[option_index].name);
                        if (optarg)
                                printf(" with arg %s", optarg);
                        printf("\n");
                        break;
                case 'a':
                        wAdapterIndex = atoi(optarg);
                        break;
                case 'b':
                        {
                                int i;
                                for (i = 1; i <= HPI_TUNER_BAND_LAST; i++)
                                        if (strcmp(band_names[i], optarg) ==
                                                0)
                                                nBand = i;
                                if (!nBand) {
                                        printf("\n****** Invalid band: %s ******\n", optarg);
                                        help();
                                }

                                query_tuner_settings = 1;
                        }
                        break;
                case 'c':
                        query_tuner_caps = 1;
                        break;
                case 'd':
                        dump_mixer = 1;
                        break;
                case 'f':
                        nTunerFrequency = atoi(optarg);
                        query_tuner_settings = 1;
                        break;
                case 'm':
                        wRssMode = atoi(optarg);
                        query_tuner_settings = 1;
                        break;
                case 'n':
                        wRdsMode = atoi(optarg);
                        query_tuner_settings = 1;
                        break;
                case 'p':
                        rds_poll_ms = atoi(optarg);
                        rds = 1;
                        break;
                case 'r':
                        rds = 1;
                        if (optarg)
                                rds_poll_loops = atoi(optarg);
                        break;
                case 's':
                        query_tuner_settings = 1;
                        break;
                case 't':
                        nTunerIndex = atoi(optarg);
                        break;
                case 'v':
                        verbose = 1;
                        break;
                case 'x':
                        nMux = atoi(optarg);
                        break;
                case '?':
                case 'h':
                        help();
                        break;

                default:
                        printf("?? getopt returned character code 0%o ??\n",
                                c);
                }
        }

        if (optind < argc) {
                // printf ("non-option ARGV-elements: ");
                /*
                   nNumFiles=0;
                   while ((optind < argc) && (nNumFiles < MAX_FILES))  {
                   strcpy( szFile[nNumFiles], argv[optind++] );
                   printf("File %d is %s\n",nNumFiles,szFile[nNumFiles]);
                   nNumFiles++;
                   }
                 */
        }

}

void print_mixer_controls(
        HPI_HMIXER hMixer
)
{
        HW16 f;
        HPI_HCONTROL hTuner;
        HW32 wHE;
        for (f = 0; f < 256; f++) {
                hpi_control_t asihpi_control;

                wHE = HPI_MixerGetControlByIndex(hSubSys, hMixer, f, &asihpi_control.wSrcNodeType, &asihpi_control.wSrcNodeIndex, &asihpi_control.wDstNodeType, &asihpi_control.wDstNodeIndex, &asihpi_control.wControlType,    // HPI_CONTROL_METER, _VOLUME etc
                        &hTuner);
                if (wHE == HPI_ERROR_CONTROL_DISABLED)
                        printf("DISABLED ");
                else if (wHE)
                        break;

                printf("HPI Control %d, %s:%s[%d]->%s[%d]\n",
                        f,
                        hpi_control_strings[asihpi_control.wControlType],
                        hpi_src_strings[asihpi_control.wSrcNodeType -
                                HPI_SOURCENODE_BASE],
                        asihpi_control.wSrcNodeIndex,
                        hpi_dst_strings[asihpi_control.wDstNodeType -
                                HPI_DESTNODE_BASE],
                        asihpi_control.wDstNodeIndex);
                if (asihpi_control.wControlType == HPI_CONTROL_MULTIPLEXER) {
                        HW16 l;
                        for (l = 0; l < 256; l++) {
                                wHE = HPI_Multiplexer_QuerySource(hSubSys,
                                        hTuner, l,
                                        &asihpi_control.
                                        wSrcNodeType,
                                        &asihpi_control.wSrcNodeIndex);
                                if (!wHE)
                                        printf("\tSource %d %s[%d]\n",
                                                l,
                                                hpi_src_strings
                                                [asihpi_control.wSrcNodeType -
                                                        HPI_SOURCENODE_BASE],
                                                asihpi_control.wSrcNodeIndex);
                                else
                                        break;
                        }
                }
        }
        printf("%d controls found\n\n", f);
}

/************************************** MAIN ***********************/
int main(
        int argc,
        char *argv[]
)
{
        HW16 wHE = 0;           // HPI error
        HW32 dwVersion = 0;
        HW16 wNumAdapters = 0;
        HW16 awAdapterList[20];
        HW16 wListLength = 20;
        HW16 wVersion;
        HW32 dwSerialNumber;
        HW16 wType;
        HW16 wNumOutStreams;
        HW16 wNumInStreams;
        int i;

        HPI_HMIXER hMixer = 0;
        HPI_HCONTROL hTuner = 0, hMux = 0;

        parse_options(argc, argv);

        /* */
        verbose_printf
                ("********************************************************************\n");
        verbose_printf
                ("\n** $Id: asihpitune.c,v 1.37 2009/03/06 00:03:46 as-ewb Exp $ **\n");
        verbose_printf("\n** HPI test code for tuners **\n");
        /*
           if (argc>1)
           sscanf(argv[1],"%d",&wAdapterIndex);
           else {
           printf("Usage:\n:Dump the mixer map of the adapter: test8700 <adapter index>\n");
           printf("Show tuner bands: test8700 <adapter index> <tuner index>\n");
           printf("Set a tuner: test8700 <adapter index> <tuner index> <band> <frequency in kHz>\n");

           exit (0);
           }
         */
        /* */
        // open subsystem and find adapters
        verbose_printf
                ("********************************************************************\n");
        verbose_printf("HPI_SubSysCreate\n");
        hSubSys = HPI_SubSysCreate();
        if (hSubSys == NULL) {
                printf("hSubSys==NULL\n");
                exit(1);
        }

        wHE = HPI_SubSysGetVersionEx(hSubSys, &dwVersion);
        HandleError(wHE);
        verbose_printf("HPI_SubSysGetVersionEx=%d.%02d.%02d\n",
                dwVersion >> 16, (dwVersion >> 8) & 0xff, dwVersion & 0xff);

        wHE = HPI_SubSysFindAdapters(hSubSys,
                &wNumAdapters, awAdapterList, wListLength);
        HandleError(wHE);
        verbose_printf("HPI_SubSysFindAdapters NumberAdapters=%d ",
                wNumAdapters);
        for (i = 0; i < 4; i++)
                verbose_printf("%d=%X ", i, awAdapterList[i]);
        verbose_printf("\n");

        wHE = HPI_AdapterClose(hSubSys, wAdapterIndex);
        HandleError(wHE);
        verbose_printf("HPI_AdapterClose \n");

        // open 1st adapter
        wHE = HPI_AdapterOpen(hSubSys, wAdapterIndex);
        HandleError(wHE);
        verbose_printf("HPI_AdapterOpen \n");

        wHE = HPI_AdapterGetInfo(hSubSys,
                wAdapterIndex,
                &wNumOutStreams,
                &wNumInStreams, &wVersion, &dwSerialNumber, &wType);
        HandleError(wHE);
        verbose_printf("HPI_AdapterGetInfo\n");
        verbose_printf("Adapter ID=%4X Index=%d NumOutStreams=%d NumInStreams=%d S/N=%d\n\tHw Version %c%d ", wType, wAdapterIndex, wNumOutStreams, wNumInStreams, dwSerialNumber, ((wVersion >> 3) & 0xf) + 'A',       // Hw version major
                wVersion & 0x7  // Hw version minor
                );

        if (verbose) {
                HW16 major, minor;
                wHE = HPI_AdapterGetProperty(hSubSys,
                        wAdapterIndex,
                        HPI_ADAPTER_PROPERTY_SOFTWARE_VERSION,
                        &major, &minor);
                HandleError(wHE);
                verbose_printf("DSP code version %d.%02d.%02d\n", major >> 8,
                        major & 0xff, minor);
        }
        // open the mixer of this adapter
        wHE = HPI_MixerOpen(hSubSys, wAdapterIndex, &hMixer);
        verbose_printf("HPI_MixerOpen: handle=%X\n", hMixer);
        HandleError(wHE);
        printf("\n");

    /******** Dump mixer ******/
        if (dump_mixer)
                print_mixer_controls(hMixer);

        if (((wType & 0xFF00) != 0x8700) && ((wType & 0xFF00) != 0x8900)) {
                printf("Only ASI87xx and ASI89xx adapters are supported/n");
                goto close;
        }

        wHE = HPI_MixerGetControl(hSubSys, hMixer, HPI_SOURCENODE_TUNER,
                nTunerIndex, 0, 0, HPI_CONTROL_TUNER, &hTuner);
        HandleErrorComment("Get Tuner Control", wHE);
        if (wHE)
                goto close;

        wHE = HPI_MixerGetControl(hSubSys, hMixer, 0, 0,
                HPI_DESTNODE_LINEOUT, 0, HPI_CONTROL_MULTIPLEXER, &hMux);
        HandleErrorComment("Get Mux", wHE);

        /******** Mode setting ******/
        if (wRssMode >= 0) {
                        wHE = HPI_Tuner_SetMode(hSubSys, hTuner,
                                HPI_TUNER_MODE_RSS, wRssMode);
                        HandleErrorComment("SetMode RSS", wHE);
        }

        if (wRdsMode >= 0) {
                        wHE = HPI_Tuner_SetMode(hSubSys, hTuner,
                                HPI_TUNER_MODE_RDS, wRdsMode);
                        HandleErrorComment("SetMode RDS", wHE);
        }

        /******** Mux setting ******/
        if (hMux && (nMux >= 0) && (nMux < 8)) {

                HandleErrorComment("Get mux ctrl", wHE);
                wHE = HPI_Multiplexer_SetSource(hSubSys, hMux,
                        HPI_SOURCENODE_TUNER, nMux);
                HandleErrorComment("SetMuxSource", wHE);

        }

/******** Band setting ******/
        if (nBand) {
                wHE = HPI_Tuner_SetBand(hSubSys, hTuner, nBand);
                HandleErrorComment("SetBand", wHE);
        }

/******** Frequency setting ******/
        if (nTunerFrequency) {
                wHE = HPI_Tuner_SetFrequency(hSubSys, hTuner,
                        nTunerFrequency);
                HandleErrorComment("SetFrequency", wHE);
        }
        asi_sleep(nanosec / 1000000);

/******** Query capabilities ******/
        if (query_tuner_caps) {
                HW32 dwIndex;
                HW16 wSetting;

                wHE = HPI_MixerGetControl(hSubSys, hMixer,
                        HPI_SOURCENODE_TUNER,
                        nTunerIndex, 0, 0, HPI_CONTROL_TUNER, &hTuner);
                if (wHE == 0) {
                        printf("\nTuner %d supports\n", nTunerIndex);
                        for (dwIndex = 0; dwIndex < 100; dwIndex++) {
                                wHE = HPI_Tuner_QueryBand(hSubSys,
                                        hTuner, dwIndex, &wSetting);
                                if (wHE != 0)
                                        break;
                                if ((wSetting == 0)
                                        || (wSetting > HPI_TUNER_BAND_LAST)) {
                                        printf("Unknown band type %d\n",
                                                wSetting);
                                        continue;
                                }

                                printf("%i %s : ", dwIndex,
                                        band_names[wSetting]);
                                {
                                        HW32 dwStart, dwEnd, dwStep;
                                        wHE = HPI_Tuner_QueryFrequency
                                                (hSubSys, hTuner, 0, wSetting,
                                                &dwStart);
                                        wHE = HPI_Tuner_QueryFrequency
                                                (hSubSys, hTuner, 1, wSetting,
                                                &dwEnd);
                                        wHE = HPI_Tuner_QueryFrequency
                                                (hSubSys, hTuner, 2, wSetting,
                                                &dwStep);
                                        printf("from %ikHz to %ikHz step %ikHz\n", dwStart, dwEnd, dwStep);
                                }
                        }
                }
        }
/******** Query settings ******/
        if (query_tuner_settings) {
                HW16 nTunerBand = 0;
                short int nRfLevel = 0;
                HW32 nTunerFrequency = 0;
                HW32 wMode = 0;
                HW16 wMuxIndex = 0;
                HW16 wStatus = 0;
                HW16 wStatusMask = 0;

                wHE = HPI_Tuner_GetBand(hSubSys, hTuner, &nTunerBand);
                HandleErrorComment("HPI_Tuner_GetBand", wHE);
                wHE = HPI_Tuner_GetFrequency(hSubSys, hTuner,
                        &nTunerFrequency);
                HandleErrorComment("HPI_Tuner_GetFrequency", wHE);

                wHE = HPI_Tuner_GetRFLevel(hSubSys, hTuner, &nRfLevel);
                HandleErrorComment("HPI_Tuner_GetRFLevel", wHE);

                wHE = HPI_Multiplexer_GetSource(hSubSys, hMux, 0, &wMuxIndex);
                HandleErrorComment("HPI_Multiplexer_GetSource", wHE);

                wHE = HPI_Tuner_GetStatus(hSubSys, hTuner,
                        &wStatusMask, &wStatus);
                HandleErrorComment("HPI_Tuner_GetStatus", wHE);

                printf("Current Settings  %s band, frequency %i. Mode %d.  RF level %3.2fdBuV. Mux=%d\n",
                                band_names[nTunerBand], nTunerFrequency, wMode, nRfLevel / 100.0, wMuxIndex);
                printf("Status mask %08X, value %08X\n", wStatusMask,
                        wStatus);
                wHE = HPI_Tuner_GetMode(hSubSys, hTuner,
                        HPI_TUNER_MODE_RSS, &wMode);
                if (wHE == HPI_ERROR_INVALID_CONTROL_ATTRIBUTE) {
                        verbose_printf("Tuner does not have MODEs.\n");
                        wMode = 0;
                } else if ( wHE ==  HPI_ERROR_INVALID_CONTROL_VALUE) {
                        verbose_printf("TUNER_MODE_RSS not supported\n");
                        wMode = 0;
                } else {
                        HandleErrorComment("HPI_Tuner_GetMode", wHE);
                        printf("TUNER_MODE_RSS %d\n", wMode);
                }

                if (wHE != HPI_ERROR_INVALID_CONTROL_ATTRIBUTE) {
                        wHE = HPI_Tuner_GetMode(hSubSys, hTuner,
                                HPI_TUNER_MODE_RDS, &wMode);
                        if ( wHE ==  HPI_ERROR_INVALID_CONTROL_VALUE) {
                                verbose_printf("TUNER_MODE_RDS not supported\n");
                                wMode = 0;
                        } else {
                                HandleErrorComment("HPI_Tuner_GetMode", wHE);
                                printf("TUNER_MODE_RDS %d\n", wMode);
                        }
                }
        }
/******** RDS ******/
        if (rds) {
                HPI_HCONTROL hPad = 0;
                unsigned int nRDSloopcount = rds_poll_loops;
                HW32 dwPTY = 0, dwPI = 0;
                HPI_ERR ecn = 0, ec = 0, epi = 0;

                wHE = HPI_MixerGetControl(hSubSys, hMixer,
                        HPI_SOURCENODE_TUNER,
                        nTunerIndex, 0, 0, HPI_CONTROL_PAD, &hPad);
                HandleErrorComment("HPI_MixerGetControl (HPI_CONTROL_PAD)",
                        wHE);

                if (wHE)
                        nRDSloopcount = 1;

                while (--nRDSloopcount) {
                        char szPAD[HPI_PAD_COMMENT_LEN];

                        printf("\nLoop - %d\n", nRDSloopcount);
                        if (verbose) {
                                unsigned char aRDS[12]; // RDS data group of (4 blocks each of 16 bits) plus 4 chars of error information

                                wHE = HPI_Tuner_GetRDS(hSubSys, hTuner,
                                        (char *)aRDS);
                                if (!wHE) {
                                        int i;

                                        printf("RDS block data ");
                                        for (i = 0; i < 12; i++)
                                                printf("%u ", aRDS[i]);
                                        printf("\n");

                                        /*
                                           Output PI from RDS data.
                                         */
                                        printf("PI from        ^---^ = 0x%X\n", aRDS[1] << 8 | aRDS[0]);
                                } else
                                        HandleErrorCommentContinue
                                                ("HPI_Tuner_GetRDS", wHE);
                        }
                        /*
                           Note, depending on whether the tuner supports HDRadio PAD information
                           or FM RDS information, different attributes may be valid.
                         */
                        if (!ecn) {
                                ecn = HPI_PAD_GetChannelName(hSubSys, hPad,
                                        szPAD, sizeof(szPAD));
                                HandleErrorCommentContinue
                                        ("HPI_PAD_GetChannelName", ecn);
                                if (!ecn)
                                        printf("ChannelName : %s\n", szPAD);
                        }

                        wHE = HPI_PAD_GetArtist(hSubSys, hPad,
                                szPAD, sizeof(szPAD));
                        HandleErrorComment("HPI_PAD_GetArtist", wHE);
                        if (!wHE)
                                printf("Artist/RT : %s\n", szPAD);

                        wHE = HPI_PAD_GetTitle(hSubSys, hPad,
                                szPAD, sizeof(szPAD));
                        HandleErrorComment("HPI_PAD_GetTitle", wHE);
                        if (!wHE)
                                printf("Title/PS : %s\n", szPAD);

                        if (!ec) {
                                ec = HPI_PAD_GetComment(hSubSys, hPad,
                                        szPAD, sizeof(szPAD));
                                HandleErrorCommentContinue
                                        ("HPI_PAD_GetComment", ec);
                                if (!ec)
                                        printf("Comment/PS : %s\n", szPAD);
                        }

                        wHE = HPI_PAD_GetProgramType(hSubSys, hPad, &dwPTY);
                        HandleErrorComment("HPI_PAD_GetProgramType", wHE);
                        if (!wHE) {
                                wHE = HPI_PAD_GetProgramTypeString(hSubSys,
                                        hPad, HPI_RDS_DATATYPE_RBDS, dwPTY,
                                        szPAD, sizeof(szPAD));
                                if (!wHE)
                                        printf("PTY : %d, %s\n", dwPTY,
                                                szPAD);
                        }

                        if (!epi) {
                                epi = HPI_PAD_GetRdsPI(hSubSys, hPad, &dwPI);
                                HandleErrorCommentContinue("HPI_PAD_GetRdsPI",
                                        epi);
                                if (!epi)
                                        printf("PI : 0x%X\n", dwPI);
                        }

                        asi_sleep(rds_poll_ms);
                }
        }
        /* if (rds) */
close:
        wHE = HPI_MixerClose(hSubSys, hMixer);
        verbose_printf("\nHPI_MixerClose\n");
        HandleError(wHE);

        wHE = HPI_AdapterClose(hSubSys, wAdapterIndex);
        verbose_printf("HPI_AdapterClose\n");
        HandleError(wHE);

        wHE = HPI_AdapterClose(hSubSys, wAdapterIndex);

        HPI_SubSysFree(hSubSys);
        return 0;
}

/****************************** HandleError **********************/
void HandleErrorCommentContinue(
        char *comment,
        HW16 wHE
)
{
        char szError[256];
        if (wHE) {
                printf("\t\t%s\n", comment);
                HPI_GetErrorText(wHE, szError);
                printf("\t\tERROR %s\n", szError);
        }

}
void HandleError(
        HW16 wHE
)
{
        char szError[256];
        char nK = 0;

        if (wHE) {
                HPI_GetErrorText(wHE, szError);
                printf("ERROR %d %s\n", wHE, szError);
                printf("\tpress Enter to continue, (q,Enter) to exit...\n");
                nK = getch();
                if (nK == 'q')
                        exit(0);
        }
}

void HandleErrorComment(
        char *comment,
        HW16 wHE
)
{
        if (wHE)
                printf("%s ", comment);
        HandleError(wHE);
}

int getch(
        void
)
{
        return getchar();
}

/****************************** asi_sleep **********************/
static void asi_sleep(
        unsigned int milliseconds
)
{
#ifdef HPI_OS_LINUX
        struct timespec req =
                { milliseconds / 1000, (milliseconds % 1000) * 1000000 };
        nanosleep(&req, 0);
#else
        Sleep(milliseconds);
#endif
}

/* END_OF_CODE */

Generated on Mon Mar 8 13:35:16 2010 for AudioScience HPI by  doxygen 1.4.6-NO