[PYTHON] Read FeliCa IDm with raspberrypi (pasori RC-S320 version)

Initially, I was thinking of using nfcpy to read FeliCa IDm, but it turned out that my pasori RC-S320 does not support it, so I used libpafe instead.

2016/10/16 postscript: If you want to read IDm with pasori RC-S380 here

environment

Hard: raspberrypi3 OS:raspbian Leader: pasori RC-S320

Preparation

--Creating a directory

$ mkdir pasori
$ cd pasori

--Connect pasori to the USB of raspberry pi3. --Confirm that it is recognized by the following command.

$ lsusb
Bus 001 Device 004: ID 054c:01bb Sony Corp. FeliCa S320 [PaSoRi]
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

--Install libusb

$ sudo apt-get install libusb-dev

Install libpafe

--Download and compile

$ git clone https://github.com/rfujita/libpafe.git 
$ cd libpafe
$ ./configure
$ make
$ sudo make install

--Check the installation result Make sure you have libpafe.so.0.0.8

$ cd /usr/local/lib
$ ls
libpafe.a   libpafe.so    libpafe.so.0.0.8  python2.7  site_ruby
libpafe.la  libpafe.so.0  pypy2.7           python3.4

Operation check

$ cd ~/pasori/libpafe-0.0.8
$ sudo ./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

udev settings

--60-Create a new libpafe.rules

$ sudo nano /lib/udev/rules.d/60-libpafe.rules

--60-libpafe.rules Edited contents

60-libpafe.rules


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}=="006c", MODE="0664", GROUP="plugdev"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="01bb", MODE="0664", GROUP="plugdev"
ATTRS{idVendor}=="054c", ATTRS{idProduct}=="02e1", MODE="0664", GROUP="plugdev"

LABEL="pasori_rules_end"
$ udevadm control --reload-rules
root privileges required
$ sudo udevadm control --reload-rules
$ sudo reboot

--Confirm that it can be executed without sudo after reboot.

$ ./pasori/libpafe-0.0.8/tests/pasori_test

Python script to read FeliCa IDm

The sample is quoted from here. If the citation source is left as it is, only the last 4 digits of IDm will be displayed, so we will make some changes to display 16 digits.

# -*- 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() #← Changed to receive 16 digits
    libpafe.felica_get_idm.restype = c_void_p
    libpafe.felica_get_idm(felica, byref(idm))

    #IDm is in hexadecimal notation
    print("%016X" % idm.value) #← Changed to display 16 digits

    #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)

reference

Read FeliCa IDm with Raspberry Pi + libpafe + Python + ctypes

Recommended Posts

Read FeliCa IDm with raspberrypi (pasori RC-S320 version)
Read FeliCa IDm with raspberrypi3 (pasori RC-S380 version)
Read FeliCa using Pasori (RC-380S) on Mac