41 #include <sys/types.h> 44 #include <sys/socket.h> 45 #include <netinet/tcp.h> 46 #include <sys/select.h> 48 #include <arpa/inet.h> 55 #define PISCOPE_MAX_REPORTS_PER_READ 4096 57 #define STACK_SIZE (256*1024) 107 if (send(fd, &cmd,
sizeof(cmd), 0) !=
sizeof(cmd))
113 if (recv(fd, &cmd,
sizeof(cmd), MSG_WAITALL) !=
sizeof(cmd))
125 (
int fd,
int command,
int p1,
int p2,
int p3,
138 if (send(fd, &cmd,
sizeof(cmd), 0) !=
sizeof(cmd))
144 for (i=0; i<extents; i++)
146 if (send(fd, ext[i].ptr, ext[i].size, 0) != ext[i].size)
153 if (recv(fd, &cmd,
sizeof(cmd), MSG_WAITALL) !=
sizeof(cmd))
167 struct addrinfo hints, *res, *rp;
168 const char *addrStr, *portStr;
174 if ((!addrStr) || (!strlen(addrStr)))
185 if ((!portStr) || (!strlen(portStr)))
192 memset (&hints, 0,
sizeof (hints));
194 hints.ai_family = PF_UNSPEC;
195 hints.ai_socktype = SOCK_STREAM;
196 hints.ai_flags |= AI_CANONNAME;
198 err = getaddrinfo (addrStr, portStr, &hints, &res);
202 for (rp=res; rp!=NULL; rp=rp->ai_next)
204 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
206 if (sock == -1)
continue;
210 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (
char*)&opt,
sizeof(
int));
212 if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
break;
243 if (changed & (1<<(p->
gpio)))
245 if ((r->
level) & (1<<(p->
gpio))) l = 1;
else l = 0;
281 bytes = read(
gPigNotify, (
char*)&gReport+got,
sizeof(gReport)-got);
283 if (bytes > 0) got += bytes;
299 if (got && r) gReport[0] = gReport[
r];
313 bits |= (1<<(p->
gpio));
324 static void _wfe(
unsigned user_gpio,
unsigned level, uint32_t tick,
void *
user)
334 if ((user_gpio >=0) && (user_gpio < 32) && (edge >=0) && (edge <= 2) && f)
342 if ((p->
gpio == user_gpio) && (p->
edge == edge) && (p->
f == f))
353 if (!gCallBackFirst) gCallBackFirst = p;
385 gettimeofday(&tv, 0);
387 t = (double)tv.tv_sec + ((
double)tv.tv_usec / 1E6);
394 struct timespec ts, rem;
399 ts.tv_nsec = (seconds-(double)ts.tv_sec) * 1E9;
401 while (clock_nanosleep(CLOCK_REALTIME, 0, &ts, &rem))
404 ts.tv_sec = rem.tv_sec;
405 ts.tv_nsec = rem.tv_nsec;
412 if (errnum > -1000)
return cmdErrStr(errnum);
418 return "failed to send to pigpiod";
420 return "failed to receive from pigpiod";
422 return "failed to find address of pigpiod";
424 return "failed to connect to pigpiod";
426 return "failed to create socket";
428 return "failed to open noib";
430 return "identical callback exists";
432 return "failed to malloc";
434 return "bad callback parameter";
436 return "failed to create notification thread";
438 return "callback not found";
440 return "unknown error";
453 pthread_attr_t pthAttr;
455 pth = malloc(
sizeof(pthread_t));
459 if (pthread_attr_init(&pthAttr))
461 perror(
"pthread_attr_init failed");
466 if (pthread_attr_setstacksize(&pthAttr,
STACK_SIZE))
468 perror(
"pthread_attr_setstacksize failed");
473 if (pthread_create(pth, &pthAttr, thread_func, arg))
475 perror(
"pthread_create socket failed");
487 pthread_cancel(*pth);
488 pthread_join(*pth, NULL);
495 if ((!addrStr) || (strlen(addrStr) == 0))
497 addrStr =
"localhost";
658 ext[0].
size =
sizeof(dutycycle);
659 ext[0].
ptr = &dutycycle;
692 if (!numPulses)
return 0;
702 unsigned user_gpio,
unsigned baud, uint32_t databits,
703 uint32_t stophalfbits, uint32_t offset,
unsigned numChar,
char *str)
719 if (!numChar)
return 0;
721 memcpy(buf, &databits, 4);
722 memcpy(buf+4, &stophalfbits, 4);
723 memcpy(buf+8, &offset, 4);
725 ext[0].
size =
sizeof(buf);
728 ext[1].
size = numChar;
732 user_gpio, baud, numChar+
sizeof(buf), 2, ext, 1);
765 ext[0].
size = bufSize;
805 int gpio_trigger(
unsigned user_gpio,
unsigned pulseLen, uint32_t level)
817 ext[0].
size =
sizeof(uint32_t);
839 ext[0].
size =
sizeof(uint32_t);
840 ext[0].
ptr = &active;
859 len = strlen(script);
869 int run_script(
unsigned script_id,
unsigned numPar, uint32_t *param)
881 ext[0].
size = 4 * numPar;
890 uint8_t scratch[4096];
891 int remaining, fetch, count;
893 if (sent < bufsize) count = sent;
else count = bufsize;
895 if (count) recv(
gPigCommand, buf, count, MSG_WAITALL);
897 remaining = sent - count;
902 if (fetch >
sizeof(scratch)) fetch =
sizeof(scratch);
921 if (param) memcpy(param, p+1,
sizeof(p)-4);
947 ext[0].
size =
sizeof(uint32_t);
948 ext[0].
ptr = &bbBits;
962 bytes =
recvMax(buf, bufSize, bytes);
976 int i2c_open(
unsigned i2c_bus,
unsigned i2c_addr, uint32_t i2c_flags)
988 ext[0].
size =
sizeof(uint32_t);
989 ext[0].
ptr = &i2c_flags;
1019 ext[0].
size =
sizeof(uint32_t);
1038 ext[0].
size =
sizeof(uint32_t);
1063 ext[0].
size =
sizeof(uint32_t);
1071 unsigned handle,
unsigned reg,
char *buf,
unsigned count)
1083 ext[0].
size = count;
1098 bytes =
recvMax(buf, 32, bytes);
1107 unsigned handle,
unsigned reg,
char *buf,
unsigned count)
1120 ext[0].
size = count;
1128 bytes =
recvMax(buf, 32, bytes);
1137 unsigned handle,
unsigned reg,
char *buf, uint32_t count)
1150 ext[0].
size =
sizeof(uint32_t);
1151 ext[0].
ptr = &count;
1158 bytes =
recvMax(buf, count, bytes);
1168 unsigned handle,
unsigned reg,
char *buf,
unsigned count)
1180 ext[0].
size = count;
1195 bytes =
recvMax(buf, count, bytes);
1215 ext[0].
size = count;
1240 ext[0].
size = inLen;
1248 bytes =
recvMax(outBuf, outLen, bytes);
1268 ext[0].
size =
sizeof(uint32_t);
1296 ext[0].
size = inLen;
1304 bytes =
recvMax(outBuf, outLen, bytes);
1312 int spi_open(
unsigned channel,
unsigned speed, uint32_t flags)
1324 ext[0].
size =
sizeof(uint32_t);
1325 ext[0].
ptr = &flags;
1343 bytes =
recvMax(buf, count, bytes);
1363 ext[0].
size = count;
1383 ext[0].
size = count;
1391 bytes =
recvMax(rxBuf, count, bytes);
1442 ext[0].
size = count;
1458 bytes =
recvMax(buf, count, bytes);
1469 int custom_1(
unsigned arg1,
unsigned arg2,
char *argx,
unsigned count)
1481 ext[0].
size = count;
1489 int custom_2(
unsigned arg1,
char *argx,
unsigned count,
1490 char *retBuf, uint32_t retMax)
1503 ext[0].
size = count;
1511 bytes =
recvMax(retBuf, retMax, bytes);
1524 {
return intCallback(user_gpio, edge, f, user, 1);}
1537 else gCallBackFirst = p->
next;
1540 else gCallBackLast = p->
prev;
1559 if (timeout <= 0.0)
return 0;
int callback_ex(unsigned user_gpio, unsigned edge, CBFuncEx_t f, void *user)
#define PIGPIOD_IF_VERSION
int set_noise_filter(unsigned user_gpio, unsigned steady, unsigned active)
int i2c_write_byte_data(unsigned handle, unsigned reg, uint32_t val)
int serial_write(unsigned handle, char *buf, unsigned count)
int bb_serial_read(unsigned user_gpio, void *buf, size_t bufSize)
int set_bank_1(uint32_t levels)
int bb_i2c_close(unsigned SDA)
int gpio_read(unsigned gpio)
static uint32_t gNotifyBits
int i2c_read_i2c_block_data(unsigned handle, unsigned reg, char *buf, uint32_t count)
int spi_xfer(unsigned handle, char *txBuf, char *rxBuf, unsigned count)
int set_bank_2(uint32_t levels)
static pthread_mutex_t command_mutex
int i2c_write_device(unsigned handle, char *buf, unsigned count)
int i2c_read_byte_data(unsigned handle, unsigned reg)
void(* CBFunc_t)(unsigned user_gpio, unsigned level, uint32_t tick)
int bb_serial_read_open(unsigned user_gpio, unsigned baud, uint32_t bbBits)
int i2c_zip(unsigned handle, char *inBuf, unsigned inLen, char *outBuf, unsigned outLen)
int wave_get_pulses(void)
int wave_send_repeat(unsigned wave_id)
int wave_add_serial(unsigned user_gpio, unsigned baud, uint32_t databits, uint32_t stophalfbits, uint32_t offset, unsigned numChar, char *str)
static gpioReport_t gReport[PISCOPE_MAX_REPORTS_PER_READ]
int script_status(unsigned script_id, uint32_t *param)
static void dispatch_notification(gpioReport_t *r)
int bb_i2c_zip(unsigned SDA, char *inBuf, unsigned inLen, char *outBuf, unsigned outLen)
static uint32_t gLastLevel
int i2c_open(unsigned i2c_bus, unsigned i2c_addr, uint32_t i2c_flags)
int bb_i2c_open(unsigned SDA, unsigned SCL, unsigned baud)
uint32_t read_bank_2(void)
int store_script(char *script)
int spi_open(unsigned channel, unsigned speed, uint32_t flags)
int serial_data_available(unsigned handle)
uint32_t get_current_tick(void)
int set_PWM_frequency(unsigned user_gpio, unsigned frequency)
int recvMax(void *buf, int bufsize, int sent)
static int pigpio_command(int fd, int command, int p1, int p2, int rl)
int i2c_close(unsigned handle)
int wave_get_max_cbs(void)
int bb_serial_invert(unsigned user_gpio, unsigned invert)
void(* CBFuncEx_t)(unsigned user_gpio, unsigned level, uint32_t tick, void *user)
int i2c_read_device(unsigned handle, char *buf, unsigned count)
int i2c_write_quick(unsigned handle, unsigned bit)
int spi_read(unsigned handle, char *buf, unsigned count)
int serial_open(char *dev, unsigned baud, unsigned flags)
int get_servo_pulsewidth(unsigned user_gpio)
static void * pthNotifyThread(void *x)
int wave_get_max_pulses(void)
#define PI_MAX_SCRIPT_PARAMS
int get_PWM_range(unsigned user_gpio)
int i2c_write_word_data(unsigned handle, unsigned reg, uint32_t val)
int serial_read_byte(unsigned handle)
int wave_get_high_cbs(void)
int spi_write(unsigned handle, char *buf, unsigned count)
int get_PWM_real_range(unsigned user_gpio)
int set_mode(unsigned gpio, unsigned mode)
int i2c_write_byte(unsigned handle, unsigned val)
int delete_script(unsigned script_id)
char * pigpio_error(int errnum)
void time_sleep(double seconds)
int run_script(unsigned script_id, unsigned numPar, uint32_t *param)
int pigpio_start(char *addrStr, char *portStr)
callback_t * gCallBackLast
int set_PWM_dutycycle(unsigned user_gpio, unsigned dutycycle)
int wave_add_generic(unsigned numPulses, gpioPulse_t *pulses)
int set_servo_pulsewidth(unsigned user_gpio, unsigned pulsewidth)
uint32_t read_bank_1(void)
int gpio_write(unsigned gpio, unsigned level)
int gpio_trigger(unsigned user_gpio, unsigned pulseLen, uint32_t level)
static void findNotifyBits(void)
uint32_t get_hardware_revision(void)
void *( gpioThreadFunc_t)(void *)
static void _wfe(unsigned user_gpio, unsigned level, uint32_t tick, void *user)
int wave_get_max_micros(void)
int i2c_process_call(unsigned handle, unsigned reg, uint32_t val)
int hardware_PWM(unsigned gpio, unsigned frequency, uint32_t dutycycle)
int wave_get_high_pulses(void)
int get_mode(unsigned gpio)
int wave_chain(char *buf, unsigned bufSize)
uint32_t get_pigpio_version(void)
int serial_close(unsigned handle)
int i2c_write_i2c_block_data(unsigned handle, unsigned reg, char *buf, unsigned count)
static int intCallback(unsigned user_gpio, unsigned edge, void *f, void *user, int ex)
static pthread_t * pthNotify
int i2c_read_block_data(unsigned handle, unsigned reg, char *buf)
int serial_read(unsigned handle, char *buf, unsigned count)
int custom_1(unsigned arg1, unsigned arg2, char *argx, unsigned count)
int i2c_write_block_data(unsigned handle, unsigned reg, char *buf, unsigned count)
int wait_for_edge(unsigned user_gpio, unsigned edge, double timeout)
int clear_bank_1(uint32_t levels)
int set_glitch_filter(unsigned user_gpio, unsigned steady)
int callback(unsigned user_gpio, unsigned edge, CBFunc_t f)
unsigned pigpiod_if_version(void)
void stop_thread(pthread_t *pth)
int i2c_block_process_call(unsigned handle, unsigned reg, char *buf, unsigned count)
int get_PWM_frequency(unsigned user_gpio)
int bb_serial_read_close(unsigned user_gpio)
int wave_send_once(unsigned wave_id)
pthread_t * start_thread(gpioThreadFunc_t thread_func, void *arg)
int i2c_read_byte(unsigned handle)
int set_PWM_range(unsigned user_gpio, unsigned range)
int spi_close(unsigned handle)
int wave_get_micros(void)
int serial_write_byte(unsigned handle, unsigned val)
#define PI_DEFAULT_SOCKET_ADDR_STR
#define PI_DEFAULT_SOCKET_PORT_STR
callback_t * gCallBackFirst
int set_pull_up_down(unsigned gpio, unsigned pud)
int clear_bank_2(uint32_t levels)
int wave_get_high_micros(void)
int custom_2(unsigned arg1, char *argx, unsigned count, char *retBuf, uint32_t retMax)
int hardware_clock(unsigned gpio, unsigned frequency)
static int pigpioOpenSocket(char *addr, char *port)
int notify_begin(unsigned handle, uint32_t bits)
static int pigpio_command_ext(int fd, int command, int p1, int p2, int p3, int extents, gpioExtent_t *ext, int rl)
int wave_delete(unsigned wave_id)
char * cmdErrStr(int error)
int callback_cancel(unsigned id)
int notify_close(unsigned handle)
int get_PWM_dutycycle(unsigned user_gpio)
int set_watchdog(unsigned user_gpio, unsigned timeout)
int notify_pause(unsigned handle)
int i2c_read_word_data(unsigned handle, unsigned reg)
int stop_script(unsigned script_id)
#define PISCOPE_MAX_REPORTS_PER_READ