1 /*
2  * Copyright 2012-2020, 2023 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 <pthread.h>
21 #include <semaphore.h>
22 #include <time.h>
23 #include <unordered_set>
24 
25 #include <assert.h>
26 #include <bit>
27 #include <cstring>
28 #include <map>
29 #include <memory>
30 #include <thread>
31 #include <type_traits>
32 #include <vector>
33 
34 
35 #include "phNxpLog.h"
36 #include "phUwbStatus.h"
37 
38 /********************* Definitions and structures *****************************/
39 /* Which is the direction of UWB Packet.
40  *
41  * Used by the @ref phNxpUciHal_print_packet API.
42  */
43 enum phNxpUciHal_Pkt_Type {
44   NXP_TML_UCI_CMD_AP_2_UWBS,
45   NXP_TML_UCI_RSP_NTF_UWBS_2_AP,
46   NXP_TML_FW_DNLD_CMD_AP_2_UWBS,
47   NXP_TML_FW_DNLD_RSP_UWBS_2_AP,
48 };
49 
50 
51 /* Semaphore handling structure */
52 typedef struct phNxpUciHal_Sem {
53   /* Semaphore used to wait for callback */
54   sem_t sem;
55 
56   /* Used to store the status sent by the callback */
57   tHAL_UWB_STATUS status;
58 
59   /* Used to provide a local context to the callback */
60   void* pContext;
61 
62 } phNxpUciHal_Sem_t;
63 
64 /* Semaphore helper macros */
SEM_WAIT(phNxpUciHal_Sem_t * pCallbackData)65 static inline int SEM_WAIT(phNxpUciHal_Sem_t* pCallbackData)
66 {
67   return sem_wait(&pCallbackData->sem);
68 }
69 
SEM_POST(phNxpUciHal_Sem_t * pCallbackData)70 static inline int SEM_POST(phNxpUciHal_Sem_t* pCallbackData)
71 {
72   return sem_post(&pCallbackData->sem);
73 }
74 
75 /************************ Exposed functions ***********************************/
76 /* NXP UCI HAL utility functions */
77 bool phNxpUciHal_init_monitor(void);
78 void phNxpUciHal_cleanup_monitor(void);
79 
80 tHAL_UWB_STATUS phNxpUciHal_init_cb_data(phNxpUciHal_Sem_t* pCallbackData,
81                                          void* pContext);
82 
83 int phNxpUciHal_sem_timed_wait_msec(phNxpUciHal_Sem_t* pCallbackData, long msec);
84 
phNxpUciHal_sem_timed_wait_sec(phNxpUciHal_Sem_t * pCallbackData,time_t sec)85 static inline int phNxpUciHal_sem_timed_wait_sec(phNxpUciHal_Sem_t* pCallbackData, time_t sec)
86 {
87   return phNxpUciHal_sem_timed_wait_msec(pCallbackData, sec * 1000L);
88 }
89 
phNxpUciHal_sem_timed_wait(phNxpUciHal_Sem_t * pCallbackData)90 static inline int phNxpUciHal_sem_timed_wait(phNxpUciHal_Sem_t* pCallbackData)
91 {
92   /* default 1 second timeout*/
93   return phNxpUciHal_sem_timed_wait_msec(pCallbackData, 1000L);
94 }
95 
96 void phNxpUciHal_cleanup_cb_data(phNxpUciHal_Sem_t* pCallbackData);
97 
98 // helper class for Semaphore
99 // phNxpUciHal_init_cb_data(), phNxpUciHal_cleanup_cb_data(),
100 // SEM_WAIT(), SEM_POST()
101 class UciHalSemaphore {
102 public:
UciHalSemaphore()103   UciHalSemaphore() {
104     phNxpUciHal_init_cb_data(&sem, NULL);
105   }
UciHalSemaphore(void * context)106   UciHalSemaphore(void *context) {
107     phNxpUciHal_init_cb_data(&sem, context);
108   }
~UciHalSemaphore()109   virtual ~UciHalSemaphore() {
110     phNxpUciHal_cleanup_cb_data(&sem);
111   }
wait()112   int wait() {
113     return sem_wait(&sem.sem);
114   }
wait_timeout_msec(long msec)115   int wait_timeout_msec(long msec) {
116     return phNxpUciHal_sem_timed_wait_msec(&sem, msec);
117   }
post()118   int post() {
119     return sem_post(&sem.sem);
120   }
post(tHAL_UWB_STATUS status)121   int post(tHAL_UWB_STATUS status) {
122     sem.status = status;
123     return sem_post(&sem.sem);
124   }
getStatus()125   tHAL_UWB_STATUS getStatus() {
126     return sem.status;
127   }
128 private:
129   phNxpUciHal_Sem_t sem;
130 };
131 
132 /*
133  * Print an UWB Packet.
134  *
135  * @param what The type and direction of packet
136  *
137  * @param p_data The packet to be printed/logged.
138  *
139  * @param len Tenth of the packet.
140  *
141  */
142 
143 void phNxpUciHal_print_packet(enum phNxpUciHal_Pkt_Type what, const uint8_t* p_data,
144                               uint16_t len);
145 void phNxpUciHal_emergency_recovery(void);
146 double phNxpUciHal_byteArrayToDouble(const uint8_t* p_data);
147 
148 template <typename T>
le_bytes_to_cpu(const uint8_t * p)149 static inline T le_bytes_to_cpu(const uint8_t *p)
150 {
151   static_assert(std::is_integral_v<T>, "bytes_to_cpu must be used with an integral type");
152   T val = 0;
153   if (std::endian::native == std::endian::little) {
154     std::memcpy(&val, p, sizeof(T));
155   } else {
156     size_t i = sizeof(T);
157     while (i--) {
158       val = (val << 8) | p[i];
159     }
160   }
161   return val;
162 }
163 
164 template <typename T>
cpu_to_le_bytes(uint8_t * p,const T num)165 static inline void cpu_to_le_bytes(uint8_t *p, const T num)
166 {
167   static_assert(std::is_integral_v<T>, "cpu_to_le_bytes must be used with an integral type");
168   T val = num;
169   if (std::endian::native == std::endian::little) {
170     std::memcpy(p, &val, sizeof(T));
171   } else {
172     for (size_t i = 0; i < sizeof(T); i++) {
173       p[i] = val & 0xff;
174       val = val >> 8;
175     }
176   }
177 }
178 
179 /* Lock unlock helper macros */
180 void REENTRANCE_LOCK();
181 void REENTRANCE_UNLOCK();
182 void CONCURRENCY_LOCK();
183 void CONCURRENCY_UNLOCK();
184 
185 // Decode bytes into map<key=T, val=LV>
186 std::map<uint16_t, std::vector<uint8_t>>
187 decodeTlvBytes(const std::vector<uint8_t> &ext_ids, const uint8_t *tlv_bytes, size_t tlv_len);
188 
189 // Encode map<key=T, val=LV> into TLV bytes
190 std::vector<uint8_t> encodeTlvBytes(const std::map<uint16_t, std::vector<uint8_t>> &tlvs);
191 
192 #endif /* _PHNXPUCIHAL_UTILS_H_ */
193