Menu

[r19]: / polycomvvxcontrol.c  Maximize  Restore  History

Download this file

270 lines (256 with data), 9.6 kB

  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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <csv.h>
#include <expat.h>
#ifdef STATIC
#define CURL_STATICLIB
#endif
#include <curl/curl.h>
#include "polycomvvx.h"
#define PROGRAM_VERSION "1.3"
#define CSV_BUFFER_SIZE 1024
//#ifdef _WIN32
//#define strcasecmp stricmp
//#endif
struct csv_callback_data {
int firstrowdone;
size_t column;
int cmd;
const char* fieldname_host;
const char* username;
const char* password;
const char* fieldname_ext;
const char* fieldname_pin;
int showstatuscode;
long fieldindex_host;
long fieldindex_ext;
long fieldindex_pin;
char* current_host;
char* current_ext;
char* current_pin;
};
void csv_callback_field (void* value, size_t valuelen, void* data)
{
struct csv_callback_data* callbackdata = (struct csv_callback_data*)data;
if (!callbackdata->firstrowdone && value) {
//determine columns with relevant fields
if (strcasecmp(value, callbackdata->fieldname_host) == 0)
callbackdata->fieldindex_host = callbackdata->column;
if (strcasecmp(value, callbackdata->fieldname_ext) == 0)
callbackdata->fieldindex_ext = callbackdata->column;
if (strcasecmp(value, callbackdata->fieldname_pin) == 0)
callbackdata->fieldindex_pin = callbackdata->column;
} else if (value) {
//get values
if (callbackdata->column == callbackdata->fieldindex_host)
callbackdata->current_host = (value ? strdup((char*)value) : NULL);
else if (callbackdata->column == callbackdata->fieldindex_ext)
callbackdata->current_ext = (value ? strdup((char*)value) : NULL);
else if (callbackdata->column == callbackdata->fieldindex_pin)
callbackdata->current_pin = (value ? strdup((char*)value) : NULL);
}
callbackdata->column++;
}
void csv_callback_eol (int c, void* data)
{
struct csv_callback_data* callbackdata = (struct csv_callback_data*)data;
if (!callbackdata->firstrowdone) {
callbackdata->firstrowdone++;
} else if (callbackdata->current_host && *(callbackdata->current_host)) {
//perform action for device listed on processed line
long statuscode = polycom_vvx_action (callbackdata->cmd, callbackdata->current_host, (callbackdata->username ? callbackdata->username : POLYCOM_VVX_DEFAULT_USER), (callbackdata->password ? callbackdata->password : POLYCOM_VVX_DEFAULT_PASS), callbackdata->current_ext, callbackdata->current_pin);
if (callbackdata->showstatuscode) {
printf("HTTP status code from %s: ", (callbackdata->current_host ? callbackdata->current_host : ""));
if (statuscode == NOCONNECTIONERROR)
printf("(no connection)\n");
else
printf("%li\n", statuscode);
}
free(callbackdata->current_host);
callbackdata->current_host = NULL;
free(callbackdata->current_ext);
callbackdata->current_ext = NULL;
free(callbackdata->current_pin);
callbackdata->current_pin = NULL;
}
callbackdata->column = 0;
}
//show command line help
void show_help()
{
curl_version_info_data* curlversion = curl_version_info(CURLVERSION_NOW);
XML_Expat_Version expatver = XML_ExpatVersionInfo();
printf(
"PolycomVVXControl - Version " PROGRAM_VERSION " - LGPL - Brecht Sanders (2015-2016)\n" \
"\tlibraries used: libcurl %s, expat %i.%i.%i, libcsv %i.%i.%i\n" \
"A command line utility for remote controlling Polycom VVX IP Phones\n" \
"Usage: PolycomVVXControl [-?|-h]\n" \
" PolycomVVXControl -a address [-u user] [-w pass] [-e extension] [-p pincode] [-s] command\n" \
" PolycomVVXControl -c file -a address [-u user] [-w pass] [-e extension] [-p pincode] [-s] command\n" \
"Parameters:\n" \
" -? | -h \tshow command line help\n" \
" -a address \tIP address or hostname of Polycom VVX IP Phone\n" \
" -u user \tusername for phone's web interface (default is " POLYCOM_VVX_DEFAULT_USER ")\n" \
" -w pass \tpassword for phone's web interface (default is " POLYCOM_VVX_DEFAULT_PASS ")\n" \
" -e extension\textension (needed for signin command)\n" \
" -p pincode \tPIN code (needed for signin command)\n" \
" -s \tshow HTTP result code\n" \
" -c file \tCSV file to process\n" \
" \tin this mode -a/-e/-p specify column names\n" \
"Commands:\n" \
" deviceinfo \tget device information\n" \
" lyncstatus \tget lync sign in status\n" \
" lyncxml \tget lync status XML\n" \
" lyncinfo \tget lync status information\n" \
" signin \tsign in with specified extension and PIN code\n" \
" signout \tsign out\n" \
" reboot \treboot\n" \
" factoryreset\treset to factory default (warning: settings will be lost)\n" \
"\n",
(curlversion ? curlversion->version : curl_version()),
expatver.major, expatver.minor, expatver.micro,
CSV_MAJOR, CSV_MINOR, CSV_RELEASE
);
}
//set variable to string parameter following flag and advance index
#define GET_ARG_STRING(var) \
if (argv[i][2]) \
param = argv[i] + 2; \
else if (i + 1 < argc && argv[i + 1]) \
param = argv[++i]; \
if (!param) \
paramerror++; \
else \
var = param;
//set boolean to true and advance index
#define GET_ARG_FLAG(var) \
if (argv[i][2]) \
paramerror++; \
else \
var = 1;
int main (int argc, char *argv[])
{
const char* host = NULL;
const char* username = NULL;
const char* password = NULL;
const char* ext = NULL;
const char* pin = NULL;
int showstatuscode = 0;
FILE* csvfile = NULL;
int cmd = CMD_NONE;
//process command line parameters
{
int i = 0;
char* param;
int paramerror = 0;
while (!paramerror && ++i < argc) {
if (argv[i][0] == '/' || argv[i][0] == '-') {
param = NULL;
switch (tolower(argv[i][1])) {
case '?' :
case 'h' :
show_help();
return 0;
case 'a' :
GET_ARG_STRING(host)
break;
case 'u' :
GET_ARG_STRING(username)
break;
case 'w' :
GET_ARG_STRING(password)
break;
case 'e' :
GET_ARG_STRING(ext)
break;
case 'p' :
GET_ARG_STRING(pin)
break;
case 's' :
GET_ARG_FLAG(showstatuscode)
break;
case 'c' :
{
char* filename = NULL;
GET_ARG_STRING(filename)
if (!filename || !*filename) {
paramerror++;
} else if (strcmp(filename, "-") == 0) {
csvfile = stdin;
} else {
if ((csvfile = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "Error reading file: %s\n", filename);
return 2;
}
}
}
break;
default :
paramerror++;
break;
}
} else if (cmd != CMD_NONE) {
paramerror++;
} else if (strcasecmp(argv[i], "deviceinfo") == 0) {
cmd = CMD_DEVICEINFO;
} else if (strcasecmp(argv[i], "lyncstatus") == 0) {
cmd = CMD_LYNCSIGNSTATUS;
} else if (strcasecmp(argv[i], "lyncxml") == 0) {
cmd = CMD_LYNCXMLSTATUS;
} else if (strcasecmp(argv[i], "lyncinfo") == 0) {
cmd = CMD_LYNCINFO;
} else if (strcasecmp(argv[i], "signin") == 0) {
cmd = CMD_SIGNIN;
} else if (strcasecmp(argv[i], "signout") == 0) {
cmd = CMD_SIGNOUT;
} else if (strcasecmp(argv[i], "reboot") == 0) {
cmd = CMD_REBOOT;
} else if (strcasecmp(argv[i], "factoryreset") == 0) {
cmd = CMD_FACTORYRESET;
}
}
if (paramerror || argc <= 1 || cmd == CMD_NONE || !host || (cmd == CMD_SIGNIN && (!ext || !pin))) {
if (paramerror)
fprintf(stderr, "Invalid command line parameters\n");
show_help();
return 1;
}
}
//perform requested action
if (!csvfile) {
//perform requested action for single device
long statuscode;
if (!username)
username = POLYCOM_VVX_DEFAULT_USER;
if (!password)
password = POLYCOM_VVX_DEFAULT_PASS;
polycom_vvx_action_common_header(cmd);
statuscode = polycom_vvx_action(cmd, host, username, password, ext, pin);
if (showstatuscode) {
printf("HTTP status code: ");
if (statuscode == NOCONNECTIONERROR)
printf("(no connection)\n");
else
printf("%li\n", statuscode);
}
return (statuscode >= 200 && statuscode < 300 ? 0 : statuscode);
} else {
//perform requested action for device listed in CSV file
struct csv_parser p;
size_t bytes_read;
char buf[CSV_BUFFER_SIZE];
struct csv_callback_data csvdata = {0, 0, cmd, host, username, password, ext, pin, showstatuscode, -1, -1, -1, NULL, NULL, NULL};
polycom_vvx_action_common_header(cmd);
csv_init(&p, CSV_APPEND_NULL | CSV_EMPTY_IS_NULL);
while ((bytes_read = fread(buf, 1, CSV_BUFFER_SIZE, csvfile)) > 0) {
if (csv_parse(&p, buf, bytes_read, csv_callback_field, csv_callback_eol, &csvdata) != bytes_read) {
fprintf(stderr, "Error while parsing file: %s\n", csv_strerror(csv_error(&p)));
}
}
csv_fini(&p, csv_callback_field, csv_callback_eol, &csvdata);
fclose(csvfile);
return 0;
}
}
MongoDB Logo MongoDB