Esempio n. 1
0
func (dev *linuxDevice) writeReport(hid_report_type int, data []byte) error {
	if dev.handle == nil {
		return errors.New("No USB device opend before.")
	}
	if len(data) > 0xffff {
		return errors.New("data longer than 65535 bytes, means overflow, isn't supported")
	}
	if len(data) == 0 {
		return nil
	}

	const reportId = 1
	const index = 0
	const timeout = 1000

	written := C.libusb_control_transfer(dev.handle,
		C.uint8_t(ENDPOINT_OUT|RECIPIENT_DEVICE|DT_REPORT|hid_report_type),
		C.uint8_t(HID_SET_REPORT),
		C.uint16_t(reportId),
		C.uint16_t(index),
		(*C.uchar)(&data[0]),
		C.uint16_t(len(data)),
		C.uint(timeout))

	if int(written) == len(data) {
		return nil
	}
	return usbError(written)
}
Esempio n. 2
0
func SetViewClear(view ViewID, clear ClearOptions, rgba uint32, depth float32, stencil uint8) {
	C.bgfx_set_view_clear(
		C.uint8_t(view),
		C.uint8_t(clear),
		C.uint32_t(rgba),
		C.float(depth),
		C.uint8_t(stencil),
	)
}
Esempio n. 3
0
// Set a pixel through direct memory access
// TODO: higher-level functions
func WritePixel(x int, y int, r byte, g byte, b byte, a byte) {
	if x > C.RESOLUTION_W || y > C.RESOLUTION_H {
		// This is important, because write_byte has no bounds checking!
		panic(fmt.Sprintf("WritePixel out of range: (%d,%d)\n", x, y))
	}
	offset := 4 * (x + y*C.RESOLUTION_W)
	C.write_byte(C.off_t(offset+0), C.uint8_t(r))
	C.write_byte(C.off_t(offset+1), C.uint8_t(g))
	C.write_byte(C.off_t(offset+2), C.uint8_t(b))
	C.write_byte(C.off_t(offset+3), C.uint8_t(a))
}
Esempio n. 4
0
File: target.go Progetto: harikb/nfc
// See documentation in Target for more details.
func (d *ISO14443b2ctTarget) Marshall() uintptr {
	c := (*C.nfc_iso14443b2ct_info)(makeTarget(ISO14443B2CT, d.Baud))

	c.btProdCode = C.uint8_t(d.ProdCode)
	c.btFabCode = C.uint8_t(d.FabCode)

	for i, b := range d.UID {
		c.abtUID[i] = C.uint8_t(b)
	}

	return uintptr(unsafe.Pointer(c))
}
Esempio n. 5
0
File: target.go Progetto: harikb/nfc
func (d *JewelTarget) Marshall() uintptr {
	c := (*C.nfc_jewel_info)(makeTarget(Jewel, d.Baud))

	c.btSensRes[0] = C.uint8_t(d.SensRes[0])
	c.btSensRes[1] = C.uint8_t(d.SensRes[1])

	for i, b := range d.Id {
		c.btId[i] = C.uint8_t(b)
	}

	return uintptr(unsafe.Pointer(c))
}
Esempio n. 6
0
File: usb.go Progetto: hanwen/usb
func (h *DeviceHandle) ControlTransfer(reqType, req byte, value, index uint16,
	data []byte, timeout int) error {
	var ptr *byte
	if len(data) > 0 {
		ptr = &data[0]
	}
	if len(data) > 0xffff {
		return fmt.Errorf("overflow")
	}
	err := C.libusb_control_transfer(h.me(),
		C.uint8_t(reqType), C.uint8_t(req), C.uint16_t(value), C.uint16_t(index),
		(*C.uchar)(ptr), C.uint16_t(len(data)), C.uint(timeout))
	return toErr(err)
}
Esempio n. 7
0
func (ro *ReadOperation) CompareAttribute(name string, operator CompareAttribute, value io.Reader) *ReadOperation {
	n := C.CString(name)
	defer freeString(n)
	bufAddr, bufLen := readerToBuf(value)
	C.rados_read_op_cmpxattr(ro.opContext, n, C.uint8_t(operator), bufAddr, C.size_t(bufLen))
	return ro
}
Esempio n. 8
0
func newInterfaces(ctx *C.struct_ibv_context) {
	var deviceAttr C.struct_ibv_device_attr
	errno := C.ibv_query_device(ctx, &deviceAttr)
	if errno != 0 {
		return
	}

	pd := C.ibv_alloc_pd(ctx)
	if pd == nil {
		panic(newError("ibv_alloc_pd", -1))
	}
	pds[pd] = true

	for port := C.uint8_t(1); port <= deviceAttr.phys_port_cnt; port++ {
		var portAttr C.struct_ibv_port_attr
		errno := C.ibv_query_port(ctx, port, &portAttr)
		if errno != 0 {
			continue
		}

		var gid C.union_ibv_gid
		errno = C.ibv_query_gid(ctx, port, 0, &gid)
		if errno != 0 {
			continue
		}
		// last 8 bytes of GID is the GUID
		guid := net.HardwareAddr(gid[8:])
		iface := &Interface{ctx, pd, uint8(port), &deviceAttr, guid}
		interfaces = append(interfaces, iface)
		guidToInterface[string([]byte(guid))] = iface
	}
}
Esempio n. 9
0
func New(c *Config) *Disassembler {
	if c.Buf != nil && c.Reader != nil {
		panic("New: must pass either bytes or a Reader")
	}
	d := &Disassembler{
		bytes: c.Buf,
		r:     c.Reader,
	}
	C.ud_init(&d.u)
	C.ud_set_user_opaque_data(&d.u, unsafe.Pointer(d))
	if d.bytes != nil {
		C.ud_set_input_buffer(&d.u, (*C.uint8_t)(unsafe.Pointer(&d.bytes[0])), C.size_t(len(d.bytes)))
	} else {
		C.ud_set_input_hook(&d.u, (*[0]byte)(C.ud_input_hook))
	}
	if c.Bits != 0 {
		C.ud_set_mode(&d.u, C.uint8_t(c.Bits))
	}
	if c.PC != 0 {
		C.ud_set_pc(&d.u, C.uint64_t(c.PC))
	}
	// We rely on the fact that the zero value is also the default
	// to udis86, and don't check if it is set.
	C.ud_set_vendor(&d.u, C.unsigned(c.Vendor))
	switch c.Syntax {
	case SyntaxIntel:
		C.ud_set_syntax(&d.u, (*[0]byte)(C.UD_SYN_INTEL))
	case SyntaxATT:
		C.ud_set_syntax(&d.u, (*[0]byte)(C.UD_SYN_ATT))
	}
	return d
}
Esempio n. 10
0
func NewSurfaceFromXCB(xcb_drawable xproto.Drawable, xcb_VI xproto.VisualInfo, width, height int) *Surface {

	var xcb_visualtype C.xcb_visualtype_t
	xcb_visualtype.visual_id = C.xcb_visualid_t(xcb_VI.VisualId)
	xcb_visualtype._class = C.uint8_t(xcb_VI.Class)
	xcb_visualtype.bits_per_rgb_value = C.uint8_t(xcb_VI.BitsPerRgbValue)
	xcb_visualtype.colormap_entries = C.uint16_t(xcb_VI.ColormapEntries)
	xcb_visualtype.red_mask = C.uint32_t(xcb_VI.RedMask)
	xcb_visualtype.green_mask = C.uint32_t(xcb_VI.GreenMask)
	xcb_visualtype.blue_mask = C.uint32_t(xcb_VI.BlueMask)

	var connect_xcb (*C.xcb_connection_t) = C.xcb_connect(nil, nil)

	s := C.cairo_xcb_surface_create(connect_xcb, C.xcb_drawable_t(xcb_drawable), &xcb_visualtype, C.int(width), C.int(height))
	return &Surface{surface: s, context: C.cairo_create(s)}
}
Esempio n. 11
0
// LockShared share locks an object. At the moment, lock duration cannot be specified so this locks the object
// indefinitely.
func (o *Object) LockShared(name, cookie, tag, description string, flags ...LibradosLock) error {
	oid := C.CString(o.name)
	defer freeString(oid)

	n := C.CString(name)
	defer freeString(n)

	c := C.CString(cookie)
	defer freeString(c)

	t := C.CString(tag)
	defer freeString(t)

	d := C.CString(description)
	defer freeString(d)

	f := 0
	for _, flag := range flags {
		f |= int(flag)
	}

	ret := C.rados_lock_shared(o.ioContext, oid, n, c, t, d, nil, C.uint8_t(f))
	switch int(ret) {
	case -int(syscall.EBUSY):
		err := toRadosError(ret)
		err.Message = fmt.Sprintf("%s is already locked by another client", o.name)
		return err
	case -int(syscall.EEXIST):
		err := toRadosError(ret)
		err.Message = fmt.Sprintf("%s is already locked by current client", o.name)
		return err
	}
	return nil
}
Esempio n. 12
0
File: target.go Progetto: harikb/nfc
// See documentation in Target for more details.
func (d *ISO14443biTarget) Marshall() uintptr {
	c := (*C.nfc_iso14443bi_info)(makeTarget(ISO14443bi, d.Baud))

	c.btVerLog = C.uint8_t(d.VerLog)
	c.btConfig = C.uint8_t(d.Config)

	for i, b := range d.Atr {
		c.abtAtr[i] = C.uint8_t(b)
	}

	for i, b := range d.DIV {
		c.abtDIV[i] = C.uint8_t(b)
	}

	return uintptr(unsafe.Pointer(c))
}
Esempio n. 13
0
File: usb.go Progetto: hanwen/usb
func (h *DeviceHandle) GetStringDescriptorASCII(descIndex byte) (string, error) {
	data := make([]byte, 1024)
	start := (*C.uchar)(unsafe.Pointer(&data[0]))
	r := C.libusb_get_string_descriptor_ascii(h.me(),
		C.uint8_t(descIndex), start, C.int(len(data)))
	return C.GoString((*C.char)(unsafe.Pointer(&data[0]))), toErr(r)
}
Esempio n. 14
0
func (wo *WriteOperation) CompareAttribute(attributeName string, operator CompareAttribute, value io.Reader) *WriteOperation {
	name := C.CString(attributeName)
	defer freeString(name)
	bufAddr, bufLen := readerToBuf(value)
	C.rados_write_op_cmpxattr(wo.opContext, name, C.uint8_t(operator), bufAddr, C.size_t(bufLen))
	return wo
}
Esempio n. 15
0
func newDescriptor(dev *C.libusb_device) (*Descriptor, error) {
	var desc C.struct_libusb_device_descriptor
	if errno := C.libusb_get_device_descriptor(dev, &desc); errno < 0 {
		return nil, usbError(errno)
	}

	// Enumerate configurations
	var cfgs []ConfigInfo
	for i := 0; i < int(desc.bNumConfigurations); i++ {
		var cfg *C.struct_libusb_config_descriptor
		if errno := C.libusb_get_config_descriptor(dev, C.uint8_t(i), &cfg); errno < 0 {
			return nil, usbError(errno)
		}
		cfgs = append(cfgs, newConfig(dev, cfg))
		C.libusb_free_config_descriptor(cfg)
	}

	return &Descriptor{
		Bus:          uint8(C.libusb_get_bus_number(dev)),
		Address:      uint8(C.libusb_get_device_address(dev)),
		Spec:         BCD(desc.bcdUSB),
		Device:       BCD(desc.bcdDevice),
		Vendor:       ID(desc.idVendor),
		Product:      ID(desc.idProduct),
		Class:        uint8(desc.bDeviceClass),
		SubClass:     uint8(desc.bDeviceSubClass),
		Protocol:     uint8(desc.bDeviceProtocol),
		SerialNumber: getSerialNumber(dev, desc.iSerialNumber),
		Configs:      cfgs,
		dev:          dev,
	}, nil
}
Esempio n. 16
0
func SetViewTransform(viewID ViewID, view, proj [16]float32) {
	C.bgfx_set_view_transform(
		C.uint8_t(viewID),
		unsafe.Pointer(&view[0]),
		unsafe.Pointer(&proj[0]),
	)
}
Esempio n. 17
0
func (d *digest) Sum(buf []byte) []byte {
	digest := make([]byte, d.Size())
	// Make a copy of d.state so that caller can keep writing and summing.
	s := d.state
	C.blake2b_final(&s, (*C.uint8_t)(&digest[0]), C.uint8_t(d.Size()))
	return append(buf, digest...)
}
Esempio n. 18
0
// NewMessage returns an initialised netlink message.
func NewMessage(command, family, flags int) (*Message, error) {
	nlm := C.nlmsg_alloc()
	if nlm == nil {
		return nil, errors.New("failed to create netlink message")
	}
	C.genlmsg_put(nlm, C.NL_AUTO_PID, C.NL_AUTO_SEQ, C.int(family), 0, C.int(flags), C.uint8_t(command), genlVersion)
	return &Message{nlm: nlm}, nil
}
Esempio n. 19
0
func (iface *Interface) Lid() uint16 {
	var portAttr C.struct_ibv_port_attr
	errno := C.ibv_query_port(iface.ctx, C.uint8_t(iface.port), &portAttr)
	if errno != 0 {
		return 0
	}
	return uint16(portAttr.lid)
}
Esempio n. 20
0
func (iface *Interface) Active() bool {
	var portAttr C.struct_ibv_port_attr
	errno := C.ibv_query_port(iface.ctx, C.uint8_t(iface.port), &portAttr)
	if errno != 0 {
		return false
	}
	return portAttr.state == C.IBV_PORT_ACTIVE
}
Esempio n. 21
0
func (d *Device) Control(rType, request uint8, val, idx uint16, data []byte) (int, error) {
	//log.Printf("control xfer: %d:%d/%d:%d %x", idx, rType, request, val, string(data))
	dataSlice := (*reflect.SliceHeader)(unsafe.Pointer(&data))
	n := C.libusb_control_transfer(
		d.handle,
		C.uint8_t(rType),
		C.uint8_t(request),
		C.uint16_t(val),
		C.uint16_t(idx),
		(*C.uchar)(unsafe.Pointer(dataSlice.Data)),
		C.uint16_t(len(data)),
		C.uint(d.ControlTimeout/time.Millisecond))
	if n < 0 {
		return int(n), usbError(n)
	}
	return int(n), nil
}
Esempio n. 22
0
func SetViewRect(view ViewID, x, y, w, h int) {
	C.bgfx_set_view_rect(
		C.uint8_t(view),
		C.uint16_t(x),
		C.uint16_t(y),
		C.uint16_t(w),
		C.uint16_t(h),
	)
}
Esempio n. 23
0
File: uwsgi.go Progetto: ahua/c
// check if a signal is registered
func SignalRegistered(signum int) bool {
	if C.uwsgi.master_process == 0 {
		return false
	}
	if int(C.uwsgi_signal_registered(C.uint8_t(signum))) == 0 {
		return false
	}
	return true
}
Esempio n. 24
0
File: uwsgi.go Progetto: ahua/c
// add a red black timer
func AddRbTimer(signum int, seconds int) bool {
	if C.uwsgi.master_process == 0 {
		return false
	}
	if int(C.uwsgi_signal_add_rb_timer(C.uint8_t(signum), C.int(seconds), C.int(0))) == 0 {
		return true
	}
	return false
}
Esempio n. 25
0
func drawText(x, y, size int, text string, fg, bg int) error {
	//RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)*3-20, "Button 3 is", COLOR_GREEN, COLOR_BLACK );

	buf := new(bytes.Buffer)
	w, err := charset.NewWriter("latin1", buf)
	if err != nil {
		return err
	}
	fmt.Fprintf(w, text)
	w.Close()

	b := []byte(buf.String())
	b = append(b, 0)
	ctext := (*C.uchar)(unsafe.Pointer(&b[0]))
	C.RAIO_SetFontSizeFactor(C.uint8_t(size))
	C.RAIO_print_text(C.uint16_t(x), C.uint16_t(y), ctext, C.uint8_t(bg), C.uint8_t(fg))
	return nil
}
Esempio n. 26
0
func (h *DeviceHandle) GetStringDescriptor(index byte, langid uint16) (string, *UsbError) {
	buf := make([]uint16, 128)

	rlen, err := decodeUsbError(C.libusb_get_string_descriptor(h.handle, C.uint8_t(index), C.uint16_t(langid), (*C.uchar)(unsafe.Pointer(&buf[0])), 256))
	if err != nil {
		return "", err
	}
	return string(utf16.Decode(buf[1 : rlen/2])), nil
}
Esempio n. 27
0
File: target.go Progetto: harikb/nfc
// See documentation in Target for more details.
func (d *ISO14443b2srTarget) Marshall() uintptr {
	c := (*C.nfc_iso14443b2sr_info)(makeTarget(ISO14443b2sr, d.Baud))

	for i, b := range d.UID {
		c.abtUID[i] = C.uint8_t(b)
	}

	return uintptr(unsafe.Pointer(c))
}
Esempio n. 28
0
func DebugTextPrintf(x, y int, attr uint8, format string, args ...interface{}) {
	text := []byte(fmt.Sprintf(format+"\x00", args...))
	C.bgfx_dbg_text_print(
		C.uint32_t(x),
		C.uint32_t(y),
		C.uint8_t(attr),
		(*C.char)(unsafe.Pointer(&text[0])),
	)
}
Esempio n. 29
0
func CreateFrameBufferFromTextures(textures []Texture, destroyTextures bool) FrameBuffer {
	h := C.bgfx_create_frame_buffer_from_handles(
		C.uint8_t(len(textures)),
		//(*C.bgfx_texture_handle_t)(unsafe.Pointer(&textures[0])),
		&textures[0].h,
		C._Bool(destroyTextures),
	)
	return FrameBuffer{h: h}
}
Esempio n. 30
0
File: target.go Progetto: harikb/nfc
// See documentation in Target for more details.
func (d *ISO14443bTarget) Marshall() uintptr {
	c := (*C.nfc_iso14443b_info)(makeTarget(ISO14443b, d.Baud))

	c.ui8CardIdentifier = C.uint8_t(d.CardIdentifier)

	for i, b := range d.Pupi {
		c.abtPupi[i] = C.uint8_t(b)
	}

	for i, b := range d.ApplicationData {
		c.abtApplicationData[i] = C.uint8_t(b)
	}

	for i, b := range d.ProtocolInfo {
		c.abtProtocolInfo[i] = C.uint8_t(b)
	}

	return uintptr(unsafe.Pointer(c))
}