pigpio/pigs.c

217 lines
5.3 KiB
C
Raw Normal View History

2013-12-12 11:27:22 +01:00
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
/*
2014-01-29 00:07:05 +01:00
This version is for pigpio version 12+
2013-12-12 11:27:22 +01:00
*/
#include <stdio.h>
#include <stdlib.h>
2014-01-21 20:04:59 +01:00
#include <stdarg.h>
2013-12-12 11:27:22 +01:00
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
2013-12-12 11:32:49 +01:00
#include <sys/types.h>
#include <netdb.h>
2013-12-12 11:27:22 +01:00
#include <arpa/inet.h>
#include "pigpio.h"
#include "command.h"
/*
2014-01-21 20:04:59 +01:00
This program provides a socket interface to some of
the commands available from pigpio.
2013-12-12 11:27:22 +01:00
*/
2014-01-21 20:04:59 +01:00
void fatal(char *fmt, ...)
{
char buf[128];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
fprintf(stderr, "%s\n", buf);
fflush(stderr);
exit(EXIT_FAILURE);
}
2013-12-12 11:32:49 +01:00
static int openSocket(void)
2013-12-12 11:27:22 +01:00
{
2013-12-12 11:32:49 +01:00
int sock, err;
struct addrinfo hints, *res, *rp;
const char *addrStr, *portStr;
portStr = getenv(PI_ENVPORT);
if (!portStr) portStr = PI_DEFAULT_SOCKET_PORT_STR;
addrStr = getenv(PI_ENVADDR);
if (!addrStr) addrStr = PI_DEFAULT_SOCKET_ADDR_STR;
memset (&hints, 0, sizeof (hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
err = getaddrinfo(addrStr, portStr, &hints, &res);
if (err) return -1;
for (rp=res; rp!=NULL; rp=rp->ai_next)
2013-12-12 11:27:22 +01:00
{
2013-12-12 11:32:49 +01:00
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock == -1) continue;
if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) break;
}
freeaddrinfo(res);
2013-12-12 11:27:22 +01:00
2013-12-12 11:32:49 +01:00
if (rp == NULL) return -1;
2013-12-12 11:27:22 +01:00
2013-12-12 11:32:49 +01:00
return sock;
}
2013-12-12 11:27:22 +01:00
2013-12-12 11:32:49 +01:00
int main(int argc , char *argv[])
{
2014-01-21 20:04:59 +01:00
int sock, r, idx, i;
2013-12-12 11:32:49 +01:00
cmdCmd_t cmd;
2014-01-21 20:04:59 +01:00
gpioExtent_t ext[3];
char buf[1024];
2013-12-12 11:27:22 +01:00
2013-12-12 11:32:49 +01:00
sock = openSocket();
2013-12-12 11:27:22 +01:00
2013-12-12 11:32:49 +01:00
if (sock != -1)
{
switch(argc)
2013-12-12 11:27:22 +01:00
{
2013-12-12 11:32:49 +01:00
case 1:
exit(0);
2013-12-12 11:27:22 +01:00
2013-12-12 11:32:49 +01:00
case 2:
sprintf(buf, "%10s", argv[1]);
break;
2013-12-12 11:27:22 +01:00
2013-12-12 11:32:49 +01:00
case 3:
sprintf(buf, "%10s %10s", argv[1], argv[2]);
break;
2013-12-12 11:27:22 +01:00
2013-12-12 11:32:49 +01:00
case 4:
sprintf(buf, "%10s %10s %10s", argv[1], argv[2], argv[3]);
break;
2013-12-12 11:27:22 +01:00
2014-01-21 20:04:59 +01:00
case 5:
sprintf(buf, "%10s %10s %10s %10s",
argv[1], argv[2], argv[3], argv[4]);
break;
case 6:
sprintf(buf, "%10s %10s %10s %10s %10s",
argv[1], argv[2], argv[3], argv[4], argv[5]);
break;
2013-12-12 11:32:49 +01:00
default:
2014-01-21 20:04:59 +01:00
fatal("what? 'pigs h' for help");
2013-12-12 11:32:49 +01:00
}
2013-12-12 11:27:22 +01:00
2014-01-21 20:04:59 +01:00
if ((idx=cmdParse(buf, &cmd, argc, argv, ext)) >= 0)
2013-12-12 11:32:49 +01:00
{
if (send(sock, &cmd, sizeof(cmdCmd_t), 0) == sizeof(cmdCmd_t))
2013-12-12 11:27:22 +01:00
{
2014-01-21 20:04:59 +01:00
/* send extensions */
for (i=0; i<cmdInfo[idx].ext; i++)
{
2014-01-29 00:07:05 +01:00
send(sock, ext[i].ptr, ext[i].size, 0);
2014-01-21 20:04:59 +01:00
}
2014-01-29 00:07:05 +01:00
if (recv(sock, &cmd, sizeof(cmdCmd_t), MSG_WAITALL) ==
sizeof(cmdCmd_t))
2013-12-12 11:27:22 +01:00
{
2013-12-12 11:32:49 +01:00
switch (cmdInfo[idx].rv)
2013-12-12 11:27:22 +01:00
{
2013-12-12 11:32:49 +01:00
case 0:
r = cmd.res;
2014-01-21 20:04:59 +01:00
if (r < 0) fatal("ERROR: %s", cmdErrStr(r));
2013-12-12 11:32:49 +01:00
break;
case 1:
r = cmd.res;
2014-01-21 20:04:59 +01:00
if (r < 0) fatal("ERROR: %s", cmdErrStr(r));
2013-12-12 11:32:49 +01:00
break;
case 2:
r = cmd.res;
2014-01-21 20:04:59 +01:00
if (r < 0) fatal("ERROR: %s", cmdErrStr(r));
2013-12-12 11:32:49 +01:00
else printf("%d\n", r);
break;
case 3:
printf("%08X\n", cmd.res);
break;
case 4:
printf("%u\n", cmd.res);
break;
case 5:
printf(cmdUsage);
break;
2014-01-29 00:07:05 +01:00
case 6:
r = cmd.res;
if (r < 0) fatal("ERROR: %s", cmdErrStr(r));
else if (r > 0)
{
recv(sock, &buf, r, MSG_WAITALL);
buf[r] = 0;
printf("%s", buf);
}
break;
2013-12-12 11:27:22 +01:00
}
}
2014-01-21 20:04:59 +01:00
else fatal("recv failed, %m");
2013-12-12 11:27:22 +01:00
}
2014-01-21 20:04:59 +01:00
else fatal("send failed, %m");
2013-12-12 11:27:22 +01:00
}
2014-01-21 20:04:59 +01:00
else fatal("what? 'pigs h' for help");
2013-12-12 11:27:22 +01:00
}
2014-01-21 20:04:59 +01:00
else fatal("connect failed, %m");
2013-12-12 11:32:49 +01:00
close(sock);
2013-12-12 11:27:22 +01:00
return 0;
}