Exemple #1
0
// NewSPI returns a new SPI object that is connected to the
// specified SPI device interface.
//
// NewSPI(X,Y) will open /dev/spidev-X.Y
//
// SPI is an object type that allows SPI transactions
// on hosts running the Linux kernel. The host kernel must have SPI
// support and SPI device interface support.
// All of these can be either built-in to the kernel, or loaded from modules.
//
// Because the SPI device interface is opened R/W, users of this
// module usually must have root permissions.
func NewSPI(bus, device int) (spi *SPI, err error) {
	err = embedded.LoadDeviceTree(fmt.Sprintf("%s%d", deviceTreePrefix, bus))
	if err != nil {
		return nil, err
	}

	spi = &SPI{bus: bus, device: device}

	path := fmt.Sprintf("/dev/spidev%d.%d", bus+1, device)
	spi.file, err = os.OpenFile(path, os.O_RDWR, 0)
	if err != nil {
		return nil, err
	}

	r, _, err := syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_MODE, uintptr(unsafe.Pointer(&spi.mode)))
	if r != 0 {
		return nil, err
	}

	r, _, err = syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_BITS_PER_WORD, uintptr(unsafe.Pointer(&spi.bitsPerWord)))
	if r != 0 {
		return nil, err
	}

	r, _, err = syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_MAX_SPEED_HZ, uintptr(unsafe.Pointer(&spi.maxSpeedHz)))
	if r != 0 {
		return nil, err
	}

	return spi, nil
}
func setCommState(h syscall.Handle) error {
	var params structDCB
	params.DCBlength = uint32(unsafe.Sizeof(params))

	//params.flags[0] = 0x01  // fBinary
	//params.flags[0] |= 0x10 // Assert DSR

	params.flags[0] = 0x00

	params.BaudRate = uint32(9600)
	params.ByteSize = 8
	params.Parity = 0
	params.StopBits = 0
	//Public Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
	//   ByRef lpDCB As DCB) As Boolean
	r, _, err := syscall.Syscall(nSetCommState, 2, uintptr(h), uintptr(unsafe.Pointer(&params)), 0)

	fmt.Println("SetCommState : ", r, err)

	if r == 0 {
		_, _, err := syscall.Syscall(nGetLastError, 1, uintptr(h), 0, 0)
		fmt.Println("LastError : ", err)
		return nil
	}
	return nil
}
Exemple #3
0
func TestFormatMessage(t *testing.T) {
	dll := windows.MustLoadDLL("pdh.dll")

	pdhOpenQuery := func(datasrc *uint16, userdata uint32, query *windows.Handle) (errno uintptr) {
		r0, _, _ := syscall.Syscall(dll.MustFindProc("PdhOpenQueryW").Addr(), 3, uintptr(unsafe.Pointer(datasrc)), uintptr(userdata), uintptr(unsafe.Pointer(query)))
		return r0
	}

	pdhCloseQuery := func(query windows.Handle) (errno uintptr) {
		r0, _, _ := syscall.Syscall(dll.MustFindProc("PdhCloseQuery").Addr(), 1, uintptr(query), 0, 0)
		return r0
	}

	var q windows.Handle
	name, err := windows.UTF16PtrFromString("no_such_source")
	if err != nil {
		t.Fatal(err)
	}
	errno := pdhOpenQuery(name, 0, &q)
	if errno == 0 {
		pdhCloseQuery(q)
		t.Fatal("PdhOpenQuery succeeded, but expected to fail.")
	}

	const flags uint32 = syscall.FORMAT_MESSAGE_FROM_HMODULE | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS
	buf := make([]uint16, 300)
	_, err = windows.FormatMessage(flags, uintptr(dll.Handle), uint32(errno), 0, buf, nil)
	if err != nil {
		t.Fatal("FormatMessage for handle=%x and errno=%x failed: %v", dll.Handle, errno, err)
	}
}
Exemple #4
0
// Tested on Suzy-Q and BeagleBone.
func openConsole(con string) (rc io.ReadCloser, err error) {
	fd, err := syscall.Open(con, syscall.O_RDONLY|syscall.O_NOCTTY|syscall.O_SYNC, 0)
	if err != nil {
		return nil, fmt.Errorf("failed to open console file: %v", err)
	}
	defer func() {
		if fd != -1 {
			syscall.Close(fd)
		}
	}()
	var term Termios
	if _, _, errno := syscall.Syscall(SYS_IOCTL, uintptr(fd), TCGETS2, uintptr(unsafe.Pointer(&term))); errno != 0 {
		return nil, fmt.Errorf("failed to get console termios: %v", errno)
	}
	// no parity bit, only need 1 stop bit, no hardware flowcontrol
	term.Cflag &^= CBAUD | CSIZE | PARENB | CSTOPB | CRTSCTS
	// ignore modem controls
	term.Cflag |= B115200 | CS8 | CLOCAL | CREAD
	// setup for non-canonical mode
	term.Iflag &^= IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON
	term.Lflag &^= ECHO | ECHONL | ICANON | ISIG | IEXTEN
	term.Oflag &^= OPOST
	term.Cc[VMIN] = 0
	term.Cc[VTIME] = 10 // 1 second timeout
	if _, _, errno := syscall.Syscall(SYS_IOCTL, uintptr(fd), TCSETS2, uintptr(unsafe.Pointer(&term))); errno != 0 {
		return nil, fmt.Errorf("failed to get console termios: %v", errno)
	}
	tmp := fd
	fd = -1
	return &tty{fd: tmp}, nil
}
Exemple #5
0
// Xfer performs a SPI transaction.
// CS will be released and reactivated between blocks.
// delay specifies delay in usec between blocks.
func (spi *SPI) Xfer(txBuf []byte, delay_usecs uint16) (rxBuf []byte, err error) {
	length := len(txBuf)
	rxBuf = make([]byte, length)

	xfer := make([]spi_ioc_transfer, length)
	for i := range xfer {
		xfer[i].tx_buf = uintptr(unsafe.Pointer(&txBuf[i]))
		xfer[i].rx_buf = uintptr(unsafe.Pointer(&rxBuf[i]))
		xfer[i].len = 1
		xfer[i].delay_usecs = delay_usecs
	}

	SPI_IOC_MESSAGE := C._IOC_WRITE<<C._IOC_DIRSHIFT | C.SPI_IOC_MAGIC<<C._IOC_TYPESHIFT | length<<C._IOC_SIZESHIFT

	r, _, err := syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), uintptr(SPI_IOC_MESSAGE), uintptr(unsafe.Pointer(&xfer[0])))
	if r != 0 {
		return nil, err
	}

	// WA:
	// in CS_HIGH mode CS isn't pulled to low after transfer, but after read
	// reading 0 bytes doesnt matter but brings cs down
	syscall.Syscall(syscall.SYS_READ, spi.file.Fd(), uintptr(unsafe.Pointer(&rxBuf[0])), 0)

	return rxBuf, nil
}
Exemple #6
0
// Configure configures f as a 8N1 serial port with the specified baudrate.
func Configure(f fder, baudrate uint32) error {
	var termios syscall.Termios
	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(f.Fd()), uintptr(syscall.TCGETS), uintptr(unsafe.Pointer(&termios))); err != 0 {
		return err
	}

	termios.Ispeed = baudrate
	termios.Ospeed = baudrate
	termios.Cflag &^= CBAUD
	termios.Cflag &^= CBAUDEX
	termios.Cflag |= baudrate

	// set 8N1
	termios.Cflag &^= syscall.PARENB
	termios.Cflag &^= syscall.CSTOPB
	termios.Cflag &^= syscall.CSIZE
	termios.Cflag |= syscall.CS8

	// Local connection, no modem control
	termios.Cflag |= (syscall.CLOCAL | syscall.CREAD)
	// Disable hardware flow control
	termios.Cflag &^= CRTSCTS
	// Block on a zero read (instead of returning EOF)
	termios.Cc[syscall.VMIN] = 1

	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(f.Fd()), syscall.TCSETS, uintptr(unsafe.Pointer(&termios))); err != 0 {
		return err
	}
	return nil
}
Exemple #7
0
func Console1(forward bool) {
	//
	const (
		xdm = 7
		msg = 10 // messages
	)
	var s stat
	syscall.Syscall(syscall.SYS_IOCTL, 0, _GETSTATE, uintptr(unsafe.Pointer(&s)))
	a := s.active
	if forward {
		switch a {
		case xdm - 1:
			a = xdm + 1
		case msg - 1:
			a = msg + 1
		case maxConsole:
			a = 1
		default:
			a++
		}
	} else {
		switch a {
		case 1:
			a = maxConsole
		case xdm + 1:
			a = xdm - 1
		case msg + 1:
			a = msg - 1
		default:
			a--
		}
	}
	syscall.Syscall(syscall.SYS_IOCTL, 0, _ACTIVATE, uintptr(a))
	syscall.Syscall(syscall.SYS_IOCTL, 0, _WAITACTIVE, uintptr(a))
}
// SaferDLLLoading sets DLL load path to be safer.
func SaferDLLLoading() error {
	kernel32, err := syscall.LoadDLL("kernel32.dll")
	if err != nil {
		return err
	}

	procSetDllDirectoryW, err := kernel32.FindProc("SetDllDirectoryW")
	if err != nil {
		return err
	}
	var zero uint16
	r1, _, e1 := syscall.Syscall(procSetDllDirectoryW.Addr(), 1,
		uintptr(unsafe.Pointer(&zero)), 0, 0)

	procSetDefaultDllDirectories, err := kernel32.FindProc("SetDefaultDllDirectories")
	if err == nil && procSetDefaultDllDirectories.Addr() != 0 {
		r1, _, e1 = syscall.Syscall(procSetDefaultDllDirectories.Addr(), 1,
			loadLibrarySearchSystem32, 0, 0)
		if r1 == 0 {
			return e1
		}
	} else {
		return errors.New("SetDefaultDllDirectories not found - please install KB2533623 for safer DLL loading")
	}

	return nil
}
Exemple #9
0
// enable raw mode and gather metrics, like number of columns
func (l *LineReader) raw() {
	// STD_OUTPUT_HANDLE
	h, errno := syscall.GetStdHandle(-11)
	t.h = uintptr(h)
	if int32(t.h) == -1 {
		err := os.Errno(errno)
		panic(err)
	}
	ok, _, e := syscall.Syscall(procGetConsoleMode, 2,
		t.h, uintptr(unsafe.Pointer(&t.origTerm)), 0)
	if ok == 0 {
		err := os.NewSyscallError("GetConsoleMode", int(e))
		panic(err)
	}

	raw := t.origTerm
	raw &^= _ENABLE_LINE_INPUT | _ENABLE_ECHO_INPUT | _ENABLE_PROCESSED_INPUT | _ENABLE_WINDOW_INPUT
	ok, _, e = syscall.Syscall(procSetConsoleMode, 2, t.h, uintptr(raw), 0)
	if ok == 0 {
		err := os.NewSyscallError("SetConsoleMode", int(e))
		panic(err)
	}

	win := t.getConsoleInfo()
	t.cols = int(win.dwSize.x)
	t.rows = int(win.dwSize.y)

	t.buf = new(buffer)
}
Exemple #10
0
func (p *Port) setControlSignal(sigmask uint, level bool) error {
	state := uint(0)
	// get current state
	_, _, errno := syscall.Syscall(
		syscall.SYS_IOCTL,
		p.file.Fd(),
		uintptr(syscall.TIOCMGET),
		uintptr(unsafe.Pointer(&state)))
	if errno != 0 {
		return fmt.Errorf("ioctl TIOCMGET %d", errno)
	}
	// modify state
	switch level {
	case true:
		state |= sigmask // set
	case false:
		state &^= sigmask // clear
	}
	// set new state
	_, _, errno = syscall.Syscall(
		syscall.SYS_IOCTL,
		p.file.Fd(),
		uintptr(syscall.TIOCMSET),
		uintptr(unsafe.Pointer(&state)))
	if errno != 0 {
		return fmt.Errorf("ioctl TIOCMSET %d", errno)
	}
	return nil
}
Exemple #11
0
// SetNOCOWFile sets NOCOW flag for file
func SetNOCOWFile(path string) error {
	file, err := os.Open(path)
	if err != nil {
		return err
	}
	defer file.Close()

	fileinfo, err := file.Stat()
	if err != nil {
		return err
	}
	if fileinfo.IsDir() {
		return fmt.Errorf("skip directory")
	}
	if fileinfo.Size() != 0 {
		return fmt.Errorf("skip nonempty file")
	}

	var attr int
	if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), FS_IOC_GETFLAGS, uintptr(unsafe.Pointer(&attr))); errno != 0 {
		return errno
	}
	attr |= FS_NOCOW_FL
	if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), FS_IOC_SETFLAGS, uintptr(unsafe.Pointer(&attr))); errno != 0 {
		return errno
	}
	log.Infof("Set NOCOW to path %v succeeded", path)
	return nil
}
Exemple #12
0
// NewSPI returns a new SPI object that is connected to the
// specified SPI device interface.
//
// NewSPI(X,Y) will open /dev/spidev-X.Y
//
// SPI is an object type that allows SPI transactions
// on hosts running the Linux kernel. The host kernel must have SPI
// support and SPI device interface support.
// All of these can be either built-in to the kernel, or loaded from modules.
//
// Because the SPI device interface is opened R/W, users of this
// module usually must have root permissions.
func NewSPI(bus, device int) (*SPI, error) {
	deviceTreeName := fmt.Sprintf("ADAFRUIT-SPI%d", bus)
	err := LoadDeviceTree(deviceTreeName)
	if err != nil {
		return nil, err
	}

	spi := new(SPI)

	path := fmt.Sprintf("/dev/spidev%d.%d", bus+1, device)
	spi.file, err = os.OpenFile(path, os.O_RDWR, 0)
	if err != nil {
		return nil, err
	}

	r, _, err := syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_MODE, uintptr(unsafe.Pointer(&spi.mode)))
	if r != 0 {
		return nil, err
	}

	r, _, err = syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_BITS_PER_WORD, uintptr(unsafe.Pointer(&spi.bitsPerWord)))
	if r != 0 {
		return nil, err
	}

	r, _, err = syscall.Syscall(syscall.SYS_IOCTL, spi.file.Fd(), C.SPI_IOC_RD_MAX_SPEED_HZ, uintptr(unsafe.Pointer(&spi.maxSpeedHz)))
	if r != 0 {
		return nil, err
	}

	return spi, nil
}
Exemple #13
0
func main() {
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() < 1 {
		usage()
	}

	vtnum, err := strconv.Atoi(flag.Arg(0))
	ck(err)

	consoles := []string{"/dev/console", "/dev/vc/0", "/dev/tty"}
	var f *os.File
	for _, cc := range consoles {
		f, err = os.OpenFile(cc, os.O_RDWR, 0644)
		if err == nil {
			break
		}
	}
	if f == nil {
		fmt.Fprintln(os.Stderr, "chvt: couldn't get file descriptor referring to console")
		os.Exit(1)
	}

	fd := f.Fd()
	_, _, err = syscall.Syscall(syscall.SYS_IOCTL, fd, 0x5606, uintptr(vtnum))
	if err != nil {
		_, _, err = syscall.Syscall(syscall.SYS_IOCTL, fd, 0x5607, uintptr(vtnum))
	}
	f.Close()

	if err != nil {
		fmt.Fprintln(os.Stderr, "chvt:", err)
		os.Exit(1)
	}
}
Exemple #14
0
// NewLiner initializes a new *State, and sets the terminal into raw mode. To
// restore the terminal to its previous state, call State.Close().
// NewLiner handles SIGWINCH, so it will leak a channel every time you call
// it. Therefore, it is recommened that NewLiner only be called once.
func NewLiner() *State {
	bad := map[string]bool{"": true, "dumb": true, "cons25": true}
	var s State
	s.r = bufio.NewReader(os.Stdin)
	s.supported = !bad[strings.ToLower(os.Getenv("TERM"))]

	if s.supported {
		syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdin), getTermios, uintptr(unsafe.Pointer(&s.origMode)))
		mode := s.origMode
		mode.Iflag &^= icrnl | inpck | istrip | ixon
		mode.Cflag |= cs8
		mode.Lflag &^= syscall.ECHO | icanon | iexten
		syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdin), setTermios, uintptr(unsafe.Pointer(&mode)))

		winch := make(chan os.Signal, 1)
		signal.Notify(winch, syscall.SIGWINCH)
		s.winch = winch

		next := make(chan nexter)
		go func() {
			for {
				var n nexter
				n.r, _, n.err = s.r.ReadRune()
				next <- n
			}
		}()
		s.next = next
	}

	return &s
}
Exemple #15
0
// MsglvlSet returns the read-msglvl, post-set-msglvl of the given interface.
func (e *Ethtool) MsglvlSet(intf string, valset uint32) (uint32, uint32, error) {
	edata := ethtoolValue{
		cmd: ETHTOOL_GMSGLVL,
	}

	var name [IFNAMSIZ]byte
	copy(name[:], []byte(intf))

	ifr := ifreq{
		ifr_name: name,
		ifr_data: uintptr(unsafe.Pointer(&edata)),
	}

	_, _, ep := syscall.Syscall(syscall.SYS_IOCTL, uintptr(e.fd),
		SIOCETHTOOL, uintptr(unsafe.Pointer(&ifr)))
	if ep != 0 {
		return 0, 0, syscall.Errno(ep)
	}

	readval := edata.data

	edata.cmd = ETHTOOL_SMSGLVL
	edata.data = valset

	_, _, ep = syscall.Syscall(syscall.SYS_IOCTL, uintptr(e.fd),
		SIOCETHTOOL, uintptr(unsafe.Pointer(&ifr)))
	if ep != 0 {
		return 0, 0, syscall.Errno(ep)
	}

	return readval, edata.data, nil
}
Exemple #16
0
func Console(a uint8) {
	//
	if a <= 0 || a > maxConsole {
		return
	}
	syscall.Syscall(syscall.SYS_IOCTL, 0, _ACTIVATE, uintptr(a))
	syscall.Syscall(syscall.SYS_IOCTL, 0, _WAITACTIVE, uintptr(a))
}
Exemple #17
0
func (w *Window) MoveToScreenCenter() {
	r := w.GetRect()
	sw, _, _ := syscall.Syscall(getSystemMetrics, 1, sm_cxscreen, 0, 0)
	sh, _, _ := syscall.Syscall(getSystemMetrics, 1, sm_cyscreen, 0, 0)
	r.Left = (int(sw) - r.Width) / 2
	r.Top = (int(sh) - r.Height) / 2
	w.SetRect(r)
}
Exemple #18
0
func openInternal(options OpenOptions) (io.ReadWriteCloser, error) {
	// Open the serial port in non-blocking mode, since otherwise the OS will
	// wait for the CARRIER line to be asserted.
	file, err :=
		os.OpenFile(
			options.PortName,
			syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK,
			0600)

	if err != nil {
		return nil, err
	}

	// We want to do blocking I/O, so clear the non-blocking flag set above.
	r1, _, errno :=
		syscall.Syscall(
			syscall.SYS_FCNTL,
			uintptr(file.Fd()),
			uintptr(syscall.F_SETFL),
			uintptr(0))

	if errno != 0 {
		return nil, os.NewSyscallError("SYS_FCNTL", errno)
	}

	if r1 != 0 {
		return nil, errors.New("Unknown error from SYS_FCNTL.")
	}

	// Set standard termios options.
	terminalOptions, err := convertOptions(options)
	if err != nil {
		return nil, err
	}

	err = setTermios(file.Fd(), terminalOptions)
	if err != nil {
		return nil, err
	}

	// Set baud rate with the IOSSIOSPEED ioctl, to support non-standard speeds.
	r2, _, errno2 := syscall.Syscall(
		syscall.SYS_IOCTL,
		uintptr(file.Fd()),
		uintptr(kIOSSIOSPEED),
		uintptr(unsafe.Pointer(&options.BaudRate)))

	if errno2 != 0 {
		return nil, os.NewSyscallError("SYS_IOCTL", errno2)
	}

	if r2 != 0 {
		return nil, errors.New("Unknown error from SYS_IOCTL.")
	}

	// We're done.
	return file, nil
}
Exemple #19
0
func syscallEvent(handle syscall.Handle, event string) error {
	switch event {
	case "setup":
		const cio = 64
		if r, _, err := syscall.Syscall(nSetupComm, 3, uintptr(handle), cio, cio); r == 0 {
			return err
		}
		return nil

	case "state":
		var params DCB
		params.DCBlength = uint32(unsafe.Sizeof(params))
		params.flags[0] = 0x01  // fBinary
		params.flags[0] |= 0x10 // Assert DSR
		// params.BaudRate = uint32(250000)
		params.BaudRate = uint32(115200)
		params.ByteSize = 8

		if r, _, err := syscall.Syscall(nSetCommState, 2, uintptr(handle), uintptr(unsafe.Pointer(&params)), 0); r == 0 {
			return err
		}
		return nil

	case "timeouts":
		//
		//  we have to set some actual values here
		//  or else the Read() will only return 1
		//  character and nothing else
		var timeouts Timeouts
		timeouts.ReadIntervalTimeout = 20
		timeouts.ReadTotalTimeoutMultiplier = 10
		timeouts.ReadTotalTimeoutConstant = 100

		if r, _, err := syscall.Syscall(nSetCommTimeouts, 2, uintptr(handle), uintptr(unsafe.Pointer(&timeouts)), 0); r == 0 {
			return err
		}
		return nil

	case "mask":
		const EV_RXFLAG = 0x0002
		if r, _, err := syscall.Syscall(nSetCommMask, 2, uintptr(handle), EV_RXFLAG, 0); r == 0 {
			return err
		}
		return nil

	case "reset":
		if r, _, err := syscall.Syscall(nResetEvent, 1, uintptr(handle), 0, 0); r == 0 {
			return err
		}
		return nil

	default:
		return fmt.Errorf("[ERROR] invalid event type: %v", event)
	}

	return nil
}
Exemple #20
0
func handleEvents() bool {
	msgRst, _, _ := syscall.Syscall6(GetMessage, 4, msgPtr, 0, 0, 0, 0, 0)
	if msgRst == 0 {
		return false
	}
	syscall.Syscall(TranslateMessage, 1, msgPtr, 0, 0)
	syscall.Syscall(DispatchMessage, 1, msgPtr, 0, 0)
	return true
}
Exemple #21
0
func InitConsole() {
	//
	var m mode
	syscall.Syscall(syscall.SYS_IOCTL, 0, _GETMODE, uintptr(unsafe.Pointer(&m)))
	m.mode = _PROCESS
	m.waitv = 0
	m.relsig = int16(syscall.SIGUSR1)
	m.acqsig = int16(syscall.SIGUSR2)
	syscall.Syscall(syscall.SYS_IOCTL, 0, _SETMODE, uintptr(unsafe.Pointer(&m)))
}
func init_terminal() {
	syscall.Syscall(syscall.SYS_IOCTL, 0, tcgets, uintptr(unsafe.Pointer(&oldtermios)))
	var newtermios = oldtermios
	newtermios.c_lflag &^= 010 // turn off echo
	newtermios.c_lflag &^= 02  // turn off canonical mode
	newtermios.c_cc[6] = 1     // set VMIN to 1
	newtermios.c_cc[5] = 0     // set VTIME to 0
	syscall.Syscall(syscall.SYS_IOCTL, 0, tcsetsf, uintptr(unsafe.Pointer(&newtermios)))

}
func Printf(color int, format string, args ...interface{}) {
	if color < 0 || color >= 16 {
		fmt.Printf(format, args...)
		return
	}
	syscall.Syscall(uintptr(GetConsoleScreenBufferInfo), 2, hnd, uintptr(unsafe.Pointer(&tmp)), 0)
	syscall.Syscall(uintptr(SetConsoleTextAttribute), 2, hnd, uintptr(color), 0)
	fmt.Printf(format, args...)
	syscall.Syscall(uintptr(SetConsoleTextAttribute), 2, hnd, uintptr(tmp.attributes), 0)
}
func resetEvent(h syscall.Handle) error {
	r, _, err := syscall.Syscall(nResetEvent, 1, uintptr(h), 0, 0)

	fmt.Println("SetCommMask : ", r, err)

	if r == 0 {
		_, _, err := syscall.Syscall(nGetLastError, 1, uintptr(h), 0, 0)
		fmt.Println("LastError :", err)
		return nil
	}
	return nil
}
// see https://github.com/skarnet/skalibs/blob/master/src/libstddjb/ndelay_off.c
func ndelay_off(fd FileDescriptor) error {
	// 32-bit will likely break because it needs SYS_FCNTL64
	got, _, e := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), uintptr(syscall.F_GETFL), 0)
	if e != 0 {
		return e
	}
	_, _, e = syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), uintptr(syscall.F_SETFL), uintptr(int(got) & ^syscall.O_NONBLOCK))
	if e != 0 {
		return e
	}
	return nil
}
func setCommMask(h syscall.Handle) error {
	const EV_RXCHAR = 0x0001
	r, _, err := syscall.Syscall(nSetCommMask, 2, uintptr(h), EV_RXCHAR, 0)

	fmt.Println("SetCommMask : ", r, err)

	if r == 0 {
		_, _, err := syscall.Syscall(nGetLastError, 1, uintptr(h), 0, 0)
		fmt.Println("LastError :", err)
		return nil
	}
	return nil
}
Exemple #27
0
// MakeRaw put the terminal connected to the given file descriptor into raw
// mode and returns the previous state of the terminal so that it can be
// restored.
func MakeRaw(fd int) (*State, error) {
	var st uint32
	_, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, uintptr(fd), uintptr(unsafe.Pointer(&st)), 0)
	if e != 0 {
		return nil, error(e)
	}
	raw := st &^ (enableEchoInput | enableProcessedInput | enableLineInput | enableProcessedOutput)
	_, _, e = syscall.Syscall(procSetConsoleMode.Addr(), 2, uintptr(fd), uintptr(raw), 0)
	if e != 0 {
		return nil, error(e)
	}
	return &State{st}, nil
}
func (port *SerialPort) SetDTR(level bool) error {
	var status uint
	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(port.handle), uintptr(syscall.TIOCMGET), uintptr(unsafe.Pointer(&status)))
	if err == 0 {
		if level == false {
			status &= ^uint(syscall.TIOCM_DTR)
		} else {
			status |= syscall.TIOCM_DTR
		}
		_, _, err = syscall.Syscall(syscall.SYS_IOCTL, uintptr(port.handle), uintptr(syscall.TIOCMSET), uintptr(unsafe.Pointer(&status)))
	}
	return err
}
Exemple #29
0
func (s *Serial) SetRTS(state uint8) {
	if s.Opened {
		var status uint
		_, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s.f.Fd()), uintptr(C.TIOCMGET), uintptr(unsafe.Pointer(&status)))
		if err == 0 {
			if state == 0 {
				status &= ^uint(C.TIOCM_RTS)
			} else {
				status |= C.TIOCM_RTS
			}
			syscall.Syscall(syscall.SYS_IOCTL, uintptr(s.f.Fd()), uintptr(C.TIOCMSET), uintptr(unsafe.Pointer(&status)))
		}
	}
}
Exemple #30
-1
// Delete the actual bridge device.
func DeleteBridge(name string) error {
	s, err := getIfSocket()
	if err != nil {
		return err
	}
	defer syscall.Close(s)

	nameBytePtr, err := syscall.BytePtrFromString(name)
	if err != nil {
		return err
	}

	var ifr ifreqFlags
	copy(ifr.IfrnName[:len(ifr.IfrnName)-1], []byte(name))
	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s),
		syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifr))); err != 0 {
		return err
	}

	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s),
		SIOC_BRDELBR, uintptr(unsafe.Pointer(nameBytePtr))); err != 0 {
		return err
	}
	return nil
}