ce_main.c revision b58ba0e89a3767e6174c42d3e90540d1eae10f81
1/******************************************************************************
2 *
3 *  Copyright (C) 2009-2012 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 "nfc_target.h"
28#include "bt_types.h"
29
30#if (NFC_INCLUDED == TRUE)
31#include "nfc_api.h"
32#include "nci_hmsgs.h"
33#include "ce_api.h"
34#include "ce_int.h"
35#include "gki.h"
36
37tCE_CB  ce_cb;
38
39/*******************************************************************************
40*******************************************************************************/
41void ce_init (void)
42{
43    memset (&ce_cb, 0, sizeof (tCE_CB));
44    ce_cb.trace_level = NFC_INITIAL_TRACE_LEVEL;
45
46    /* Initialize tag-specific fields of ce control block */
47    ce_t3t_init ();
48}
49
50/*******************************************************************************
51**
52** Function         CE_SendRawFrame
53**
54** Description      This function sends a raw frame to the peer device.
55**
56** Returns          tNFC_STATUS
57**
58*******************************************************************************/
59tNFC_STATUS CE_SendRawFrame (UINT8 *p_raw_data, UINT16 data_len)
60{
61    tNFC_STATUS status = NFC_STATUS_FAILED;
62    BT_HDR  *p_data;
63    UINT8   *p;
64
65    if (ce_cb.p_cback)
66    {
67        /* a valid opcode for RW */
68        p_data = (BT_HDR *) GKI_getpoolbuf (NFC_RW_POOL_ID);
69        if (p_data)
70        {
71            p_data->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
72            p = (UINT8 *) (p_data + 1) + p_data->offset;
73            memcpy (p, p_raw_data, data_len);
74            p_data->len = data_len;
75            CE_TRACE_EVENT1 ("CE SENT raw frame (0x%x)", data_len);
76            status = NFC_SendData (NFC_RF_CONN_ID, p_data);
77        }
78
79    }
80    return status;
81}
82
83/*******************************************************************************
84**
85** Function         CE_SetActivatedTagType
86**
87** Description      This function selects the tag type for CE mode.
88**
89** Returns          tNFC_STATUS
90**
91*******************************************************************************/
92tNFC_STATUS CE_SetActivatedTagType (tNFC_ACTIVATE_DEVT *p_activate_params, UINT16 t3t_system_code, tCE_CBACK *p_cback)
93{
94    tNFC_STATUS status = NFC_STATUS_FAILED;
95    tNFC_PROTOCOL protocol = p_activate_params->protocol;
96
97    CE_TRACE_API1 ("CE_SetActivatedTagType protocol:%d", protocol);
98
99    switch (protocol)
100    {
101    case NFC_PROTOCOL_T1T:
102    case NFC_PROTOCOL_T2T:
103        return NFC_STATUS_FAILED;
104
105    case NFC_PROTOCOL_T3T:   /* Type3Tag    - NFC-F */
106        /* store callback function before NFC_SetStaticRfCback () */
107        ce_cb.p_cback  = p_cback;
108        status = ce_select_t3t (t3t_system_code, p_activate_params->rf_tech_param.param.lf.nfcid2);
109        break;
110
111    case NFC_PROTOCOL_ISO_DEP:     /* ISODEP/4A,4B- NFC-A or NFC-B */
112        /* store callback function before NFC_SetStaticRfCback () */
113        ce_cb.p_cback  = p_cback;
114        status = ce_select_t4t ();
115        break;
116
117    default:
118        CE_TRACE_ERROR0 ("CE_SetActivatedTagType Invalid protocol");
119        return NFC_STATUS_FAILED;
120    }
121
122    if (status != NFC_STATUS_OK)
123    {
124        NFC_SetStaticRfCback (NULL);
125        ce_cb.p_cback  = NULL;
126    }
127    return status;
128}
129
130/*******************************************************************************
131**
132** Function         CE_SetTraceLevel
133**
134** Description      This function sets the trace level for Card Emulation mode.
135**                  If called with a value of 0xFF,
136**                  it simply returns the current trace level.
137**
138** Returns          The new or current trace level
139**
140*******************************************************************************/
141UINT8 CE_SetTraceLevel (UINT8 new_level)
142{
143    if (new_level != 0xFF)
144        ce_cb.trace_level = new_level;
145
146    return (ce_cb.trace_level);
147}
148
149#endif /* NFC_INCLUDED == TRUE */
150