/*
 * Open the physical device.
 */
static int ProtocolPortOpen(void)
{
#ifdef PPP_DEV
    int pppcom;
    uint32_t lctl;

    /* Open PPP device. Specify physical device, user and password. */
    printf("Open PPP interface at " PPP_DEV_NAME "...");
    pppcom = _open("ppp:" PPP_DEV_NAME "/" PPP_USER "/" PPP_PASS, _O_RDWR | _O_BINARY);
    if (pppcom == -1) {
        puts("failed");
        return -1;
    }

    /*
     * Set PPP line speed.
     */
    lctl = PPP_SPEED;
    _ioctl(pppcom, UART_SETSPEED, &lctl);
    printf("%lu baud", lctl);

    /*
     * The PPP driver doesn't set any receive timeout, but
     * may require it.
     */
    lctl = PPP_RXTO;
    _ioctl(pppcom, UART_SETREADTIMEOUT, &lctl);
    printf(", %lu ms timeout\n", lctl);

    return pppcom;
#else
    /* Nothing to be done for Ethernet. */
    return 0;
#endif
}
Exemple #2
0
static int
read_frame(void)
{
    struct v4l2_buffer buf;

    CLEAR (buf);

    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory = V4L2_MEMORY_MMAP;

    if (-1 == _ioctl (video.fd, VIDIOC_DQBUF, &buf)) {
        switch (errno) {
            case EAGAIN:
                return 0;

            case EIO:
                /* Could ignore EIO, see spec. */
                /* fall through */

            default:
                assert(0, "failure on ioctl.VIDIOC_DQBUF");
        }
    }

    assert(buf.index < n_buffers, "non-fatal assert");

    thread_lock(video.array);
    video.array.val = buffers[buf.index].start;
    thread_cond_broadcast(&video.array_new);

    assert_fatal(-1 != _ioctl (video.fd, VIDIOC_QBUF, &buf),
            "failure on ioctl.VIDIOC_QBUF");

    return 1;
}
Exemple #3
0
int
tcflow(int fd, int action)
{
	struct termios term;
	u_char c;

	switch (action) {
	case TCOOFF:
		return (_ioctl(fd, TIOCSTOP, 0));
	case TCOON:
		return (_ioctl(fd, TIOCSTART, 0));
	case TCION:
	case TCIOFF:
		if (tcgetattr(fd, &term) == -1)
			return (-1);
		c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
		if (c != _POSIX_VDISABLE && _write(fd, &c, sizeof(c)) == -1)
			return (-1);
		return (0);
	default:
		errno = EINVAL;
		return (-1);
	}
	/* NOTREACHED */
}
void devEthernet::_setFlag(const ACE_UINT32 flag, const bool newSetting) {
	ACE_TRACE("devEthernet::_setFlag");
	Linux::ifreq ifr;

	try {
		_ioctl(SIOCGIFFLAGS, ifr, "Error getting flags for interface " + getIfaceName() + " via SIOCGIFFLAGS: ");
	}
	catch (const IOCtlFailed& e) {
		MOD_ERROR("%s", e.what());
		return;
	}

	std::bitset<numFlags> flags(ifr.ifr_flags);

	if ( flags[flag] != newSetting ) {
		flags[flag] = newSetting;
		ifr.ifr_flags = flags.to_ulong();

		// IOCtlFailed is thrown if there's an error
		try {
			_ioctl(SIOCSIFFLAGS, ifr, "Error setting flags via SIOCGIFFLAGS: ");
		}
		catch (const IOCtlFailed& e) {
			MOD_ERROR("%s", e.what());
			return;
		}
	}
}
Exemple #5
0
int
tcsendbreak(int fd, int len __unused)
{
	struct timeval sleepytime;

	sleepytime.tv_sec = 0;
	sleepytime.tv_usec = 400000;
	if (_ioctl(fd, TIOCSBRK, 0) == -1)
		return (-1);
	(void)_select(0, 0, 0, 0, &sleepytime);
	if (_ioctl(fd, TIOCCBRK, 0) == -1)
		return (-1);
	return (0);
}
Exemple #6
0
static void
init_mmap(void)
{
    struct v4l2_requestbuffers req;

    CLEAR (req);

    req.count               = 4;
    req.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    req.memory              = V4L2_MEMORY_MMAP;

    if (-1 == _ioctl (video.fd, VIDIOC_REQBUFS, &req)) {
        if (EINVAL == errno) {
            assert_fatal(0, "%s does not support "
                "memory mapping", video.dev_name);
        } else {
            assert_fatal(0, "failure on ioctl.VIDIOC_REQBUFS");
        }
    }

    assert_fatal(req.count >= 2, "Insufficient buffer memory on %s", video.dev_name);

    buffers = calloc (req.count, sizeof (*buffers));

    assert_fatal(buffers, "Out of memory");

    for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
        struct v4l2_buffer buf;

        CLEAR (buf);

        buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory      = V4L2_MEMORY_MMAP;
        buf.index       = n_buffers;

        assert_fatal(-1 != _ioctl (video.fd, VIDIOC_QUERYBUF, &buf),
            "failure on ioctl.VIDIOC_QUERYBUF");

        buffers[n_buffers].length = buf.length;
        buffers[n_buffers].start =
            mmap (NULL /* start anywhere */,
                    buf.length,
                    PROT_READ | PROT_WRITE /* required */,
                    MAP_SHARED /* recommended */,
                    video.fd, buf.m.offset);

        assert_fatal(MAP_FAILED != buffers[n_buffers].start, "mmap failed");
    }
}
Exemple #7
0
int
ioctl(int d, unsigned long request, ...)
{
    libc_func(ioctl, int, int, unsigned long, ...);
    int result;
    va_list ap;
    void* arg;

    /* one cannot reliably forward arbitrary varargs
     * (http://c-faq.com/varargs/handoff.html), but we know that ioctl gets at
     * most one extra argument, and almost all of them are pointers or ints,
     * both of which fit into a void*.
     */
    va_start(ap, request);
    arg = va_arg(ap, void*);
    va_end(ap);

    result = ioctl_emulate(d, request, arg);
    if (result != UNHANDLED) {
	DBG(DBG_IOCTL, "ioctl fd %i request %lX: emulated, result %i\n", d, request, result);
	return result;
    }

    /* call original ioctl */
    result = _ioctl(d, request, arg);
    DBG(DBG_IOCTL, "ioctl fd %i request %lX: original, result %i\n", d, request, result);

    if (result != -1 && ioctl_record_fd == d)
	record_ioctl(request, arg, result);

    return result;
}
Exemple #8
0
/*!
 * \brief Perform PPP control functions.
 *
 * \param dev  Identifies the device that receives the device-control
 *             function.
 * \param req  Requested control function. May be set to one of the
 *             following constants:
 *             - LCP_OPEN
 *             - LCP_CLOSE
 *             - LCP_LOWERUP
 *             - LCP_LOWERDOWN
 *             Any other function will be passed to the physical driver.
 *
 * \param conf Points to a buffer that contains any data required for
 *             the given control function or receives data from that
 *             function.
 * \return 0 on success, -1 otherwise.
 *
 */
static int NutPppIOCtl(NUTDEVICE * dev, int req, void *conf)
{
    int rc = 0;

    switch (req) {
    case LCP_OPEN:
        LcpOpen(dev);
        break;

    case LCP_CLOSE:
        LcpClose(dev);
        break;

    case LCP_LOWERUP:
        LcpLowerUp(dev);
        break;

    case LCP_LOWERDOWN:
        LcpLowerDown(dev);
        break;

    default:
        rc = _ioctl(((PPPDCB *) (dev->dev_dcb))->dcb_fd, req, conf);
        break;
    }
    return rc;
}
Exemple #9
0
/*
 * Main application routine.
 */
int main(void)
{
    uint32_t baud = 115200;

    /*
     * Register the UART device, open it, assign stdout to it and set
     * the baudrate.
     */
    NutRegisterDevice(&DEV_CONSOLE, 0, 0);
    freopen(DEV_CONSOLE.dev_name, "w", stdout);
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
    puts("\n\nNut/OS timer sample");

    /*
     * Display some general info.
     */
    printf("CPU running at %lu Hz\n", NutGetCpuClock());
    printf("%lu system ticks per second\n", NutTimerMillisToTicks(1000));

    /*
     * Run the demos.
     */
    OneShotDemo(10);
    PeriodicDemo(2);
    DelayDemo();
    SleepDemo(); /* Never returns. */

    return 0;
}
Exemple #10
0
int getFrameSize( int fd, int index, uint32_t code, uint32_t frameSize[6] )
{
  struct v4l2_frmsizeenum vfse;
  CLEAR( vfse );
  vfse.index = index;
  vfse.pixel_format = code;

  int res = _ioctl( fd, VIDIOC_ENUM_FRAMESIZES, &vfse );

  if ( res < 0 ) { return res; }

  switch ( vfse.type ) {
  case V4L2_FRMSIZE_TYPE_DISCRETE:
    frameSize[0] = vfse.discrete.width;
    frameSize[1] = vfse.discrete.width;
    frameSize[2] = 0;
    frameSize[3] = vfse.discrete.height;
    frameSize[4] = vfse.discrete.height;
    frameSize[5] = 0;
    return res;

  case V4L2_FRMSIZE_TYPE_CONTINUOUS:
  case V4L2_FRMSIZE_TYPE_STEPWISE:
    frameSize[0] = vfse.stepwise.min_width;
    frameSize[1] = vfse.stepwise.max_width;
    frameSize[2] = vfse.stepwise.step_width;
    frameSize[3] = vfse.stepwise.min_height;
    frameSize[4] = vfse.stepwise.max_height;
    frameSize[5] = vfse.stepwise.step_height;
    return res;
  }

  return res;
}
Exemple #11
0
int mmapQueryBuffer( int fd, uint32_t index, uint32_t* length, void** start )
{
  struct v4l2_buffer buf;
  CLEAR( buf );
  buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  buf.memory      = V4L2_MEMORY_MMAP;
  buf.index       = index;

  int res = _ioctl( fd, VIDIOC_QUERYBUF, &buf );

  if ( res < 0 ) { return res; }

  *length = buf.length;
  *start = mmap( NULL /* start anywhere */,
                 buf.length,
                 PROT_READ | PROT_WRITE /* required */,
                 MAP_SHARED /* recommended */,
                 fd, buf.m.offset );

  if ( *start == MAP_FAILED ) {
    return -1;
  } else {
    return 0;
  }
}
Exemple #12
0
int
tcsetpgrp(int fd, pid_t pgrp)
{
	int s;

	s = pgrp;
	return (_ioctl(fd, TIOCSPGRP, &s));
}
Exemple #13
0
int sockatmark(int s)
{
	int atmark;

	if (_ioctl(s, SIOCATMARK, &atmark) == -1)
		return -1;
	return atmark;
}
Exemple #14
0
/**
 * Tells the webcam to go into streaming mode, or to
 * stop streaming.
 * When going into streaming mode, it also creates
 * a thread running the webcam_streaming function.
 * When exiting the streaming mode, it sets the streaming
 * bit to false, and waits for the thread to finish.
 */
void webcam_stream(struct webcam *w, bool flag)
{
    uint8_t i;

    struct v4l2_buffer buf;
    enum v4l2_buf_type type;

    if (flag) {
        // Clear buffers
        for (i = 0; i < w->nbuffers; i++) {
            CLEAR(buf);
            buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
            buf.memory = V4L2_MEMORY_MMAP;
            buf.index = i;

            if (-1 == _ioctl(w->fd, VIDIOC_QBUF, &buf)) {
                fprintf(stderr, "[v4l2] Error clearing buffers on %s\n", w->name);
                return;
            }
        }

        // Turn on streaming
        type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        if (-1 == _ioctl(w->fd, VIDIOC_STREAMON, &type)) {
            fprintf(stderr, "[v4l2] Could not turn on streaming on %s\n", w->name);
            return;
        }

        // Set streaming to true and start thread
        w->streaming = true;
        pthread_create(&w->thread, NULL, webcam_streaming, (void *)w);
    } else {
        // Set streaming to false and wait for thread to finish
        w->streaming = false;
        pthread_join(w->thread, NULL);

        // Turn off streaming
        type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        if (-1 == _ioctl(w->fd, VIDIOC_STREAMOFF, &type)) {
            fprintf(stderr, "[v4l2] Could not turn streaming off on %s\n", w->name);
            return;
        }
    }
}
/*
 * Hang up.
 */
static void ProtocolPortClose(int pppcom)
{
#ifdef PPP_DEV
    /* Set the UART back to normal mode. */
    _ioctl(pppcom, HDLC_SETIFNET, NULL);
    /* Close the physical port. This may or may not hang up. Please
       check the modem's documentation. */
    _close(pppcom);
#endif
}
void alarmBeep(void)
{
	u_long baud = DBG_BAUDRATE;
	
	NutRegisterDevice(&DBG_DEVICE, 0, 0);
	freopen(DBG_DEVNAME, "w", stdout);
	_ioctl(_fileno(stdout), UART_SETSPEED, &baud);
	
	VsBeep(440,10000);
}
Exemple #17
0
/*static */
void Main::init_term() {
	FILE *uart_terminal;
	u_long baud = 57600;

	btn_hardware_init();
	NutRegisterDevice(&APP_UART, 0, 0);
	uart_terminal = fopen(APP_UART.dev_name, "r+");    
	_ioctl(_fileno(uart_terminal), UART_SETSPEED, &baud);
	freopen(APP_UART.dev_name, "w", stdout);   
}
Exemple #18
0
pid_t
tcgetsid(int fd)
{
	int s;

	if (_ioctl(fd, TIOCGSID, &s) < 0)
		return ((pid_t)-1);

	return ((pid_t)s);
}
Exemple #19
0
pid_t
tcgetpgrp(int fd)
{
	int s;

	if (_ioctl(fd, TIOCGPGRP, &s) < 0)
		return ((pid_t)-1);

	return ((pid_t)s);
}
/*
 * Open serial debug port for standard output.
 */
static void DebugPortOpen(void)
{
    uint32_t baud = 115200;

    /* Register debug UART. */
    NutRegisterDevice(&DEV_CONSOLE, 0, 0);
    /* Open debug device for standard output. */
    freopen(DEV_CONSOLE_NAME, "w", stdout);
    /* Set baud rate. */
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
}
Exemple #21
0
int
tcsetsid(int fd, pid_t pid)
{

	if (pid != getsid(0)) {
		errno = EINVAL;
		return (-1);
	}

	return (_ioctl(fd, TIOCSCTTY, NULL));
}
bool devEthernet::_checkFlag(const ACE_UINT32 flag) {
	ACE_TRACE("devEthernet::_checkFlag");

	Linux::ifreq ifr;

	_ioctl(SIOCGIFFLAGS, ifr, "Error getting flags for interface " + getIfaceName() + " via SIOCGIFFLAGS: ");

	std::bitset<numFlags> flags(ifr.ifr_flags);

	return flags[flag];
}
Exemple #23
0
char *
fdevname_r(int fd, char *buf, int len)
{
	struct fiodgname_arg fgn;

	fgn.buf = buf;
	fgn.len = len;

	if (_ioctl(fd, FIODGNAME, &fgn) == -1)
		return (NULL);
	return (buf);
}
Exemple #24
0
int mmapEnqueueBuffer( int fd, uint32_t index )
{
  struct v4l2_buffer buf;
  CLEAR( buf );

  buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  buf.memory = V4L2_MEMORY_MMAP;
  buf.index = index;

  int res = _ioctl( fd, VIDIOC_QBUF, &buf );
  return res;
}
Exemple #25
0
/**
 * Reads a frame from the webcam, converts it into the RGB colorspace
 * and stores it in the webcam structure
 */
static void webcam_read(struct webcam *w)
{
    struct v4l2_buffer buf;

    // Try getting an image from the device
    for(;;) {
        CLEAR(buf);
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory = V4L2_MEMORY_MMAP;

        // Dequeue a (filled) buffer from the video device
        if (-1 == _ioctl(w->fd, VIDIOC_DQBUF, &buf)) {
            switch(errno) {
                case EAGAIN:
                    continue;

                case EIO:
                default:
                    fprintf(stderr, "[v4l2] %d: Could not read from device %s\n", errno, w->name);
                    break;
            }
        }

        // Make sure we are not out of bounds
        assert(buf.index < w->nbuffers);

        // Lock frame mutex, and store RGB
        pthread_mutex_lock(&w->mtx_frame);
        convertToRGB(w->buffers[buf.index], &w->frame);
        pthread_mutex_unlock(&w->mtx_frame);
        break;
    }

    // Queue buffer back into the video device
    if (-1 == _ioctl(w->fd, VIDIOC_QBUF, &buf)) {
        fprintf(stderr, "[v4l2] Error while swapping buffers on %s\n", w->name);
        return;
    }
}
Exemple #26
0
int
tcsetattr(int fd, int opt, const struct termios *t)
{
	struct termios localterm;

	if (opt & TCSASOFT) {
		localterm = *t;
		localterm.c_cflag |= CIGNORE;
		t = &localterm;
	}
	switch (opt & ~TCSASOFT) {
	case TCSANOW:
		return (_ioctl(fd, TIOCSETA, t));
	case TCSADRAIN:
		return (_ioctl(fd, TIOCSETAW, t));
	case TCSAFLUSH:
		return (_ioctl(fd, TIOCSETAF, t));
	default:
		errno = EINVAL;
		return (-1);
	}
}
void devEthernet::setMTU(const size_t mtu) {
	ACE_TRACE("devEthernet::setMTU");
	Linux::ifreq ifr;
	ifr.ifr_mtu = mtu;

	MOD_DEBUG("Setting MTU to %d.", mtu);

	// IOCtlFailed is thrown if there's an error
	_ioctl(SIOCSIFMTU, ifr, "Error setting MTU via SIOCSIFMTU: ");
	CEcfg::instance()->getOrAddInt(cfgKey("MTU"), static_cast<int>(mtu));

	MOD_DEBUG("Finished setting MTU.");
}
size_t devEthernet::getMTU() {
	ACE_TRACE("devEthernet::getMTU");
	Linux::ifreq ifr;

	MOD_DEBUG("Getting MTU from device.");

	// IOCtlFailed is thrown if there's an error
	_ioctl(SIOCGIFMTU, ifr, "Error getting MTU via SIOCGIFMTU: ");

	MOD_DEBUG("Found MTU to be %d.", ifr.ifr_mtu);

	return ifr.ifr_mtu;
}
devEthernet::~devEthernet() {
	MOD_DEBUG("Running ~devEthernet().");
	try {
		_ioctl(SIOCSIFFLAGS, _savedFlags, "Error restoring saved flags via SIOCSIFFLAGS: ");
	}
	catch (...) {
	}

	closeSocket();

	if ( _netlinkSocket ) Linux::nl_socket_free(_netlinkSocket);

}
Exemple #30
0
static int
handle_dkio_partitions(int des, int request, int arg)
{
	struct s5_dk_cinfo	cinfo;
	struct dk_allmap	map;
	struct dk_map		*part;
	int			ret;
	extern int		errno;

	part = (struct dk_map *) arg;
	
	ret = _ioctl(des, S5DKIOCINFO, &cinfo);

	if ((cinfo.dki_partition < 0) || (cinfo.dki_partition >= NDKMAP)) {
		errno = EINVAL;
		return (-1);
	}
	
	if (ret != -1) {
		ret = _ioctl(des, S5DKIOCGAPART, &map);
		if (ret != -1) {
			if (request == DKIOCGPART) {
				part->dkl_cylno =
				    map.dka_map[cinfo.dki_partition].dkl_cylno;
				part->dkl_nblk = 
				    map.dka_map[cinfo.dki_partition].dkl_nblk;
			} else {
				map.dka_map[cinfo.dki_partition].dkl_cylno = 
					part->dkl_cylno;
				map.dka_map[cinfo.dki_partition].dkl_nblk =
					part->dkl_nblk;
				ret = _ioctl(des, S5DKIOCSAPART, &map);
			}
		}
	}
	return (ret);
}