1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
'\" t
.\" Title: upscli_get
.\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\" Date: 08/08/2025
.\" Manual: NUT Manual
.\" Source: Network UPS Tools 2.8.4
.\" Language: English
.\"
.TH "UPSCLI_GET" "3" "08/08/2025" "Network UPS Tools 2\&.8\&.4" "NUT Manual"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
upscli_get \- Retrieve data from an UPS
.SH "SYNOPSIS"
.sp
.nf
#include <upsclient\&.h>
int upscli_get(
UPSCONN_t *ups,
size_t numq,
const char **query,
size_t *numa,
char ***answer)
.fi
.SH "DESCRIPTION"
.sp
The \fBupscli_get()\fR function takes the pointer \fIups\fR to a UPSCONN_t state structure, and the pointer \fIquery\fR to an array of \fInumq\fR query elements\&. It builds a properly\-formatted request from those elements and transmits it to \fBupsd\fR(8)\&.
.sp
Upon success, the response will be split into separate components\&. A pointer to those components will be returned in \fIanswer\fR\&. The number of usable answer components will be returned in \fInuma\fR\&.
.SH "USES"
.sp
This function implements the "GET" command in the NUT protocol\&. As a result, you can use it to request many different things from the server\&.
.sp
Some examples are:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
GET NUMLOGINS <ups>
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
GET UPSDESC <ups>
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
GET VAR <ups> <var>
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
GET TYPE <ups> <var>
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
GET DESC <ups> <var>
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
GET CMDDESC <ups> <cmd>
.RE
.SH "QUERY FORMATTING"
.sp
To generate a request for GET NUMLOGINS su700, you would populate query and numq as follows:
.sp
.if n \{\
.RS 4
.\}
.nf
size_t numq;
const char *query[2];
query[0] = "NUMLOGINS";
query[1] = "su700";
numq = 2;
.fi
.if n \{\
.RE
.\}
.sp
All escaping of special characters and quoting of elements with spaces is handled for you inside this function\&.
.SH "ANSWER FORMATTING"
.sp
The raw response from upsd to the above query would be NUMLOGINS su700 1\&.
.sp
Since this is split up for you, the values work out like this:
.sp
.if n \{\
.RS 4
.\}
.nf
size_t numa;
numa = 3;
answer[0] = "NUMLOGINS"
answer[1] = "su700"
answer[2] = "1"
.fi
.if n \{\
.RE
.\}
.sp
Notice that the value which you seek typically starts at answer[numq]\&.
.SH "ERROR CHECKING"
.sp
This function will check your query against the response from \fBupsd\fR(8) data server\&.
.sp
For example, if you send "VAR" "su700" "ups\&.status", it will expect to see those at the beginning of the response\&.
.sp
If the results from upsd do not pass this case\-insensitive test against your request, this function will return an error\&. When this happens, \fBupscli_upserror\fR(3) will return \fIUPSCLI_ERR_PROTOCOL\fR\&.
.SH "ANSWER ARRAY LIFETIME"
.sp
The pointers contained within the \fIanswer\fR array are only valid until the next call to an \fIupsclient\fR function which references them\&. If you need to use data from multiple calls, you must copy it somewhere else first\&.
.sp
The \fIanswer\fR 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 \fInuma\fR elements in \fIanswer\fR\&. Such behavior is undefined, and may yield bogus data or a crash\&.
.sp
The array will be deleted after calling \fBupscli_disconnect\fR(3)\&. Any access after that point is also undefined\&.
.SH "RETURN VALUE"
.sp
The \fBupscli_get()\fR function returns \fI0\fR on success, or \fI\-1\fR if an error occurs\&.
.sp
If \fBupsd\fR 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\&.
.sp
The following code in your initialization function will allow the \fBupscli_get()\fR call to return an error in that case:
.sp
.if n \{\
.RS 4
.\}
.nf
#include <signal\&.h>
\&.\&.\&.
signal (SIGPIPE, SIG_IGN);
\&.\&.\&.
.fi
.if n \{\
.RE
.\}
.SH "SEE ALSO"
.sp
\fBupscli_list_start\fR(3), \fBupscli_list_next\fR(3), \fBupscli_strerror\fR(3), \fBupscli_upserror\fR(3)
|