1 /*
2 * Copyright (C) 2021 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 #include <nfc_api.h>
18 #include <nfc_int.h>
19 #include <rw_int.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <tags_defs.h>
23
24 #include "../includes/common.h"
25
26 #define T3T_MSG_FELICALITE_MC_OFFSET 0x01
27
28 bool testInProgress = false;
29
30 struct sigaction new_action, old_action;
31
sigsegv_handler(int signum,siginfo_t * info,void * context)32 void sigsegv_handler(int signum, siginfo_t *info, void *context) {
33 if (testInProgress && info->si_signo == SIGSEGV) {
34 (*old_action.sa_sigaction)(signum, info, context);
35 return;
36 }
37 exit (EXIT_FAILURE);
38 }
39
40 extern tRW_CB rw_cb;
41 extern tNFC_CB nfc_cb;
42 tNFC_CONN *p_data;
43 void rw_init(void);
44 tNFC_STATUS rw_t3t_select(uint8_t peer_nfcid2[NCI_RF_F_UID_LEN],
45 uint8_t mrti_check, uint8_t mrti_update);
46
allocate_memory(size_t size)47 void *allocate_memory(size_t size) {
48 void *ptr = malloc(size);
49 if (ptr) {
50 memset(ptr, 0x0, size);
51 }
52 return ptr;
53 }
54
55 /* States */
56 enum {
57 RW_T3T_STATE_NOT_ACTIVATED, RW_T3T_STATE_IDLE, RW_T3T_STATE_COMMAND_PENDING
58 };
59
60 /* Enumeration of API commands */
61 enum {
62 RW_T3T_CMD_DETECT_NDEF,
63 RW_T3T_CMD_CHECK_NDEF,
64 RW_T3T_CMD_UPDATE_NDEF,
65 RW_T3T_CMD_CHECK,
66 RW_T3T_CMD_UPDATE,
67 RW_T3T_CMD_SEND_RAW_FRAME,
68 RW_T3T_CMD_GET_SYSTEM_CODES,
69 RW_T3T_CMD_FORMAT,
70 RW_T3T_CMD_SET_READ_ONLY_SOFT,
71 RW_T3T_CMD_SET_READ_ONLY_HARD,
72 RW_T3T_CMD_MAX
73 };
74
75 /* Sub-states */
76 enum {
77 /* Sub states for formatting Felica-Lite */
78 RW_T3T_FMT_SST_POLL_FELICA_LITE, /* Waiting for POLL Felica-Lite response (for
79 formatting) */
80 RW_T3T_FMT_SST_CHECK_MC_BLK, /* Waiting for Felica-Lite MC (MemoryControl)
81 block-read to complete */
82 RW_T3T_FMT_SST_UPDATE_MC_BLK, /* Waiting for Felica-Lite MC (MemoryControl)
83 block-write to complete */
84 RW_T3T_FMT_SST_UPDATE_NDEF_ATTRIB, /* Waiting for NDEF attribute block-write
85 to complete */
86 /* Sub states for setting Felica-Lite read only */
87 RW_T3T_SRO_SST_POLL_FELICA_LITE, /* Waiting for POLL Felica-Lite response (for
88 setting read only) */
89 RW_T3T_SRO_SST_UPDATE_NDEF_ATTRIB, /* Waiting for NDEF attribute block-write
90 to complete */
91 RW_T3T_SRO_SST_CHECK_MC_BLK, /* Waiting for Felica-Lite MC (MemoryControl)
92 block-read to complete */
93 RW_T3T_SRO_SST_UPDATE_MC_BLK /* Waiting for Felica-Lite MC (MemoryControl)
94 block-write to complete */
95 };
96
97 enum {
98 P_MC_VAL = !T3T_MSG_FELICALITE_MC_OFFSET
99 };
100
poc_cback(tRW_EVENT event,tRW_DATA * p_rw_data)101 void poc_cback(tRW_EVENT event, tRW_DATA *p_rw_data) {
102 (void) event;
103 (void) p_rw_data;
104 }
105
GKI_freebuf(void * p_buf)106 void GKI_freebuf(void* p_buf __attribute__((unused))) {
107 }
108
GKI_start_timer(uint8_t,int32_t,bool)109 void GKI_start_timer(uint8_t, int32_t, bool) {
110 }
111
GKI_stop_timer(uint8_t)112 void GKI_stop_timer(uint8_t) {
113 }
114
exit_handler(void)115 void exit_handler(void) {
116 if (p_data) {
117 if (p_data->data.p_data) {
118 free(p_data->data.p_data);
119 p_data->data.p_data = nullptr;
120 }
121 free(p_data);
122 p_data = nullptr;
123 }
124 }
125
main()126 int main() {
127 atexit(exit_handler);
128 sigemptyset(&new_action.sa_mask);
129 new_action.sa_flags = SA_SIGINFO;
130 new_action.sa_sigaction = sigsegv_handler;
131 sigaction(SIGSEGV, &new_action, &old_action);
132
133 tNFC_ACTIVATE_DEVT p_activate_params = { };
134 p_activate_params.protocol = NFC_PROTOCOL_ISO_DEP;
135 p_activate_params.rf_tech_param.mode = NFC_DISCOVERY_TYPE_POLL_A;
136 RW_SetActivatedTagType(&p_activate_params, &poc_cback);
137 FAIL_CHECK(rw_cb.p_cback == &poc_cback);
138
139 tRW_T3T_CB *p_t3t = &rw_cb.tcb.t3t;
140 GKI_init();
141 rw_init();
142
143 rw_cb.p_cback = &poc_cback;
144 uint8_t peer_nfcid2[NCI_RF_F_UID_LEN];
145 uint8_t mrti_check = 1, mrti_update = 1;
146 FAIL_CHECK(rw_t3t_select(peer_nfcid2, mrti_check, mrti_update) == NFC_STATUS_OK);
147
148 p_data = (tNFC_CONN *) allocate_memory(sizeof(tNFC_CONN));
149 FAIL_CHECK(p_data);
150
151 p_data->data.p_data = (NFC_HDR *) allocate_memory(sizeof(NFC_HDR) * 4);
152 FAIL_CHECK(p_data->data.p_data);
153
154 p_data->status = NFC_STATUS_OK;
155 p_t3t->cur_cmd = RW_T3T_CMD_FORMAT;
156 p_t3t->rw_state = RW_T3T_STATE_COMMAND_PENDING;
157 p_t3t->rw_substate = RW_T3T_FMT_SST_CHECK_MC_BLK;
158 NFC_HDR *p_msg = (p_data->data).p_data;
159 p_msg->len = T3T_MSG_RSP_COMMON_HDR_LEN;
160 uint8_t *p_t3t_rsp = (uint8_t *) (p_msg + 1) + (p_msg->offset + 1);
161 p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] = T3T_MSG_OPC_CHECK_RSP;
162 p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] = T3T_MSG_RSP_STATUS_OK;
163 uint8_t *p_mc = &p_t3t_rsp[T3T_MSG_RSP_OFFSET_CHECK_DATA];
164 p_mc[T3T_MSG_FELICALITE_MC_OFFSET_SYS_OP] = P_MC_VAL;
165 tNFC_CONN_CB *p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
166 tNFC_CONN_EVT event = NFC_DATA_CEVT;
167 memcpy(p_t3t->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM],
168 NCI_NFCID2_LEN);
169
170 testInProgress = true;
171 p_cb->p_cback(0, event, p_data);
172 testInProgress = false;
173
174 return EXIT_SUCCESS;
175 }
176