HCE Felica CTS tests.
Bug: 22379408
Change-Id: I13164bb35187d0e6ee7895fada988feff56b7293
diff --git a/apps/CtsVerifier/AndroidManifest.xml b/apps/CtsVerifier/AndroidManifest.xml
index e920b83..17d6a07 100644
--- a/apps/CtsVerifier/AndroidManifest.xml
+++ b/apps/CtsVerifier/AndroidManifest.xml
@@ -478,6 +478,16 @@
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
+ <activity android:name="com.android.cts.verifier.nfc.hcef.HceFReaderTestActivity"
+ android:label="@string/nfc_hce_f_reader_tests"
+ android:configChanges="keyboardHidden|orientation|screenSize">
+ </activity>
+
+ <activity android:name="com.android.cts.verifier.nfc.hcef.HceFEmulatorTestActivity"
+ android:label="@string/nfc_hce_f_emulator_tests"
+ android:configChanges="keyboardHidden|orientation|screenSize">
+ </activity>
+
<activity android:name=".nfc.NdefPushSenderActivity"
android:label="@string/nfc_ndef_push_sender"
android:configChanges="keyboardHidden|orientation|screenSize" />
@@ -582,6 +592,14 @@
android:label="@string/nfc_hce_other_conflicting_prefix_aids_emulator"
android:configChanges="keyboardHidden|orientation|screenSize" />
+ <activity android:name=".nfc.hcef.HceFEmulatorActivity"
+ android:label="@string/nfc_hce_f_emulator"
+ android:configChanges="keyboardHidden|orientation|screenSize" />
+
+ <activity android:name=".nfc.hcef.HceFReaderActivity"
+ android:label="@string/nfc_hce_f_reader"
+ android:configChanges="keyboardHidden|orientation|screenSize" />
+
<!-- services used for testing NFC host-based card emulation -->
<service android:name=".nfc.hce.PaymentService1" android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE"
@@ -711,6 +729,15 @@
<meta-data android:name="android.nfc.cardemulation.host_apdu_service" android:resource="@xml/payment_aid_list_1"/>
</service>
+ <service
+ android:name=".nfc.hcef.MyHostFelicaService"
+ android:enabled="true"
+ android:exported="true" android:permission="android.permission.BIND_NFC_SERVICE">
+ <intent-filter>
+ <action android:name="android.nfc.cardemulation.action.HOST_NFCF_SERVICE"/>
+ </intent-filter>
+ <meta-data android:name="android.nfc.cardemulation.host_nfcf_service" android:resource="@xml/felicaservice"/>
+ </service>
<!-- Service used for Camera ITS tests -->
<service android:name=".camera.its.ItsService" >
<intent-filter>
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index 28321e4..2a83d4d 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -468,8 +468,13 @@
<string name="nfc_ndef_content">Id: %1$s\nMime: %2$s\nPayload: %3$s</string>
<string name="nfc_hce">Host-based card emulation</string>
+ <string name="nfc_hce_f">Host-based Felica card emulation</string>
<string name="nfc_hce_reader_tests">HCE reader tests</string>
+ <string name="nfc_hce_f_reader_tests">HCE Felica reader tests</string>
<string name="nfc_hce_emulator_tests">HCE emulator tests</string>
+ <string name="nfc_hce_f_emulator_tests">HCE Felica emulator tests</string>
+ <string name="nfc_hce_f_emulator">HCE Felica emulator</string>
+ <string name="nfc_hce_f_reader">HCE Felica reader</string>
<string name="nfc_hce_emulator_test_info">The host-based card emulation
tests require two devices to be completed. The HCE emulator tests are used
to actually test the host-based card emulation feature of the device-under-test. So the
@@ -577,6 +582,7 @@
<string name="transportService2">TransportService #2</string>
<string name="accessService">AccessService</string>
<string name="offhostService">OffhostService</string>
+ <string name="felicaservice">Felica Service</string>
<!-- Strings for Sensor Test Activities -->
<string name="snsr_device_admin_receiver">Sensor Tests Device Admin Receiver</string>
diff --git a/apps/CtsVerifier/res/xml/felicaservice.xml b/apps/CtsVerifier/res/xml/felicaservice.xml
new file mode 100644
index 0000000..7d1b164
--- /dev/null
+++ b/apps/CtsVerifier/res/xml/felicaservice.xml
@@ -0,0 +1,5 @@
+<host-nfcf-service xmlns:android="http://schemas.android.com/apk/res/android"
+ android:description="@string/felicaservice">
+ <system-code-filter android:name="4001" />
+ <nfcid2-filter android:name="02FE000000001481" />
+</host-nfcf-service>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NfcTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NfcTestActivity.java
index 68fc027..eb84d1e 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NfcTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/NfcTestActivity.java
@@ -22,6 +22,8 @@
import com.android.cts.verifier.TestListAdapter.TestListItem;
import com.android.cts.verifier.nfc.hce.HceEmulatorTestActivity;
import com.android.cts.verifier.nfc.hce.HceReaderTestActivity;
+import com.android.cts.verifier.nfc.hcef.HceFEmulatorTestActivity;
+import com.android.cts.verifier.nfc.hcef.HceFReaderTestActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
@@ -82,6 +84,16 @@
new Intent(this, HceEmulatorTestActivity.class), null));
}
+ if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF)) {
+ adapter.add(TestListItem.newCategory(this, R.string.nfc_hce_f));
+ adapter.add(TestListItem.newTest(this, R.string.nfc_hce_f_reader_tests,
+ HceFReaderTestActivity.class.getName(),
+ new Intent(this, HceFReaderTestActivity.class), null));
+ adapter.add(TestListItem.newTest(this, R.string.nfc_hce_f_emulator_tests,
+ HceFEmulatorTestActivity.class.getName(),
+ new Intent(this, HceFEmulatorTestActivity.class), null));
+ }
+
setTestListAdapter(adapter);
}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFEmulatorActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFEmulatorActivity.java
new file mode 100644
index 0000000..89f040b
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFEmulatorActivity.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.cts.verifier.nfc.hcef;
+
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.nfc.NfcAdapter;
+import android.nfc.cardemulation.NfcFCardEmulation;
+import android.os.Bundle;
+
+import com.android.cts.verifier.PassFailButtons;
+import com.android.cts.verifier.R;
+
+public class HceFEmulatorActivity extends PassFailButtons.Activity{
+ static String ACTION_TEST_SUCCESS = "success";
+
+ NfcAdapter mAdapter;
+ NfcFCardEmulation mNfcFCardEmulation;
+
+ final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+
+ if (ACTION_TEST_SUCCESS.equals(action)) {
+ getPassButton().setEnabled(true);
+ }
+ }
+ };
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.pass_fail_text);
+ setPassFailButtonClickListeners();
+ getPassButton().setEnabled(false);
+ mAdapter = NfcAdapter.getDefaultAdapter(this);
+ mNfcFCardEmulation = NfcFCardEmulation.getInstance(mAdapter);
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ IntentFilter filter = new IntentFilter(ACTION_TEST_SUCCESS);
+ registerReceiver(mReceiver, filter);
+ ComponentName hceFService = new ComponentName("com.android.cts.verifier",
+ MyHostFelicaService.class.getName());
+ mNfcFCardEmulation.enableNfcFForegroundService(this, hceFService);
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ unregisterReceiver(mReceiver);
+ }
+
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFEmulatorTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFEmulatorTestActivity.java
new file mode 100644
index 0000000..026bb45
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFEmulatorTestActivity.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.cts.verifier.nfc.hcef;
+
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.nfc.NfcAdapter;
+import android.nfc.cardemulation.CardEmulation;
+import android.os.Bundle;
+
+import com.android.cts.verifier.ArrayTestListAdapter;
+import com.android.cts.verifier.PassFailButtons;
+import com.android.cts.verifier.R;
+import com.android.cts.verifier.TestListAdapter.TestListItem;
+
+/** Activity that lists all the NFC HCE emulator tests. */
+public class HceFEmulatorTestActivity extends PassFailButtons.TestListActivity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.pass_fail_list);
+ setInfoResources(R.string.nfc_test, R.string.nfc_hce_emulator_test_info, 0);
+ setPassFailButtonClickListeners();
+
+ ArrayTestListAdapter adapter = new ArrayTestListAdapter(this);
+
+ NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
+ CardEmulation cardEmulation = CardEmulation.getInstance(nfcAdapter);
+ if (getPackageManager().hasSystemFeature(
+ PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF)) {
+ adapter.add(TestListItem.newCategory(this, R.string.nfc_hce_f_emulator_tests));
+
+ adapter.add(TestListItem.newTest(this, R.string.nfc_hce_f_emulator,
+ HceFEmulatorActivity.class.getName(),
+ new Intent(this, HceFEmulatorActivity.class), null));
+ }
+
+ setTestListAdapter(adapter);
+ }
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFReaderActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFReaderActivity.java
new file mode 100644
index 0000000..ba31ff7
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFReaderActivity.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.cts.verifier.nfc.hcef;
+
+import android.annotation.TargetApi;
+import android.nfc.NfcAdapter;
+import android.nfc.NfcAdapter.ReaderCallback;
+import android.nfc.Tag;
+import android.nfc.tech.NfcF;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.widget.AdapterView;
+import android.widget.AdapterView.OnItemSelectedListener;
+
+import com.android.cts.verifier.PassFailButtons;
+import com.android.cts.verifier.R;
+
+import java.io.IOException;
+
+@TargetApi(24)
+public class HceFReaderActivity extends PassFailButtons.Activity implements ReaderCallback,
+ OnItemSelectedListener {
+ public static final String TAG = "HceFReaderActivity";
+
+ NfcAdapter mAdapter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.pass_fail_text);
+ setPassFailButtonClickListeners();
+ getPassButton().setEnabled(false);
+
+ mAdapter = NfcAdapter.getDefaultAdapter(this);
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ mAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_F |
+ NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK, null);
+ }
+
+ static byte[] createEchoCommand(byte[] nfcid2, byte[] payload) {
+ byte length = (byte) (2 + nfcid2.length + payload.length);
+
+ byte[] echo_cmd = new byte[length];
+ echo_cmd[0] = length;
+ echo_cmd[1] = MyHostFelicaService.CMD_ECHO;
+ System.arraycopy(nfcid2, 0, echo_cmd, 2, nfcid2.length);
+ System.arraycopy(payload, 0, echo_cmd, 2 + nfcid2.length, payload.length);
+ return echo_cmd;
+ }
+
+ static byte[] createSuccessCommand(byte[] nfcid2) {
+ byte[] cmd = new byte[2 + nfcid2.length];
+ cmd[0] = (byte) (2 + nfcid2.length);
+ cmd[1] = MyHostFelicaService.CMD_SUCCESS;
+ System.arraycopy(nfcid2, 0, cmd, 2, nfcid2.length);
+ return cmd;
+ }
+
+ static boolean verifyResponse(byte[] cmd, byte[] resp) {
+ if (resp == null) return false;
+
+ // Verify length
+ if (resp[0] != resp.length) return false;
+ if (resp.length != cmd.length) return false;
+ // Verify cmd
+ if (resp[1] != MyHostFelicaService.RESPONSE_ECHO) return false;
+
+ // Verify rest of data
+ for (int i = 2; i < resp.length; i++) {
+ if (resp[i] != cmd[i]) return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public void onTagDiscovered(Tag tag) {
+ NfcF felica = NfcF.get(tag);
+ if (felica == null) return;
+
+ try {
+ felica.connect();
+ for (int i = 0; i < 32; i++) {
+ byte[] payload = new byte[] {0x14, (byte)i};
+ byte[] echo_cmd = createEchoCommand(MyHostFelicaService.NFCID2, payload);
+ byte[] resp = felica.transceive(echo_cmd);
+ if (!verifyResponse(echo_cmd, resp)) {
+ Log.e(TAG, "Echo response not correct.");
+ return;
+ }
+ }
+ // All successful, send success cmd
+ byte[] success_cmd = createSuccessCommand(MyHostFelicaService.NFCID2);
+ felica.transceive(success_cmd);
+
+ runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ getPassButton().setEnabled(true);
+ }
+ });
+ } catch (IOException e) {
+ Log.e(TAG, "IOException, try again.");
+ }
+ }
+
+ @Override
+ public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
+
+ }
+
+ @Override
+ public void onNothingSelected(AdapterView<?> parent) {
+
+ }
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFReaderTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFReaderTestActivity.java
new file mode 100644
index 0000000..52de01d
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/HceFReaderTestActivity.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.cts.verifier.nfc.hcef;
+
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.os.Bundle;
+
+import com.android.cts.verifier.ArrayTestListAdapter;
+import com.android.cts.verifier.PassFailButtons;
+import com.android.cts.verifier.R;
+import com.android.cts.verifier.TestListAdapter.TestListItem;
+
+/** Activity that lists all the NFC HCE reader tests. */
+public class HceFReaderTestActivity extends PassFailButtons.TestListActivity {
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.pass_fail_list);
+ setInfoResources(R.string.nfc_test, R.string.nfc_hce_reader_test_info, 0);
+ setPassFailButtonClickListeners();
+
+ ArrayTestListAdapter adapter = new ArrayTestListAdapter(this);
+
+ if (getPackageManager().hasSystemFeature(
+ PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF)) {
+ adapter.add(TestListItem.newCategory(this, R.string.nfc_hce_f_reader_tests));
+
+ adapter.add(TestListItem.newTest(this, R.string.nfc_hce_f_reader,
+ HceFReaderActivity.class.getName(),
+ new Intent(this, HceFReaderActivity.class), null));
+
+ }
+
+ setTestListAdapter(adapter);
+ }
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/MyHostFelicaService.java b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/MyHostFelicaService.java
new file mode 100644
index 0000000..941ea2c
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/nfc/hcef/MyHostFelicaService.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.cts.verifier.nfc.hcef;
+
+import android.content.Intent;
+import android.nfc.cardemulation.HostNfcFService;
+import android.os.Bundle;
+import android.util.Log;
+
+import com.android.cts.verifier.nfc.hce.HceUtils;
+
+public class MyHostFelicaService extends HostNfcFService {
+
+ static final String TAG = "MyHostFelicaService";
+ static byte[] NFCID2 = {0x02, (byte) 0xFE, 0x00, 0x00, 0x00, 0x00, 0x14, (byte)0x81};
+
+ static byte CMD_REQUEST_SYSTEM_CODES = 0x0C;
+ static byte RESPONSE_SYSTEM_CODES = 0x0D;
+ static byte CMD_ECHO = (byte) 0xFE;
+ static byte RESPONSE_ECHO = (byte) 0xFF;
+ static byte CMD_SUCCESS = (byte) 0x81;
+
+ static byte[] handleSystemCodeRequest(byte[] sc_request) {
+ // Request system code command
+ byte[] response = new byte[13];
+ response[0] = 0x0D; // length
+ response[1] = RESPONSE_SYSTEM_CODES; // get system codes resp
+ System.arraycopy(sc_request, 2, response, 2, 8);
+ response[10] = 0x01;
+ response[11] = 0x40;
+ response[12] = 0x01;
+ return response;
+ }
+
+ static byte[] handleEchoRequest(byte[] echo_request) {
+ byte[] response = new byte[echo_request.length];
+ response[0] = (byte) echo_request.length;
+ response[1] = RESPONSE_ECHO;
+ for (int i = 2; i < echo_request.length; i++) {
+ // Copy NFCID2 and rest of data
+ response[i] = echo_request[i];
+ }
+ return response;
+
+ }
+
+ @Override
+ public byte[] processNfcFPacket(byte[] bytes, Bundle bundle) {
+ // Verify that NFCID2 matches with this service
+ if (bytes.length < 2 + NFCID2.length) {
+ Log.e(TAG, "Packet not long enough.");
+ return null;
+ }
+ for (int i = 0; i < NFCID2.length; i++) {
+ if (bytes[2 + i] != NFCID2[i]) {
+ Log.e(TAG, "NFCID2 does not match.");
+ return null;
+ }
+ }
+ byte cmd = bytes[1];
+ if (cmd == CMD_REQUEST_SYSTEM_CODES) {
+ return handleSystemCodeRequest(bytes);
+ } else if (cmd == CMD_ECHO) {
+ return handleEchoRequest(bytes);
+ } else if (cmd == CMD_SUCCESS) {
+ // Mark the test a success
+ Intent successIntent = new Intent(HceFEmulatorActivity.ACTION_TEST_SUCCESS);
+ sendBroadcast(successIntent);
+ // And just echo cmd back
+ return bytes;
+ } else {
+ Log.e(TAG, "Invalid command received");
+ }
+
+ return null;
+ }
+
+ @Override
+ public void onDeactivated(int i) {
+
+ }
+}