• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-2012 Broadcom Corporation
4  *  Copyright 2018-2019 NXP
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 #pragma once
20 #include <pthread.h>
21 #include <utils/RefBase.h>
22 
23 #include "config.h"
24 #include "hal_uwb.h"
25 #include "uwb_hal_api.h"
26 #include "uwb_hal_int.h"
27 #include "uwb_target.h"
28 
29 class ThreadMutex {
30  public:
31   ThreadMutex();
32   virtual ~ThreadMutex();
33   void lock();
34   void unlock();
35   operator pthread_mutex_t*() { return &mMutex; }
36 
37  private:
38   pthread_mutex_t mMutex;
39 };
40 
41 class ThreadCondVar : public ThreadMutex {
42  public:
43   ThreadCondVar();
44   virtual ~ThreadCondVar();
45   void signal();
46   void wait();
47   operator pthread_cond_t*() { return &mCondVar; }
48   operator pthread_mutex_t*() {
49     return ThreadMutex::operator pthread_mutex_t*();
50   }
51 
52  private:
53   pthread_cond_t mCondVar;
54 };
55 
56 class AutoThreadMutex {
57  public:
58   AutoThreadMutex(ThreadMutex& m);
59   virtual ~AutoThreadMutex();
60   operator ThreadMutex&() { return mm; }
61   operator pthread_mutex_t*() { return (pthread_mutex_t*)mm; }
62 
63  private:
64   ThreadMutex& mm;
65 };
66 
67 class UwbAdaptation {
68  public:
69   virtual ~UwbAdaptation();
70   void Initialize();
71   void Finalize(bool graceExit);
72   static UwbAdaptation& GetInstance();
73   tHAL_UWB_ENTRY* GetHalEntryFuncs();
74   static tUWB_STATUS CoreInitialization();
75   static tUWB_STATUS SessionInitialization(int sessionId);
76 
77  private:
78   UwbAdaptation();
79   void signal();
80   static UwbAdaptation* mpInstance;
81   static ThreadMutex sLock;
82   static ThreadMutex sIoctlLock;
83   ThreadCondVar mCondVar;
84   tHAL_UWB_ENTRY mHalEntryFuncs;  // function pointers for HAL entry points
85 
86   static tHAL_UWB_CBACK* mHalCallback;
87   static tHAL_UWB_DATA_CBACK* mHalDataCallback;
88 
89   static uint32_t UWBA_TASK(uint32_t arg);
90   static uint32_t Thread(uint32_t arg);
91   void InitializeHalDeviceContext();
92   static void HalOpen(tHAL_UWB_CBACK* p_hal_cback,
93                       tHAL_UWB_DATA_CBACK* p_data_cback);
94   static void HalClose();
95   static void HalWrite(uint16_t data_len, uint8_t* p_data);
96 };
97