How do I use cURL to control the IO over Unix Domain Socket on the BB-400?

FAQs

This FAQ will explain what cURL is, and how it can be used to retrieve/modify information about the IO status over Unix Domain Socket.

cURL

cURL or ‘Client URL’ is a command line tool used to receive or modify data from the server. cURL is available on Linux and the latest version of Windows 10.

The BB-400 can listen to the REST commands (GET and POST) to control the IO lines; cURL is a useful way to send such requests.

GET requests

GET commands can be used to receive the IO line status of the BB-400.

Syntax
The cURL GET commands using UDS are sent using the following format:
$ curl -GET --unix-socket [Socket File location] [endpoint]

The IO socket file is located at “/var/run/bb-io.sock”. For Unix Domain Socket cURL uses an endpoint beginning with “http://localhost/” followed by 1 or more paths which are separated by “/”. This article provides examples of some of the allowed paths on the BB-400.

The following cURL commands used on the Linux command line provide examples of how to obtain the IO line status.

1. Get the current IO state

$ sudo curl -GET --unix-socket /var/run/bb-io.sock http://localhost/io

Output:

{
    "counts":[
        1,
        1,
        2,
        2,
        4,
        1,
        1,
        1
    ],
    "inputs":[
        1,
        1,
        0,
        1,
        1,
        1,
        1,
        1
    ],
    "outputs":[
        0,
        0,
        1,
        0,
        0,
        0,
        0,
        0
    ]
}

2. To stream changes in the IO lines status

Unix Domain Socket can also work by keeping a connection open to stream the IO status. The path “/events” is used to stream changes in the IO status. Please note that when streaming events, the “–no-buffer” command is necessary so that the changes are updated in the terminal:

$ sudo curl --no-buffer -GET --unix-socket /var/run/bb-io.sock http://localhost/events

Output

The output will look like the following and will update each time there is a change in the IO status:

{"io":{"counts":[1,1,2,2,4,1,1,1],"inputs":[1,1,0,1,1,1,1,1],"outputs":[0,0,1,0,0,0,0,0]}}
{"io":{"counts":[1,1,2,2,4,1,1,1],"inputs":[1,1,0,1,1,1,1,1],"outputs":[0,0,0,0,0,0,0,0]}}
{"io":{"counts":[1,1,2,2,4,1,1,1],"inputs":[1,1,1,1,1,1,1,1],"outputs":[0,0,0,0,0,0,0,0]}}
{"io":{"counts":[1,1,2,2,4,1,1,1],"inputs":[1,1,1,1,1,1,1,1],"outputs":[0,1,0,0,0,0,0,0]}}
{"io":{"counts":[1,2,2,2,4,1,1,1],"inputs":[1,0,1,1,1,1,1,1],"outputs":[0,1,0,0,0,0,0,0]}}
{"io":{"counts":[1,2,2,2,4,1,1,1],"inputs":[1,0,1,1,1,1,1,1],"outputs":[0,0,0,0,0,0,0,0]}}
{"io":{"counts":[1,2,2,2,4,1,1,1],"inputs":[1,1,1,1,1,1,1,1],"outputs":[0,0,0,0,0,0,0,0]}}
{"io":{"counts":[2,2,2,2,4,1,1,1],"inputs":[0,1,1,1,1,1,1,1],"outputs":[1,0,0,0,0,0,0,0]}}
{"io":{"counts":[2,2,2,2,4,1,1,1],"inputs":[0,1,1,1,1,1,1,1],"outputs":[1,0,0,0,0,0,0,0]}}
{"io":{"counts":[2,2,2,2,4,1,1,1],"inputs":[0,1,1,1,1,1,1,1],"outputs":[0,0,0,0,0,0,0,0]}}
{"io":{"counts":[2,2,2,2,4,1,1,1],"inputs":[1,1,1,1,1,1,1,1],"outputs":[0,0,0,0,0,0,0,0]}}
{"io":{"counts":[2,2,2,2,4,1,1,1],"inputs":[1,1,1,1,1,1,1,1],"outputs":[0,1,0,0,0,0,0,0]}}
{"io":{"counts":[2,3,2,2,4,1,1,1],"inputs":[1,0,1,1,1,1,1,1],"outputs":[0,1,0,0,0,0,0,0]}}
{"io":{"counts":[2,3,2,2,4,1,1,1],"inputs":[1,0,1,1,1,1,1,1],"outputs":[0,0,0,0,0,0,0,0]}}
{"io":{"counts":[2,3,2,2,4,1,1,1],"inputs":[1,1,1,1,1,1,1,1],"outputs":[0,0,0,0,0,0,0,0]}}
...

3. Get the state of an output

If you are only interested in a subset of the information you can apply a path to the URL, for example, to get the output state of DIO 7:

$ sudo curl -GET --unix-socket /var/run/bb-io.sock http://localhost/io/outputs/7

Output:

0

POST requests

POST requests can be used to modify data, and in this example, the POST commands will be used to set the IO lines of the BB-400. The POST request follows the format:

$ sudo curl -d [DATA] -POST --unix-socket [Socket File location] [URL]

The -d identifier specifies that data will be sent in a POST request to the server.

1. To set the output states of the IO lines

In this example, the DIO 7 output will be set to 1 and the other outputs to 0. The data is the outputs in a JSON format. Please ensure that the JSON you use is well-formed:

$ sudo curl -d "[0,0,0,0,0,0,0,1]" -POST --unix-socket /var/run/bb-io.sock http://localhost/io/outputs

2. To set the output state of a selected IO line.

To change the state of a particular IO line, e.g turn DIO 7 off:

$ sudo curl -POST -d 1 --unix-socket /var/run/bb-io.sock http://localhost/io/outputs/7

For all post commands the “-i” identifier can be specified to see the status code of the request. The output will look similar to below:

HTTP/1.0 200 OK
Server: BB IO 1.0.1067.576af1d7
Date: Wed, 21 Aug 2019 08:31:05 GMT

This FAQ has provided examples of cURL commands that can be used to retrieve and modify information from the IO status using Unix Domain Socket. Examples of GET and POST commands have been provided.


FAQs