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