Example #1
0
DWORD WINAPI report_thread(LPVOID arg) {
  struct xillyinfo info;  
  struct xillyfifo *fifo = (struct xillyfifo*)arg;
  FILE* of = fopen("reply.bin", "wb");//O_WRONLY | _O_BINARY);
  if(!of) {
    perror("Failed to create result file\n");
    return errno;
  }
  for(; ;) {
    int wc, i
      , rd_bytes = fifo_request_drain(fifo, &info, 1);//block for reply
    //Loaned "rd_bytes" number of bytes from FIFO vvvvvvvvvvvvvvvvvvvvvvvvv
    if(rd_bytes < 0) continue;// Nothing to read! Not an error in this case
    if(!rd_bytes) break; //FIFO closed.  Do NOT check fifo->done
    if((rd_bytes & 0x3) != 0) {
      fprintf(stderr, "fifo_request_drain returned non-aligned data %d bytes"
          , rd_bytes);
      return rd_bytes;
    }
    for(i = 0; i < rd_bytes; ++i) {
      unsigned char msg = *(((unsigned char*)info.addr) + i);
      printf("%02X ", msg);
    }
    for(wc = 0; wc < rd_bytes; ) {
      int new_wc = fwrite((const char*)info.addr + wc, 1, (rd_bytes - wc)
        , of); //_write(of, info.addr, rd_bytes);
      // TODO: Should check for new_wc < 0 (error) and new_wc == 0 (EOF?)
      wc += new_wc;
    }
    fifo_drained(fifo, rd_bytes);//return ALL bytes I borrowed ^^^^^^^^^^^^
  }
  fclose(of);//_close(of);
  return 0;
}
Example #2
0
void *write_thread(void *arg)
{
  struct xillyfifo *fifo = arg;
  int do_bytes, written_bytes;
  struct xillyinfo info;
  unsigned char *buf;

  while (1) {
    do_bytes = fifo_request_drain(fifo, &info);

    if (do_bytes == 0)
      return NULL;

    for (buf = info.addr; do_bytes > 0;
	 buf += written_bytes, do_bytes -= written_bytes) {

      written_bytes = write(1, buf, do_bytes);

      if ((written_bytes < 0) && (errno != EINTR)) {
	perror("write() failed");
	return NULL;
      }

      if (written_bytes == 0) {
	fprintf(stderr, "Reached write EOF (?!)\n");
	fifo_done(fifo);
	return NULL;
      }

      if (written_bytes < 0) { // errno is EINTR
	written_bytes = 0;
	continue;
      }
      
      fifo_drained(fifo, written_bytes);
    }
  }
}