pigpio/pigs.c

394 lines
9.1 KiB
C
Raw Permalink 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/>
*/
/*
This version is for pigpio version 67+
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>
2015-06-01 19:32:03 +02:00
#include <ctype.h>
2013-12-12 11:27:22 +01:00
#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"
#include "pigs.h"
2013-12-12 11:27:22 +01:00
/*
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
*/
2016-07-10 22:29:14 +02:00
char command_buf[CMD_MAX_EXTENSION];
char response_buf[CMD_MAX_EXTENSION];
2014-03-13 16:50:23 +01:00
2015-06-01 19:32:03 +02:00
int printFlags = 0;
int status = PIGS_OK;
2014-04-19 13:19:29 +02:00
#define SOCKET_OPEN_FAILED -1
2015-06-01 19:32:03 +02:00
#define PRINT_HEX 1
#define PRINT_ASCII 2
void report(int err, char *fmt, ...)
2014-01-21 20:04:59 +01:00
{
char buf[128];
va_list ap;
if (err > status) status = err;
2014-01-21 20:04:59 +01:00
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
fprintf(stderr, "%s\n", buf);
fflush(stderr);
}
2015-06-01 19:32:03 +02:00
static int initOpts(int argc, char *argv[])
{
int opt, args;
opterr = 0;
2015-06-01 19:32:03 +02:00
args = 1;
while ((opt = getopt(argc, argv, "ax")) != -1)
{
switch (opt)
{
case 'a':
printFlags |= PRINT_ASCII;
args++;
break;
case 'x':
printFlags |= PRINT_HEX;
args++;
break;
default:
args++;
report(PIGS_OPTION_ERR, "ERROR: bad option %c", optopt);
}
2015-06-01 19:32:03 +02:00
}
return args;
}
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);
2014-04-19 13:19:29 +02:00
if (err) return SOCKET_OPEN_FAILED;
2013-12-12 11:32:49 +01:00
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
2014-04-19 13:19:29 +02:00
if (rp == NULL) return SOCKET_OPEN_FAILED;
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
2014-03-13 16:50:23 +01:00
void print_result(int sock, int rv, cmdCmd_t cmd)
{
2015-06-01 19:32:03 +02:00
int i, r, ch;
2014-03-13 16:50:23 +01:00
uint32_t *p;
r = cmd.res;
switch (rv)
{
case 0:
case 1:
2014-08-01 10:30:25 +02:00
if (r < 0)
{
printf("%d\n", r);
report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r));
2014-08-01 10:30:25 +02:00
}
2014-03-13 16:50:23 +01:00
break;
case 2:
2014-08-01 10:30:25 +02:00
printf("%d\n", r);
if (r < 0) report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r));
2014-03-13 16:50:23 +01:00
break;
case 3:
printf("%08X\n", cmd.res);
break;
case 4:
printf("%u\n", cmd.res);
break;
case 5:
2016-02-06 19:46:07 +01:00
printf("%s", cmdUsage);
2014-03-13 16:50:23 +01:00
break;
2016-07-10 22:29:14 +02:00
case 6: /*
2016-10-30 16:30:20 +01:00
BI2CZ CF2 FL FR I2CPK I2CRD I2CRI I2CRK
I2CZ SERR SLR SPIX SPIR
2016-07-10 22:29:14 +02:00
*/
2014-08-01 10:30:25 +02:00
printf("%d", r);
if (r < 0) report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r));
2014-08-01 10:30:25 +02:00
if (r > 0)
2014-03-13 16:50:23 +01:00
{
2015-06-01 19:32:03 +02:00
if (printFlags == PRINT_ASCII) printf(" ");
2014-08-01 10:30:25 +02:00
for (i=0; i<r; i++)
{
2015-06-01 19:32:03 +02:00
ch = response_buf[i];
if (printFlags & PRINT_HEX) printf(" %hhx", ch);
else if (printFlags & PRINT_ASCII)
{
2016-07-10 22:29:14 +02:00
if (isprint(ch) || (ch == '\n') || (ch == '\r'))
printf("%c", ch);
2015-06-01 19:32:03 +02:00
else printf("\\x%02hhx", ch);
}
else printf(" %hhu", response_buf[i]);
2014-08-01 10:30:25 +02:00
}
2014-03-13 16:50:23 +01:00
}
2014-08-01 10:30:25 +02:00
printf("\n");
2014-03-13 16:50:23 +01:00
break;
case 7: /* PROCP */
2014-08-12 19:47:26 +02:00
if (r != (4 + (4*PI_MAX_SCRIPT_PARAMS)))
{
printf("%d", r);
report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r));
2014-08-12 19:47:26 +02:00
}
else
2014-03-13 16:50:23 +01:00
{
p = (uint32_t *)response_buf;
2014-08-12 19:47:26 +02:00
printf("%d", p[0]);
2014-04-19 13:19:29 +02:00
for (i=0; i<PI_MAX_SCRIPT_PARAMS; i++)
2014-03-13 16:50:23 +01:00
{
2014-08-12 19:47:26 +02:00
printf(" %d", p[i+1]);
2014-03-13 16:50:23 +01:00
}
}
2014-08-01 10:30:25 +02:00
printf("\n");
2014-03-13 16:50:23 +01:00
break;
2016-10-30 16:30:20 +01:00
case 8: /*
BSCX
*/
if (r < 0)
{
printf("%d", r);
report(PIGS_SCRIPT_ERR, "ERROR: %s", cmdErrStr(r));
2016-10-30 16:30:20 +01:00
}
p = (uint32_t *)response_buf;
printf("%d %d", r-3, p[0]);
if (r > 4)
{
if (printFlags == PRINT_ASCII) printf(" ");
for (i=4; i<r; i++)
{
ch = response_buf[i];
if (printFlags & PRINT_HEX) printf(" %hhx", ch);
else if (printFlags & PRINT_ASCII)
{
if (isprint(ch) || (ch == '\n') || (ch == '\r'))
printf("%c", ch);
else printf("\\x%02hhx", ch);
}
else printf(" %hhu", response_buf[i]);
}
}
printf("\n");
break;
2014-03-13 16:50:23 +01:00
}
}
void get_extensions(int sock, int command, int res)
{
switch (command)
{
2015-05-17 13:36:40 +02:00
case PI_CMD_BI2CZ:
2016-10-30 16:30:20 +01:00
case PI_CMD_BSCX:
2016-09-22 15:12:26 +02:00
case PI_CMD_BSPIX:
2015-02-02 22:11:28 +01:00
case PI_CMD_CF2:
2016-07-10 22:29:14 +02:00
case PI_CMD_FL:
case PI_CMD_FR:
2014-06-12 19:31:00 +02:00
case PI_CMD_I2CPK:
case PI_CMD_I2CRD:
case PI_CMD_I2CRI:
case PI_CMD_I2CRK:
2015-05-17 13:36:40 +02:00
case PI_CMD_I2CZ:
2014-08-12 19:47:26 +02:00
case PI_CMD_PROCP:
2014-06-12 19:31:00 +02:00
case PI_CMD_SERR:
case PI_CMD_SLR:
case PI_CMD_SPIX:
case PI_CMD_SPIR:
2014-03-13 16:50:23 +01:00
if (res > 0)
{
recv(sock, response_buf, res, MSG_WAITALL);
response_buf[res] = 0;
}
break;
}
}
2013-12-12 11:32:49 +01:00
int main(int argc , char *argv[])
{
2014-03-13 16:50:23 +01:00
int sock, command;
2015-06-01 19:32:03 +02:00
int args, idx, i, pp, l, len;
2013-12-12 11:32:49 +01:00
cmdCmd_t cmd;
2018-12-08 18:04:22 +01:00
uintptr_t p[CMD_P_ARR];
2014-04-19 13:19:29 +02:00
cmdCtlParse_t ctl;
cmdScript_t s;
2014-06-12 19:31:00 +02:00
char v[CMD_MAX_EXTENSION];
2013-12-12 11:27:22 +01:00
2014-04-19 13:19:29 +02:00
sock = openSocket();
2013-12-12 11:27:22 +01:00
2015-06-01 19:32:03 +02:00
args = initOpts(argc, argv);
2014-04-19 13:19:29 +02:00
command_buf[0] = 0;
l = 0;
pp = 0;
2013-12-12 11:27:22 +01:00
2015-06-01 19:32:03 +02:00
for (i=args; i<argc; i++)
2014-04-19 13:19:29 +02:00
{
l += (strlen(argv[i]) + 1);
if (l < sizeof(command_buf))
{sprintf(command_buf+pp, "%s ", argv[i]); pp=l;}
}
2013-12-12 11:27:22 +01:00
2014-04-19 13:19:29 +02:00
if (pp) {command_buf[--pp] = 0;}
2014-01-21 20:04:59 +01:00
2014-04-19 13:19:29 +02:00
ctl.eaten = 0;
2014-01-21 20:04:59 +01:00
2014-04-19 13:19:29 +02:00
len = strlen(command_buf);
idx = 0;
2013-12-12 11:27:22 +01:00
2014-04-19 13:19:29 +02:00
while ((idx >= 0) && (ctl.eaten < len))
{
2014-06-12 19:31:00 +02:00
if ((idx=cmdParse(command_buf, p, CMD_MAX_EXTENSION, v, &ctl)) >= 0)
2013-12-12 11:32:49 +01:00
{
2014-04-19 13:19:29 +02:00
command = p[0];
2014-01-21 20:04:59 +01:00
2014-04-19 13:19:29 +02:00
if (command < PI_CMD_SCRIPT)
{
if (command == PI_CMD_HELP)
{
2016-02-06 19:46:07 +01:00
printf("%s", cmdUsage);
2014-04-19 13:19:29 +02:00
}
else if (command == PI_CMD_PARSE)
{
2014-06-12 19:31:00 +02:00
cmdParseScript(v, &s, 1);
2014-04-19 13:19:29 +02:00
if (s.par) free (s.par);
}
else
2014-01-21 20:04:59 +01:00
{
2014-03-13 16:50:23 +01:00
cmd.cmd = command;
cmd.p1 = p[1];
cmd.p2 = p[2];
2014-06-12 19:31:00 +02:00
cmd.p3 = p[3];
2013-12-12 11:32:49 +01:00
2014-04-19 13:19:29 +02:00
if (sock != SOCKET_OPEN_FAILED)
2014-03-13 16:50:23 +01:00
{
2014-04-19 13:19:29 +02:00
if (send(sock, &cmd, sizeof(cmdCmd_t), 0) ==
2014-03-13 16:50:23 +01:00
sizeof(cmdCmd_t))
{
2014-06-12 19:31:00 +02:00
if (p[3]) send(sock, v, p[3], 0); /* send extensions */
2014-01-29 00:07:05 +01:00
2014-04-19 13:19:29 +02:00
if (recv(sock, &cmd, sizeof(cmdCmd_t), MSG_WAITALL) ==
sizeof(cmdCmd_t))
{
get_extensions(sock, command, cmd.res);
print_result(sock, cmdInfo[idx].rv, cmd);
}
else report(PIGS_CONNECT_ERR, "socket receive failed");
2014-03-13 16:50:23 +01:00
}
else report(PIGS_CONNECT_ERR, "socket send failed");
2013-12-12 11:27:22 +01:00
}
else report(PIGS_CONNECT_ERR, "socket connect failed");
2013-12-12 11:27:22 +01:00
}
}
else report(PIGS_SCRIPT_ERR,
"%s only allowed within a script", cmdInfo[idx].name);
2014-04-19 13:19:29 +02:00
}
else
{
if (idx == CMD_UNKNOWN_CMD)
report(PIGS_SCRIPT_ERR,
"%s? unknown command, pigs h for help", cmdStr());
2014-04-19 13:19:29 +02:00
else
report(PIGS_SCRIPT_ERR,
"%s: bad parameter, pigs h for help", cmdStr());
2013-12-12 11:27:22 +01:00
}
}
2013-12-12 11:32:49 +01:00
2014-04-19 13:19:29 +02:00
if (sock >= 0) close(sock);
2013-12-12 11:27:22 +01:00
return status;
2013-12-12 11:27:22 +01:00
}