PaSoRi

2年くらい前だっただろうか、iPhoneが決済システムを搭載するという話になって、「おサイフケータイ搭載か?!」と話題を呼んだ。結局、未だにiPhoneはおサイフケータイに対応せず、NFCもロックされていて一般には使えないという状況である。

まあ、世の中、技術が揃っていれば実現可能とも言えるというのが、筆者の考えで、今まで、いろんなモノをゴニョゴニョしてきた。そこで、今回も、一発やってやろうという話なのだが、その前にちょっと補足を。

前回の投稿時、筆者は、豪州に住んでいた。あれから3年以上経ってしまって、ブログの体を成していない訳なのであるが、時代の流れがSNS中心になってしまっているので、なかなかブログに書くということも思いつきもしないのだ。お許し願いたい。とは言え、ここのところ、書きとどめておきたいと思うような話はあまりなかった。最悪の道を回避するだけでは、最高の道に進むことはできないのだと思い知った。できることなら4年ほど時を巻き戻したい。

さて、筆者の開発環境であるが、当時は、Macbook Pro 13in Early 2011 i7モデルだった。数々の故障を自分でなおしてきたが、一月ほど前についに内部のSATAポートが死んで起動しなくなってしまった。そこで、購入したのが、Macbook Pro 13in Mid 2012である。なぜ、4年前のモデルを購入するのか。それは、なんといっても「改造できるから」である。そんな、ちょっとオカシイ筆者の環境を貼り付けておく。

Model: MacBookPro9,2, BootROM MBP91.00D3.B0D, 2 processors, Intel Core i7, 2.9 GHz, 16 GB, SMC 2.2f44
Graphics: Intel HD Graphics 4000, Intel HD Graphics 4000, Built-In
Memory Module: BANK 0/DIMM0, 8 GB, DDR3, 1333 MHz, 0x029E
Memory Module: BANK 1/DIMM0, 8 GB, DDR3, 1333 MHz, 0x029E
AirPort: spairport_wireless_card_type_airport_extreme (0x14E4, 0xF5), Broadcom BCM43xx 1.0 (7.21.95.175.1a6)
Bluetooth: Version 4.4.5f3 17904, 3 services, 27 devices, 1 incoming serial ports
Network Service: Wi-Fi, AirPort, en1
Serial ATA Device: Crucial_CT256MX100SSD1, 256.06 GB
Serial ATA Device: Hitachi HTS547575A9E384, 750.16 GB
USB Device: USB 2.0 Bus
USB Device: Hub
USB Device: Hub
USB Device: Apple Internal Keyboard / Trackpad
USB Device: IR Receiver
USB Device: BRCM20702 Hub
USB Device: Bluetooth USB Host Controller
USB Device: USB 2.0 Bus
USB Device: Hub
USB Device: FaceTime HD Camera (Built-in)
USB Device: USB 3.0 Bus
USB Device: RC-S360/S
Thunderbolt Bus: MacBook Pro, Apple Inc., 25.1

話を戻そう。取り敢えず、筆者としては、NFC機能のないiPhoneに、おサイフケータイに近い、可能なかぎりのものを実装しようというのが狙いである。(なお、RCS390の存在は知っている。)この問題を単純化していくと、まずは、DarwinなマシンでFelicaを読み書きできればよろしいのであろうというところだ。というのも、Felica開発元のSonyはWindowsを念頭に置いた開発環境ばかり作っているので、既存のサンプルを移植するにできないという話なのだ。

今回使うことにしたのは、libusb + libnfcである。依存関係がシンプルで、移植が簡単そうだったからだ。ネットを見渡すと、これでLinuxでパソリリーダーを使えたというような記述が散見される。実際に始めてみて気づいたのは、使えたって言っても、IDmの読み取りくらいしかしてない人のブログの投稿ばかりなのだ。しかもlibnfcは、NXP純正のリーダーを前提に作られているようで、大Sony様が変更した部分のAPIとか、なにそれ美味しいの、みたいなスタンスだった。筆者が用意したのは、「楽天Edyオフィシャルショップの楽天Edyリーダー()」で、一応、というか中身はそのまんま、SonyのRC-S360/Sだった。

さて、いろんなブログの説明通りにインストールまではできた。Macportsでlibusb-compatまでインストールすれば、あとは、libnfcを ./configureしてmake installで大丈夫だ。もちろんこのままでちゃんと動いたわけではなかった。ログを取るために

./configure –prefix=/opt/local –includedir=/opt/local/include –enable-debug

としてライブラリから再インストールした。

pn53x.cには、若干の変更を加えた。まず、200行目付近

  switch (pbtTx[0]) {

case PowerDown:

case InDataExchange:

case InCommunicateThru:

case CommunicateThruEX://RCS360用に追加

case InJumpForPSL:

case InPSL:

あとは、1400行目付近、pn53x_initiator_transceive_bytes関数内

    return pnd->last_error;

}

 

if (CHIP_DATA(pnd)->type == RCS360) {//ここから

// Copy the data into the command frame

abtCmd[0] = CommunicateThruEX;

abtCmd[1] = 0×20;//800×0.5

abtCmd[2] = 0×03;//millisconds

memcpy(abtCmd + 3, pbtTx, szTx);

szExtraTxLen = 3;

}else{//ここまで

// Copy the data into the command frame

if (pnd->bEasyFraming) {

それから、pn53x-internal.hに定義を追加しておけば、とりあえずなんとかなる。

#define CommunicateThruEX 0xA0

最後に、動作検証に使ったプログラムのソースを載せておく。これ以上の話は、別の投稿にしようと思う。

// To compile this simple example:

// $ gcc -o felica_idm felica_idm.c -lnfc -L/opt/local/lib -I/opt/local/include

 

#include <stdlib.h>

#include <string.h>

#include <nfc/nfc.h>

#include <libkern/OSByteOrder.h>

 

uint8_t request_edy_service_command[16] = {0×10,/*cmd*/ 0×06,/*IDm*/ 0×00, 0×00, 0×00, 0×00, 0×00, 0×00, 0×00, 0×00,/*sys,serv*/ 0×01, 0x0f, 0×17, 0×01, 0×80, 0×00};

uint8_t read_wo_encryption_edy_command[34] = {0×22, 0×06, 0×00, 0×00, 0×00, 0×00, 0×00, 0×00, 0×00, 0×00, 0×03, 0x0b, 0×11, 0×17, 0×13, 0x0f, 0×17, 0×08, 0×80, 0×00, 0×81, 0×00, 0×82, 0×00, 0×82, 0×01, 0×82, 0×02, 0×82, 0×03, 0×82, 0×04, 0×82, 0×05};

 

static void

print_hex(const uint8_t *pbtData, const size_t szBytes)

{

size_t  szPos;

for (szPos = 0; szPos < szBytes; szPos++) {

printf(“%02x  “, pbtData[szPos]);

}

printf(“\n”);

}

 

union IDmHolder{

uint64_t IDm64;

uint8_t IDm8[8];

};

 

int

main(int argc, const char *argv[])

{

nfc_device *pnd;

nfc_target nt;

// Allocate only a pointer to nfc_context

nfc_context *context;

// Initialize libnfc and set the nfc_context

nfc_init(&context);

if (context == NULL) {

printf(“Unable to init libnfc (malloc)\n”);

exit(EXIT_FAILURE);

}

// Display libnfc version

const char *acLibnfcVersion = nfc_version();

(void)argc;

printf(“%s uses libnfc %s\n”, argv[0], acLibnfcVersion);

pnd = nfc_open(context, NULL);

if (pnd == NULL) {

printf(“ERROR: %s\n”, “Unable to open NFC device.”);

exit(EXIT_FAILURE);

}

// Set opened NFC device to initiator mode

if (nfc_initiator_init(pnd) < 0) {

nfc_perror(pnd, “nfc_initiator_init”);

exit(EXIT_FAILURE);

}

printf(“NFC reader: %s opened\n”, nfc_device_get_name(pnd));

// Poll for a Felica tag

const nfc_modulation nmFelica = {

.nmt = NMT_FELICA,

.nbr = NBR_212,

};

uint64_t  tempIdCS = 0;

while (-1)

{

if (nfc_initiator_select_passive_target(pnd, nmFelica, NULL, 0, &nt) > 0) {

if(tempIdCS != *(uint64_t *)(nt.nti.nfi.abtId)){

printf(“The following (NFC) felica tag was found:\n”);

printf(”       mID (NFCID2): “);

printf(“0x%016llx\n\r”, OSSwapBigToHostInt64 (*(uint64_t *)(nt.nti.nfi.abtId)));

tempIdCS = *(uint64_t *)(nt.nti.nfi.abtId);

memcpy(&(read_wo_encryption_edy_command[2]), nt.nti.nfi.abtId, 8);

read_wo_encryption_edy_command[2] += 0×10;//なぜかこれが無いと動かない

printf(“now sending:\n”);

print_hex(read_wo_encryption_edy_command, sizeof(request_service_edy_no_command));

#define MAX_FRAME_LEN 264

uint8_t  abtRx[MAX_FRAME_LEN];

uint8_t  pbtRxPar;

if (nfc_initiator_transceive_bytes(pnd, read_wo_encryption_edy_command, sizeof(read_wo_encryption_edy_command), abtRx, sizeof(abtRx), 0) > 0) {

printf(“got reply:\n”);

print_hex(abtRx, sizeof(abtRx));

}

}

}

}

// Close NFC device

nfc_close(pnd);

// Release the context

nfc_exit(context);

exit(EXIT_SUCCESS);

}

参考資料

http://www.sony.co.jp/Products/felica/business/tech-support/st_fprcs620s_command.html

http://www.nxp.com/documents/user_manual/157830_PN533_um080103.pdf

http://jennychan.web.fc2.com/format/edy.html#110B

http://www.kokozo.net/AndroidNFC_FeliCa/

http://www.libnfc.org/api/group__initiator.html

パケットをダンプするにあたって、餌食になったアプリ

http://www.adobe.com/jp/newsletters/edge/january2010/articles/article5/index.html

Posted in 未分類 | Leave a comment

CMakeをiOS用にビルドしようとするとbootstrapでコケる件

問題は山積みで、まずsedがGNU版だと良くない。

これは、AppleのOpenSourceからtext_cmdsを引っ張ってきて、BSD固有の部分(line35, each c file)をコメントアウトすれば、gccで手動でコンパイルできる。

さて、これはともかく、最近のiOS(>=4.3?)は、署名がないとKiled: 9になってしまう問題の回避方法がない。

そうすると、何をコンパイルするにも

$./configure

でコケてしまう。

仕方ないのでldidを隨所に忍ばせるのだが不便でどうしようもない。

この辺までセットで脱獄してくれると助かるんですがねぇ

さて、ようやく、CMakeのビルドに入るわけだが、どうしたことか、バイナリまではできるのに、いざ実行する段になるとセグフォが出てコケてしまう。

KERN_INVALID_ADDRESS at address: 0×00000000

というのは、初期化の段階で根本的に間違ってるように思える。

メモリ割り当てに失敗したポインタを参照してるとか、そういうものの気がする。

誰か解決してCydiaに上げてくれると助かるんですけど。

———————————————

CMake 2.8.8, Copyright 2000-2011 Kitware, Inc.

C compiler on this system is: /usr/bin/gcc

C++ compiler on this system is: /usr/bin/g++ -I/private/var/include/c++/4.0.0/ -I/private/var/include/c++/4.0.0/i686-apple-darwin9/

Makefile processor on this system is: make

/usr/bin/g++ is GNU compiler

/usr/bin/g++ has STL in std:: namespace

/usr/bin/g++ has ANSI streams

/usr/bin/g++ has streams in std:: namespace

/usr/bin/g++ has sstream

/usr/bin/g++ has operator!=(string, char*)

/usr/bin/g++ has stl iterator_traits

/usr/bin/g++ has standard template allocator

/usr/bin/g++ has allocator<>::rebind<>

/usr/bin/g++ does not have non-standard allocator<>::max_size argument

/usr/bin/g++ has stl containers supporting allocator objects

/usr/bin/g++ has header cstddef

/usr/bin/g++ requires template friends to use <>

/usr/bin/g++ supports member templates

/usr/bin/g++ has standard template specialization syntax

/usr/bin/g++ has argument dependent lookup

/usr/bin/g++ does not have struct stat with st_mtim member

/usr/bin/g++ has ios::binary openmode

/usr/bin/g++ has ANSI for scoping

———————————————

make: `cmake’ is up to date.

GNU gdb 6.3.50.20050815-cvs (Fri May 20 08:08:42 UTC 2011)

Copyright 2004 Free Software Foundation, Inc.

GDB is free software, covered by the GNU General Public License, and you are

welcome to change it and/or distribute copies of it under certain conditions.

Type “show copying” to see the conditions.

There is absolutely no warranty for GDB.  Type “show warranty” for details.

This GDB was configured as “–host=arm-apple-darwin9 –target=”…Reading symbols for shared libraries . done

 

(gdb) r

Starting program: /private/var/mobile/jail/buildhouse/cmake-2.8.8/Bootstrap.cmk/cmake

 

Program received signal EXC_BAD_ACCESS, Could not access memory.

Reason: KERN_INVALID_ADDRESS at address: 0×00000000

0×00000000 in ?? ()

(gdb) where

#0  0×00000000 in ?? ()

(gdb) info frame

Stack level 0, frame at 0×0:

pc = 0×0; saved pc 0x2c45dc

Arglist at 0x2fdfe2f8, args:

Locals at 0x2fdfe2f8,

(gdb) q

The program is running.  Exit anyway? (y or n) y

./bootstrap: line 1523: 45776 Segmentation fault: 11  “${cmake_bootstrap_dir}/cmake” “${cmake_source_dir}” “-C${cmake_bootstrap_dir}/InitialCacheFlags.cmake” “-G${cmake_bootstrap_generator}” ${cmake_options} ${cmake_bootstrap_system_libs} “$@”

Problem while running initial CMake

———————————————

# otool -L Bootstrap.cmk/cmake

Bootstrap.cmk/cmake:

/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation (compatibility version 150.0.0, current version 690.1.0)

/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 53.4.0)

/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 6.0.0)

/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 161.1.0)

# uname -a

Darwin iPad 11.0.0 Darwin Kernel Version 11.0.0: Sun Apr  8 21:51:47 PDT 2012; root:xnu-1878.11.10~1/RELEASE_ARM_S5L8940X iPad2,1 arm K93AP Darwin

Posted in 未分類 | Leave a comment

Looks like Lion is leaking memory???

Posted in 未分類 | Leave a comment

iTunesに曲が追加できなくなる問題の対処法

ここに書いてあった。
それでも、まだ完治してない気がする。おかしいなぁ。

Posted in 未分類 | Leave a comment

OSX Lion の不具合色々

・Mission Controlの画面切り替えがスワイプで出来るはずだが、すんなり行かず、何回かに一度かしかできない。

・ブラウザの、「戻る」「進む」は、3本指でなく2本指のスワイプになったが、これもいつもうまく行くわけではない上に、Firefoxでは、できない。

・iTunesに曲を追加しようとすると、ディスクのアクセス権に問題があるとか言われる。

Posted in 未分類 | Leave a comment

壊れたiPhone

落っことしたら壊れた。2年間良く耐えたモノだとおもふ。
さうしたら、バイブレータがなるたびにバリバリいふ。本當にえらいこっちゃだョ。

Posted in 未分類 | Leave a comment

タバコを買ったら

マタだって。
言われてしもうた。

20110713-011814.jpg

Posted in 未分類 | Leave a comment

To compile libicu for Wii – checkpoint 1

Comment outbelow from umutex.h

#if defined(U_DARWIN)
#if defined(__STRICT_ANSI__)
#define UPRV_REMAP_INLINE
#define inline
#endif
#include #define USE_MAC_OS_ATOMIC_INCREMENT 1
#if defined(UPRV_REMAP_INLINE)
#undef inline
#undef UPRV_REMAP_INLINE
#endif
#endif

Posted in 未分類 | Leave a comment

Wiiのバイナリ – 続き

この辺かな?
-mrvl -mcpu=750 -meabi -mhard-float

Posted in 未分類 | Leave a comment

Wiiのバイナリ

GCC のオプションは、-meabi だけでいいのかな?
ライブラリをコンパイルしたいから、もう少しいろいろと詳細が知りたい。

Posted in 未分類 | Leave a comment