Install libusb
$ sudo apt-get install libusb-dev
Clone libpafe from GitHub
$ git clone https://github.com/rfujita/libpafe.git
Compile & install
$ cd libpafe
$ ./configure
$ make
$ sudo make install
Operation check
$ cd libpafe
$ ./tests/pasori_test
PaSoRi (RC-S320)
firmware version 1.40
Echo test... success
EPROM test... success
RAM test... success
CPU test... success
Polling test... success
Create 60-libpafe.rules
* $ sudo vi /lib/udev/rules.d/60-libpafe.rules
Describe the following contents
ACTION!="add", GOTO="pasori_rules_end"
SUBSYSTEM=="usb_device", GOTO="pasori_rules_start"
SUBSYSTEM!="usb", GOTO="pasori_rules_end"
LABEL="pasori_rules_start"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="01bb", MODE="0664", GROUP="plugdev"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="02e1", MODE="0664", GROUP="plugdev"
LABEL="pasori_rules_end"
Run $ sudo udevadm control --reload-rules
Reboot with $ sudo reboot
.
Read FeliCa IDm with Raspberry Pi + libpafe + Python + ctypes (Almost referring to what was posted here)
read_idm.py
# -*- coding: utf-8 -*-
from __future__ import print_function
from ctypes import *
# libpafe.Defined on line 77 of h
FELICA_POLLING_ANY = 0xffff
if __name__ == '__main__':
libpafe = cdll.LoadLibrary("/usr/local/lib/libpafe.so")
libpafe.pasori_open.restype = c_void_p
pasori = libpafe.pasori_open()
libpafe.pasori_init(pasori)
libpafe.felica_polling.restype = c_void_p
felica = libpafe.felica_polling(pasori, FELICA_POLLING_ANY, 0, 0)
idm = c_ulonglong()
libpafe.felica_get_idm.restype = c_void_p
libpafe.felica_get_idm(felica, byref(idm))
#IDm is in hexadecimal notation
print("IDm:", "%016X" % idm.value)
#From the README, felica_polling()Free after use()use
#In addition, free seems to be automatically included in the library
libpafe.free(felica)
libpafe.pasori_close(pasori)
int felica_get_idm(felica *f, uint8 *idm);
How to display Suica balance?
It is necessary to fetch the information in the IC card and pick up the balance information from it.
Summary of Suica data structure: suica --FeliCa Library Wiki --FeliCa Library --geotel
It would be good if we could get the data around 10-11.
Since various information can be fetched by the function called felica_read of libpafe, use it. Reference: Continued blog: Raspberry Pi talks about the balance of the IC card
↑ Since this is written in C language, rewrite it so that felica_read can be called on Python using ctypes.
The argument may indicate a pointer to a structure, so define it in advance and adjust it.
read_balance.py
# -*- coding: utf-8 -*-
from __future__ import print_function
from ctypes import *
# libpafe.Defined on line 77 of h
FELICA_POLLING_ANY = 0xffff
#Definition of a class to replace the structure
class felica_block_info(Structure):
_fields_ = [
("service", c_uint16),
("mode", c_uint8),
("block", c_uint16)
]
if __name__ == '__main__':
libpafe = cdll.LoadLibrary("/usr/local/lib/libpafe.so")
libpafe.pasori_open.restype = c_void_p
pasori = libpafe.pasori_open()
libpafe.pasori_init(pasori)
libpafe.felica_polling.restype = c_void_p
felica = libpafe.felica_polling(pasori, FELICA_POLLING_ANY, 0, 0)
#Definition of C int type array(Length 16)
int_array16 = c_uint8 * 16
#Response data
data = int_array16()
#List of service codes
info = felica_block_info(c_uint16(0x090f), c_uint8(0), c_uint16(0))
for i in range(0, 32):
c_i = c_int(i)
libpafe.felica_read(felica, byref(c_i), byref(info), byref(data))
if (data[1] > 0) or (data[2] > 0):
print("Balance:", data[11] * 256 + data[10], "Circle")
break
libpafe.free(felica)
libpafe.pasori_close(pasori)
Recommended Posts