Esempio n. 1
0
static int iguana_init()
{
	int recv_pipe[2], retval = 0;

	init_rec_buffer();

	if (pipe(recv_pipe) != 0)
        {
		logprintf(LOG_ERR, "couldn't open pipe: %s", strerror(errno));
        }
	else
	{
		int notify[2];

		if (pipe(notify) != 0)
		{
			logprintf(LOG_ERR, "couldn't open pipe: %s", strerror(errno));
			close(recv_pipe[0]);
			close(recv_pipe[1]);
		}
		else
		{
			hw.fd = recv_pipe[0];

			child = fork();
			if (child == -1)
			{
				logprintf(LOG_ERR, "couldn't fork child process: %s", strerror(errno));
			}
			else if (child == 0)
			{
				close(recv_pipe[0]);
				close(notify[0]);
				recv_loop(recv_pipe[1], notify[1]);
				_exit(0);
			}
			else
			{
				int dummy;
				close(recv_pipe[1]);
				close(notify[1]);
				/* make sure child has set its signal handler to avoid race with iguana_deinit() */
				read(notify[0], &dummy, 1);
				close(notify[0]);
				sendConn = iguanaConnect(hw.device);
				if (sendConn == -1)
					logprintf(LOG_ERR, "couldn't open connection to iguanaIR daemon: %s", strerror(errno));
				else
					retval = 1;
			}
		}
        }

	return retval;
}
Esempio n. 2
0
/* 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;
}
Esempio n. 3
0
WL_API int init(HANDLE exitEvent) {

	init_rec_buffer();
	initHardwareStruct();

	threadExitEvent = exitEvent;
	dataReadyEvent	= CreateEvent(nullptr,TRUE,FALSE,nullptr);

	sendReceiveData = new SendReceiveData();

	if(!sendReceiveData->init()) return 0;

	return 1;
}
Esempio n. 4
0
WL_API int init(HANDLE exitEvent) {

	init_rec_buffer();
	init_send_buffer();
	initHardwareStruct();

	threadExitEvent = exitEvent;
	dataReadyEvent	= CreateEvent(NULL,TRUE,FALSE,NULL);

	sendReceiveData = new SendReceiveData();

	if(!sendReceiveData->init()) return 0;
	if(!sendReceiveData->setTransmitters(settings.getTransmitterChannels())) return 0;

	return 1;
}
Esempio n. 5
0
File: udp.c Progetto: matzrh/lirc
int udp_init()
{
	int port;
	struct sockaddr_in addr;

	logprintf(LIRC_INFO, "Initializing UDP: %s", drv.device);

	init_rec_buffer();

	port = atoi(drv.device);
	if (port == 0) {
		logprintf(LIRC_ERROR, "invalid port: %s", drv.device);
		return 0;
	}

	/* drv.fd needs to point somewhere when we have extra data */
	if ((zerofd = open("/dev/zero", O_RDONLY)) < 0) {
		logprintf(LIRC_ERROR, "can't open /dev/zero: %s", strerror(errno));
		return 0;
	}

	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		logprintf(LIRC_ERROR, "error creating socket: %s", strerror(errno));
		close(zerofd);
		return 0;
	}

	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	addr.sin_port = htons(port);

	if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		logprintf(LIRC_ERROR, "can't bind socket to port %d: %s", port, strerror(errno));
		close(sockfd);
		close(zerofd);
		return 0;
	}

	logprintf(LIRC_INFO, "Listening on port %d/udp", port);

	drv.fd = sockfd;

	return (1);
}
Esempio n. 6
0
int iguana_init()
{
	int recv_pipe[2], retval = 0;

	init_rec_buffer();

	if (pipe(recv_pipe) != 0)
        {
		logprintf(LOG_ERR, "couldn't open pipe: %s", strerror(errno));
        }
	else
	{
		hw.fd = recv_pipe[0];

		child = fork();
		if (child == -1)
		{
			logprintf(LOG_ERR, "couldn't fork child process: %s", strerror(errno));
		}
		else if (child == 0)
		{
			recv_loop(recv_pipe[1]);
			exit(0);
		}
		else
		{
			sendConn = iguanaConnect(hw.device);
			if (sendConn == -1)
				logprintf(LOG_ERR, "couldn't open connection to iguanaIR daemon: %s", strerror(errno));
			else
				retval = 1;
		}
        }

	return retval;
}
Esempio n. 7
0
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);
}
Esempio n. 8
0
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);
}
Esempio n. 9
0
int default_init()
{
#if defined(SIM_SEND) && !defined(DAEMONIZE)
	hw.fd=STDOUT_FILENO;
	hw.features=LIRC_CAN_SEND_PULSE;
	hw.send_mode=LIRC_MODE_PULSE;
	hw.rec_mode=0;
#elif defined(SIM_REC) && !defined(DAEMONIZE)
	hw.fd=STDIN_FILENO;
	hw.features=LIRC_CAN_REC_MODE2;
	hw.send_mode=0;
	hw.rec_mode=LIRC_MODE_MODE2;
#else
	struct stat s;
	int i;
	
	/* FIXME: other modules might need this, too */
	init_rec_buffer();
	init_send_buffer();
	if((hw.fd=open(hw.device,O_RDWR))<0)
	{
		logprintf(LOG_ERR,"could not open %s",hw.device);
		logperror(LOG_ERR,"default_init()");
		return(0);
	}
	if(fstat(hw.fd,&s)==-1)
	{
		default_deinit();
		logprintf(LOG_ERR,"could not get file information");
		logperror(LOG_ERR,"default_init()");
		return(0);
	}
	if(S_ISFIFO(s.st_mode))
	{
		LOGPRINTF(1,"using defaults for the Irman");
		hw.features=LIRC_CAN_REC_MODE2;
		hw.rec_mode=LIRC_MODE_MODE2; /* this might change in future */
		return(1);
	}
	else if(!S_ISCHR(s.st_mode))
	{
		default_deinit();
		logprintf(LOG_ERR,"%s is not a character device!!!",
			  hw.device);
		logperror(LOG_ERR,"something went wrong during "
			  "installation");
		return(0);
	}
	else if(ioctl(hw.fd,LIRC_GET_FEATURES,&hw.features)==-1)
	{
		logprintf(LOG_ERR,"could not get hardware features");
		logprintf(LOG_ERR,"this device driver does not "
			  "support the new LIRC interface");
		logprintf(LOG_ERR,"make sure you use a current "
			  "version of the driver");
		default_deinit();
		return(0);
	}
#       ifdef DEBUG
	else
	{
		if(!(LIRC_CAN_SEND(hw.features) || 
		     LIRC_CAN_REC(hw.features)))
		{
			LOGPRINTF(1,"driver supports neither "
				  "sending nor receiving of IR signals");
		}
		if(LIRC_CAN_SEND(hw.features) && LIRC_CAN_REC(hw.features))
		{
			LOGPRINTF(1,"driver supports both sending and "
				  "receiving");
		}
		else if(LIRC_CAN_SEND(hw.features))
		{
			LOGPRINTF(1,"driver supports sending");
		}
		else if(LIRC_CAN_REC(hw.features))
		{
			LOGPRINTF(1,"driver supports receiving");
		}
	}
#       endif
	
	/* set send/receive method */
	hw.send_mode=0;
	if(LIRC_CAN_SEND(hw.features))
	{
		for(i=0;supported_send_modes[i]!=0;i++)
		{
			if(hw.features&supported_send_modes[i])
			{
				unsigned long mode;

				mode=LIRC_SEND2MODE(supported_send_modes[i]);
				if(ioctl(hw.fd,LIRC_SET_SEND_MODE,&mode)==-1)
				{
					logprintf(LOG_ERR,"could not set "
						  "send mode");
					logperror(LOG_ERR,"default_init()");
					default_deinit();
					return(0);
				}
				hw.send_mode=LIRC_SEND2MODE
				(supported_send_modes[i]);
				break;
			}
		}
		if(supported_send_modes[i]==0)
		{
			logprintf(LOG_NOTICE,"the send method of the "
				  "driver is not yet supported by lircd");
		}
	}
	hw.rec_mode=0;
	if(LIRC_CAN_REC(hw.features))
	{
		for(i=0;supported_rec_modes[i]!=0;i++)
		{
			if(hw.features&supported_rec_modes[i])
			{
				unsigned long mode;

				mode=LIRC_REC2MODE(supported_rec_modes[i]);
				if(ioctl(hw.fd,LIRC_SET_REC_MODE,&mode)==-1)
				{
					logprintf(LOG_ERR,"could not set "
						  "receive mode");
					logperror(LOG_ERR,"default_init()");
					return(0);
				}
				hw.rec_mode=LIRC_REC2MODE
				(supported_rec_modes[i]);
				break;
			}
		}
		if(supported_rec_modes[i]==0)
		{
			logprintf(LOG_NOTICE,"the receive method of the "
				  "driver is not yet supported by lircd");
		}
	}
	if(hw.rec_mode==LIRC_MODE_CODE)
	{
		hw.code_length=8;
	}
	else if(hw.rec_mode==LIRC_MODE_LIRCCODE)
	{
		if(ioctl(hw.fd,LIRC_GET_LENGTH,&hw.code_length)==-1)
		{
			logprintf(LOG_ERR,"could not get code length");
			logperror(LOG_ERR,"default_init()");
			default_deinit();
			return(0);
		}
		if(hw.code_length>sizeof(ir_code)*CHAR_BIT)
		{
			logprintf(LOG_ERR,"lircd can not handle %lu bit "
				  "codes",hw.code_length);
			default_deinit();
			return(0);
		}
	}
	if(!(hw.send_mode || hw.rec_mode))
	{
		default_deinit();
		return(0);
	}
#endif
	return(1);
}
Esempio n. 10
0
/* initialize driver -- returns 1 on success, 0 on error */
static int ati_init()
{
	struct usb_device *usb_dev;
	int pipe_fd[2] = { -1, -1 };

	LOGPRINTF(1, "initializing USB receiver");

	init_rec_buffer();

	/* 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];

	usb_dev = find_usb_device();
	if (usb_dev == NULL) {
		logprintf(LOG_ERR, "couldn't find a compatible USB device");
		return 0;
	}

	if (!find_device_endpoints(usb_dev)) {
		logprintf(LOG_ERR, "couldn't find device endpoints");
		return 0;
	}

	dev_handle = usb_open(usb_dev);
	if (dev_handle == NULL) {
		logperror(LOG_ERR, "couldn't open USB receiver");
		goto fail;
	}

	if (usb_claim_interface(dev_handle, 0) != 0) {
		logperror(LOG_ERR, "couldn't claim USB interface");
		goto fail;
	}

	errno = 0;
	if ((usb_interrupt_write(dev_handle, dev_ep_out->bEndpointAddress, init1, sizeof(init1), 100) != sizeof(init1))
	    || (usb_interrupt_write(dev_handle, dev_ep_out->bEndpointAddress, init2, sizeof(init2), 100) !=
		sizeof(init2))) {
		logprintf(LOG_ERR, "couldn't initialize USB receiver: %s", errno ? strerror(errno) : "short write");
		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;
}
Esempio n. 11
0
static int uirt2_raw_init(void)
{
	int version;

	if (!tty_create_lock(hw.device)) {
		logprintf(LOG_ERR, "uirt2_raw: could not create lock files");
		return (0);
	}

	if ((hw.fd = open(hw.device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
		logprintf(LOG_ERR, "uirt2_raw: could not open %s", hw.device);
		tty_delete_lock();
		return (0);
	}

	if (!tty_reset(hw.fd)) {
		logprintf(LOG_ERR, "uirt2_raw: could not reset tty");
		close(hw.fd);
		tty_delete_lock();
		return (0);
	}

	/* Wait for UIRT device to power up */
	usleep(100 * 1000);

	if (!tty_setbaud(hw.fd, 115200)) {
		logprintf(LOG_ERR, "uirt2_raw: could not set baud rate");
		close(hw.fd);
		tty_delete_lock();
		return (0);
	}

	if (!tty_setcsize(hw.fd, 8)) {
		logprintf(LOG_ERR, "uirt2_raw: could not set csize");
		close(hw.fd);
		tty_delete_lock();
		return (0);
	}

	if (!tty_setrtscts(hw.fd, 1)) {
		logprintf(LOG_ERR, "uirt2_raw: could not enable hardware flow");
		close(hw.fd);
		tty_delete_lock();
		return (0);
	}

	if ((dev = uirt2_init(hw.fd)) == NULL) {
		logprintf(LOG_ERR, "uirt2_raw: No UIRT2 device found at %s", hw.device);
		close(hw.fd);
		tty_delete_lock();
		return (0);
	}

	if (uirt2_setmoderaw(dev) < 0) {
		logprintf(LOG_ERR, "uirt2_raw: could not set raw mode");
		uirt2_raw_deinit();
		return (0);
	}

	if (uirt2_getversion(dev, &version) < 0) {
		uirt2_raw_deinit();
		return (0);
	}
	if (version >= 0x0905) {
		if (!tty_setdtr(hw.fd, 0)) {
			logprintf(LOG_ERR, "uirt2_raw: could not set DTR");
			uirt2_raw_deinit();
			return (0);
		}
	}

	init_rec_buffer();
	init_send_buffer();

	rec_rptr = 0;
	rec_wptr = 0;
	rec_size = sizeof(rec_buf) / sizeof(rec_buf[0]);

	return (1);
}
Esempio n. 12
0
int audio_alsa_init()
{
	int fd, err;
	char *pcm_rate;
	char tmp_name[20];

	init_rec_buffer();

	/* Create a temporary filename for our FIFO,
	 * Use mkstemp() instead of mktemp() although we need a FIFO not a
	 * regular file. We do this since glibc barfs at mktemp() and this
	 * scares the users :-)
	 */
	strcpy(tmp_name, "/tmp/lircXXXXXX");
	fd = mkstemp(tmp_name);
	close(fd);

	/* Start the race! */
	unlink(tmp_name);
	if (mknod(tmp_name, S_IFIFO | S_IRUSR | S_IWUSR, 0)) {
		logprintf(LOG_ERR, "could not create FIFO %s", tmp_name);
		logperror(LOG_ERR, "audio_alsa_init ()");
		return 0;
	}
	/* Phew, we won the race ... */

	/* Open the pipe and hand it to LIRC ... */
	hw.fd = open(tmp_name, O_RDWR);
	if (hw.fd < 0) {
		logprintf(LOG_ERR, "could not open pipe %s", tmp_name);
		logperror(LOG_ERR, "audio_alsa_init ()");
error:		unlink(tmp_name);
		audio_alsa_deinit();
		return 0;
	}

	/* Open the other end of the pipe and hand it to ALSA code.
	 * We're opening it in non-blocking mode to avoid lockups.
	 */
	alsa_hw.fd = open(tmp_name, O_RDWR | O_NONBLOCK);
	/* Ok, we don't need the FIFO visible in the filesystem anymore ... */
	unlink(tmp_name);

	/* Examine the device name, if it contains a sample rate */
	strncpy(tmp_name, hw.device, sizeof(tmp_name) - 1);
	pcm_rate = strchr(tmp_name, '@');
	if (pcm_rate) {
		int rate;
		char *stereo_channel;

		/* Examine if we need to capture in stereo
		 * looking for an 'l' or 'r' character to indicate
		 * which channel to look at.*/
		stereo_channel = strchr(pcm_rate, ',');

		if (stereo_channel) {

			/* Syntax in device string indicates we need
			   to use stereo */
			alsa_hw.num_channels = 2;
			/* As we are requesting stereo now, use the
			   more common signed 16bit samples */
			alsa_hw.format = SND_PCM_FORMAT_S16_LE;

			if (stereo_channel[1] == 'l') {
				alsa_hw.channel = 0;
			} else if (stereo_channel[1] == 'r') {
				alsa_hw.channel = 1;
			} else {
				logperror(LOG_WARNING,
					  "dont understand which channel to use - defaulting to left\n");
			}
		}

		/* Remove the sample rate from device name (and
		   channel indicator if present) */
		*pcm_rate++ = 0;
		/* See if rate is meaningful */
		rate = atoi(pcm_rate);
		if (rate > 0) {
			alsa_hw.rate = rate;
		}
	}

	/* Open the audio card in non-blocking mode */
	err = snd_pcm_open(&alsa_hw.handle, tmp_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
	if (err < 0) {
		logprintf(LOG_ERR, "could not open audio device %s: %s", hw.device, snd_strerror(err));
		logperror(LOG_ERR, "audio_alsa_init ()");
		goto error;
	}

	/* Set up the I/O signal handler */
	if (alsa_error
	    ("async_add_handler", snd_async_add_pcm_handler(&alsa_hw.sighandler, alsa_hw.handle, alsa_sig_io, NULL)))
		goto error;

	/* Set sampling parameters */
	if (alsa_set_hwparams(alsa_hw.handle))
		goto error;

	LOGPRINTF(LOG_INFO, "hw_audio_alsa: Using device '%s', sampling rate %dHz\n", tmp_name, alsa_hw.rate);

	/* Start sampling data */
	if (alsa_error("start", snd_pcm_start(alsa_hw.handle)))
		goto error;

	return 1;
}