1 /*
2  * Copyright (C) 2022 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 #include "../includes/common.h"
17 #include "../includes/memutils.h"
18 
19 #include <nfa_api.h>
20 #include <nfa_dm_int.h>
21 #include <nfa_rw_int.h>
22 #include <nfc_api.h>
23 #include <nfc_int.h>
24 #include <rw_int.h>
25 
26 bool testInProgress = false;
27 
28 struct sigaction new_action, old_action;
29 
sigsegv_handler(int signum,siginfo_t * info,void * context)30 void sigsegv_handler(int signum, siginfo_t *info, void *context) {
31   if (testInProgress && info->si_signo == SIGSEGV) {
32     (*old_action.sa_sigaction)(signum, info, context);
33     return;
34   }
35   exit(EXIT_FAILURE);
36 }
37 
38 char enable_selective_overload = ENABLE_NONE;
39 void *libHandle = nullptr;
40 
41 /* States */
42 enum {
43   RW_T3T_STATE_NOT_ACTIVATED,
44   RW_T3T_STATE_IDLE,
45   RW_T3T_STATE_COMMAND_PENDING
46 };
47 
48 /* Enumeration of API commands */
49 enum {
50   RW_T3T_CMD_DETECT_NDEF,
51   RW_T3T_CMD_CHECK_NDEF,
52   RW_T3T_CMD_UPDATE_NDEF,
53   RW_T3T_CMD_CHECK,
54   RW_T3T_CMD_UPDATE,
55   RW_T3T_CMD_SEND_RAW_FRAME,
56   RW_T3T_CMD_GET_SYSTEM_CODES,
57   RW_T3T_CMD_FORMAT,
58   RW_T3T_CMD_SET_READ_ONLY_SOFT,
59   RW_T3T_CMD_SET_READ_ONLY_HARD,
60   RW_T3T_CMD_MAX
61 };
62 
63 tNFA_RW_MSG *p_data = nullptr;
64 tNFC_DATA_CEVT *p_data1 = nullptr;
65 extern void rw_t3t_data_cback(__attribute__((unused)) uint8_t conn_id,
66                        tNFC_DATA_CEVT* p_data);
67 extern bool nfa_rw_activate_ntf(tNFA_RW_MSG* p_data);
68 extern tRW_CB rw_cb;
69 extern tNFC_CB nfc_cb;
70 tNFA_DM_CB nfa_dm_cb = {};
71 void rw_init(void);
72 void NFA_Init(tHAL_NFC_ENTRY *p_hal_entry_tbl);
73 
74 extern void rw_t2t_handle_rsp(uint8_t *p_data);
75 
GKI_freebuf(void * p_buf)76 void GKI_freebuf(void *p_buf __attribute__((unused))) {}
77 
GKI_start_timer(uint8_t,int32_t,bool)78 void GKI_start_timer(uint8_t, int32_t, bool) {}
79 
GKI_stop_timer(uint8_t)80 void GKI_stop_timer(uint8_t) {}
81 
poc_cback(tRW_EVENT event,tRW_DATA * p_rw_data)82 void poc_cback(tRW_EVENT event, tRW_DATA *p_rw_data) {
83   (void)event;
84   (void)p_rw_data;
85 }
86 
p_conn_cback(uint8_t event,tNFA_CONN_EVT_DATA * p_rw_data)87 void p_conn_cback(uint8_t event, tNFA_CONN_EVT_DATA *p_rw_data) {
88   (void)event;
89   (void)p_rw_data;
90 }
91 
exit_handler(void)92 void exit_handler(void) {
93   if (p_data) {
94       if (p_data->activate_ntf.p_activate_params) {
95           free(p_data->activate_ntf.p_activate_params);
96           p_data->activate_ntf.p_activate_params = nullptr;
97       }
98       free(p_data);
99       p_data = nullptr;
100   }
101 
102   if (p_data1) {
103       if(p_data1->p_data) {
104           free(p_data1->p_data);
105           p_data1->p_data = nullptr;
106       }
107     free(p_data1);
108     p_data1 = nullptr;
109   }
110 }
main()111 int main() {
112   atexit(exit_handler);
113   sigemptyset(&new_action.sa_mask);
114   new_action.sa_flags = SA_SIGINFO;
115   new_action.sa_sigaction = sigsegv_handler;
116   sigaction(SIGSEGV, &new_action, &old_action);
117 
118   tNFC_ACTIVATE_DEVT p_activate_params = {};
119   p_activate_params.protocol = NFC_PROTOCOL_ISO_DEP;
120   p_activate_params.rf_tech_param.mode = NFC_DISCOVERY_TYPE_POLL_A;
121   RW_SetActivatedTagType(&p_activate_params, &poc_cback);
122   FAIL_CHECK(rw_cb.p_cback == &poc_cback);
123 
124   GKI_init();
125   rw_init();
126   tHAL_NFC_ENTRY p_hal_entry_tbl;
127   NFA_Init(&p_hal_entry_tbl);
128   nfa_dm_cb.p_conn_cback = p_conn_cback;
129   p_data = (tNFA_RW_MSG *)calloc(1, sizeof(tNFA_RW_MSG));
130   FAIL_CHECK(p_data);
131 
132   p_data->activate_ntf.p_activate_params =
133       (tNFC_ACTIVATE_DEVT *)calloc(1, sizeof(tNFC_ACTIVATE_DEVT));
134   FAIL_CHECK(p_data->activate_ntf.p_activate_params);
135 
136   p_data->activate_ntf.p_activate_params->protocol = NFC_PROTOCOL_T2T;
137 
138   nfa_rw_activate_ntf(p_data);
139   tRW_CBACK *p_cback = rw_cb.p_cback;
140   FAIL_CHECK(p_cback);
141 
142   enable_selective_overload = ENABLE_ALL;
143   nfa_rw_cb.cur_op = NFA_RW_OP_READ_NDEF;
144   nfa_rw_cb.p_ndef_buf = (uint8_t *)calloc(1, sizeof(uint8_t));
145   FAIL_CHECK(nfa_rw_cb.p_ndef_buf);
146 
147   tRW_T3T_CB *p_cb = &rw_cb.tcb.t3t;
148   p_cb->rw_state = RW_T3T_STATE_COMMAND_PENDING;
149   p_cb->cur_cmd = RW_T3T_CMD_CHECK;
150   p_data1 = (tNFC_DATA_CEVT *)calloc(1, sizeof(tNFC_DATA_CEVT));
151   FAIL_CHECK(p_data1);
152   p_data1->p_data = (NFC_HDR *)calloc(3, sizeof(NFC_HDR));
153   FAIL_CHECK(p_data1->p_data);
154   enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
155 
156   NFC_HDR *p_msg = p_data1->p_data;
157   p_msg->offset = 0;
158   p_msg->len = T3T_MSG_RSP_COMMON_HDR_LEN;
159   uint8_t *p = (uint8_t *)(p_msg + 1) + p_msg->offset;
160   p[0] = NCI_STATUS_OK;
161 
162   uint8_t *p_t3t_rsp = p + 1;
163   p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] = T3T_MSG_OPC_CHECK_RSP;
164   p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] = T3T_MSG_RSP_STATUS_OK;
165   memcpy(p_cb->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM], NCI_NFCID2_LEN);
166   uint8_t conn_id = 0;
167 
168   testInProgress = true;
169   rw_t3t_data_cback(conn_id, p_data1);
170   testInProgress = false;
171 
172   return EXIT_SUCCESS;
173 }
174