AudioScience HPI Version_4.20.11

Network configuration

HPI example code for configuring HPIDUP networking options.

/******************************************************************************
$Header: /home/eliot/asi/repo/cvsrepo/Repository/apps/microtests/subsysconfig/subsysconfig.c,v 1.17 2011/01/05 15:11:13 as-age Exp $

Test Hardware Programming Interface (HPI) using HPI functions.
Currently tests HPIUDP configuration.

Usage: subsysconfig --help

note to cleanup this file, use "astyle --style = linux -s4 subsysconfig.c"
******************************************************************************/

#define SECONDS (1000/POLL_INTERVAL)
#define MINUTES (60*SECONDS)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <arpa/inet.h>
#endif
#include <getopt.h>
#include <hpi.h>
#include <hpidebug.h>

#define verbose_printf if (verbose) printf

// local protos
static void HandleError(hpi_err_t err);

// global

/* Option variables */
static uint32_t broadcast = 0;
static uint32_t unicast = 0;
static uint32_t dump = 0;
static char *netif_ip = NULL;
static int verbose = 0;

static struct option long_options[] = {
        {"address", required_argument, 0, 'a'},
        {"broadcast", required_argument, 0, 'b'},
        {"unicast", required_argument, 0, 'u'},
        {"net", required_argument, 0, 'n'},
        {"mask", required_argument, 0, 'm'},
        {"dump", no_argument, 0, 'd'},
        {"verbose", no_argument, 0, 'v'},
        {"help", no_argument, 0, 'h'},
        {0, 0, 0, 0}
};

static const char *short_options = "a:b:u:n:m:dvh?";

static const char *option_help[] = {
        "remote IP address to add to HPIUDP sub-system (can be repeated).",
        "[0|1] Broadcast discovery method enabled for HPIUDP subsystem.",
        "[0|1] Unicast discovery method enabled for HPIUDP subsystem.",
        "Network interface to use (IP addr).",
        "Mask for network interface.",
        "Dump config entity.",
        "Verbose mode.",
        "Show this text.",
        0
};
static void help(void)
{
        int i = 0;
        printf("\nSUBSYSCONFIG - HPI SubSystem Configuration Test");
        printf("\nUsage - subsysconfig [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("\nExample usage\n");
        printf("\nsubsysconfig -u1 -n192.168.1.24 -a192.168.1.1 -a192.168.1.2\n");
        exit(0);
}

static void parse_options(
        int argc,
        char *argv[]
)
{
        hpi_err_t err;
        hpi_handle_t h;
        struct in_addr ip_adr;
        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':
                        ip_adr.s_addr = inet_addr(optarg);
                        err = HPI_Object_UriToHandle(
                                "hpi://subsystem/network/ip/adapter_address_add", &h);
                        HandleError(err);
                        if( !err ) {
                                err = HPI_Object_SetValue(h, entity_type_ip4_address, 1,
                                        &ip_adr.s_addr, sizeof(ip_adr.s_addr));
                                HandleError(err);
                                verbose_printf("hpiconfig_ipaddress_add(%s)\n", optarg);
                        }
                        //count++;
                        break;
                case 'n':
                        // configuration - network interface to use
                        netif_ip = optarg;
                        ip_adr.s_addr = inet_addr(optarg);
                        err = HPI_Object_UriToHandle(
                                "hpi://subsystem/network/ip/address", &h);
                        HandleError(err);
                        if( !err ) {
                                err = HPI_Object_SetValue(h, entity_type_ip4_address, 1,
                                                &ip_adr.s_addr, sizeof(ip_adr.s_addr));
                                HandleError(err);
                                verbose_printf("subsystem/network/ip/address(%s)\n", optarg);
                        }
                        break;
                case 'm':
                        // configuration - network interface mask to use
                        ip_adr.s_addr = inet_addr(optarg);
                        err = HPI_Object_UriToHandle(
                                "hpi://subsystem/network/ip/mask", &h);
                        HandleError(err);
                        if( !err ) {
                                err = HPI_Object_SetValue(h, entity_type_ip4_address, 1,
                                                &ip_adr.s_addr, sizeof(ip_adr.s_addr));
                                HandleError(err);
                                verbose_printf("subsystem/network/ip/mask(%s)\n", optarg);
                        }
                        break;
                case 'b':
                        broadcast = atoi(optarg);
                        // configuration - broadcast
                        err = HPI_Object_UriToHandle(
                                "hpi://subsystem/network/ip/udp/broadcast_enabled", &h);
                        HandleError(err);
                        if( !err ) {
                                err = HPI_Object_SetValue(h, entity_type_int, 1,
                                                &broadcast, sizeof(broadcast));
                                HandleError(err);
                                verbose_printf("subsystem/network/ip/udp/broadcast_enabled(%d)\n", broadcast);
                        }
                        break;
                case 'u':
                        unicast = atoi(optarg);
                        // configuration - unicast
                        err = HPI_Object_UriToHandle(
                                "hpi://subsystem/network/ip/udp/unicast_enabled", &h);
                        HandleError(err);
                        if( !err ) {
                                err = HPI_Object_SetValue(h, entity_type_int, 1,
                                                &unicast, sizeof(unicast));
                                HandleError(err);
                                verbose_printf("subsystem/network/ip/udp/unicast_enabled(%d)\n", unicast);
                        }
                        break;
                case 'd':
                        dump = 1;
                        break;
                case 'v':
                        verbose = 1;
                        break;
                case '?':
                case 'h':
                        help();
                        break;

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

        if (optind < argc) {
                // printf ("non-option ARGV-elements: ");
        }
}

/************************************** MAIN ***********************/
int main(int argc, char *argv[])
{
        const struct hpi_hsubsys *ss;
        hpi_err_t err = 0;
        hpi_handle_t h;
        int num_adapters = 0;
        uint16_t wVersion;
        uint32_t dwSerialNumber;
        uint16_t wType;
        uint16_t wNumOutStreams;
        uint16_t wNumInStreams;
        int enable = 1;

        int i = 0;


        printf("***********************************************\n");
        printf("Test HPI subsys configuration \n");
        printf("***********************************************\n");
        // open subsystem and set configuration
        ss = HPI_SubSysCreate();
        if (!ss) {
                printf("HPI_SubSysCreate failed\n");
                exit(1);
        }

        parse_options(argc, argv);

        // Start UDP running
        err = HPI_Object_UriToHandle("hpi://subsystem/network/enabled", &h);
        HandleError(err);
        err = HPI_Object_SetValue(h, entity_type_int, 1, &enable, sizeof(enable));
        HandleError(err);

        // test adapters have been found

        err = HPI_SubSysGetNumAdapters(ss, &num_adapters);
        HandleError(err);
        verbose_printf("HPI_SubSysGetNumAdapters returned %d \n", num_adapters);

        // iterate over the adapters
        for (i = 0; i < num_adapters; i++) {
                uint32_t index;
                uint16_t type;
                uint16_t wIPmsb, wIPlsb;

                err = HPI_SubSysGetAdapter(ss, i, &index, &type);
                HandleError(err);
                verbose_printf("\nHPI_SubSysGetAdapter(iter %d)\n", i);

                // get the IP address
                err = HPI_AdapterGetProperty(ss, index, HPI_ADAPTER_PROPERTY_IP_ADDRESS,
                                                &wIPmsb, &wIPlsb);

                if (!err) {
                        HandleError(err);
                        verbose_printf("HPI_AdapterGetProperty(ip address)\n");

                        printf("Checking adapter[%d] with IP address : %u.%u.%u.%u \n",
                                index,
                                (wIPmsb>>8) & 0xff, wIPmsb & 0xff,
                                (wIPlsb>>8) & 0xff, wIPlsb & 0xff);
                }

                // read back the adapter info
                err = HPI_AdapterGetInfo(ss, index,
                                        &wNumOutStreams, &wNumInStreams,
                                        &wVersion, &dwSerialNumber, &wType);
                HandleError(err);
                verbose_printf("HPI_AdapterGetInfo[%d]\n",index);

                printf("Adapter ID = %4X Index = %d NumOutStreams = %d NumInStreams = %d S/N = %d\n"
                        "Hw Version %c%d DSP code version %03d\n",
                        wType, index, wNumOutStreams, wNumInStreams,
                        dwSerialNumber, ((wVersion >> 3) & 0xf) + 'A',  // Hw version major
                        wVersion & 0x7, // Hw version minor
                        ((wVersion >> 13) * 100) + ((wVersion >> 7) & 0x3f)     // DSP code version
                        );
        }

        HPI_SubSysFree(ss);
        return 0;
}

/****************************** HandleError **********************/
static int getch(void)
{
        return getchar();
}

static void HandleError(hpi_err_t err)
{
        char szError[256];
        char nK = 0;

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