HalAdaptation.cpp revision 5a7e857aee2ebba4e369936aa5acdc7bcecf83e7
1/******************************************************************************
2 *
3 *  Copyright (C) 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 *  HAL Adaptation Interface (HAI). This interface regulates the interaction
22 *  between standard Android HAL and Broadcom-specific HAL.  It adapts
23 *  Broadcom-specific features to the Android framework.
24 *
25 ******************************************************************************/
26#define LOG_TAG "NfcNciHal"
27#include "OverrideLog.h"
28#include "HalAdaptation.h"
29#include "SyncEvent.h"
30#include "config.h"
31#include "nfc_hal_int.h"
32#include "nfc_hal_post_reset.h"
33#include <errno.h>
34#include <pthread.h>
35#include "buildcfg.h"
36extern void delete_hal_non_volatile_store (bool forceDelete);
37extern void verify_hal_non_volatile_store ();
38extern "C"
39{
40#include "userial.h"
41}
42
43
44///////////////////////////////////////
45// private declaration, definition
46
47
48static nfc_stack_callback_t* gAndroidHalCallback = NULL;
49static nfc_stack_data_callback_t* gAndroidHalDataCallback = NULL;
50static SyncEvent gOpenCompletedEvent;
51static SyncEvent gPostInitCompletedEvent;
52static SyncEvent gCloseCompletedEvent;
53
54UINT32 ScrProtocolTraceFlag = SCR_PROTO_TRACE_ALL; //0x017F00;
55
56static void BroadcomHalCallback (UINT8 event, tHAL_NFC_STATUS status);
57static void BroadcomHalDataCallback (UINT16 data_len, UINT8* p_data);
58
59extern tNFC_HAL_CFG *p_nfc_hal_cfg;
60extern const UINT8  nfca_version_string [];
61extern const UINT8  nfa_version_string [];
62
63///////////////////////////////////////
64
65
66int HaiInitializeLibrary (const bcm2079x_dev_t* device)
67{
68    ALOGD ("%s: enter", __FUNCTION__);
69    ALOGE ("%s: ver=%s nfa=%s", __FUNCTION__, nfca_version_string, nfa_version_string);
70    int retval = EACCES;
71    unsigned long freq = 0;
72    unsigned long num = 0;
73    char temp[120];
74    UINT8 logLevel = 0;
75
76    logLevel = InitializeGlobalAppLogLevel ();
77
78    verify_hal_non_volatile_store ();
79    if ( GetNumValue ( NAME_PRESERVE_STORAGE, (char*)&num, sizeof ( num ) ) &&
80            (num == 1) )
81        ALOGD ("%s: preserve HAL NV store", __FUNCTION__);
82    else
83    {
84        delete_hal_non_volatile_store (false);
85    }
86
87    // Initialize protocol logging level
88    if ( GetNumValue ( NAME_PROTOCOL_TRACE_LEVEL, &num, sizeof ( num ) ) )
89        ScrProtocolTraceFlag = num;
90
91    tUSERIAL_OPEN_CFG cfg;
92    struct tUART_CONFIG  uart;
93
94    if ( GetStrValue ( NAME_UART_PARITY, temp, sizeof ( temp ) ) )
95    {
96        if ( strcmp ( temp, "even" ) == 0 )
97            uart.m_iParity = USERIAL_PARITY_EVEN;
98        else if ( strcmp ( temp, "odd" ) == 0 )
99            uart.m_iParity = USERIAL_PARITY_ODD;
100        else if ( strcmp ( temp, "none" ) == 0 )
101            uart.m_iParity = USERIAL_PARITY_NONE;
102    }
103    else
104        uart.m_iParity = USERIAL_PARITY_NONE;
105
106    if ( GetStrValue ( NAME_UART_STOPBITS, temp, sizeof ( temp ) ) )
107    {
108        if ( strcmp ( temp, "1" ) == 0 )
109            uart.m_iStopbits = USERIAL_STOPBITS_1;
110        else if ( strcmp ( temp, "2" ) == 0 )
111            uart.m_iStopbits = USERIAL_STOPBITS_2;
112        else if ( strcmp ( temp, "1.5" ) == 0 )
113            uart.m_iStopbits = USERIAL_STOPBITS_1_5;
114    }
115    else if ( GetNumValue ( NAME_UART_STOPBITS, &num, sizeof ( num ) ) )
116    {
117        if ( num == 1 )
118            uart.m_iStopbits = USERIAL_STOPBITS_1;
119        else if ( num == 2 )
120            uart.m_iStopbits = USERIAL_STOPBITS_2;
121    }
122    else
123        uart.m_iStopbits = USERIAL_STOPBITS_1;
124
125    if ( GetNumValue ( NAME_UART_DATABITS, &num, sizeof ( num ) ) )
126    {
127        if ( 5 <= num && num <= 8 )
128            uart.m_iDatabits = ( 1 << ( num + 1 ) );
129    }
130    else
131        uart.m_iDatabits = USERIAL_DATABITS_8;
132
133    if ( GetNumValue ( NAME_UART_BAUD, &num, sizeof ( num ) ) )
134    {
135        if ( num == 300 ) uart.m_iBaudrate = USERIAL_BAUD_300;
136        else if ( num == 600 ) uart.m_iBaudrate = USERIAL_BAUD_600;
137        else if ( num == 1200 ) uart.m_iBaudrate = USERIAL_BAUD_1200;
138        else if ( num == 2400 ) uart.m_iBaudrate = USERIAL_BAUD_2400;
139        else if ( num == 9600 ) uart.m_iBaudrate = USERIAL_BAUD_9600;
140        else if ( num == 19200 ) uart.m_iBaudrate = USERIAL_BAUD_19200;
141        else if ( num == 57600 ) uart.m_iBaudrate = USERIAL_BAUD_57600;
142        else if ( num == 115200 ) uart.m_iBaudrate = USERIAL_BAUD_115200;
143        else if ( num == 230400 ) uart.m_iBaudrate = USERIAL_BAUD_230400;
144        else if ( num == 460800 ) uart.m_iBaudrate = USERIAL_BAUD_460800;
145        else if ( num == 921600 ) uart.m_iBaudrate = USERIAL_BAUD_921600;
146    }
147    else if ( GetStrValue ( NAME_UART_BAUD, temp, sizeof ( temp ) ) )
148    {
149        if ( strcmp ( temp, "auto" ) == 0 )
150            uart.m_iBaudrate = USERIAL_BAUD_AUTO;
151    }
152    else
153        uart.m_iBaudrate = USERIAL_BAUD_115200;
154
155    memset (&cfg, 0, sizeof(tUSERIAL_OPEN_CFG));
156    cfg.fmt = uart.m_iDatabits | uart.m_iParity | uart.m_iStopbits;
157    cfg.baud = uart.m_iBaudrate;
158
159    ALOGD ("%s: uart config=0x%04x, %d\n", __func__, cfg.fmt, cfg.baud);
160    USERIAL_Init(&cfg);
161
162    if ( GetNumValue ( NAME_NFCC_ENABLE_TIMEOUT, &num, sizeof ( num ) ) )
163    {
164        p_nfc_hal_cfg->nfc_hal_nfcc_enable_timeout = num;
165    }
166
167    HAL_NfcInitialize ();
168    HAL_NfcSetTraceLevel (logLevel); // Initialize HAL's logging level
169
170    retval = 0;
171    ALOGD ("%s: exit %d", __FUNCTION__, retval);
172    return retval;
173}
174
175
176int HaiTerminateLibrary ()
177{
178    int retval = EACCES;
179    ALOGD ("%s: enter", __FUNCTION__);
180
181    HAL_NfcTerminate ();
182    gAndroidHalCallback = NULL;
183    gAndroidHalDataCallback = NULL;
184    GKI_shutdown ();
185    retval = 0;
186    ALOGD ("%s: exit %d", __FUNCTION__, retval);
187    return retval;
188}
189
190
191int HaiOpen (const bcm2079x_dev_t* device, nfc_stack_callback_t* halCallbackFunc, nfc_stack_data_callback_t* halDataCallbackFunc)
192{
193    ALOGD ("%s: enter", __FUNCTION__);
194    int retval = EACCES;
195
196    gAndroidHalCallback = halCallbackFunc;
197    gAndroidHalDataCallback = halDataCallbackFunc;
198
199    SyncEventGuard guard (gOpenCompletedEvent);
200    HAL_NfcOpen (BroadcomHalCallback, BroadcomHalDataCallback);
201    gOpenCompletedEvent.wait ();
202
203    retval = 0;
204    ALOGD ("%s: exit %d", __FUNCTION__, retval);
205    return retval;
206}
207
208
209void BroadcomHalCallback (UINT8 event, tHAL_NFC_STATUS status)
210{
211    ALOGD ("%s: enter; event=0x%X", __FUNCTION__, event);
212    switch (event)
213    {
214    case HAL_NFC_OPEN_CPLT_EVT:
215        {
216            ALOGD ("%s: HAL_NFC_OPEN_CPLT_EVT; status=0x%X", __FUNCTION__, status);
217            SyncEventGuard guard (gOpenCompletedEvent);
218            gOpenCompletedEvent.notifyOne ();
219            break;
220        }
221
222    case HAL_NFC_POST_INIT_CPLT_EVT:
223        {
224            ALOGD ("%s: HAL_NFC_POST_INIT_CPLT_EVT", __FUNCTION__);
225            SyncEventGuard guard (gPostInitCompletedEvent);
226            gPostInitCompletedEvent.notifyOne ();
227            break;
228        }
229
230    case HAL_NFC_CLOSE_CPLT_EVT:
231        {
232            ALOGD ("%s: HAL_NFC_CLOSE_CPLT_EVT", __FUNCTION__);
233            SyncEventGuard guard (gCloseCompletedEvent);
234            gCloseCompletedEvent.notifyOne ();
235            break;
236        }
237
238    case HAL_NFC_ERROR_EVT:
239        {
240            ALOGD ("%s: HAL_NFC_ERROR_EVT", __FUNCTION__);
241            {
242                SyncEventGuard guard (gOpenCompletedEvent);
243                gOpenCompletedEvent.notifyOne ();
244            }
245            {
246                SyncEventGuard guard (gPostInitCompletedEvent);
247                gPostInitCompletedEvent.notifyOne ();
248            }
249            {
250                SyncEventGuard guard (gCloseCompletedEvent);
251                gCloseCompletedEvent.notifyOne ();
252            }
253            break;
254        }
255    }
256    gAndroidHalCallback (event, status);
257    ALOGD ("%s: exit; event=0x%X", __FUNCTION__, event);
258}
259
260
261void BroadcomHalDataCallback (UINT16 data_len, UINT8* p_data)
262{
263    ALOGD ("%s: enter; len=%u", __FUNCTION__, data_len);
264    gAndroidHalDataCallback (data_len, p_data);
265}
266
267
268int HaiClose (const bcm2079x_dev_t* device)
269{
270    ALOGD ("%s: enter", __FUNCTION__);
271    int retval = EACCES;
272
273    SyncEventGuard guard (gCloseCompletedEvent);
274    HAL_NfcClose ();
275    gCloseCompletedEvent.wait ();
276    retval = 0;
277    ALOGD ("%s: exit %d", __FUNCTION__, retval);
278    return retval;
279}
280
281
282int HaiCoreInitialized (const bcm2079x_dev_t* device, uint8_t* coreInitResponseParams)
283{
284    ALOGD ("%s: enter", __FUNCTION__);
285    int retval = EACCES;
286
287    SyncEventGuard guard (gPostInitCompletedEvent);
288    HAL_NfcCoreInitialized (coreInitResponseParams);
289    gPostInitCompletedEvent.wait ();
290    retval = 0;
291    ALOGD ("%s: exit %d", __FUNCTION__, retval);
292    return retval;
293}
294
295
296int HaiWrite (const bcm2079x_dev_t* dev, uint16_t dataLen, const uint8_t* data)
297{
298    ALOGD ("%s: enter; len=%u", __FUNCTION__, dataLen);
299    int retval = EACCES;
300
301    HAL_NfcWrite (dataLen, const_cast<UINT8*> (data));
302    retval = 0;
303    ALOGD ("%s: exit %d", __FUNCTION__, retval);
304    return retval;
305}
306
307
308int HaiPreDiscover (const bcm2079x_dev_t* device)
309{
310    ALOGD ("%s: enter", __FUNCTION__);
311    int retval = EACCES;
312
313    retval = HAL_NfcPreDiscover () ? 1 : 0;
314    ALOGD ("%s: exit %d", __FUNCTION__, retval);
315    return retval;
316}
317
318
319int HaiControlGranted (const bcm2079x_dev_t* device)
320{
321    ALOGD ("%s: enter", __FUNCTION__);
322    int retval = EACCES;
323
324    HAL_NfcControlGranted ();
325    retval = 0;
326    ALOGD ("%s: exit %d", __FUNCTION__, retval);
327    return retval;
328}
329
330
331int HaiPowerCycle (const bcm2079x_dev_t* device)
332{
333    ALOGD ("%s: enter", __FUNCTION__);
334    int retval = EACCES;
335
336    HAL_NfcPowerCycle ();
337    retval = 0;
338    ALOGD ("%s: exit %d", __FUNCTION__, retval);
339    return retval;
340}
341
342