32 #include <sys/types.h> 45 #include <sys/socket.h> 81 vsnprintf(buf,
sizeof(buf), fmt, ap);
84 fprintf(stderr,
"%s\n", buf);
93 fprintf(stderr,
"\n" \
95 "Usage: sudo pigpiod [OPTION] ...\n" \
96 " -a value, DMA mode, 0=AUTO, 1=PMAP, 2=MBOX, default AUTO\n" \
97 " -b value, sample buffer size in ms, default 120\n" \
98 " -c value, library internal settings, default 0\n" \
99 " -d value, primary DMA channel, 0-14, default 14\n" \
100 " -e value, secondary DMA channel, 0-14, default 6\n" \
101 " -f, disable fifo interface, default enabled\n" \
102 " -g, run in foreground (do not fork), default disabled\n" \
103 " -k, disable socket interface, default enabled\n" \
104 " -l, localhost socket only default local+remote\n" \
105 " -m, disable alerts default enabled\n" \
106 " -n IP addr, allow address, name or dotted, default allow all\n" \
107 " -p value, socket port, 1024-32000, default 8888\n" \
108 " -s value, sample rate, 1, 2, 4, 5, 8, or 10, default 5\n" \
109 " -t value, clock peripheral, 0=PWM 1=PCM, default PCM\n" \
110 " -v, -V, display pigpio version and exit\n" \
111 " -x mask, GPIO which may be updated, default board GPIO\n" \
113 "sudo pigpiod -s 2 -b 200 -f\n" \
114 " Set a sample rate of 2 microseconds with a 200 millisecond\n" \
115 " buffer. Disable the fifo interface.\n" \
119 static uint64_t
getNum(
char *str,
int *err)
125 val = strtoll(str, &endptr, 0);
126 if (*endptr) {*err = 1; val = -1;}
133 struct addrinfo hints, *res;
134 struct sockaddr_in *sin;
142 memset (&hints, 0,
sizeof (hints));
144 hints.ai_family = AF_INET;
145 hints.ai_socktype = SOCK_STREAM;
146 hints.ai_flags |= AI_CANONNAME;
148 err = getaddrinfo(addrStr, portStr, &hints, &res);
152 sin = (
struct sockaddr_in *)res->ai_addr;
153 addr = sin->sin_addr.s_addr;
166 while ((opt = getopt(argc, argv,
"a:b:c:d:e:fgkln:mp:s:t:x:vV")) != -1)
174 else fatal(
"invalid -a option (%d)", i);
181 else fatal(
"invalid -b option (%d)", i);
188 else fatal(
"invalid -c option (%x)", i);
195 else fatal(
"invalid -d option (%d)", i);
202 else fatal(
"invalid -e option (%d)", i);
229 else fatal(
"invalid -n option (%s)", optarg);
236 else fatal(
"invalid -p option (%d)", i);
254 fatal(
"invalid -s option (%d)", i);
263 else fatal(
"invalid -t option (%d)", i);
273 mask =
getNum(optarg, &err);
279 else fatal(
"invalid -x option (%s)", optarg);
295 fprintf(
errFifo,
"SIGHUP/SIGTERM received\n");
307 int main(
int argc,
char **argv)
321 if (pid < 0) { exit(EXIT_FAILURE); }
325 if (pid > 0) { exit(EXIT_SUCCESS); }
337 if (setsid() < 0)
fatal(
"setsid failed (%m)");
341 if ((chdir(
"/")) < 0)
fatal(
"chdir failed (%m)");
388 flags = fcntl(fileno(
errFifo), F_GETFL, 0);
389 fcntl(fileno(
errFifo), F_SETFL, flags | O_NONBLOCK);
409 fprintf(stderr,
"freopen failed (%m)");
int gpioSetSignalFunc(unsigned signum, gpioSignalFunc_t f)
#define MAX_CONNECT_ADDRESSES
static uint32_t sockNetAddr[MAX_CONNECT_ADDRESSES]
static uint64_t updateMask
#define PI_DEFAULT_CLK_PERIPHERAL
int gpioCfgDMAchannels(unsigned primaryChannel, unsigned secondaryChannel)
static unsigned clockMicros
static uint64_t getNum(char *str, int *err)
#define PI_LOCALHOST_SOCK_IF
#define PI_DEFAULT_SOCKET_PORT
void fatal(char *fmt,...)
void terminate(int signum)
#define PI_DEFAULT_MEM_ALLOC_MODE
#define PI_DEFAULT_CFG_INTERNALS
int gpioCfgPermissions(uint64_t updateMask)
int gpioCfgSetInternals(uint32_t cfgVal)
static unsigned bufferSizeMilliseconds
static void initOpts(int argc, char *argv[])
#define PI_MAX_DMA_CHANNEL
#define PI_BUF_MILLIS_MIN
static unsigned clockPeripheral
static unsigned DMAprimaryChannel
int gpioCfgSocketPort(unsigned port)
#define PI_DISABLE_FIFO_IF
#define PI_BUF_MILLIS_MAX
#define PI_DEFAULT_DMA_PRIMARY_CHANNEL
static unsigned DMAsecondaryChannel
#define PI_DISABLE_SOCK_IF
#define PI_DEFAULT_FOREGROUND
static uint32_t checkAddr(char *addrStr)
int gpioCfgClock(unsigned micros, unsigned peripheral, unsigned source)
static unsigned socketPort
#define PI_MAX_SOCKET_PORT
#define PI_MIN_DMA_CHANNEL
static unsigned memAllocMode
int gpioCfgMemAlloc(unsigned memAllocMode)
int gpioCfgInterfaces(unsigned ifFlags)
#define PI_DEFAULT_DMA_SECONDARY_CHANNEL
static int numSockNetAddr
#define PI_DEFAULT_IF_FLAGS
#define PI_DEFAULT_SOCKET_PORT_STR
static uint32_t cfgInternals
#define PI_MEM_ALLOC_AUTO
int gpioCfgNetAddr(int numSockAddr, uint32_t *sockAddr)
#define PI_DEFAULT_CLK_MICROS
int gpioCfgBufferSize(unsigned millis)
int main(int argc, char **argv)
#define PI_MIN_SOCKET_PORT
#define PI_CFG_ILLEGAL_VAL
#define PI_DEFAULT_BUFFER_MILLIS
#define PI_MEM_ALLOC_MAILBOX