106 lines
2.9 KiB
C
106 lines
2.9 KiB
C
|
/***************************************************************************
|
||
|
* Copyright (C) 03/2009 by Olaf Rempel *
|
||
|
* razzor@kopf-tisch.de *
|
||
|
* *
|
||
|
* This program is free software; you can redistribute it and/or modify *
|
||
|
* it under the terms of the GNU General Public License as published by *
|
||
|
* the Free Software Foundation; version 2 of the License *
|
||
|
* *
|
||
|
* This program is distributed in the hope that it will be useful, *
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||
|
* GNU General Public License for more details. *
|
||
|
* *
|
||
|
* You should have received a copy of the GNU General Public License *
|
||
|
* along with this program; if not, write to the *
|
||
|
* Free Software Foundation, Inc., *
|
||
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||
|
***************************************************************************/
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#include <getopt.h>
|
||
|
|
||
|
#include "logging.h"
|
||
|
#include "unixsocket.h"
|
||
|
|
||
|
#define DEFAULT_SOCKET "alix-usvd.sock"
|
||
|
|
||
|
static struct option opts[] = {
|
||
|
{"command", 1, 0, 'c'},
|
||
|
{"help", 0, 0, 'h'},
|
||
|
{"socket", 1, 0, 's'},
|
||
|
{0, 0, 0, 0}
|
||
|
};
|
||
|
|
||
|
int main(int argc, char *argv[])
|
||
|
{
|
||
|
char *socket_path = DEFAULT_SOCKET;
|
||
|
char *cmd = NULL;
|
||
|
|
||
|
int code, arg = 0;
|
||
|
do {
|
||
|
code = getopt_long(argc, argv, "c:hs:", opts, &arg);
|
||
|
switch (code) {
|
||
|
case 'c': /* command */
|
||
|
if (strncasecmp(optarg, "IDLE", 4) == 0)
|
||
|
cmd = optarg;
|
||
|
|
||
|
else if (strncasecmp(optarg, "TEST", 4) == 0)
|
||
|
cmd = optarg;
|
||
|
|
||
|
else if (strncasecmp(optarg, "CHARGE", 6) == 0)
|
||
|
cmd = optarg;
|
||
|
|
||
|
else if (strncasecmp(optarg, "POWEROFF", 8) == 0)
|
||
|
cmd = optarg;
|
||
|
|
||
|
else if (strncasecmp(optarg, "STATUS", 6) == 0)
|
||
|
cmd = optarg;
|
||
|
|
||
|
break;
|
||
|
|
||
|
case 's': /* socket */
|
||
|
socket_path = optarg;
|
||
|
break;
|
||
|
|
||
|
case 'h': /* help */
|
||
|
printf("Usage: alix-usv [-s socket] -c <status|idle|charge|test|poweroff>\n\n");
|
||
|
exit(0);
|
||
|
break;
|
||
|
|
||
|
case '?': /* error */
|
||
|
exit(1);
|
||
|
break;
|
||
|
|
||
|
default: /* unknown / all options parsed */
|
||
|
break;
|
||
|
}
|
||
|
} while (code != -1);
|
||
|
|
||
|
if (cmd == NULL) {
|
||
|
log_print(LOG_ERROR, "invalid command: %s", cmd);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int sock = unix_connect(socket_path);
|
||
|
if (sock < 0)
|
||
|
return -1;
|
||
|
|
||
|
write(sock, cmd, strlen(cmd));
|
||
|
|
||
|
char buf[64];
|
||
|
int len = read(sock, buf, sizeof(buf));
|
||
|
if (len <= 0) {
|
||
|
log_print(LOG_ERROR, "no answer from server");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
log_print(LOG_INFO, "%s", buf);
|
||
|
|
||
|
close(sock);
|
||
|
return 0;
|
||
|
}
|