1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.cts.verifier.nfc.hcef;
18 
19 import android.content.Intent;
20 import android.nfc.cardemulation.HostNfcFService;
21 import android.os.Bundle;
22 import android.util.Log;
23 
24 import com.android.cts.verifier.nfc.hce.HceUtils;
25 
26 public class MyHostFelicaService extends HostNfcFService {
27 
28     static final String TAG = "MyHostFelicaService";
29     static byte[] NFCID2 = {0x02, (byte) 0xFE, 0x00, 0x00, 0x00, 0x00, 0x14, (byte)0x81};
30 
31     static byte CMD_REQUEST_SYSTEM_CODES = 0x0C;
32     static byte RESPONSE_SYSTEM_CODES = 0x0D;
33     static byte CMD_ECHO = (byte) 0xFE;
34     static byte RESPONSE_ECHO = (byte) 0xFF;
35     static byte CMD_SUCCESS = (byte) 0x81;
36 
handleSystemCodeRequest(byte[] sc_request)37     static byte[] handleSystemCodeRequest(byte[] sc_request) {
38         // Request system code command
39         byte[] response = new byte[13];
40         response[0] = 0x0D; // length
41         response[1] = RESPONSE_SYSTEM_CODES; // get system codes resp
42         System.arraycopy(sc_request, 2, response, 2, 8);
43         response[10] = 0x01;
44         response[11] = 0x40;
45         response[12] = 0x01;
46         return response;
47     }
48 
handleEchoRequest(byte[] echo_request)49     static byte[] handleEchoRequest(byte[] echo_request) {
50         byte[] response = new byte[echo_request.length];
51         response[0] = (byte) echo_request.length;
52         response[1] = RESPONSE_ECHO;
53         for (int i = 2; i < echo_request.length; i++) {
54             // Copy NFCID2 and rest of data
55             response[i] = echo_request[i];
56         }
57         return response;
58 
59     }
60 
61     @Override
processNfcFPacket(byte[] bytes, Bundle bundle)62     public byte[] processNfcFPacket(byte[] bytes, Bundle bundle) {
63         // Verify that NFCID2 matches with this service
64         if (bytes.length < 2 + NFCID2.length) {
65             Log.e(TAG, "Packet not long enough.");
66             return null;
67         }
68         for (int i = 0; i < NFCID2.length; i++) {
69            if (bytes[2 + i] != NFCID2[i]) {
70                Log.e(TAG, "NFCID2 does not match.");
71                return null;
72            }
73         }
74         byte cmd = bytes[1];
75         if (cmd == CMD_REQUEST_SYSTEM_CODES) {
76             return handleSystemCodeRequest(bytes);
77         } else if (cmd == CMD_ECHO) {
78             return handleEchoRequest(bytes);
79         } else if (cmd == CMD_SUCCESS) {
80             // Mark the test a success
81             Intent successIntent = new Intent(HceFEmulatorActivity.ACTION_TEST_SUCCESS);
82             sendBroadcast(successIntent);
83             // And just echo cmd back
84             return bytes;
85         } else {
86             Log.e(TAG, "Invalid command received");
87         }
88 
89         return null;
90     }
91 
92     @Override
onDeactivated(int i)93     public void onDeactivated(int i) {
94 
95     }
96 }
97