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 static void *(*real_GKI_getbuf)(uint16_t size) = nullptr;
38 static void (*real_GKI_freebuf)(void *ptr) = nullptr;
39 
init(void)40 void init(void) {
41   real_GKI_getbuf = (void *(*)(uint16_t))dlsym(RTLD_NEXT, "_Z10GKI_getbuft");
42   if (!real_GKI_getbuf) {
43     return;
44   }
45 
46   real_GKI_freebuf = (void (*)(void *))dlsym(RTLD_NEXT, "_Z11GKI_freebufPv");
47   if (!real_GKI_freebuf) {
48     return;
49   }
50 
51   kIsInitialized = true;
52 }
53 
GKI_getbuf(uint16_t size)54 void *GKI_getbuf(uint16_t size) {
55   if (!kIsInitialized) {
56     init();
57   }
58   return malloc(size);
59 }
60 
GKI_freebuf(void * ptr)61 void GKI_freebuf(void *ptr) {
62   if (!kIsInitialized) {
63     init();
64   }
65   free(ptr);
66 }
67 
main()68 int main() {
69   tRW_I93_CB *p_i93 = &rw_cb.tcb.i93;
70 
71   GKI_init();
72   rw_init();
73 
74   uint8_t p_uid = 1;
75   if (rw_i93_select(&p_uid) != NFC_STATUS_OK) {
76     return EXIT_FAILURE;
77   }
78 
79   tNFC_CONN_CB *p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
80   nfc_cb.quick_timer_queue.p_first = (TIMER_LIST_ENT *)malloc(16);
81   tNFC_CONN_EVT event = NFC_DATA_CEVT;
82   p_i93->state = RW_I93_STATE_SET_READ_ONLY;
83   p_i93->i93_t5t_mode = RW_I93_GET_SYS_INFO_MEM_INFO;
84   p_i93->sub_state = RW_I93_SUBSTATE_WAIT_CC;
85   p_i93->block_size = 255;
86 
87   enable_selective_overload = ENABLE_ALL;
88   tNFC_CONN *p_data = (tNFC_CONN *)malloc(sizeof(tNFC_CONN));
89   if (!p_data) {
90     free(nfc_cb.quick_timer_queue.p_first);
91     return EXIT_FAILURE;
92   }
93 
94   p_data->data.p_data = (NFC_HDR *)GKI_getbuf(sizeof(NFC_HDR));
95   if (!(p_data->data.p_data)) {
96     free(p_data);
97     free(nfc_cb.quick_timer_queue.p_first);
98     return EXIT_FAILURE;
99   }
100   enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
101 
102   (p_data->data.p_data)->len = 10;
103   p_data->data.p_data->offset = 0;
104   p_data->status = NFC_STATUS_OK;
105 
106   p_cb->p_cback(0, event, p_data);
107 
108   free(p_data);
109   free(nfc_cb.quick_timer_queue.p_first);
110   return EXIT_SUCCESS;
111 }
112