• 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 PHTMLUWB_H
18 #define PHTMLUWB_H
19 
20 #include <memory>
21 
22 #include <phUwbCommon.h>
23 #include <phMessageQueue.h>
24 
25 /*
26  * Message posted by Reader thread uponl
27  * completion of requested operation
28  */
29 #define PH_TMLUWB_READ_MESSAGE (0xAA)
30 
31 /*
32  * Message posted by Writer thread upon
33  * completion of requested operation
34  */
35 #define PH_TMLUWB_WRITE_MESSAGE (0x55)
36 
37 /*
38  * Value indicates to reset device
39  */
40 #define PH_TMLUWB_RESETDEVICE (0x00008001)
41 
42 /*
43 ***************************Globals,Structure and Enumeration ******************
44 */
45 
46 /*
47  * Transaction (Tx/Rx) completion information structure of TML
48  *
49  * This structure holds the completion callback information of the
50  * transaction passed from the TML layer to the Upper layer
51  * along with the completion callback.
52  *
53  * The value of field wStatus can be interpreted as:
54  *
55  *     - UWBSTATUS_SUCCESS                    Transaction performed
56  * successfully.
57  *     - UWBSTATUS_FAILED                     Failed to wait on Read/Write
58  * operation.
59  *     - UWBSTATUS_INSUFFICIENT_STORAGE       Not enough memory to store data in
60  * case of read.
61  *     - UWBSTATUS_BOARD_COMMUNICATION_ERROR  Failure to Read/Write from the
62  * file or timeout.
63  */
64 
65 typedef struct phTmlUwb_TransactInfo {
66   tHAL_UWB_STATUS wStatus;       /* Status of the Transaction Completion*/
67   uint8_t* pBuff;          /* Response Data of the Transaction*/
68   uint16_t wLength;        /* Data size of the Transaction*/
69 } phTmlUwb_TransactInfo_t; /* Instance of Transaction structure */
70 
71 /*
72  * TML transreceive completion callback to Upper Layer
73  *
74  * pContext - Context provided by upper layer
75  * pInfo    - Transaction info. See phTmlUwb_TransactInfo
76  */
77 typedef void (*pphTmlUwb_TransactCompletionCb_t)(
78     void* pContext, phTmlUwb_TransactInfo_t* pInfo);
79 
80 /*
81  * Structure containing details related to read and write operations
82  *
83  */
84 typedef struct phTmlUwb_ReadWriteInfo {
85   volatile bool bThreadShouldStop;
86   volatile bool bThreadRunning;
87   uint8_t
88       bThreadBusy; /*Flag to indicate thread is busy on respective operation */
89   /* Transaction completion Callback function */
90   pphTmlUwb_TransactCompletionCb_t pThread_Callback;
91   void* pContext;        /*Context passed while invocation of operation */
92   uint8_t* pBuffer;      /*Buffer passed while invocation of operation */
93   uint16_t wLength;      /*Length of data read/written */
94   tHAL_UWB_STATUS wWorkStatus; /*Status of the transaction performed */
95 } phTmlUwb_ReadWriteInfo_t;
96 
97 /*
98  *Base Context Structure containing members required for entire session
99  */
100 typedef struct phTmlUwb_Context {
101   pthread_t readerThread; /*Handle to the thread which handles write and read
102                              operations */
103   pthread_t writerThread;
104 
105   phTmlUwb_ReadWriteInfo_t tReadInfo;  /*Pointer to Reader Thread Structure */
106   phTmlUwb_ReadWriteInfo_t tWriteInfo; /*Pointer to Writer Thread Structure */
107   void* pDevHandle;                    /* Pointer to Device Handle */
108   std::shared_ptr<MessageQueue<phLibUwb_Message>> pClientMq; /* Pointer to Client thread message queue */
109   sem_t rxSemaphore;
110   sem_t txSemaphore;      /* Lock/Acquire txRx Semaphore */
111 
112   pthread_cond_t wait_busy_condition; /*Condition to wait reader thread*/
113   pthread_mutex_t wait_busy_lock;     /*Condition lock to wait reader thread*/
114   volatile uint8_t wait_busy_flag;    /*Condition flag to wait reader thread*/
115   volatile uint8_t gWriterCbflag;    /* flag to indicate write callback message is pushed to
116                            queue*/
117 } phTmlUwb_Context_t;
118 
119 /*
120  * Enum definition contains  supported ioctl control codes.
121  *
122  * phTmlUwb_Spi_IoCtl
123  */
124 enum class phTmlUwb_ControlCode_t {
125   Invalid = 0,
126   SetPower,
127   EnableFwdMode,
128   EnableThroughPut,
129   EseReset,
130 };
131 
132 /* Function declarations */
133 tHAL_UWB_STATUS phTmlUwb_Init(const char* pDevName, std::shared_ptr<MessageQueue<phLibUwb_Message>> pClientMq);
134 tHAL_UWB_STATUS phTmlUwb_Shutdown(void);
135 void phTmlUwb_Suspend(void);
136 void phTmlUwb_Resume(void);
137 
138 // Writer: caller should call this for every write io
139 tHAL_UWB_STATUS phTmlUwb_Write(uint8_t* pBuffer, uint16_t wLength,
140                          pphTmlUwb_TransactCompletionCb_t pTmlWriteComplete,
141                          void* pContext);
142 
143 // Reader: caller calls this once, callback will be called for every received packet.
144 //         and call StopRead() to unscribe RX packet.
145 tHAL_UWB_STATUS phTmlUwb_StartRead(uint8_t* pBuffer, uint16_t wLength,
146                         pphTmlUwb_TransactCompletionCb_t pTmlReadComplete,
147                         void* pContext);
148 void phTmlUwb_StopRead();
149 
150 void phTmlUwb_Chip_Reset(void);
151 void phTmlUwb_DeferredCall(std::shared_ptr<phLibUwb_Message> msg);
152 #endif /*  PHTMLUWB_H  */
153