1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2014 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This file contains functions that interface with the NFC NCI transport.
22  *  On the receive side, it routes events to the appropriate handler
23  *  (callback). On the transmit side, it manages the command transmission.
24  *
25  ******************************************************************************/
26 #include <string.h>
27 #include "bt_types.h"
28 #include "nfc_target.h"
29 
30 #include "ce_api.h"
31 #include "ce_int.h"
32 #include "gki.h"
33 #include "nci_hmsgs.h"
34 #include "nfc_api.h"
35 
36 tCE_CB ce_cb;
37 
38 /*******************************************************************************
39 *******************************************************************************/
ce_init(void)40 void ce_init(void) {
41   memset(&ce_cb, 0, sizeof(tCE_CB));
42   ce_cb.trace_level = NFC_INITIAL_TRACE_LEVEL;
43 
44   /* Initialize tag-specific fields of ce control block */
45   ce_t3t_init();
46 }
47 
48 /*******************************************************************************
49 **
50 ** Function         CE_SendRawFrame
51 **
52 ** Description      This function sends a raw frame to the peer device.
53 **
54 ** Returns          tNFC_STATUS
55 **
56 *******************************************************************************/
CE_SendRawFrame(uint8_t * p_raw_data,uint16_t data_len)57 tNFC_STATUS CE_SendRawFrame(uint8_t* p_raw_data, uint16_t data_len) {
58   tNFC_STATUS status = NFC_STATUS_FAILED;
59   NFC_HDR* p_data;
60   uint8_t* p;
61 
62   if (ce_cb.p_cback) {
63     /* a valid opcode for RW */
64     p_data = (NFC_HDR*)GKI_getpoolbuf(NFC_RW_POOL_ID);
65     if (p_data) {
66       p_data->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
67       p = (uint8_t*)(p_data + 1) + p_data->offset;
68       memcpy(p, p_raw_data, data_len);
69       p_data->len = data_len;
70       CE_TRACE_EVENT1("CE SENT raw frame (0x%x)", data_len);
71       status = NFC_SendData(NFC_RF_CONN_ID, p_data);
72     }
73   }
74   return status;
75 }
76 
77 /*******************************************************************************
78 **
79 ** Function         CE_SetActivatedTagType
80 **
81 ** Description      This function selects the tag type for CE mode.
82 **
83 ** Returns          tNFC_STATUS
84 **
85 *******************************************************************************/
CE_SetActivatedTagType(tNFC_ACTIVATE_DEVT * p_activate_params,uint16_t t3t_system_code,tCE_CBACK * p_cback)86 tNFC_STATUS CE_SetActivatedTagType(tNFC_ACTIVATE_DEVT* p_activate_params,
87                                    uint16_t t3t_system_code,
88                                    tCE_CBACK* p_cback) {
89   tNFC_STATUS status = NFC_STATUS_FAILED;
90   tNFC_PROTOCOL protocol = p_activate_params->protocol;
91 
92   CE_TRACE_API1("CE_SetActivatedTagType protocol:%d", protocol);
93 
94   switch (protocol) {
95     case NFC_PROTOCOL_T1T:
96     case NFC_PROTOCOL_T2T:
97       return NFC_STATUS_FAILED;
98 
99     case NFC_PROTOCOL_T3T: /* Type3Tag    - NFC-F */
100       /* store callback function before NFC_SetStaticRfCback () */
101       ce_cb.p_cback = p_cback;
102       status = ce_select_t3t(t3t_system_code,
103                              p_activate_params->rf_tech_param.param.lf.nfcid2);
104       break;
105 
106     case NFC_PROTOCOL_ISO_DEP: /* ISODEP/4A,4B- NFC-A or NFC-B */
107       /* store callback function before NFC_SetStaticRfCback () */
108       ce_cb.p_cback = p_cback;
109       status = ce_select_t4t();
110       break;
111 
112     default:
113       CE_TRACE_ERROR0("CE_SetActivatedTagType Invalid protocol");
114       return NFC_STATUS_FAILED;
115   }
116 
117   if (status != NFC_STATUS_OK) {
118     NFC_SetStaticRfCback(NULL);
119     ce_cb.p_cback = NULL;
120   }
121   return status;
122 }
123 
124 /*******************************************************************************
125 **
126 ** Function         CE_SetTraceLevel
127 **
128 ** Description      This function sets the trace level for Card Emulation mode.
129 **                  If called with a value of 0xFF,
130 **                  it simply returns the current trace level.
131 **
132 ** Returns          The new or current trace level
133 **
134 *******************************************************************************/
CE_SetTraceLevel(uint8_t new_level)135 uint8_t CE_SetTraceLevel(uint8_t new_level) {
136   if (new_level != 0xFF) ce_cb.trace_level = new_level;
137 
138   return (ce_cb.trace_level);
139 }
140