1/******************************************************************************
2 *
3 *  Copyright (C) 2011-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#pragma once
19#include <pthread.h>
20
21#include "nfc_hal_api.h"
22#include "nfc_target.h"
23
24#include <utils/RefBase.h>
25
26namespace android {
27namespace hardware {
28namespace nfc {
29namespace V1_0 {
30struct INfc;
31struct INfcClientCallback;
32}
33}
34}
35}
36
37class ThreadMutex {
38 public:
39  ThreadMutex();
40  virtual ~ThreadMutex();
41  void lock();
42  void unlock();
43  operator pthread_mutex_t*() { return &mMutex; }
44
45 private:
46  pthread_mutex_t mMutex;
47};
48
49class ThreadCondVar : public ThreadMutex {
50 public:
51  ThreadCondVar();
52  virtual ~ThreadCondVar();
53  void signal();
54  void wait();
55  operator pthread_cond_t*() { return &mCondVar; }
56  operator pthread_mutex_t*() {
57    return ThreadMutex::operator pthread_mutex_t*();
58  }
59
60 private:
61  pthread_cond_t mCondVar;
62};
63
64class AutoThreadMutex {
65 public:
66  AutoThreadMutex(ThreadMutex& m);
67  virtual ~AutoThreadMutex();
68  operator ThreadMutex&() { return mm; }
69  operator pthread_mutex_t*() { return (pthread_mutex_t*)mm; }
70
71 private:
72  ThreadMutex& mm;
73};
74
75class NfcAdaptation {
76 public:
77  virtual ~NfcAdaptation();
78  void Initialize();
79  void Finalize();
80  static NfcAdaptation& GetInstance();
81  tHAL_NFC_ENTRY* GetHalEntryFuncs();
82  void DownloadFirmware();
83  void Dump(int fd);
84
85 private:
86  NfcAdaptation();
87  void signal();
88  static NfcAdaptation* mpInstance;
89  static ThreadMutex sLock;
90  ThreadCondVar mCondVar;
91  tHAL_NFC_ENTRY mHalEntryFuncs;  // function pointers for HAL entry points
92  static nfc_nci_device_t* mHalDeviceContext;
93  static android::sp<android::hardware::nfc::V1_0::INfc> mHal;
94  static android::hardware::nfc::V1_0::INfcClientCallback* mCallback;
95  static tHAL_NFC_CBACK* mHalCallback;
96  static tHAL_NFC_DATA_CBACK* mHalDataCallback;
97  static ThreadCondVar mHalOpenCompletedEvent;
98  static ThreadCondVar mHalCloseCompletedEvent;
99
100  static uint32_t NFCA_TASK(uint32_t arg);
101  static uint32_t Thread(uint32_t arg);
102  void InitializeHalDeviceContext();
103  static void HalDeviceContextCallback(nfc_event_t event,
104                                       nfc_status_t event_status);
105  static void HalDeviceContextDataCallback(uint16_t data_len, uint8_t* p_data);
106
107  static void HalInitialize();
108  static void HalTerminate();
109  static void HalOpen(tHAL_NFC_CBACK* p_hal_cback,
110                      tHAL_NFC_DATA_CBACK* p_data_cback);
111  static void HalClose();
112  static void HalCoreInitialized(uint16_t data_len,
113                                 uint8_t* p_core_init_rsp_params);
114  static void HalWrite(uint16_t data_len, uint8_t* p_data);
115  static bool HalPrediscover();
116  static void HalControlGranted();
117  static void HalPowerCycle();
118  static uint8_t HalGetMaxNfcee();
119  static void HalDownloadFirmwareCallback(nfc_event_t event,
120                                          nfc_status_t event_status);
121  static void HalDownloadFirmwareDataCallback(uint16_t data_len,
122                                              uint8_t* p_data);
123};
124