Example #1
0
int transmitPacket(unsigned int* packet, size_t size)
{
	int rc = IR_FAILURE;

	PIPE_PTR conn = iguanaConnect("0");
	
	if (conn == INVALID_PIPE)
	{
		rc = IR_FAILURE;
		perror("iguanaConnect failed");
	}
	else
	{
		iguanaPacket req;
		req = iguanaCreateRequest(IG_DEV_SEND, sizeof(unsigned int) * size, packet);
		
		if (!iguanaWriteRequest(req, conn))
		{
			perror("iguanaWriteRequest failed");
		}

		/* because we did not dynamically allocate the buffer we need
		to remove it before freeing the packet it was added to. */
		iguanaRemoveData(req, NULL);
		iguanaFreePacket(req);
	}

	return rc;
}
Example #2
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;
}
Example #3
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;
}
Example #4
0
static void recv_loop(int fd, int notify)
{
	int conn;

	alarm(0);
	signal(SIGTERM, quitHandler);
	/*    signal(SIGPIPE, SIG_DFL);*/
	signal(SIGINT, quitHandler);
	signal(SIGHUP, SIG_IGN);
	signal(SIGALRM, SIG_IGN);

	/* notify parent by closing notify */
	close(notify);

	conn = iguanaConnect(hw.device);
	if (conn != -1)
	{
		iguanaPacket request, response;
		lirc_t prevCode = -1;

		request = iguanaCreateRequest(IG_DEV_RECVON, 0, NULL);
		if (iguanaWriteRequest(request, conn))
			while(! recvDone)
			{
				/* read from device */
				do
				{
					response = iguanaReadResponse(conn, 1000);
				}
				while (!recvDone &&
					((response == NULL && errno == ETIMEDOUT)
					|| (iguanaResponseIsError(response) && errno == ETIMEDOUT)));

				if (iguanaResponseIsError(response))
				{
					/* be quiet during exit */
					if (! recvDone)
						logprintf(LOG_ERR, "error response: %s\n", strerror(errno));
					break;
				}
				else if (iguanaCode(response) == IG_DEV_RECV)
				{
					uint32_t *code;
					unsigned int length, x, y = 0;
					lirc_t buffer[8]; /* we read 8 bytes max at a time
							   * from the device, i.e. packet
							   * can only contain 8
							   * signals. */

					/* pull the data off the packet */
					code = (uint32_t*)iguanaRemoveData(response, &length);
					length /= sizeof(uint32_t);

					/* translate the code into lirc_t pulses (and make
					 * sure they don't split across iguana packets. */
					for(x = 0; x < length; x++)
					{
						if (prevCode == -1)
						{
							prevCode = (code[x] & IG_PULSE_MASK);
							if(prevCode > PULSE_MASK) prevCode = PULSE_MASK;
							if(code[x] & IG_PULSE_BIT) prevCode |= PULSE_BIT;
						}
						else if (((prevCode & PULSE_BIT) && (code[x]  & IG_PULSE_BIT)) ||
							 (!(prevCode & PULSE_BIT) && !(code[x]  & IG_PULSE_BIT)))
						{
							/* can overflow pulse mask, so just set to
							 * largest possible */
							if ((prevCode & PULSE_MASK) + (code[x] & IG_PULSE_MASK) >
							    PULSE_MASK)
								prevCode = (prevCode & PULSE_BIT) | PULSE_MASK;
							else
								prevCode += code[x] & IG_PULSE_MASK;
						}
						else
						{
							buffer[y] = prevCode;
							y++;

							prevCode = (code[x] & IG_PULSE_MASK);
							if(prevCode > PULSE_MASK) prevCode = PULSE_MASK;
							if(code[x] & IG_PULSE_BIT) prevCode |= PULSE_BIT;
						}
					}

					/* write the data and free it */
					if (y > 0)
						write(fd, buffer, sizeof(lirc_t) * y);
					free(code);
				}

				iguanaFreePacket(response);
			}

		iguanaFreePacket(request);
	}

	iguanaClose(conn);
	close(fd);
}
Example #5
0
int main(int argc, char **argv)
{
    /* connect to a device, in this case the first device found by igdaemon */
    PIPE_PTR conn = iguanaConnect("0");
    if (conn == INVALID_PIPE)
        perror("iguanaConnect failed");
    else
    {
        iguanaPacket req, resp;

        /* check the version just to demonstrate the server/client protocol */
        req = iguanaCreateRequest(IG_DEV_GETVERSION, 0, NULL);
        if (! iguanaWriteRequest(req, conn))
            perror("iguanaWriteRequest failed");
        else
        {
            /* wait up to 1000 milliseconds for a response */
            resp = iguanaReadResponse(conn, 1000);
            if (iguanaResponseIsError(resp))
                perror("iguanaReadResponse errored");
            else
            {
                /* the get version request returns 2 version bytes */
                unsigned int len;
                unsigned char *buffer = iguanaRemoveData(resp, &len);
                printf("Firmware version 0x%02x\n", *((short*)buffer));
                free(buffer);
            }
            iguanaFreePacket(resp);
        }
        iguanaFreePacket(req);

        /* the sendable data should be an unsigned int array
           containing the pulse/space data with the pulses OR'd with
           IG_PULSE_BIT.  I believe it must start and end with a pulse
           as well.  All lengths are in microseconds. */
        unsigned int buffer[] = {
            8000 | IG_PULSE_BIT,
            1000,
            8000 | IG_PULSE_BIT,
            100,
            800 | IG_PULSE_BIT,
            100,
            800 | IG_PULSE_BIT,
            100,
            8000 | IG_PULSE_BIT,
            1000,
            8000 | IG_PULSE_BIT,
        };

        req = iguanaCreateRequest(IG_DEV_SEND,
                                  sizeof(unsigned int) * 11, buffer);
        if (! iguanaWriteRequest(req, conn))
            perror("iguanaWriteRequest failed");
        else
        {
            resp = iguanaReadResponse(conn, 1000);
            if (iguanaResponseIsError(resp))
                perror("iguanaReadResponse errored");
            else
                /* send just gives back success or failure */
                printf("Send successful.\n");
            iguanaFreePacket(resp);
        }

        /* because we did not dynamically allocate the buffer we need
           to remove it before freeing the packet it was added to. */
        iguanaRemoveData(req, NULL);
        iguanaFreePacket(req);
    }

    return 0;
}