NfcAdaptation.cpp revision 45faad0ff5deeb0c676356345d99398cc4ab695a
1/****************************************************************************
2**
3**  Name:       patchram.c
4**
5**  Function:   this file contains functions related to NfcAdaptation
6**
7**
8**  Copyright (c) 1999-2011, Broadcom Corp., All Rights Reserved.
9**  Broadcom Bluetooth Core. Proprietary and confidential.
10**
11*****************************************************************************/
12#include "NfcAdaptation.h"
13extern "C"
14{
15    #include "gki.h"
16    #include "nfa_api.h"
17    #include "nci_int.h"
18    #include "nfc_int.h"
19    #include <cutils/log.h>
20    #include "userial.h"
21}
22#include "config.h"
23
24#define LOG_TAG "NfcAdaptation"
25tUSERIAL_OPEN_CFG cfg;
26
27extern const UINT8 nfca_version_string[];
28
29extern "C" void GKI_shutdown();
30extern void resetConfig();
31
32NfcAdaptation* NfcAdaptation::mpInstance = NULL;
33ThreadMutex NfcAdaptation::sLock;
34
35/*******************************************************************************
36**
37** Function:    NfcAdaptation::NfcAdaptation()
38**
39** Description: class constructor
40**
41** Returns:     none
42**
43*******************************************************************************/
44NfcAdaptation::NfcAdaptation()
45{
46}
47
48/*******************************************************************************
49**
50** Function:    NfcAdaptation::~NfcAdaptation()
51**
52** Description: class destructor
53**
54** Returns:     none
55**
56*******************************************************************************/
57NfcAdaptation::~NfcAdaptation()
58{
59    mpInstance = NULL;
60}
61
62/*******************************************************************************
63**
64** Function:    NfcAdaptation::GetInstance()
65**
66** Description: access class singleton
67**
68** Returns:     pointer to the singleton object
69**
70*******************************************************************************/
71NfcAdaptation& NfcAdaptation::GetInstance()
72{
73    AutoThreadMutex  a(sLock);
74
75    if (!mpInstance)
76        mpInstance = new NfcAdaptation;
77    return *mpInstance;
78}
79
80/*******************************************************************************
81**
82** Function:    NfcAdaptation::Initialize()
83**
84** Description: class initializer
85**
86** Returns:     none
87**
88*******************************************************************************/
89void NfcAdaptation::Initialize ()
90{
91    const char* func = "NfcAdaptation::Initialize";
92    ALOGD("%s: enter\n", func);
93
94    readDefaultConfig();
95
96    GKI_init ();
97    GKI_enable ();
98    GKI_create_task ((TASKPTR)NFCA_TASK, BTU_TASK, (INT8*)"NFCA_TASK", 0, 0, (pthread_cond_t*)NULL, NULL);
99    {
100        AutoThreadMutex guard(mCondVar);
101        GKI_create_task ((TASKPTR)Thread, MMI_TASK, (INT8*)"NFCA_THREAD", 0, 0, (pthread_cond_t*)NULL, NULL);
102        mCondVar.wait();
103    }
104    ALOGD ("%s: exit", func);
105}
106
107/*******************************************************************************
108**
109** Function:    NfcAdaptation::Finalize()
110**
111** Description: class finalizer
112**
113** Returns:     none
114**
115*******************************************************************************/
116void NfcAdaptation::Finalize()
117{
118    AutoThreadMutex  a(sLock);
119
120    ALOGD ("%s: enter", __func__);
121    GKI_shutdown ();
122
123    resetConfig();
124    ALOGD ("%s: exit", __func__);
125    delete this;
126}
127
128/*******************************************************************************
129**
130** Function:    NfcAdaptation::signal()
131**
132** Description: signal the CondVar to release the thread that is waiting
133**
134** Returns:     none
135**
136*******************************************************************************/
137void NfcAdaptation::signal ()
138{
139    mCondVar.signal();
140}
141
142/*******************************************************************************
143**
144** Function:    NfcAdaptation::NFCA_TASK()
145**
146** Description: NFCA_TASK runs the GKI main task
147**
148** Returns:     none
149**
150*******************************************************************************/
151UINT32 NfcAdaptation::NFCA_TASK (UINT32 arg)
152{
153    ALOGD ("%s: enter", __func__);
154    GKI_run (0);
155    ALOGD ("%s: exit", __func__);
156    return NULL;
157}
158
159/*******************************************************************************
160**
161** Function:    NfcAdaptation::Thread()
162**
163** Description: Creates work threads
164**
165** Returns:     none
166**
167*******************************************************************************/
168UINT32 NfcAdaptation::Thread (UINT32 arg)
169{
170    ALOGD ("%s: enter", __func__);
171
172    memset (&cfg, 0, sizeof(tUSERIAL_OPEN_CFG));
173    cfg.fmt = uartConfig.m_iDatabits | uartConfig.m_iParity | uartConfig.m_iStopbits;
174    cfg.baud = uartConfig.m_iBaudrate;
175
176    ALOGD ("%s: uart config=0x%04x, %d\n", __func__, cfg.fmt, cfg.baud);
177    USERIAL_Init(&cfg);
178    {
179        ThreadCondVar    CondVar;
180        AutoThreadMutex  guard(CondVar);
181        GKI_create_task ((TASKPTR)nci_task, NCI_TASK, (INT8*)"NCI_TASK", 0, 0, (pthread_cond_t*)CondVar, (pthread_mutex_t*)CondVar);
182        CondVar.wait();
183    }
184    {
185        ThreadCondVar    CondVar;
186        AutoThreadMutex  guard(CondVar);
187        GKI_create_task ((TASKPTR)nfc_task, NFC_TASK, (INT8*)"NFC_TASK", 0, 0, (pthread_cond_t*)CondVar, (pthread_mutex_t*)CondVar);
188        CondVar.wait();
189    }
190
191    NfcAdaptation::GetInstance().signal();
192
193    ALOGD ("%s: exit", __func__);
194    return NULL;
195}
196
197/*******************************************************************************
198**
199** Function:    ThreadMutex::ThreadMutex()
200**
201** Description: class constructor
202**
203** Returns:     none
204**
205*******************************************************************************/
206ThreadMutex::ThreadMutex()
207{
208    pthread_mutexattr_t mutexAttr;
209
210    pthread_mutexattr_init(&mutexAttr);
211    pthread_mutex_init(&mMutex, &mutexAttr);
212    pthread_mutexattr_destroy(&mutexAttr);
213}
214
215/*******************************************************************************
216**
217** Function:    ThreadMutex::~ThreadMutex()
218**
219** Description: class destructor
220**
221** Returns:     none
222**
223*******************************************************************************/
224ThreadMutex::~ThreadMutex()
225{
226    pthread_mutex_destroy(&mMutex);
227}
228
229/*******************************************************************************
230**
231** Function:    ThreadMutex::lock()
232**
233** Description: lock kthe mutex
234**
235** Returns:     none
236**
237*******************************************************************************/
238void ThreadMutex::lock()
239{
240    pthread_mutex_lock(&mMutex);
241}
242
243/*******************************************************************************
244**
245** Function:    ThreadMutex::unblock()
246**
247** Description: unlock the mutex
248**
249** Returns:     none
250**
251*******************************************************************************/
252void ThreadMutex::unlock()
253{
254    pthread_mutex_unlock(&mMutex);
255}
256
257/*******************************************************************************
258**
259** Function:    ThreadCondVar::ThreadCondVar()
260**
261** Description: class constructor
262**
263** Returns:     none
264**
265*******************************************************************************/
266ThreadCondVar::ThreadCondVar()
267{
268    pthread_condattr_t CondAttr;
269
270    pthread_condattr_init(&CondAttr);
271    pthread_cond_init(&mCondVar, &CondAttr);
272
273    pthread_condattr_destroy(&CondAttr);
274}
275
276/*******************************************************************************
277**
278** Function:    ThreadCondVar::~ThreadCondVar()
279**
280** Description: class destructor
281**
282** Returns:     none
283**
284*******************************************************************************/
285ThreadCondVar::~ThreadCondVar()
286{
287    pthread_cond_destroy(&mCondVar);
288}
289
290/*******************************************************************************
291**
292** Function:    ThreadCondVar::wait()
293**
294** Description: wait on the mCondVar
295**
296** Returns:     none
297**
298*******************************************************************************/
299void ThreadCondVar::wait()
300{
301    pthread_cond_wait(&mCondVar, *this);
302    pthread_mutex_unlock(*this);
303}
304
305/*******************************************************************************
306**
307** Function:    ThreadCondVar::signal()
308**
309** Description: signal the mCondVar
310**
311** Returns:     none
312**
313*******************************************************************************/
314void ThreadCondVar::signal()
315{
316    AutoThreadMutex  a(*this);
317    pthread_cond_signal(&mCondVar);
318}
319
320/*******************************************************************************
321**
322** Function:    AutoThreadMutex::AutoThreadMutex()
323**
324** Description: class constructor, automatically lock the mutex
325**
326** Returns:     none
327**
328*******************************************************************************/
329AutoThreadMutex::AutoThreadMutex(ThreadMutex &m)
330    : mm(m)
331{
332    mm.lock();
333}
334
335/*******************************************************************************
336**
337** Function:    AutoThreadMutex::~AutoThreadMutex()
338**
339** Description: class destructor, automatically unlock the mutex
340**
341** Returns:     none
342**
343*******************************************************************************/
344AutoThreadMutex::~AutoThreadMutex()
345{
346    mm.unlock();
347}
348