Example #1
0
File: target.go Project: harikb/nfc
// Make a string from a target with proper error reporting. This is a wrapper
// around str_nfc_target.
func TargetString(t Target, verbose bool) (string, error) {
	ptr := unsafe.Pointer(t.Marshall())

	var result *C.char = nil

	length := C.str_nfc_target(&result, (*C.nfc_target)(ptr), C.bool(verbose))
	defer C.nfc_free(unsafe.Pointer(result))

	if length < 0 {
		return "", Error(length)
	}

	return C.GoStringN(result, C.int(length)), nil
}
Example #2
0
File: device.go Project: harikb/nfc
// Print information about an NFC device.
func (d Device) Information() (string, error) {
	if *d.d == nil {
		return "", errors.New("device closed")
	}

	var ptr *C.char
	buflen := C.nfc_device_get_information_about(*d.d, &ptr)

	if buflen < 0 {
		return "", Error(buflen)
	}

	// documentation for nfc_device_get_information_about says that buflen
	// contains the length of the string that is returned. Apparently, for
	// some drivers, buflen is always 0 so we disregard it.
	str := C.GoString(ptr)
	C.nfc_free(unsafe.Pointer(ptr))

	return str, nil
}