• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012-2020 NXP
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _PHNXPUCIHAL_UTILS_H_
18 #define _PHNXPUCIHAL_UTILS_H_
19 
20 #include <assert.h>
21 #include <phUwbStatus.h>
22 #include <pthread.h>
23 #include <semaphore.h>
24 
25 /********************* Definitions and structures *****************************/
26 
27 /* List structures */
28 struct listNode {
29   void* pData;
30   struct listNode* pNext;
31 };
32 
33 struct listHead {
34   struct listNode* pFirst;
35   pthread_mutex_t mutex;
36 };
37 
38 /* Semaphore handling structure */
39 typedef struct phNxpUciHal_Sem {
40   /* Semaphore used to wait for callback */
41   sem_t sem;
42 
43   /* Used to store the status sent by the callback */
44   tHAL_UWB_STATUS status;
45 
46   /* Used to provide a local context to the callback */
47   void* pContext;
48 
49 } phNxpUciHal_Sem_t;
50 
51 /* Semaphore helper macros */
52 #define SEM_WAIT(cb_data) sem_wait(&((cb_data).sem))
53 #define SEM_POST(p_cb_data) sem_post(&((p_cb_data)->sem))
54 
55 /* Semaphore and mutex monitor */
56 typedef struct phNxpUciHal_Monitor {
57   /* Mutex protecting native library against reentrance */
58   pthread_mutex_t reentrance_mutex;
59 
60   /* Mutex protecting native library against concurrency */
61   pthread_mutex_t concurrency_mutex;
62 
63   /* List used to track pending semaphores waiting for callback */
64   struct listHead sem_list;
65 
66 } phNxpUciHal_Monitor_t;
67 
68 /************************ Exposed functions ***********************************/
69 /* List functions */
70 int listInit(struct listHead* pList);
71 int listDestroy(struct listHead* pList);
72 int listAdd(struct listHead* pList, void* pData);
73 int listRemove(struct listHead* pList, void* pData);
74 int listGetAndRemoveNext(struct listHead* pList, void** ppData);
75 void listDump(struct listHead* pList);
76 
77 /* NXP UCI HAL utility functions */
78 phNxpUciHal_Monitor_t* phNxpUciHal_init_monitor(void);
79 void phNxpUciHal_cleanup_monitor(void);
80 phNxpUciHal_Monitor_t* phNxpUciHal_get_monitor(void);
81 tHAL_UWB_STATUS phNxpUciHal_init_cb_data(phNxpUciHal_Sem_t* pCallbackData,
82                                    void* pContext);
83 void phNxpUciHal_sem_timed_wait(phNxpUciHal_Sem_t* pCallbackData);
84 void phNxpUciHal_cleanup_cb_data(phNxpUciHal_Sem_t* pCallbackData);
85 void phNxpUciHal_releaseall_cb_data(void);
86 void phNxpUciHal_print_packet(const char* pString, const uint8_t* p_data,
87                               uint16_t len);
88 void phNxpUciHal_emergency_recovery(void);
89 double phNxpUciHal_byteArrayToDouble(const uint8_t* p_data);
90 
91 /* Lock unlock helper macros */
92 /* Lock unlock helper macros */
93 #define REENTRANCE_LOCK()        \
94   if (phNxpUciHal_get_monitor()) \
95   pthread_mutex_lock(&phNxpUciHal_get_monitor()->reentrance_mutex)
96 #define REENTRANCE_UNLOCK()      \
97   if (phNxpUciHal_get_monitor()) \
98   pthread_mutex_unlock(&phNxpUciHal_get_monitor()->reentrance_mutex)
99 #define CONCURRENCY_LOCK()       \
100   if (phNxpUciHal_get_monitor()) \
101   pthread_mutex_lock(&phNxpUciHal_get_monitor()->concurrency_mutex)
102 #define CONCURRENCY_UNLOCK()     \
103   if (phNxpUciHal_get_monitor()) \
104   pthread_mutex_unlock(&phNxpUciHal_get_monitor()->concurrency_mutex)
105 #define STREAM_TO_UINT8(u8, p) \
106   {                            \
107     u8 = (uint8_t)(*(p));      \
108     (p) += 1;                  \
109   }
110 
111 #define BE_STREAM_TO_UINT32(u32, p)                                    \
112   {                                                                    \
113     u32 = ((uint32_t)(*((p) + 3)) + ((uint32_t)(*((p) + 2)) << 8) +    \
114            ((uint32_t)(*((p) + 1)) << 16) + ((uint32_t)(*(p)) << 24)); \
115     (p) += 4;                                                          \
116   }
117 
118 #endif /* _PHNXPUCIHAL_UTILS_H_ */
119