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; }
static bool daemonTransaction(unsigned char code, void *value, size_t size) { uint8_t *data; bool retval = false; data = (uint8_t*) malloc(size); if (data != NULL) { iguanaPacket request, response = NULL; memcpy(data, value, size); request = iguanaCreateRequest(code, size, data); if (request) { if (iguanaWriteRequest(request, sendConn)) response = iguanaReadResponse(sendConn, 10000); iguanaFreePacket(request); } else free(data); /* handle success */ if (! iguanaResponseIsError(response)) retval = true; iguanaFreePacket(response); } return retval; }
static int iguana_send(struct ir_remote *remote, struct ir_ncode *code) { int retval = 0; uint32_t freq; /* set the carrier frequency if necessary */ freq = htonl(remote->freq); if (remote->freq != currentCarrier && remote->freq >= 25000 && remote->freq <= 100000 && daemonTransaction(IG_DEV_SETCARRIER, &freq, sizeof(freq))) currentCarrier = remote->freq; if (init_send(remote, code)) { int length, x; lirc_t *signals; uint32_t *igsignals; length = send_buffer.wptr; signals = send_buffer.data; igsignals = (uint32_t*)malloc(sizeof(uint32_t) * length); if (igsignals != NULL) { iguanaPacket request, response = NULL; /* must pack the data into a unit32_t array */ for(x = 0; x < length; x++) { igsignals[x] = signals[x] & PULSE_MASK; if (signals[x] & PULSE_BIT) igsignals[x] |= IG_PULSE_BIT; } /* construct a request and send it to the daemon */ request = iguanaCreateRequest(IG_DEV_SEND, sizeof(uint32_t) * length, igsignals); if (iguanaWriteRequest(request, sendConn)) { /* response will only come back after the device has * transmitted */ response = iguanaReadResponse(sendConn, 10000); if (! iguanaResponseIsError(response)) { retval = 1; } iguanaFreePacket(response); } /* free the packet and the data */ iguanaFreePacket(request); } } return retval; }
int iguana_send(struct ir_remote *remote, struct ir_ncode *code) { int retval = 0; if (init_send(remote, code)) { int length, x; lirc_t *signals; uint32_t *igsignals; length = send_buffer.wptr; signals = send_buffer.data; igsignals = (uint32_t*)malloc(sizeof(uint32_t) * length); if (igsignals != NULL) { iguanaPacket request, response = NULL; /* must pack the data into a unit32_t array */ for(x = 0; x < length; x++) { igsignals[x] = signals[x] & PULSE_MASK; if (signals[x] & PULSE_BIT) igsignals[x] |= IG_PULSE_BIT; } /* construct a request and send it to the daemon */ request = iguanaCreateRequest(IG_DEV_SEND, sizeof(uint32_t) * length, igsignals); if (iguanaWriteRequest(request, sendConn)) { /* response will only come back after the device has * transmitted */ response = iguanaReadResponse(sendConn, 10000); if (! iguanaResponseIsError(response)) { remote->last_code = code; retval = 1; } iguanaFreePacket(response); } /* free the packet and the data */ iguanaFreePacket(request); } } return retval; }
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); }
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; }