Sixnet\Red Lion Controls SN-Series IOCTRL library Version 1.0
snhwEx.c

This code is written to give an example or two on how to access the Sixnet\Red Lion Controls I/O modules from external OEM applications.

/* -*- mode:c;  c-basic-offset: 4; tab-width: 8; -*- vi: set sw=4 ts=8:*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include "snhw.h"

int main() {

    int returnValue = 0;
    int controlFd   = -1;
    char * controlPath;
    int din, dout;
    double ain;
    int debug = 4;

    /* Set the modem I/O driverPath, controlDevicePath, adcControlDevicePath
     * and defines number of available modules (DI, DO, AI). 
     */
    hwConfig_SetModemModel();

    /* Open FD (file discriptor) */
    returnValue = hwControl_Io_openControl(0, &controlFd, &controlPath);    

    if (returnValue >= 0 ) {

        /* Turn on D0 (Discrete Output) Set to 1 */
        hwControl_Io_setDO(1,debug);

        if (debug) {
            printf("D0 is set to on\n");
        }

        /* Get DO (Discrete Output) value */
        hwControl_Io_getDO(&dout,debug);
        if (debug) {
            printf("DOUT:%d\n",dout);
        }

        /* Turn off D0 (Discrete Output) Set to 0 */
        hwControl_Io_setDO(0,debug);

        if (debug) {
            printf("D0 is set to off\n");
        }

        /* Get DO (Discrete Output) value */
        hwControl_Io_getDO(&dout,debug);
        if (debug) {
            printf("DOUT:%d\n",dout);
        }

        /* Get DIN (Discrete Input) value */
        hwControl_Io_getDI(&din,debug);
        if (debug) {
            printf("DIN:%d\n",din);
        }

        /* Get AIN (Analog Input) value */
        hwControl_Io_getAI(&ain, debug);

        if (debug) {
            printf("AIN:%-5.2f\n",ain);
        }

        close(controlFd);
    }

    /* Exit with latest processed command return code */
    exit(returnValue);
}

void hwConfig_SetModemModel() {
         
            /* Currently common to all models IO */
            snprintf(driverPath,
                         sizeof(driverPath),
                         "btdev/bt_io.ko");
            snprintf(controlDevicePath,
                         sizeof(controlDevicePath),
                         "/dev/bt_io/3");
            snprintf(adcControlDevicePath,
                         sizeof(adcControlDevicePath),
                         "/sys/devices/platform/btw_adc/pc");
                                       
            /* IO */
            hw_analogInputs   = 1;
            hw_digitalOutputs = 1;
            hw_digitalInputs  = 1;
    }


int hwControl_Io_setDO(int value, int debug) {
    int returnValue = 0;
    int ioNum = 1;
    
    if ( returnValue == 0 ) {
        /* Set the IO */
        int controlBits;
        int action;
        int controlFd   = -1;
        char * controlPath;
        
        returnValue = hwControl_Io_openControl(0, &controlFd, &controlPath);    

        if ( value == 0 ) {
            action      = BT_IOCBIC;
        }
        else if ( value == 1 ) {
            action      = BT_IOCBIS;
        }
        else {
            returnValue = -1;
        }

        /* Set D0 to 0 or 1 */
        if  (returnValue >= 0 ) {
            controlBits = BT_OUT1 << (ioNum - 1);
            returnValue = hwControl_ioctl(controlFd, 
                        action, 
                        &controlBits, 
                        controlPath, 
                        debug);
        }

        if ( controlFd >= 0 ) {
            close(controlFd);
        }
    }
    
    return returnValue;
}

int hwControl_Io_openControl(int  debug,
                             int  * p_controlFd,
                             char ** p_controlPath) {
    int returnValue = 0;
    
    /* IO[0]: */
    if ( p_controlFd == NULL ) {
        returnValue = -1;
    }
    
    if ( returnValue >= 0) {
        if ( p_controlPath != NULL ) {
            *p_controlPath = controlDevicePath;
        }
        *p_controlFd   = open(controlDevicePath, O_RDWR | O_NOCTTY);
        if ( *p_controlFd < 0 ) {
           fprintf(stderr, "%s: Failed - Can't open control device \"%s\" (errno=%d)\n",
                        __FUNCTION__,
                        controlDevicePath,
                        errno);
           returnValue = -1;
        }
    }
    
    return returnValue;
}


int hwControl_ioctl(int controlFd, int controlAction, int * controlBits, char * controlPath, int debug) {
    int returnValue = 0;
    int closeOnExit = 0;

    /* Open control file descriptor if not already done */
    if ( controlFd == -1 ) {
        controlFd = open(controlPath, O_RDWR | O_NOCTTY);
        closeOnExit = 1;
    }
    
    if ( controlFd >= 0 ) {
        
        /* Perform the ioctl */
        if ( ioctl(controlFd, controlAction, controlBits) != 0 ) {
            fprintf(stderr, "%s: ioctl failed (errno=%d)\n", __FUNCTION__, errno);
            returnValue = -1;
        }
              
        if ( closeOnExit ) {
            close(controlFd);
        }
    }
    else {
        fprintf(stderr, "%s: Failed - Can't open control device \"%s\" (errno=%d)\n",
                        __FUNCTION__,
                        controlPath,
                        errno);
        returnValue = -1;
    }
        
    return returnValue; 

}

int hwControl_Io_getDI(int * p_value, int debug) {
    int returnValue = 0;
    int ioNum = 1;
    
    if ( p_value == NULL ) {
        returnValue = -1;
    }
    
    if ( returnValue == 0 ) {
        /* Get the IO */
        int controlBits;
        int action;
        int controlFd   = -1;
        char * controlPath;
        
        returnValue = hwControl_Io_openControl(0, &controlFd, &controlPath);    

        if  (returnValue >= 0 ) {
            controlBits = 0;
            action      = BT_IOCGET;

            returnValue = hwControl_ioctl(controlFd, 
                        action, 
                        &controlBits, 
                        controlPath, 
                        debug);
                        
            *p_value = (controlBits & (BT_IN1 << (ioNum - 1))) ? 1 : 0;
        }
    
        if ( controlFd >= 0 ) {
            close(controlFd);
        }
    }
    
    return returnValue;

}


int hwControl_Io_getDO(int * p_value, int debug) {
    int returnValue = 0;
    int ioNum = 1;
    
    if ( p_value == NULL ) {
        returnValue = -1;
    }
    
    if ( returnValue == 0 ) {
        /* Get the IO */
        int controlBits;
        int action;
        int controlFd   = -1;
        char * controlPath;
        
        returnValue = hwControl_Io_openControl(0, &controlFd, &controlPath);    

        if  (returnValue >= 0 ) {
            controlBits = 0;
            action      = BT_IOCGET;

            returnValue = hwControl_ioctl(controlFd, 
                        action, 
                        &controlBits, 
                        controlPath, 
                        debug);
                        
            *p_value = (controlBits & (BT_OUT1 << (ioNum - 1))) ? 1 : 0;
        }
    
        if ( controlFd >= 0 ) {
            close(controlFd);
        }
    }
    
    return returnValue;

}

int hwControl_Io_getAI(double * p_value, int debug) {
    int returnValue = 0;
    int ioNum = 3;

    if ( p_value == NULL ) {
        returnValue = -1;
    }
    
    if ( returnValue == 0 ) {
        /* Get the IO */
        FILE * aiFile;
        char controlPath[HWCONFIG_MAX_PATH];
        int  aiValue;
        
        snprintf(controlPath, sizeof(controlPath), "%s%d", 
             adcControlDevicePath,
             ioNum - 1);
        
        
        aiFile = fopen(controlPath, "r");
        
        if ( aiFile == NULL ) {
            fprintf(stderr, "%s: Failed - Can't open control device \"%s\" (errno=%d)\n",
                        __FUNCTION__,
                        controlPath,
                        errno);
            returnValue = -1;
        }
        else {
           
           if ( fscanf(aiFile, "%d", &aiValue) == 1 ) {
           
                *p_value = ((double) aiValue) * 15.0 / 1023.0;
           }
           else {
                fprintf(stderr, "%s: Failed - Can't read control device \"%s\" (errno=%d)\n",
                        __FUNCTION__,
                        controlPath,
                        errno);
                returnValue = -1;
           }
           fclose(aiFile);
        }
    }
    
    return returnValue;
}
 All Files Functions