int default_send(struct ir_remote *remote, struct ir_ncode *code) { /* things are easy, because we only support one mode */ if (drv.send_mode != LIRC_MODE_PULSE) return (0); if (drv.features & LIRC_CAN_SET_SEND_CARRIER) { unsigned int freq; freq = remote->freq == 0 ? DEFAULT_FREQ : remote->freq; if (default_ioctl(LIRC_SET_SEND_CARRIER, &freq) == -1) { logprintf(LIRC_ERROR, "could not set modulation frequency"); logperror(LIRC_ERROR, NULL); return (0); } } if (drv.features & LIRC_CAN_SET_SEND_DUTY_CYCLE) { unsigned int duty_cycle; duty_cycle = remote->duty_cycle == 0 ? 50 : remote->duty_cycle; if (default_ioctl(LIRC_SET_SEND_DUTY_CYCLE, &duty_cycle) == -1) { logprintf(LIRC_ERROR, "could not set duty cycle"); logperror(LIRC_ERROR, NULL); return (0); } } if (!init_send(remote, code)) return (0); if (write_send_buffer(drv.fd) == -1) { logprintf(LIRC_ERROR, "write failed"); logperror(LIRC_ERROR, NULL); return (0); } return (1); }
/* initialize driver -- returns 1 on success, 0 on error */ static int dfc_init() { struct usb_device *usb_dev; int pipe_fd[2] = { -1, -1 }; LOGPRINTF(1, "initializing USB receiver"); init_rec_buffer(); usb_dev = find_usb_device(); if (usb_dev == NULL) { logprintf(LOG_ERR, "couldn't find a compatible USB device"); return 0; } /* A separate process will be forked to read data from the USB * receiver and write it to a pipe. hw.fd is set to the readable * end of this pipe. */ if (pipe(pipe_fd) != 0) { logperror(LOG_ERR, "couldn't open pipe"); return 0; } hw.fd = pipe_fd[0]; dev_handle = usb_open(usb_dev); if (dev_handle == NULL) { logperror(LOG_ERR, "couldn't open USB receiver"); goto fail; } child = fork(); if (child == -1) { logperror(LOG_ERR, "couldn't fork child process"); goto fail; } else if (child == 0) { usb_read_loop(pipe_fd[1]); } LOGPRINTF(1, "USB receiver initialized"); return 1; fail: if (dev_handle) { usb_close(dev_handle); dev_handle = NULL; } if (pipe_fd[0] >= 0) close(pipe_fd[0]); if (pipe_fd[1] >= 0) close(pipe_fd[1]); return 0; }
int silitek_init(void) { if(!tty_create_lock(hw.device)) { logprintf(LOG_ERR, "could not create lock files"); return(0); } if((hw.fd = open(hw.device, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) { logprintf(LOG_ERR, "could not open %s", hw.device); logperror(LOG_ERR, "silitek_init()"); tty_delete_lock(); return(0); } if(!tty_reset(hw.fd)) { logprintf(LOG_ERR,"could not reset %s", hw.device); silitek_deinit(); return(0); } if(!tty_setbaud(hw.fd, 1200)) { logprintf(LOG_ERR,"could not set baud rate on %s", hw.device); silitek_deinit(); return(0); } return(1); }
struct ir_remote* read_lircd_conf(const char* configfile) { FILE* f; struct ir_remote* remotes; const char* filename = configfile; filename = configfile == NULL ? LIRCDCFGFILE : configfile; f = fopen(filename, "r"); if (f == NULL) { logprintf(LIRC_ERROR, "could not open config file '%s'", filename); logperror(LIRC_ERROR, NULL); exit(EXIT_FAILURE); } remotes = read_config(f, configfile); fclose(f); if (remotes == (void*)-1) { logprintf(LIRC_ERROR, "reading of config file failed"); exit(EXIT_FAILURE); } else { logprintf(LIRC_DEBUG, "config file read"); if (remotes == NULL) { logprintf(LIRC_ERROR, "config file contains no valid remote control definition"); exit(EXIT_FAILURE); } } return remotes; }
int silitek_init(void) { if (!tty_create_lock(drv.device)) { logprintf(LIRC_ERROR, "could not create lock files"); return 0; } drv.fd = open(drv.device, O_RDWR | O_NOCTTY | O_NDELAY); if (drv.fd < 0) { logprintf(LIRC_ERROR, "could not open %s", drv.device); logperror(LIRC_ERROR, "silitek_init()"); tty_delete_lock(); return 0; } if (!tty_reset(drv.fd)) { logprintf(LIRC_ERROR, "could not reset %s", drv.device); silitek_deinit(); return 0; } if (!tty_setbaud(drv.fd, 1200)) { logprintf(LIRC_ERROR, "could not set baud rate on %s", drv.device); silitek_deinit(); return 0; } return 1; }
int mp3anywhere_init(void) { signal_length=hw.code_length*1000000/9600; if(!tty_create_lock(hw.device)) { logprintf(LOG_ERR,"could not create lock files"); return(0); } if((hw.fd=open(hw.device,O_RDWR|O_NONBLOCK|O_NOCTTY))<0) { logprintf(LOG_ERR,"could not open %s",hw.device); logperror(LOG_ERR,"mp3anywhere_init()"); tty_delete_lock(); return(0); } if(!tty_reset(hw.fd)) { logprintf(LOG_ERR,"could not reset tty"); mp3anywhere_deinit(); return(0); } if(!tty_setbaud(hw.fd,9600)) { logprintf(LOG_ERR,"could not set baud rate"); mp3anywhere_deinit(); return(0); } return(1); }
static int atwf83_init(void) { logprintf(LIRC_INFO, "initializing '%s'", drv.device); fd_hidraw = open(drv.device, O_RDONLY); if (fd_hidraw < 0) { logprintf(LIRC_ERROR, "unable to open '%s'", drv.device); return 0; } drv.fd = fd_hidraw; /* Create pipe so that events sent by the repeat thread will * trigger main thread */ if (pipe(fd_pipe) != 0) { logperror(LIRC_ERROR, "couldn't open pipe"); close(fd_hidraw); return 0; } drv.fd = fd_pipe[0]; /* Create thread to simulate repetitions */ if (pthread_create(&repeat_thread, NULL, atwf83_repeat, NULL)) { logprintf(LIRC_ERROR, "Could not create \"repeat thread\""); return 0; } return 1; }
int default_readdata(lirc_t timeout) { int data, ret; if (!waitfordata((long)timeout)) return 0; ret = read(drv.fd, &data, sizeof(data)); if (ret != sizeof(data)) { logprintf(LIRC_ERROR, "error reading from %s (ret %d, expected %d)", drv.device, ret, sizeof(data)); logperror(LIRC_ERROR, NULL); default_deinit(); return 0; } if (data == 0) { static int data_warning = 1; if (data_warning) { logprintf(LIRC_WARNING, "read invalid data from device %s", drv.device); data_warning = 0; } data = 1; } return data ; }
void cp_file(const char *src, const char *dest) { int src_fd, dest_fd; char *buf; size_t size; if (!test_file(src)) { logprintf("1file %s doesn't exist, so can't copy", src); return; } buf = malloc(BUF_SIZE); if (buf == NULL) { logperror("2can't allocate buffer"); return; } src_fd = open(src, O_RDONLY); if (src_fd < 0) { logperror("2can't open source file"); return; } dest_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC); if (dest_fd < 0) { logperror("2can't open destination file"); close(src_fd); return; } do { size = read(src_fd, buf, BUF_SIZE); if (size == -1) { logperror("error reading from source file"); break; } write(dest_fd, buf, size); if (size == -1) { logperror("error writing to destination file"); break; } } while (size == BUF_SIZE); close(src_fd); close(dest_fd); free(buf); }
int bte_connect(void) { struct termios tattr; LOGPRINTF(3, "bte_connect called"); if (hw.fd >= 0) close(hw.fd); do //try block { errno = 0; if ((hw.fd = open(hw.device, O_RDWR | O_NOCTTY)) == -1) { LOGPRINTF(1, "could not open %s", hw.device); LOGPERROR(1, "bte_connect"); break; } if (tcgetattr(hw.fd, &tattr) == -1) { LOGPRINTF(1, "bte_connect: tcgetattr() failed"); LOGPERROR(1, "bte_connect"); break; } LOGPRINTF(1, "opened %s", hw.device); LOGPERROR(1, "bte_connect"); cfmakeraw(&tattr); tattr.c_cc[VMIN] = 1; tattr.c_cc[VTIME] = 0; if (tcsetattr(hw.fd, TCSAFLUSH, &tattr) == -1) { LOGPRINTF(1, "bte_connect: tcsetattr() failed"); LOGPERROR(1, "bte_connect"); break; } if (!tty_setbaud(hw.fd, 115200)) { LOGPRINTF(1, "bte_connect: could not set baud rate %s", hw.device); LOGPERROR(1, "bte_connect"); break; } logprintf(LOG_ERR, "bte_connect: connection established"); io_failed = 0; if (bte_sendcmd("E?", BTE_INIT)) // Ask for echo state just to syncronise { return (1); } else { LOGPRINTF(1, "bte_connect: device did not respond"); } } while (0); //try block failed io_failed = 1; if (hw.fd >= 0) close(hw.fd); if ((hw.fd = open("/dev/zero", O_RDONLY)) == -1) { logprintf(LOG_ERR, "could not open /dev/zero/"); logperror(LOG_ERR, "bte_init()"); } sleep(1); return 0; }
static int alsa_error(const char *errstr, int errcode) { if (errcode < 0) { logprintf(LOG_ERR, "ALSA function snd_pcm_%s returned error: %s", errstr, snd_strerror(errcode)); logperror(LOG_ERR, errstr); return -1; } return 0; }
int tty_delete_lock(void) { DIR *dp; struct dirent *ep; int lock; int len; char id[20+1],*endptr; char filename[FILENAME_MAX+1]; long pid; int retval=1; dp=opendir("/var/lock/"); if(dp!=NULL) { while((ep=readdir(dp))) { strcpy(filename,"/var/lock/"); if(strlen(filename)+strlen(ep->d_name)>FILENAME_MAX) {retval=0;continue;} strcat(filename,ep->d_name); lock=open(filename,O_RDONLY); if(lock==-1) {retval=0;continue;} len=read(lock,id,20); close(lock); if(len<=0) {retval=0;continue;} id[len]=0; pid=strtol(id,&endptr,10); if(!*id || *endptr!='\n') { logprintf(LOG_WARNING,"invalid lockfile (%s) " "detected",filename); retval=0; continue; } if(pid==getpid()) { if(unlink(filename)==-1) { logprintf(LOG_ERR,"could not delete " "file \"%s\"",filename); logperror(LOG_ERR,NULL); retval=0; continue; } } } closedir(dp); } else { logprintf(LOG_ERR,"could not open directory \"/var/lock/\""); return(0); } return(retval); }
char *creative_rec(struct ir_remote *remotes) { char *m; int i; b[0]=0x4d; b[1]=0x05; b[4]=0xac; b[5]=0x21; last=end; gettimeofday(&start,NULL); for(i=0;i<NUMBYTES;i++) { if(i>0) { if(!waitfordata(TIMEOUT)) { logprintf(LOG_ERR,"timeout reading byte %d",i); return(NULL); } } if(read(hw.fd,&b[i],1)!=1) { logprintf(LOG_ERR,"reading of byte %d failed",i); logperror(LOG_ERR,NULL); return(NULL); } if(b[0]!=0x4d || b[1]!=0x05 /* || b[4]!=0xac || b[5]!=0x21 */) { logprintf(LOG_ERR,"bad envelope"); return(NULL); } if(i==5) { if(b[2]!=((~b[3])&0xff)) { logprintf(LOG_ERR,"bad checksum"); return(NULL); } } LOGPRINTF(1,"byte %d: %02x",i,b[i]); } gettimeofday(&end,NULL); /* pre=0x8435; */ pre=reverse((((ir_code) b[4])<<8) | ((ir_code) b[5]),16); code = reverse((((ir_code) b[2]) << 8) | ((ir_code) b[3]), 16); m=decode_all(remotes); return(m); }
char *logitech_rec(struct ir_remote *remotes) { char *m; int i=0; int repeat, mouse_event; b[i]=0x00; last=end; gettimeofday(&start,NULL); while(b[i] != 0xAA) { i++; if(i>=NUMBYTES) { LOGPRINTF(0,"buffer overflow at byte %d",i); break; } if(i>0) { if(!waitfordata(TIMEOUT)) { LOGPRINTF(0,"timeout reading byte %d",i); return(NULL); } } if(read(hw.fd,&b[i],1)!=1) { logprintf(LOG_ERR,"reading of byte %d failed",i); logperror(LOG_ERR,NULL); return(NULL); } LOGPRINTF(1,"byte %d: %02x",i,b[i]); if(b[i] >= 0x40 && b[i] <= 0x6F) { mouse_event=b[i]; b[1]=0xA0; b[2]=mouse_event; LOGPRINTF(1,"mouse event: %02x",mouse_event); break; } } gettimeofday(&end,NULL); if(b[1] == 0xA0) repeat=0; else repeat=1; if(!repeat) pre=(ir_code) b[1]; else pre=0xA0; code=(ir_code) b[2]; m=decode_all(remotes); return(m); }
char *mp3anywhere_rec(struct ir_remote *remotes) { char *m; int i=0; b[0]=0x00; b[1]=0xd5; b[2]=0xaa; b[3]=0xee; b[5]=0xad; last=end; gettimeofday(&start,NULL); while(b[i] != 0xAD) { i++; if(i>=NUMBYTES) { LOGPRINTF(0,"buffer overflow at byte %d",i); break; } if(i>0) { if(!waitfordata(TIMEOUT)) { LOGPRINTF(0,"timeout reading byte %d",i); return(NULL); } } if(read(hw.fd,&b[i],1)!=1) { logprintf(LOG_ERR,"reading of byte %d failed",i); logperror(LOG_ERR,NULL); return(NULL); } if(b[1]!= 0xd5 || b[2]!= 0xaa || b[3]!= 0xee || b[5]!= 0xad ) { logprintf(LOG_ERR,"bad envelope"); return(NULL); } LOGPRINTF(1,"byte %d: %02x",i,b[i]); } gettimeofday(&end,NULL); pre=0xD5AAEE; code=(ir_code) b[4]; m=decode_all(remotes); return(m); }
int audio_send(struct ir_remote *remote, struct ir_ncode *code) { int length; lirc_t *signals; int flags; char completed; lirc_t freq; static lirc_t prevfreq = 0; if (!init_send(remote, code)) return 0; length = send_buffer.wptr; signals = send_buffer.data; if (length <= 0 || signals == NULL) { LOGPRINTF(1, "nothing to send"); return 0; } /* set completed pipe to non blocking */ flags = fcntl(completedPipe[0], F_GETFL, 0); fcntl(completedPipe[0], F_SETFL, flags | O_NONBLOCK); /* remove any unwanted completed bytes */ while (read(completedPipe[0], &completed, sizeof(completed)) == 1) ; /* set completed pipe to blocking */ fcntl(completedPipe[0], F_SETFL, flags & ~O_NONBLOCK); /* write carrier frequency */ freq = remote->freq ? remote->freq : DEFAULT_FREQ; write(sendPipe[1], &freq, sizeof(freq)); if (freq != prevfreq) { prevfreq = freq; logprintf(LOG_INFO, "Using carrier frequency %i", freq); } /* write signals to sendpipe */ if (write(sendPipe[1], signals, length * sizeof(lirc_t)) == -1) { logprintf(LOG_ERR, "write failed"); logperror(LOG_ERR, "write()"); return 0; } /* wait for the callback to signal us that all signals are written */ read(completedPipe[0], &completed, sizeof(completed)); return 1; }
int caraca_init(void) { signal_length = hw.code_length * 1000000 / 1200; if ((hw.fd = caraca_open(PACKAGE)) < 0) { logprintf(LOG_ERR, "could not open lirc"); logperror(LOG_ERR, "caraca_init()"); return (0); } /*accept IR-Messages (16 : RC5 key code) for all nodes on the bus */ if (set_filter(hw.fd, 0x400, 0x7c0, 0) <= 0) { logprintf(LOG_ERR, "could not set filter for IR-Messages"); caraca_deinit(); return (0); } return (1); }
int default_send(struct ir_remote *remote, struct ir_ncode *code) { /* things are easy, because we only support one mode */ if (drv.send_mode != LIRC_MODE_PULSE) return (0); if (!init_send(remote, code)) return (0); if (write_send_buffer(drv.fd) == -1) { logprintf(LIRC_ERROR, "write failed"); logperror(LIRC_ERROR, NULL); return (0); } return (1); }
int irman_init(void) { if(!tty_create_lock(hw.device)) { logprintf(LOG_ERR,"could not create lock files"); return(0); } if((hw.fd=ir_init(hw.device))<0) { logprintf(LOG_ERR,"could not open %s",hw.device); logperror(LOG_ERR,"irman_init()"); tty_delete_lock(); return(0); } return(1); }
lirc_t tira_readdata(lirc_t timeout) { lirc_t data = 0; int ret; if (!waitfordata((long) timeout)) return 0; ret=read(hw.fd,&data,sizeof(data)); if(ret!=sizeof(data)) { logprintf(LOG_ERR, "error reading from %s", hw.device); logperror(LOG_ERR, NULL); tira_deinit(); return 0; } return data; }
char *tira_rec (struct ir_remote *remotes) { char *m; int i, x; last = end; x = 0; gettimeofday (&start, NULL); for (i = 0 ; i < 6; i++) { if (i > 0) { if (!waitfordata(20000)) { LOGPRINTF(0,"timeout reading byte %d",i); /* likely to be !=6 bytes, so flush. */ tcflush(hw.fd, TCIFLUSH); return NULL; } } if (read(hw.fd, &b[i], 1) != 1) { logprintf(LOG_ERR, "reading of byte %d failed.", i); logperror(LOG_ERR,NULL); return NULL; } LOGPRINTF(1, "byte %d: %02x", i, b[i]); x++; } gettimeofday(&end,NULL); code = 0; for ( i = 0 ; i < x ; i++ ) { code |= ((ir_code) b[i]); code = code << 8; } LOGPRINTF(1," -> %0llx",(unsigned long long) code); m = decode_all(remotes); return m; }
char *pinsys_rec(struct ir_remote *remotes) { char *m; int i; last=end; gettimeofday(&start,NULL); for(i=0;i<3;i++) { if (i>0) { if(!waitfordata(10000)) { logprintf(LOG_WARNING, "timeout reading byte %d",i); /* likely to be !=3 bytes, so flush. */ tcflush(hw.fd, TCIFLUSH); return(NULL); } } if(read(hw.fd,&b[i],1)!=1) { logprintf(LOG_ERR,"reading of byte %d failed",i); logperror(LOG_ERR,NULL); return(NULL); } LOGPRINTF(1,"byte %d: %02x",i,b[i]); } gettimeofday(&end,NULL); #ifdef PINSYS_THREEBYTE code = (b[2]) | (b[1]<<8) | (b[0]<<16); #else code = b[2]; #endif LOGPRINTF(1," -> %016lx",(unsigned long) code); m=decode_all(remotes); return(m); }
/* * Parse an absolute date using a datemsk config file. * Return the difference (measured in seconds) between that date/time and * the current date/time. * If the date has passed return zero. * * Returns true if ok (and *resp updated) and false if failed. * XXX Due to getdate limitations can not exceed year 2038. */ static boolean_t parse_date(char *str, uint_t *resp) { struct tm *tm; struct timeval tvs; time_t time, ntime; if (getenv("DATEMSK") == NULL) { (void) putenv("DATEMSK=/etc/inet/datemsk.ndpd"); } if (gettimeofday(&tvs, NULL) < 0) { logperror("gettimeofday"); return (_B_FALSE); } time = tvs.tv_sec; tm = getdate(str); if (tm == NULL) { logmsg(LOG_ERR, "Bad date <%s> (error %d)\n", str, getdate_err); return (_B_FALSE); } ntime = mktime(tm); if (debug & D_PARSE) { char buf[BUFSIZ]; (void) strftime(buf, sizeof (buf), "%Y-%m-%d %R %Z", tm); logmsg(LOG_DEBUG, "parse_date: <%s>, delta %ld seconds\n", buf, ntime - time); } if (ntime < time) { conferr("Date in the past <%s>\n", str); *resp = 0; return (_B_TRUE); } *resp = (ntime - time); return (_B_TRUE); }
void sysprintf(const char *fmt, ...) { va_list args; char *buf; size_t size; va_start(args, fmt); size = vsnprintf(NULL, 0, fmt, args); va_end(args); buf = malloc(size + 1); if (buf == NULL) { logperror("command buffer allocation failed"); return; } va_start(args, fmt); vsnprintf(buf, size + 1, fmt, args); va_end(args); system_logged(buf); free(buf); }
char *pixelview_rec(struct ir_remote *remotes) { char *m; int i; last=end; gettimeofday(&start,NULL); for(i=0;i<3;i++) { if(i>0) { if(!waitfordata(50000)) { logprintf(LOG_ERR,"timeout reading " "byte %d",i); return(NULL); } } if(read(hw.fd,&b[i],1)!=1) { logprintf(LOG_ERR,"reading of byte %d failed",i); logperror(LOG_ERR,NULL); return(NULL); } LOGPRINTF(1,"byte %d: %02x",i,b[i]); } gettimeofday(&end,NULL); pre=(reverse((ir_code) b[0],8)<<1)|1; code=(reverse((ir_code) b[1],8)<<1)|1; code=code<<10; code|=(reverse((ir_code) b[2],8)<<1)|1; m=decode_all(remotes); return(m); }
int waitfordata(unsigned long maxusec) { fd_set fds; int ret; struct timeval tv; while (1) { FD_ZERO(&fds); FD_SET(hw.fd, &fds); do { do { if (maxusec > 0) { tv.tv_sec = maxusec / 1000000; tv.tv_usec = maxusec % 1000000; ret = select(hw.fd + 1, &fds, NULL, NULL, &tv); if (ret == 0) return (0); } else { ret = select(hw.fd + 1, &fds, NULL, NULL, NULL); } } while (ret == -1 && errno == EINTR); if (ret == -1) { logprintf(LOG_ERR, "select() failed\n"); logperror(LOG_ERR, NULL); continue; } } while (ret == -1); if (FD_ISSET(hw.fd, &fds)) { /* we will read later */ return (1); } } }
//This process reads data from the device and forwards to the hw pipe //as PULSE/SPACE data int child_process(int pipe_w,int oldprotocol) { alarm(0); signal(SIGTERM, SIG_DFL); signal(SIGPIPE, SIG_DFL); signal(SIGINT, SIG_DFL); signal(SIGHUP, SIG_IGN); signal(SIGALRM, SIG_IGN); unsigned char tirabuffer[64]; int tirabuflen = 0; int readsize,tmp; lirc_t data,tdata; fd_set read_set; struct timeval tv,trailtime,currtime; unsigned long eusec; tv.tv_sec = 0; tv.tv_usec=1000; FD_ZERO(&read_set); trailtime.tv_sec = 0; trailtime.tv_usec = 0; while (1) { FD_SET(hw.fd, &read_set); tmp = select(hw.fd+1, &read_set, NULL, NULL, &tv); if (tmp == 0) continue; if (tmp < 0) { logprintf(LOG_ERR,"Error select()"); return 0; } if (!FD_ISSET(hw.fd, &read_set)) continue; readsize = read(hw.fd, &tirabuffer[tirabuflen], sizeof(tirabuffer)-tirabuflen); if (readsize <= 0) { logprintf(LOG_ERR, "Error reading from Tira"); logperror(LOG_ERR, NULL); return 0; } if (readsize == 0) continue; tirabuflen += readsize; tmp = 0; while (tmp < tirabuflen-1) { data = tirabuffer[tmp];data<<=8;data += tirabuffer[tmp+1]; if (!oldprotocol) data <<= 3;else data<<=5; if (data == 0) { if (tmp > tirabuflen-4) break; //we have to receive more data if (tirabuffer[tmp+3] != 0xB2) { logprintf(LOG_ERR,"Tira error 00 00 xx B2 trailing : missing 0xB2"); return 0; } if ((trailtime.tv_sec == 0) && (trailtime.tv_usec == 0)) gettimeofday (&trailtime, NULL); if (tmp > tirabuflen-6) break; //we have to receive more data tmp += 4; continue; } tmp += 2; if ((trailtime.tv_sec != 0) || (trailtime.tv_usec != 0)) { gettimeofday (&currtime, NULL); if (trailtime.tv_usec > currtime.tv_usec) { currtime.tv_usec += 1000000; currtime.tv_sec--; } eusec = time_elapsed(&trailtime, &currtime); if(eusec > LIRC_VALUE_MASK) eusec = LIRC_VALUE_MASK; trailtime.tv_sec = 0; trailtime.tv_usec = 0; if (eusec > data) { pulse_space = 1; tdata = LIRC_SPACE(eusec); if (write(pipe_w, &tdata, sizeof(tdata)) != sizeof(tdata)) { logprintf(LOG_ERR,"Error writing pipe"); return 0; } } } data = pulse_space ? LIRC_PULSE(data):LIRC_SPACE(data); pulse_space = 1-pulse_space; if (write(pipe_w, &data, sizeof(data)) != sizeof(data)) { logprintf(LOG_ERR,"Error writing pipe"); return 0; } } //Scroll buffer to next position if (tmp > 0) { memmove(tirabuffer, tirabuffer+tmp, tirabuflen-tmp); tirabuflen -= tmp; } } }
int default_init() { struct stat s; int i; /* FIXME: other modules might need this, too */ init_rec_buffer(); init_send_buffer(); if (stat(drv.device, &s) == -1) { logprintf(LIRC_ERROR, "could not get file information for %s", drv.device); logperror(LIRC_ERROR, "default_init()"); return (0); } /* file could be unix socket, fifo and native lirc device */ if (S_ISSOCK(s.st_mode)) { struct sockaddr_un addr; addr.sun_family = AF_UNIX; strncpy(addr.sun_path, drv.device, sizeof(addr.sun_path) - 1); drv.fd = socket(AF_UNIX, SOCK_STREAM, 0); if (drv.fd == -1) { logprintf(LIRC_ERROR, "could not create socket"); logperror(LIRC_ERROR, "default_init()"); return (0); } if (connect(drv.fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { logprintf(LIRC_ERROR, "could not connect to unix socket %s", drv.device); logperror(LIRC_ERROR, "default_init()"); default_deinit(); close(drv.fd); return (0); } LOGPRINTF(1, "using unix socket lirc device"); drv.features = LIRC_CAN_REC_MODE2 | LIRC_CAN_SEND_PULSE; drv.rec_mode = LIRC_MODE_MODE2; /* this might change in future */ drv.send_mode = LIRC_MODE_PULSE; return (1); } if ((drv.fd = open(drv.device, O_RDWR)) < 0) { logprintf(LIRC_ERROR, "could not open %s", drv.device); logperror(LIRC_ERROR, "default_init()"); return (0); } if (S_ISFIFO(s.st_mode)) { LOGPRINTF(1, "using defaults for the Irman"); drv.features = LIRC_CAN_REC_MODE2; drv.rec_mode = LIRC_MODE_MODE2; /* this might change in future */ return (1); } else if (!S_ISCHR(s.st_mode)) { default_deinit(); logprintf(LIRC_ERROR, "%s is not a character device!!!", drv.device); logperror(LIRC_ERROR, "something went wrong during installation"); return (0); } else if (default_ioctl(LIRC_GET_FEATURES, &drv.features) == -1) { logprintf(LIRC_ERROR, "could not get hardware features"); logprintf(LIRC_ERROR, "this device driver does not support the LIRC ioctl interface"); if (major(s.st_rdev) == 13) { logprintf(LIRC_ERROR, "did you mean to use the devinput driver instead of the %s driver?", drv.name); } else { logprintf(LIRC_ERROR, "major number of %s is %lu", drv.device, (__u32) major(s.st_rdev)); logprintf(LIRC_ERROR, "make sure %s is a LIRC device and use a current version of the driver", drv.device); } default_deinit(); return (0); } else { if (!(LIRC_CAN_SEND(drv.features) || LIRC_CAN_REC(drv.features))) { LOGPRINTF(1, "driver supports neither sending nor receiving of IR signals"); } if (LIRC_CAN_SEND(drv.features) && LIRC_CAN_REC(drv.features)) { LOGPRINTF(1, "driver supports both sending and receiving"); } else if (LIRC_CAN_SEND(drv.features)) { LOGPRINTF(1, "driver supports sending"); } else if (LIRC_CAN_REC(drv.features)) { LOGPRINTF(1, "driver supports receiving"); } } /* set send/receive method */ drv.send_mode = 0; if (LIRC_CAN_SEND(drv.features)) { for (i = 0; supported_send_modes[i] != 0; i++) { if (LIRC_CAN_SEND(drv.features) == supported_send_modes[i]) { drv.send_mode = LIRC_SEND2MODE(supported_send_modes[i]); break; } } if (supported_send_modes[i] == 0) { logprintf(LIRC_NOTICE, "the send method of the driver is not yet supported by lircd"); } } drv.rec_mode = 0; if (LIRC_CAN_REC(drv.features)) { for (i = 0; supported_rec_modes[i] != 0; i++) { if (LIRC_CAN_REC(drv.features) == supported_rec_modes[i]) { drv.rec_mode = LIRC_REC2MODE(supported_rec_modes[i]); break; } } if (supported_rec_modes[i] == 0) { logprintf(LIRC_NOTICE, "the receive method of the driver is not yet supported by lircd"); } } if (drv.rec_mode == LIRC_MODE_MODE2) { /* get resolution */ drv.resolution = 0; if ((drv.features & LIRC_CAN_GET_REC_RESOLUTION) && (default_ioctl(LIRC_GET_REC_RESOLUTION, &drv.resolution) != -1)) { LOGPRINTF(1, "resolution of receiver: %d", drv.resolution); } } else if (drv.rec_mode == LIRC_MODE_LIRCCODE) { if (default_ioctl(LIRC_GET_LENGTH, (void*) &drv.code_length) == -1) { logprintf(LIRC_ERROR, "could not get code length"); logperror(LIRC_ERROR, "default_init()"); default_deinit(); return (0); } if (drv.code_length > sizeof(ir_code) * CHAR_BIT) { logprintf(LIRC_ERROR, "lircd can not handle %lu bit codes", drv.code_length); default_deinit(); return (0); } } if (!(drv.send_mode || drv.rec_mode)) { default_deinit(); return (0); } return (1); }
int audio_init() { PaStreamParameters inputParameters; PaStreamParameters outputParameters; PaError err; int flags; struct termios t; char api[1024]; char device[1024]; double latency; LOGPRINTF(1, "hw_audio_init()"); // logprintf(LOG_INFO, "Initializing %s...", hw.device); init_rec_buffer(); rewind_rec_buffer(); /* new */ data.lastFrames[0] = 128; data.lastFrames[1] = 128; data.lastFrames[2] = 128; data.lastSign = 0; data.lastCount = 0; data.pulseSign = 0; data.carrierPos = 0.0; data.remainingSignal = 0.0; data.signalPhase = 0; data.signaledDone = 1; data.samplesToIgnore = 0; data.carrierFreq = DEFAULT_FREQ; err = Pa_Initialize(); if (err != paNoError) goto error; audio_parsedevicestr(api, device, &data.samplerate, &latency); logprintf(LOG_INFO, "Using samplerate %i", data.samplerate); /* choose input device */ audio_choosedevice(&inputParameters, 1, api, device, latency); if (inputParameters.device == paNoDevice) { logprintf(LOG_ERR, "No input device found"); goto error; } inputParameters.channelCount = NUM_CHANNELS; /* stereo input */ inputParameters.sampleFormat = PA_SAMPLE_TYPE; inputParameters.hostApiSpecificStreamInfo = NULL; /* choose output device */ audio_choosedevice(&outputParameters, 0, api, device, latency); if (outputParameters.device == paNoDevice) { logprintf(LOG_ERR, "No output device found"); goto error; } outputParameters.channelCount = NUM_CHANNELS; /* stereo output */ outputParameters.sampleFormat = PA_SAMPLE_TYPE; outputParameters.hostApiSpecificStreamInfo = NULL; outputLatency = outputParameters.suggestedLatency * 1000000; /* Record some audio. -------------------------------------------- */ err = Pa_OpenStream(&stream, &inputParameters, &outputParameters, data.samplerate, 512, /* frames per buffer */ paPrimeOutputBuffersUsingStreamCallback, recordCallback, &data); if (err != paNoError) goto error; /* open pty */ if (openpty(&master, &ptyfd, ptyName, 0, 0) == -1) { logprintf(LOG_ERR, "openpty failed"); logperror(LOG_ERR, "openpty()"); goto error; } /* regular device file */ if (tcgetattr(master, &t) < 0) { logprintf(LOG_ERR, "tcgetattr failed"); logperror(LOG_ERR, "tcgetattr()"); } cfmakeraw(&t); /* apply file descriptor options */ if (tcsetattr(master, TCSANOW, &t) < 0) { logprintf(LOG_ERR, "tcsetattr failed"); logperror(LOG_ERR, "tcsetattr()"); } flags = fcntl(ptyfd, F_GETFL, 0); if (flags != -1) { fcntl(ptyfd, F_SETFL, flags | O_NONBLOCK); } LOGPRINTF(LOG_INFO, "PTY name: %s", ptyName); hw.fd = ptyfd; /* make a pipe for sending signals to the callback */ /* make a pipe for signaling from the callback that everything was sent */ if (pipe(sendPipe) == -1 || pipe(completedPipe) == -1) { logprintf(LOG_ERR, "pipe failed"); logperror(LOG_ERR, "pipe()"); } /* make the readable end non-blocking */ flags = fcntl(sendPipe[0], F_GETFL, 0); if (flags != -1) { fcntl(sendPipe[0], F_SETFL, flags | O_NONBLOCK); } else { logprintf(LOG_ERR, "fcntl failed"); logperror(LOG_ERR, "fcntl()"); } err = Pa_StartStream(stream); if (err != paNoError) goto error; /* wait for portaudio to settle */ usleep(50000); return (1); error: Pa_Terminate(); logprintf(LOG_ERR, "an error occured while using the portaudio stream"); logprintf(LOG_ERR, "error number: %d", err); logprintf(LOG_ERR, "error message: %s", Pa_GetErrorText(err)); return (0); }
int tira_setup_timing(int oldprotocol) { long fd_flags; int i; if (oldprotocol) if (!tty_setbaud(hw.fd, 57600)) return 0; logprintf(LOG_INFO, "Switching to timing mode"); if (!oldprotocol) { if (write(hw.fd, "IC\0\0", 4 ) != 4) { logprintf(LOG_ERR, "failed switching device into timing mode"); return 0; } /* wait for the chars to be written */ usleep (2 * (100 * 1000)); i = read (hw.fd, response, 3); if (i != 3) { logprintf(LOG_ERR, "failed reading response to timing mode command"); return 0; } if (strncmp(response, "OIC", 3) != 0) return 0; } pulse_space=1; //pulse /* Allocate a pipe for lircd to read from */ if (pipe(pipe_fd) == -1) { logperror(LOG_ERR, "unable to create pipe"); goto fail; } fd_flags = fcntl(pipe_fd[0], F_GETFL); if(fcntl(pipe_fd[0], F_SETFL, fd_flags | O_NONBLOCK) == -1) { logperror(LOG_ERR, "can't set pipe to non-blocking"); goto fail; } /* Spawn the child process */ child_pid = fork(); if (child_pid == -1) { logperror(LOG_ERR, "unable to fork child process"); goto fail; } else if (child_pid == 0) { close(pipe_fd[0]); child_process(pipe_fd[1], oldprotocol); close(pipe_fd[1]); _exit(0); } else { /* parent reads from pipe */ close(hw.fd); hw.fd = pipe_fd[0]; } close(pipe_fd[1]); displayonline(); return(1); fail: if (pipe_fd[0] != -1) { close(pipe_fd[0]); close(pipe_fd[1]); } return(0); }