コード例 #1
0
ファイル: initiator.go プロジェクト: harikb/nfc
// List passive or emulated tags. The NFC device will try to find the available
// passive tags. Some NFC devices are capable to emulate passive tags. The
// standards (ISO18092 and ECMA-340) describe the modulation that can be used
// for reader to passive communications. The chip needs to know with what kind
// of tag it is dealing with, therefore the initial modulation and speed (106,
// 212 or 424 kbps) should be supplied.
func (d Device) InitiatorListPassiveTargets(m Modulation) ([]Target, error) {
	if *d.d == nil {
		return nil, errors.New("device closed")
	}

	mod := C.nfc_modulation{
		nmt: C.nfc_modulation_type(m.Type),
		nbr: C.nfc_baud_rate(m.BaudRate),
	}

	tar := C.list_targets_wrapper(*d.d, mod)
	defer C.free(unsafe.Pointer(tar.entries))
	if tar.count < 0 {
		return nil, Error(tar.count)
	}

	targets := make([]Target, tar.count)
	for i := range targets {
		// index the C array using pointer arithmetic
		ptr := uintptr(unsafe.Pointer(tar.entries)) + uintptr(i)*unsafe.Sizeof(*tar.entries)
		targets[i] = unmarshallTarget((*C.nfc_target)(unsafe.Pointer(ptr)))
	}

	return targets, nil
}
コード例 #2
0
ファイル: device.go プロジェクト: harikb/nfc
// Get suported baud rates. Returns either a slice of supported baud rates or an
// error. This function wraps nfc_device_get_supported_baud_rate().
func (d Device) SupportedBaudRates(modulationType int) ([]int, error) {
	if *d.d == nil {
		return nil, errors.New("device closed")
	}

	// The documentation inside the libnfc is a bit unclear on how the
	// array returned through supported_mt is to be threated. The code
	// itself suggest that it points to an array of entries terminated with
	// UNDEFINED = 0.
	var br_arr *C.nfc_baud_rate
	ret := C.nfc_device_get_supported_baud_rate(
		*d.d,
		C.nfc_modulation_type(modulationType),
		&br_arr,
	)
	if ret != 0 {
		return nil, Error(ret)
	}

	brs := []int{}
	type br C.nfc_baud_rate
	ptr := unsafe.Pointer(br_arr)

	for *(*br)(ptr) != 0 {
		brs = append(brs, int(*(*br)(ptr)))
		ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*br_arr))
	}

	return brs, nil
}
コード例 #3
0
ファイル: target.go プロジェクト: harikb/nfc
// make an nfc_target with modulation set. This returns a pointer allocated by
// C.malloc which needs to be free'd later on.
func makeTarget(mod, baud int) unsafe.Pointer {
	ptr := C.malloc(C.size_t(unsafe.Sizeof(C.nfc_target{})))
	(*C.nfc_target)(ptr).nm = C.nfc_modulation{
		nmt: C.nfc_modulation_type(mod),
		nbr: C.nfc_baud_rate(baud),
	}

	// C89 says: A pointer to a struct is equal to a point to its first
	// member and a pointer to a union is equal to a pointer to any of its
	// members. Therefore we can simply return ptr to fit all use cases
	return ptr
}
コード例 #4
0
ファイル: initiator.go プロジェクト: harikb/nfc
// Select a passive or emulated tag.
func (d *Device) InitiatorSelectPassiveTarget(m Modulation, initData []byte) (Target, error) {
	if d.d == nil {
		return nil, errors.New("Device closed")
	}

	var pnt C.nfc_target

	err := Error(C.nfc_initiator_select_passive_target(
		d.d,
		C.nfc_modulation{C.nfc_modulation_type(m.Type), C.nfc_baud_rate(m.BaudRate)},
		(*C.uint8_t)(&initData[0]),
		C.size_t(len(initData)),
		&pnt))

	return unmarshallTarget(&pnt), err
}
コード例 #5
0
ファイル: initiator.go プロジェクト: harikb/nfc
// Select a passive or emulated tag.
func (d Device) InitiatorSelectPassiveTarget(m Modulation, initData []byte) (Target, error) {
	if *d.d == nil {
		return nil, errors.New("device closed")
	}

	var pnt C.nfc_target

	var initDataPtr *byte
	if len(initData) > 0 {
		initDataPtr = &initData[0]
	}

	n := C.nfc_initiator_select_passive_target(
		*d.d,
		C.nfc_modulation{C.nfc_modulation_type(m.Type), C.nfc_baud_rate(m.BaudRate)},
		(*C.uint8_t)(initDataPtr),
		C.size_t(len(initData)),
		&pnt)
	if n <= 0 {
		return nil, Error(n)
	}

	return unmarshallTarget(&pnt), nil
}