1 /*
2 * Copyright (C) 2020 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 "../includes/common.h"
19 #include "../includes/memutils.h"
20
21 char enable_selective_overload = ENABLE_NONE;
22
23 #include <dlfcn.h>
24 #include <nfc_api.h>
25 #include <nfc_int.h>
26 #include <rw_int.h>
27 #include <tags_defs.h>
28
29 // borrowed from rw_i93.cc
30 extern tRW_CB rw_cb;
31 extern tNFC_CB nfc_cb;
32 void rw_init(void);
33 tNFC_STATUS rw_i93_select(uint8_t *p_uid);
34
35 bool kIsInitialized = false;
36
37 // borrowed from rw_i93.cc
38 enum {
39 RW_I93_STATE_NOT_ACTIVATED, /* ISO15693 is not activated */
40 RW_I93_STATE_IDLE, /* waiting for upper layer API */
41 RW_I93_STATE_BUSY, /* waiting for response from tag */
42
43 RW_I93_STATE_DETECT_NDEF, /* performing NDEF detection precedure */
44 RW_I93_STATE_READ_NDEF, /* performing read NDEF procedure */
45 RW_I93_STATE_UPDATE_NDEF, /* performing update NDEF procedure */
46 RW_I93_STATE_FORMAT, /* performing format procedure */
47 RW_I93_STATE_SET_READ_ONLY, /* performing set read-only procedure */
48
49 RW_I93_STATE_PRESENCE_CHECK /* checking presence of tag */
50 };
51
52 // borrowed from rw_i93.cc
53 enum {
54 RW_I93_SUBSTATE_WAIT_UID, /* waiting for response of inventory */
55 RW_I93_SUBSTATE_WAIT_SYS_INFO, /* waiting for response of get sys info */
56 RW_I93_SUBSTATE_WAIT_CC, /* waiting for reading CC */
57 RW_I93_SUBSTATE_SEARCH_NDEF_TLV, /* searching NDEF TLV */
58 RW_I93_SUBSTATE_CHECK_LOCK_STATUS, /* check if any NDEF TLV is locked */
59
60 RW_I93_SUBSTATE_RESET_LEN, /* set length to 0 to update NDEF TLV */
61 RW_I93_SUBSTATE_WRITE_NDEF, /* writing NDEF and Terminator TLV */
62 RW_I93_SUBSTATE_UPDATE_LEN, /* set length into NDEF TLV */
63
64 RW_I93_SUBSTATE_WAIT_RESET_DSFID_AFI, /* reset DSFID and AFI */
65 RW_I93_SUBSTATE_CHECK_READ_ONLY, /* check if any block is locked */
66 RW_I93_SUBSTATE_WRITE_CC_NDEF_TLV, /* write CC and empty NDEF/Terminator TLV
67 */
68
69 RW_I93_SUBSTATE_WAIT_UPDATE_CC, /* updating CC as read-only */
70 RW_I93_SUBSTATE_LOCK_NDEF_TLV, /* lock blocks of NDEF TLV */
71 RW_I93_SUBSTATE_WAIT_LOCK_CC /* lock block of CC */
72 };
73
74 static void *(*real_GKI_getbuf)(uint16_t size) = nullptr;
75 static void (*real_GKI_freebuf)(void *ptr) = nullptr;
76
init(void)77 void init(void) {
78 real_GKI_getbuf = (void *(*)(uint16_t))dlsym(RTLD_NEXT, "_Z10GKI_getbuft");
79 if (!real_GKI_getbuf) {
80 return;
81 }
82
83 real_GKI_freebuf = (void (*)(void *))dlsym(RTLD_NEXT, "_Z11GKI_freebufPv");
84 if (!real_GKI_freebuf) {
85 return;
86 }
87
88 kIsInitialized = true;
89 }
90
GKI_getbuf(uint16_t size)91 void *GKI_getbuf(uint16_t size) {
92 if (!kIsInitialized) {
93 init();
94 }
95 return malloc(size);
96 }
97
GKI_freebuf(void * ptr)98 void GKI_freebuf(void *ptr) {
99 if (!kIsInitialized) {
100 init();
101 }
102 free(ptr);
103 }
104
main()105 int main() {
106 tRW_I93_CB *p_i93 = &rw_cb.tcb.i93;
107
108 GKI_init();
109 rw_init();
110
111 uint8_t p_uid = 1;
112 if (rw_i93_select(&p_uid) != NFC_STATUS_OK) {
113 return EXIT_FAILURE;
114 }
115
116 tNFC_CONN_CB *p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
117 nfc_cb.quick_timer_queue.p_first = (TIMER_LIST_ENT *)malloc(16);
118 tNFC_CONN_EVT event = NFC_DATA_CEVT;
119 p_i93->state = RW_I93_STATE_UPDATE_NDEF;
120 p_i93->sub_state = RW_I93_SUBSTATE_UPDATE_LEN;
121 p_i93->block_size = 30;
122 p_i93->rw_length = 1;
123
124 enable_selective_overload = ENABLE_ALL;
125 tNFC_CONN *p_data = (tNFC_CONN *)malloc(sizeof(tNFC_CONN));
126 if (!p_data) {
127 free(nfc_cb.quick_timer_queue.p_first);
128 return EXIT_FAILURE;
129 }
130
131 p_data->data.p_data = (NFC_HDR *)GKI_getbuf(sizeof(NFC_HDR));
132 if (!(p_data->data.p_data)) {
133 free(p_data);
134 free(nfc_cb.quick_timer_queue.p_first);
135 return EXIT_FAILURE;
136 }
137 enable_selective_overload = ENABLE_NONE;
138
139 (p_data->data.p_data)->len = 10;
140 p_data->data.p_data->offset = 0;
141 p_data->status = NFC_STATUS_OK;
142
143 p_cb->p_cback(0, event, p_data);
144
145 free(p_data);
146 free(nfc_cb.quick_timer_queue.p_first);
147 return EXIT_SUCCESS;
148 }
149