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 <mutex>
22 
23 #define UIPC_CH_ID_AV_CTRL 0
24 #define UIPC_CH_ID_AV_AUDIO 1
25 #define UIPC_CH_NUM 2
26 
27 #define UIPC_CH_ID_ALL 3 /* used to address all the ch id at once */
28 
29 #define DEFAULT_READ_POLL_TMO_MS 100
30 
31 typedef uint8_t tUIPC_CH_ID;
32 
33 /* Events generated */
34 typedef enum {
35   UIPC_OPEN_EVT = 0x0001,
36   UIPC_CLOSE_EVT = 0x0002,
37   UIPC_RX_DATA_EVT = 0x0004,
38   UIPC_RX_DATA_READY_EVT = 0x0008,
39   UIPC_TX_DATA_READY_EVT = 0x0010
40 } tUIPC_EVENT;
41 
42 /*
43  * UIPC IOCTL Requests
44  */
45 
46 #define UIPC_REQ_RX_FLUSH 1
47 #define UIPC_REG_CBACK 2
48 #define UIPC_REG_REMOVE_ACTIVE_READSET 3
49 #define UIPC_SET_READ_POLL_TMO 4
50 
51 typedef void(tUIPC_RCV_CBACK)(
52     tUIPC_CH_ID ch_id,
53     tUIPC_EVENT event); /* points to BT_HDR which describes event type and
54                            length of data; len contains the number of bytes of
55                            entire message (sizeof(BT_HDR) + offset + size of
56                            data) */
57 
58 const char* dump_uipc_event(tUIPC_EVENT event);
59 
60 typedef struct {
61   int srvfd;
62   int fd;
63   int read_poll_tmo_ms;
64   int task_evt_flags; /* event flags pending to be processed in read task */
65   tUIPC_RCV_CBACK* cback;
66 } tUIPC_CHAN;
67 
68 struct tUIPC_STATE {
69   pthread_t tid; /* main thread id */
70   int running;
71   std::recursive_mutex mutex;
72 
73   fd_set active_set;
74   fd_set read_set;
75   int max_fd;
76   int signal_fds[2];
77 
78   tUIPC_CHAN ch[UIPC_CH_NUM];
79 };
80 
81 /**
82  * Initialize UIPC module
83  *
84  * @param user User ID who uses UIPC
85  */
86 std::unique_ptr<tUIPC_STATE> UIPC_Init();
87 
88 /**
89  * Open a UIPC channel
90  *
91  * @param ch_id Channel ID
92  * @param p_cback Callback handler
93  * @param socket_path Path to the socket
94  * @return true on success, otherwise false
95  */
96 bool UIPC_Open(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, tUIPC_RCV_CBACK* p_cback,
97                const char* socket_path);
98 
99 /**
100  * Closes a channel in UIPC or the entire UIPC module
101  *
102  * @param ch_id Channel ID; if ch_id is UIPC_CH_ID_ALL, then cleanup UIPC
103  */
104 void UIPC_Close(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id);
105 
106 /**
107  * Send a message over UIPC
108  *
109  * @param ch_id Channel ID
110  * @param msg_evt Message event type
111  * @param p_buf Buffer for the message
112  * @param msglen Message length
113  * @return true on success, otherwise false
114  */
115 bool UIPC_Send(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint16_t msg_evt,
116                const uint8_t* p_buf, uint16_t msglen);
117 
118 /**
119  * Read a message from UIPC
120  *
121  * @param ch_id Channel ID
122  * @param p_msg_evt Message event type
123  * @param p_buf Buffer for the message
124  * @param len Bytes to read
125  * @return true on success, otherwise false
126  */
127 uint32_t UIPC_Read(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint16_t* p_msg_evt,
128                    uint8_t* p_buf, uint32_t len);
129 
130 /**
131  * Control the UIPC parameter
132  *
133  * @param ch_id Channel ID
134  * @param request Request type
135  * @param param Optional parameters
136  * @return true on success, otherwise false
137  */
138 bool UIPC_Ioctl(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint32_t request,
139                 void* param);
140 
141 #endif /* UIPC_H */
142