Newer
Older
Import / research / embedded / src / tests / debug2.c
/*

   Compile with:
         gcc debug2.c -I ../library -o debug2 -DARCH_MacOSX64

   Trace with:
         sudo dtruss ./test

*/
#include <termios.h>
#include <stdio.h>
#include <oslayer.c>
#include <corelayer.c>

/*
#include <unistd.h>
#include <sys/fcntl.h>
extern "C" {
ssize_t	read(int, void *, size_t) __DARWIN_ALIAS_C(read);
int open(const char *path, int oflag, ...);
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, timeval *timeout);
}
*/

#define IOCTL_GET_FLAGS       0x40487413
#define IOCTL_SET_FLAGS_FLUSH 0x80487416

/*

open("/dev/tty\0", 0x6, 0x7FFF5F919C60)          = 3 0
ioctl(0x3, 0x40487413, 0x7FFF5F919BE0)           = 0 0
ioctl(0x3, 0x80487416, 0x7FFF5F919BE0)           = 0 0

select(0x4, 0x7FFF5F919B08, 0x7FFF5F919A88, 0x7FFF5F919A08, 0x0)                 = 1 0
read(0x3, "\n\0", 0x1)           = 1 0

select(0x4, 0x7FFF5F919B08, 0x7FFF5F919A88, 0x7FFF5F919A08, 0x0)                 = 1 0
read(0x3, "\n\0", 0x1)           = 1 0

*/


int main()
{
  struct termios tty;
  struct termios oldTty;

  int fd = fsOpen("/dev/tty", O_NONBLOCK | O_RDWR);

  sysIoctl(fd, IOCTL_GET_FLAGS, &tty);
  tcgetattr(fd, &tty); // get tty setting

  oldTty = tty;
  tty.c_lflag &= ~( ICANON | ECHO );
  tty.c_cc[ VMIN ] = 1;
  tty.c_cc[ VTIME ] = 0;

  tcsetattr(fd, TCSAFLUSH, &tty); // install
  sysIoctl(fd, IOCTL_SET_FLAGS_FLUSH, &tty);

  for (int i = 0; i < 10; i++)
  {
    int ch;
    fd_set rs, ws, xs;
    FD_ZERO(&rs);
    FD_ZERO(&ws);
    FD_ZERO(&xs);
    FD_SET(fd, &rs);

    sysSelect(fd+1, &rs, &ws, &xs, 0);
    if (fsRead(fd, &ch, 1) == -1)
    {
      strPrintf("no data\n");
    }

    strPrintf("got ch: %x (%c)\n", ch, ch);
  }

  tcsetattr(fd, TCSAFLUSH, &oldTty); // install
  return 0;
}