1 /******************************************************************************
2  *
3  *  Copyright 2007-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #ifndef UIPC_H
19 #define UIPC_H
20 
21 #include <sys/select.h>
22 
23 #include <memory>
24 #include <mutex>
25 
26 #include "stack/include/bt_hdr.h"
27 
28 #define UIPC_CH_ID_AV_CTRL 0
29 #define UIPC_CH_ID_AV_AUDIO 1
30 #define UIPC_CH_NUM 2
31 
32 #define UIPC_CH_ID_ALL 3 /* used to address all the ch id at once */
33 
34 #define DEFAULT_READ_POLL_TMO_MS 100
35 
36 typedef uint8_t tUIPC_CH_ID;
37 
38 /* Events generated */
39 typedef enum {
40   UIPC_OPEN_EVT = 0x0001,
41   UIPC_CLOSE_EVT = 0x0002,
42   UIPC_RX_DATA_EVT = 0x0004,
43   UIPC_RX_DATA_READY_EVT = 0x0008,
44   UIPC_TX_DATA_READY_EVT = 0x0010
45 } tUIPC_EVENT;
46 
47 /*
48  * UIPC IOCTL Requests
49  */
50 
51 #define UIPC_REQ_RX_FLUSH 1
52 #define UIPC_REG_REMOVE_ACTIVE_READSET 3
53 #define UIPC_SET_READ_POLL_TMO 4
54 
55 typedef void(tUIPC_RCV_CBACK)(tUIPC_CH_ID ch_id,
56                               tUIPC_EVENT event); /* points to BT_HDR which describes event type and
57                                                      length of data; len contains the number of
58                                                      bytes of entire message (sizeof(BT_HDR) +
59                                                      offset + size of data) */
60 
61 const char* dump_uipc_event(tUIPC_EVENT event);
62 
63 typedef struct {
64   int srvfd;
65   int fd;
66   int read_poll_tmo_ms;
67   int task_evt_flags; /* event flags pending to be processed in read task */
68   tUIPC_RCV_CBACK* cback;
69 } tUIPC_CHAN;
70 
71 struct tUIPC_STATE {
72   pthread_t tid; /* main thread id */
73   int running;
74   std::recursive_mutex mutex;
75 
76   fd_set active_set;
77   fd_set read_set;
78   int max_fd;
79   int signal_fds[2];
80 
81   tUIPC_CHAN ch[UIPC_CH_NUM];
82 };
83 
84 /**
85  * Initialize UIPC module
86  *
87  * @param user User ID who uses UIPC
88  */
89 std::unique_ptr<tUIPC_STATE> UIPC_Init();
90 
91 /**
92  * Open a UIPC channel
93  *
94  * @param ch_id Channel ID
95  * @param p_cback Callback handler
96  * @param socket_path Path to the socket
97  * @return true on success, otherwise false
98  */
99 bool UIPC_Open(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, tUIPC_RCV_CBACK* p_cback,
100                const char* socket_path);
101 
102 /**
103  * Closes a channel in UIPC or the entire UIPC module
104  *
105  * @param ch_id Channel ID; if ch_id is UIPC_CH_ID_ALL, then cleanup UIPC
106  */
107 void UIPC_Close(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id);
108 
109 /**
110  * Send a message over UIPC
111  *
112  * @param ch_id Channel ID
113  * @param msg_evt Message event type
114  * @param p_buf Buffer for the message
115  * @param msglen Message length
116  * @return true on success, otherwise false
117  */
118 bool UIPC_Send(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint16_t msg_evt, const uint8_t* p_buf,
119                uint16_t msglen);
120 
121 /**
122  * Read a message from UIPC
123  *
124  * @param ch_id Channel ID
125  * @param p_msg_evt Message event type
126  * @param p_buf Buffer for the message
127  * @param len Bytes to read
128  * @return true on success, otherwise false
129  */
130 uint32_t UIPC_Read(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint8_t* p_buf, uint32_t len);
131 
132 /**
133  * Control the UIPC parameter
134  *
135  * @param ch_id Channel ID
136  * @param request Request type
137  * @param param Optional parameters
138  * @return true on success, otherwise false
139  */
140 bool UIPC_Ioctl(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint32_t request, void* param);
141 
142 #endif /* UIPC_H */
143