SDK Script dio-server.are

Connect to the tcp port with telnet or somethine elase and you can get and set digital in and output.

Please keep in mind there is no authentification!

Everybody who has access to the IP of the Router could access this server

Available commands

  • Set output port: out1 = 1, out1=0, out2=1, out2=0
  • Get status of input port: in1, in2
  • Get status of output port: out2, out2
  • Stop server: quit
  • Disconnect from server: bye
dio-server.are
/* DESC: This script implements a TCP server which can be used to control the DIO ports.
 * Copyright (C) 2012 NetModule AG, Switzerland
 */
 
MAX_CONNECTIONS = 1;
PORT = 2158;
ADDR = "";
NUMERIC = 0;
 
string handle_plain (string msg) 
{
    if (left(msg, 5) == "out1=") { 
        state  = (int) substr(msg, 5, 1);
        if (state != 1) state = 0;
        nb_dio_set("out1", state);
        return "OK\n";
    } else if (left(msg, 5) == "out2=") { 
        state  = (int) substr(msg, 5, 1);
        if (state != 1) state = 0;
        nb_dio_set("out2", state);
        return "OK\n";
    } else if (left(msg, 4) == "out1") { 
        state = nb_dio_get("out1");
        return sprintf("out1=%d\n", state);
    } else if (left(msg, 4) == "out2") { 
        state = nb_dio_get("out2");	
        return sprintf("out2=%d\n", state);
    } else if (left(msg, 3) == "in1") { 
        state = nb_dio_get("in1");	
        return sprintf("in1=%d\n", state);
    } else if (left(msg, 3) == "in2") { 
        state = nb_dio_get("in2");	
        return sprintf("in2=%d\n", state);
    } else {
        return "ERROR: invalid command\n";
    }
}
 
 
/* handle legacy numeric mode */
string handle_numeric (string msg) 
{
    if (left(msg, 5) == "10000") {
        /* get request */
        return sprintf("%d%d%d%d",
                       nb_dio_get("in1"),
                       nb_dio_get("in2"),
                       nb_dio_get("out1"),
                       nb_dio_get("out2"));
    } else {
        /* set request */
        x = substr(msg, 0, 1);
        if (x == "1") {
            nb_dio_set("out1", 1);
        } else if (x == "0") {
            nb_dio_set("out1", 0);
        } else {
            return "ERROR: invalid command\n";
        }
 
        x = substr(msg, 1, 1);
        if (x == "1") {
            nb_dio_set("out2", 1);
        } else if (x == "0") {
            nb_dio_set("out2", 0);
        } else {
            return "ERROR: invalid command\n";
        }
    }
    return "";
}
 
void usage()
{
    printf("Usage: dio-server.are [-n] [-i <ifc>] -p <port>\n\n");
 
    printf("Options:\n");
    printf("\t-n              operate in numeric hex mode\n");
    printf("\t-i <ifc>        bind to interface <ifc>\n");
    printf("\t-p <port>       listen on TCP port <port>\n");
    printf("\n'");
 
    printf("Available commands:\n");
    printf("\tSet output port:              out<1|0>=<1|0>\n");
    printf("\tGet status of input port:     in<1|2>\n");
    printf("\tGet status of output port:    out<1|2>\n");
    printf("\tStop server:                  quit\n");
    printf("\tDisconnect from server:       bye\n");
    printf("\n");
    exit(1);
}
 
 
/* get options */
for (i=1; i<argc; i++) {
    if (argv[i] == "-n") {
        NUMERIC = 1;
 
    } else if (argv[i] == "-i") {
        ADDR = nb_ifc_address(argv[++i]);
        if (ADDR == "") {
            printf("ERROR: invalid interface\n");
            exit(1);
        }
 
    } else if (argv[i] == "-p") {
        PORT = (int) argv[++i];
        if (PORT <= 1024 || PORT >= 65536) {
            printf("ERROR: invalid port %d\n", port);
            exit(1);
        }
 
    } else {
        usage();
    }
}
 
port = (int) argv[opt];
 
/* create server socket */
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0) {
    printf("ERROR: Unable to open socket\n");
    exit(1);
}
 
/* bind to port (and address) */
ret = bind(sock, PORT, ADDR);
if (ret == -1) {
    printf("ERROR: Unable to bind port %d\n", port);
    close(sock);
    exit(1);
}
 
/* listen on socket */
ret = listen(sock, MAX_CONNECTIONS);
if (ret == -1) {
    printf("ERROR: Unable to listen\n");
    close(sock);
    exit(1);
}
 
printf("Listening for connections (addr %s, port %d)\n", ADDR, PORT);
 
quit = 0;
timeout = 10;
while (!quit) {
    /* check if new client has connected */
    client = accept(sock);
    if (client < 0) {
        printf("accept failed\n");
        sleep(1);
        continue;
    }
    printf("New client connected\n");
 
    while (1) {
        /* wait for client socket data  */
        rv = select(client, timeout);
 
        if (rv == -1) {
            printf("ERROR: select failed\n");
            close(sock);
            exit(1);
        } else if (rv == 0) {
            /* nothing received */
            //printf("timeout\n");
        } else {
            /* received something */
            msg = recv(client);
            if (left(msg,4) == "quit") {
                printf("Received quit, terminating server\n");
                quit = 1;
                break;
            } else if (left(msg,3) == "bye"){
                printf("Received bye, closing client connection...\n");
                break;
            } else if (msg != "") {        
                if (NUMERIC == 1) {
                    answer = handle_numeric(msg);
                } else {
                    answer = handle_plain(msg);
                }
                if (answer != "") {
                    send(client, answer);
                }
            }
        }
    }
    close(client);
}
 
close(sock);
exit(0);