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)( 56 tUIPC_CH_ID ch_id, 57 tUIPC_EVENT event); /* points to BT_HDR which describes event type and 58 length of data; len contains the number of bytes of 59 entire message (sizeof(BT_HDR) + offset + size of 60 data) */ 61 62 const char* dump_uipc_event(tUIPC_EVENT event); 63 64 typedef struct { 65 int srvfd; 66 int fd; 67 int read_poll_tmo_ms; 68 int task_evt_flags; /* event flags pending to be processed in read task */ 69 tUIPC_RCV_CBACK* cback; 70 } tUIPC_CHAN; 71 72 struct tUIPC_STATE { 73 pthread_t tid; /* main thread id */ 74 int running; 75 std::recursive_mutex mutex; 76 77 fd_set active_set; 78 fd_set read_set; 79 int max_fd; 80 int signal_fds[2]; 81 82 tUIPC_CHAN ch[UIPC_CH_NUM]; 83 }; 84 85 /** 86 * Initialize UIPC module 87 * 88 * @param user User ID who uses UIPC 89 */ 90 std::unique_ptr<tUIPC_STATE> UIPC_Init(); 91 92 /** 93 * Open a UIPC channel 94 * 95 * @param ch_id Channel ID 96 * @param p_cback Callback handler 97 * @param socket_path Path to the socket 98 * @return true on success, otherwise false 99 */ 100 bool UIPC_Open(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, tUIPC_RCV_CBACK* p_cback, 101 const char* socket_path); 102 103 /** 104 * Closes a channel in UIPC or the entire UIPC module 105 * 106 * @param ch_id Channel ID; if ch_id is UIPC_CH_ID_ALL, then cleanup UIPC 107 */ 108 void UIPC_Close(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id); 109 110 /** 111 * Send a message over UIPC 112 * 113 * @param ch_id Channel ID 114 * @param msg_evt Message event type 115 * @param p_buf Buffer for the message 116 * @param msglen Message length 117 * @return true on success, otherwise false 118 */ 119 bool UIPC_Send(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint16_t msg_evt, 120 const uint8_t* p_buf, uint16_t msglen); 121 122 /** 123 * Read a message from UIPC 124 * 125 * @param ch_id Channel ID 126 * @param p_msg_evt Message event type 127 * @param p_buf Buffer for the message 128 * @param len Bytes to read 129 * @return true on success, otherwise false 130 */ 131 uint32_t UIPC_Read(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint8_t* p_buf, 132 uint32_t len); 133 134 /** 135 * Control the UIPC parameter 136 * 137 * @param ch_id Channel ID 138 * @param request Request type 139 * @param param Optional parameters 140 * @return true on success, otherwise false 141 */ 142 bool UIPC_Ioctl(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id, uint32_t request, 143 void* param); 144 145 #endif /* UIPC_H */ 146