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 <nfc_int.h>
19 #include <rw_int.h>
20 
21 extern tRW_CB rw_cb;
22 extern tNFC_CB nfc_cb;
23 tNFC_CONN *p_data;
24 void rw_init(void);
25 tNFC_STATUS rw_t3t_select(uint8_t peer_nfcid2[NCI_RF_F_UID_LEN],
26         uint8_t mrti_check, uint8_t mrti_update);
27 
allocate_memory(size_t size)28 void *allocate_memory(size_t size) {
29     void *ptr = malloc(size);
30     memset(ptr, 0x0, size);
31     return ptr;
32 }
33 
34 /* States */
35 enum {
36     RW_T3T_STATE_NOT_ACTIVATED,
37     RW_T3T_STATE_IDLE,
38     RW_T3T_STATE_COMMAND_PENDING
39 };
40 
41 /* Enumeration of API commands */
42 enum {
43     RW_T3T_CMD_DETECT_NDEF,
44     RW_T3T_CMD_CHECK_NDEF,
45     RW_T3T_CMD_UPDATE_NDEF,
46     RW_T3T_CMD_CHECK,
47     RW_T3T_CMD_UPDATE,
48     RW_T3T_CMD_SEND_RAW_FRAME,
49     RW_T3T_CMD_GET_SYSTEM_CODES,
50     RW_T3T_CMD_FORMAT,
51     RW_T3T_CMD_SET_READ_ONLY_SOFT,
52     RW_T3T_CMD_SET_READ_ONLY_HARD,
53     RW_T3T_CMD_MAX
54 };
55 
poc_cback(tRW_EVENT event,tRW_DATA * p_rw_data)56 void poc_cback(tRW_EVENT event, tRW_DATA* p_rw_data) {
57     (void)event;
58     (void)p_rw_data;
59     free(p_data->data.p_data);
60     free(p_data);
61 }
62 
GKI_start_timer(uint8_t,int32_t,bool)63 void GKI_start_timer(uint8_t, int32_t, bool) {
64 }
65 
GKI_stop_timer(uint8_t)66 void GKI_stop_timer(uint8_t) {
67 }
68 
main()69 int main() {
70     tRW_T3T_CB* p_t3t = &rw_cb.tcb.t3t;
71 
72     GKI_init();
73     rw_init();
74     rw_cb.p_cback = &poc_cback;
75 
76     uint8_t peer_nfcid2[NCI_RF_F_UID_LEN];
77     uint8_t mrti_check = 1, mrti_update = 1;
78     if (rw_t3t_select(peer_nfcid2, mrti_check, mrti_update) != NFC_STATUS_OK) {
79         return EXIT_FAILURE;
80     }
81 
82     p_data = (tNFC_CONN *) allocate_memory(sizeof(tNFC_CONN));
83     if (!p_data) {
84         return EXIT_FAILURE;
85     }
86 
87     p_data->data.p_data = (NFC_HDR*)allocate_memory(3 * sizeof(NFC_HDR));
88     if(!p_data->data.p_data) {
89         free(p_data);
90         return EXIT_FAILURE;
91     }
92     p_data->status = NFC_STATUS_OK;
93 
94     p_t3t->rw_state = RW_T3T_STATE_COMMAND_PENDING;
95     p_t3t->cur_cmd = RW_T3T_CMD_DETECT_NDEF;
96 
97     NFC_HDR* p_msg = (p_data->data).p_data;;
98     p_msg->len = T3T_MSG_RSP_COMMON_HDR_LEN;
99 
100     uint8_t* p_t3t_rsp = (uint8_t*) (p_msg + 1) + (p_msg->offset + 1);
101     p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] = T3T_MSG_OPC_CHECK_RSP;
102     p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] = T3T_MSG_RSP_STATUS_OK;
103 
104     tNFC_CONN_CB* p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
105     tNFC_CONN_EVT event = NFC_DATA_CEVT;
106     memcpy(p_t3t->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM],
107            NCI_NFCID2_LEN);
108     p_cb->p_cback(0, event, p_data);
109     return EXIT_SUCCESS;
110 }
111