nut/docs/man/upscli_get.txt

132 lines
3.4 KiB
Text
Raw Normal View History

2011-01-26 09:35:08 +00:00
UPSCLI_GET(3)
=============
NAME
----
2022-06-29 10:37:36 +00:00
upscli_get - retrieve data from an UPS
2011-01-26 09:35:08 +00:00
SYNOPSIS
--------
#include <upsclient.h>
2022-06-29 10:37:36 +00:00
int upscli_get(UPSCONN_t *ups, size_t numq, const char **query,
size_t *numa, char ***answer)
2011-01-26 09:35:08 +00:00
DESCRIPTION
-----------
2022-06-29 10:37:36 +00:00
2011-01-26 09:35:08 +00:00
The *upscli_get()* function takes the pointer 'ups' to a
`UPSCONN_t` state structure, and the pointer 'query' to an array of
'numq' query elements. It builds a properly-formatted request from
those elements and transmits it to linkman:upsd[8].
2022-06-29 10:37:36 +00:00
Upon success, the response will be split into separate components.
A pointer to those components will be returned in 'answer'.
The number of usable answer components will be returned in 'numa'.
2011-01-26 09:35:08 +00:00
USES
----
2022-06-29 10:37:36 +00:00
This function implements the "GET" command in the protocol.
As a result, you can use it to request many different things
from the server.
2011-01-26 09:35:08 +00:00
Some examples are:
* GET NUMLOGINS <ups>
* GET UPSDESC <ups>
* GET VAR <ups> <var>
* GET TYPE <ups> <var>
* GET DESC <ups> <var>
* GET CMDDESC <ups> <cmd>
QUERY FORMATTING
----------------
2022-06-29 10:37:36 +00:00
2011-01-26 09:35:08 +00:00
To generate a request for `GET NUMLOGINS su700`, you would populate
query and numq as follows:
2022-06-29 10:37:36 +00:00
------
size_t numq;
2011-01-26 09:35:08 +00:00
const char *query[2];
query[0] = "NUMLOGINS";
query[1] = "su700";
numq = 2;
2022-06-29 10:37:36 +00:00
------
2011-01-26 09:35:08 +00:00
All escaping of special characters and quoting of elements with spaces
is handled for you inside this function.
ANSWER FORMATTING
-----------------
2022-06-29 10:37:36 +00:00
The raw response from `upsd` to the above query would be `NUMLOGINS su700 1`.
2011-01-26 09:35:08 +00:00
Since this is split up for you, the values work out like this:
2022-06-29 10:37:36 +00:00
------
size_t numa;
2015-04-30 13:53:36 +00:00
2011-01-26 09:35:08 +00:00
numa = 3;
answer[0] = "NUMLOGINS"
answer[1] = "su700"
answer[2] = "1"
2022-06-29 10:37:36 +00:00
------
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
Notice that the value which you seek typically starts at `answer[numq]`.
2011-01-26 09:35:08 +00:00
ERROR CHECKING
--------------
2022-06-29 10:37:36 +00:00
2011-01-26 09:35:08 +00:00
This function will check your query against the response from
2022-06-29 10:37:36 +00:00
linkman:upsd[8]. For example, if you send "VAR" "su700" "ups.status",
it will expect to see those at the beginning of the response.
2011-01-26 09:35:08 +00:00
If the results from *upsd* do not pass this case-insensitive test
against your request, this function will return an error. When this
happens, linkman:upscli_upserror[3] will return 'UPSCLI_ERR_PROTOCOL'.
ANSWER ARRAY LIFETIME
---------------------
2022-06-29 10:37:36 +00:00
2011-01-26 09:35:08 +00:00
The pointers contained within the 'answer' array are only valid
until the next call to a 'upsclient' function which references them.
If you need to use data from multiple calls, you must copy it somewhere
else first.
The 'answer' array and its elements may change locations, so you
must not rely on previous addresses. You must only use the addresses
which were returned by the most recent call. You also must not attempt
to use more than 'numa' elements in 'answer'. Such behavior is
undefined, and may yield bogus data or a crash.
2022-06-29 10:37:36 +00:00
The array will be deleted after calling linkman:upscli_disconnect[3].
Any access after that point is also undefined.
2011-01-26 09:35:08 +00:00
RETURN VALUE
------------
2022-06-29 10:37:36 +00:00
2011-01-26 09:35:08 +00:00
The *upscli_get()* function returns 0 on success, or -1 if an
error occurs.
2022-06-29 10:37:36 +00:00
If *upsd* disconnects, you may need to handle or ignore `SIGPIPE`
in order to prevent your program from terminating the next time that
the library writes to the disconnected socket.
The following code in your initialization function will allow the
*upscli_get()* call to return an error in that case:
2015-04-30 13:53:36 +00:00
#include <signal.h>
...
signal (SIGPIPE, SIG_IGN);
...
2011-01-26 09:35:08 +00:00
SEE ALSO
--------
2022-06-29 10:37:36 +00:00
2011-01-26 09:35:08 +00:00
linkman:upscli_list_start[3], linkman:upscli_list_next[3],
linkman:upscli_strerror[3], linkman:upscli_upserror[3]