Пример #1
0
// 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
// 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
}
Пример #3
0
// 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
}
Пример #4
0
// 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
}