• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // TODO: 1) use UciHalSemaphore, 2) use std::binary_semaphore
51 /* Semaphore handling structure */
52 typedef struct phNxpUciHal_Sem {
53   sem_t sem;
54   tHAL_UWB_STATUS status;
55 } phNxpUciHal_Sem_t;
56 
57 /* Semaphore helper macros */
SEM_WAIT(phNxpUciHal_Sem_t * pCallbackData)58 static inline int SEM_WAIT(phNxpUciHal_Sem_t* pCallbackData)
59 {
60   return sem_wait(&pCallbackData->sem);
61 }
62 
SEM_POST(phNxpUciHal_Sem_t * pCallbackData)63 static inline int SEM_POST(phNxpUciHal_Sem_t* pCallbackData)
64 {
65   return sem_post(&pCallbackData->sem);
66 }
67 
68 /************************ Exposed functions ***********************************/
69 /* NXP UCI HAL utility functions */
70 bool phNxpUciHal_init_monitor(void);
71 void phNxpUciHal_cleanup_monitor(void);
72 
73 tHAL_UWB_STATUS phNxpUciHal_init_cb_data(phNxpUciHal_Sem_t* pCallbackData);
74 
75 int phNxpUciHal_sem_timed_wait_msec(phNxpUciHal_Sem_t* pCallbackData, long msec);
76 
phNxpUciHal_sem_timed_wait_sec(phNxpUciHal_Sem_t * pCallbackData,time_t sec)77 static inline int phNxpUciHal_sem_timed_wait_sec(phNxpUciHal_Sem_t* pCallbackData, time_t sec)
78 {
79   return phNxpUciHal_sem_timed_wait_msec(pCallbackData, sec * 1000L);
80 }
81 
phNxpUciHal_sem_timed_wait(phNxpUciHal_Sem_t * pCallbackData)82 static inline int phNxpUciHal_sem_timed_wait(phNxpUciHal_Sem_t* pCallbackData)
83 {
84   /* default 1 second timeout*/
85   return phNxpUciHal_sem_timed_wait_msec(pCallbackData, 1000L);
86 }
87 
88 void phNxpUciHal_cleanup_cb_data(phNxpUciHal_Sem_t* pCallbackData);
89 
90 // helper class for Semaphore
91 // phNxpUciHal_init_cb_data(), phNxpUciHal_cleanup_cb_data(),
92 // SEM_WAIT(), SEM_POST()
93 class UciHalSemaphore {
94 public:
UciHalSemaphore()95   UciHalSemaphore() {
96     phNxpUciHal_init_cb_data(&sem);
97   }
~UciHalSemaphore()98   virtual ~UciHalSemaphore() {
99     phNxpUciHal_cleanup_cb_data(&sem);
100   }
wait()101   int wait() {
102     return sem_wait(&sem.sem);
103   }
wait_timeout_msec(long msec)104   int wait_timeout_msec(long msec) {
105     return phNxpUciHal_sem_timed_wait_msec(&sem, msec);
106   }
post()107   int post() {
108     sem.status = UWBSTATUS_SUCCESS;
109     return sem_post(&sem.sem);
110   }
post(tHAL_UWB_STATUS status)111   int post(tHAL_UWB_STATUS status) {
112     sem.status = status;
113     return sem_post(&sem.sem);
114   }
getStatus()115   tHAL_UWB_STATUS getStatus() {
116     return sem.status;
117   }
118 private:
119   phNxpUciHal_Sem_t sem;
120 };
121 
122 /*
123  * Print an UWB Packet.
124  *
125  * @param what The type and direction of packet
126  *
127  * @param p_data The packet to be printed/logged.
128  *
129  * @param len Tenth of the packet.
130  *
131  */
132 
133 void phNxpUciHal_print_packet(enum phNxpUciHal_Pkt_Type what, const uint8_t* p_data,
134                               uint16_t len);
135 void phNxpUciHal_emergency_recovery(void);
136 double phNxpUciHal_byteArrayToDouble(const uint8_t* p_data);
137 
138 template <typename T>
le_bytes_to_cpu(const uint8_t * p)139 static inline T le_bytes_to_cpu(const uint8_t *p)
140 {
141   static_assert(std::is_integral_v<T>, "bytes_to_cpu must be used with an integral type");
142   T val = 0;
143   if (std::endian::native == std::endian::little) {
144     std::memcpy(&val, p, sizeof(T));
145   } else {
146     size_t i = sizeof(T);
147     while (i--) {
148       val = (val << 8) | p[i];
149     }
150   }
151   return val;
152 }
153 
154 template <typename T>
cpu_to_le_bytes(uint8_t * p,const T num)155 static inline void cpu_to_le_bytes(uint8_t *p, const T num)
156 {
157   static_assert(std::is_integral_v<T>, "cpu_to_le_bytes must be used with an integral type");
158   T val = num;
159   if (std::endian::native == std::endian::little) {
160     std::memcpy(p, &val, sizeof(T));
161   } else {
162     for (size_t i = 0; i < sizeof(T); i++) {
163       p[i] = val & 0xff;
164       val = val >> 8;
165     }
166   }
167 }
168 
169 /* Lock unlock helper macros */
170 void REENTRANCE_LOCK();
171 void REENTRANCE_UNLOCK();
172 void CONCURRENCY_LOCK();
173 void CONCURRENCY_UNLOCK();
174 
175 // Decode bytes into map<key=T, val=LV>
176 std::map<uint16_t, std::vector<uint8_t>>
177 decodeTlvBytes(const std::vector<uint8_t> &ext_ids, const uint8_t *tlv_bytes, size_t tlv_len);
178 
179 // Encode map<key=T, val=LV> into TLV bytes
180 std::vector<uint8_t> encodeTlvBytes(const std::map<uint16_t, std::vector<uint8_t>> &tlvs);
181 
182 #endif /* _PHNXPUCIHAL_UTILS_H_ */
183