Based on myBlueUSB and rosserial_mbed
Dependencies: mbed myUSBHost AvailableMemory myBlueUSB
rfcomm/RFCOMM.cpp@0:7684b95768c7, 2011-09-17 (annotated)
- Committer:
- OTL
- Date:
- Sat Sep 17 14:21:35 2011 +0000
- Revision:
- 0:7684b95768c7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
OTL | 0:7684b95768c7 | 1 | #include "mbed.h" |
OTL | 0:7684b95768c7 | 2 | #include "Utils.h" |
OTL | 0:7684b95768c7 | 3 | #include "RFCOMM.h" |
OTL | 0:7684b95768c7 | 4 | |
OTL | 0:7684b95768c7 | 5 | // Control field values bit no. 1 2 3 4 5 6 7 8 |
OTL | 0:7684b95768c7 | 6 | #define BT_RFCOMM_SABM 0x3F // 1 1 1 1 P/F 1 0 0 |
OTL | 0:7684b95768c7 | 7 | #define BT_RFCOMM_UA 0x73 // 1 1 0 0 P/F 1 1 0 |
OTL | 0:7684b95768c7 | 8 | #define BT_RFCOMM_DM 0x0F // 1 1 1 1 P/F 0 0 0 |
OTL | 0:7684b95768c7 | 9 | #define BT_RFCOMM_DM_PF 0x1F |
OTL | 0:7684b95768c7 | 10 | #define BT_RFCOMM_DISC 0x53 // 1 1 0 0 P/F 0 1 0 |
OTL | 0:7684b95768c7 | 11 | #define BT_RFCOMM_UIH 0xEF // 1 1 1 1 P/F 1 1 1 |
OTL | 0:7684b95768c7 | 12 | #define BT_RFCOMM_UIH_PF 0xFF |
OTL | 0:7684b95768c7 | 13 | |
OTL | 0:7684b95768c7 | 14 | // Multiplexer message types |
OTL | 0:7684b95768c7 | 15 | #define BT_RFCOMM_PN_CMD 0x83 |
OTL | 0:7684b95768c7 | 16 | #define BT_RFCOMM_PN_RSP 0x81 |
OTL | 0:7684b95768c7 | 17 | #define BT_RFCOMM_TEST_CMD 0x23 |
OTL | 0:7684b95768c7 | 18 | #define BT_RFCOMM_TEST_RSP 0x21 |
OTL | 0:7684b95768c7 | 19 | #define BT_RFCOMM_FCON_CMD 0xA3 |
OTL | 0:7684b95768c7 | 20 | #define BT_RFCOMM_FCON_RSP 0xA1 |
OTL | 0:7684b95768c7 | 21 | #define BT_RFCOMM_FCOFF_CMD 0x63 |
OTL | 0:7684b95768c7 | 22 | #define BT_RFCOMM_FCOFF_RSP 0x61 |
OTL | 0:7684b95768c7 | 23 | #define BT_RFCOMM_MSC_CMD 0xE3 |
OTL | 0:7684b95768c7 | 24 | #define BT_RFCOMM_MSC_RSP 0xE1 |
OTL | 0:7684b95768c7 | 25 | #define BT_RFCOMM_RPN_CMD 0x93 |
OTL | 0:7684b95768c7 | 26 | #define BT_RFCOMM_RPN_RSP 0x91 |
OTL | 0:7684b95768c7 | 27 | #define BT_RFCOMM_RLS_CMD 0x53 |
OTL | 0:7684b95768c7 | 28 | #define BT_RFCOMM_RLS_RSP 0x51 |
OTL | 0:7684b95768c7 | 29 | #define BT_RFCOMM_NSC_RSP 0x11 |
OTL | 0:7684b95768c7 | 30 | |
OTL | 0:7684b95768c7 | 31 | // FCS calc |
OTL | 0:7684b95768c7 | 32 | #define BT_RFCOMM_CODE_WORD 0xE0 // pol = x8+x2+x1+1 |
OTL | 0:7684b95768c7 | 33 | #define BT_RFCOMM_CRC_CHECK_LEN 3 |
OTL | 0:7684b95768c7 | 34 | #define BT_RFCOMM_UIHCRC_CHECK_LEN 2 |
OTL | 0:7684b95768c7 | 35 | |
OTL | 0:7684b95768c7 | 36 | #define NR_CREDITS 1 |
OTL | 0:7684b95768c7 | 37 | #define INITIAL_CREDITS 1 //0...7 |
OTL | 0:7684b95768c7 | 38 | |
OTL | 0:7684b95768c7 | 39 | #define DCD (1<<7) //DV data carrier detect |
OTL | 0:7684b95768c7 | 40 | #define RI (1<<6) //IC ring indicator |
OTL | 0:7684b95768c7 | 41 | #define RTS (1<<3) //RTR request to send |
OTL | 0:7684b95768c7 | 42 | #define DSR (1<<2) //RTC data set ready |
OTL | 0:7684b95768c7 | 43 | #define FC (1<<1) //Flow Control |
OTL | 0:7684b95768c7 | 44 | #define EA (1<<0) //extended address (always 1) |
OTL | 0:7684b95768c7 | 45 | #define SIGNALS (DCD | RTS | DSR | EA) |
OTL | 0:7684b95768c7 | 46 | |
OTL | 0:7684b95768c7 | 47 | #define DEBUG 1 |
OTL | 0:7684b95768c7 | 48 | #define TAKE_INITIATIVE |
OTL | 0:7684b95768c7 | 49 | |
OTL | 0:7684b95768c7 | 50 | RFCOMMManager rfcomm_manager; |
OTL | 0:7684b95768c7 | 51 | |
OTL | 0:7684b95768c7 | 52 | //uint8_t rfcomm_out_buffer[1000];//seems a bit big as default max framesize is 127 |
OTL | 0:7684b95768c7 | 53 | //unsigned rfcomm::maxframesize = MAX_FRAME_SIZE; //only initial value |
OTL | 0:7684b95768c7 | 54 | |
OTL | 0:7684b95768c7 | 55 | //these functions are obtained from rfcomm.c on google code |
OTL | 0:7684b95768c7 | 56 | void _bt_rfcomm_send_sabm(unsigned short source_cid, unsigned char initiator, unsigned char channel); |
OTL | 0:7684b95768c7 | 57 | void _bt_rfcomm_send_uih_pn_command(unsigned short source_cid, unsigned char initiator, unsigned char channel, unsigned short max_frame_size); |
OTL | 0:7684b95768c7 | 58 | void _bt_rfcomm_send_uih_msc_cmd(unsigned short source_cid, unsigned char initiator, unsigned char channel, unsigned char signals); |
OTL | 0:7684b95768c7 | 59 | void _bt_rfcomm_send_uih_rpn_cmd(uint16_t source_cid, uint8_t initiator, uint8_t dlci, port_settings *val); |
OTL | 0:7684b95768c7 | 60 | int rfcomm_send_packet(unsigned short source_cid, unsigned char address, unsigned char control, unsigned char credits, const unsigned char *data, unsigned short len); |
OTL | 0:7684b95768c7 | 61 | uint8_t crc8_calc(uint8_t *data, uint16_t len); |
OTL | 0:7684b95768c7 | 62 | |
OTL | 0:7684b95768c7 | 63 | |
OTL | 0:7684b95768c7 | 64 | //find a free socket slot for channel ch |
OTL | 0:7684b95768c7 | 65 | int rfcomm::find_slot(unsigned ch) { |
OTL | 0:7684b95768c7 | 66 | for (int i = 0; i < MAX_RFCOMM_SCKTS; i++) { |
OTL | 0:7684b95768c7 | 67 | if (sckts[i] != 0) { //socket is in use |
OTL | 0:7684b95768c7 | 68 | RFCOMMSocket *s = (RFCOMMSocket*)GetSocketInternal(sckts[i]); |
OTL | 0:7684b95768c7 | 69 | if (s==0) { |
OTL | 0:7684b95768c7 | 70 | printf("find_slot: socket %d not found\n", sckts[i]); |
OTL | 0:7684b95768c7 | 71 | continue; |
OTL | 0:7684b95768c7 | 72 | } |
OTL | 0:7684b95768c7 | 73 | if (s->dlci >> 1 == ch) { |
OTL | 0:7684b95768c7 | 74 | printf("Channel %d is already in use on socket %d\n", ch, sckts[i]); |
OTL | 0:7684b95768c7 | 75 | return -1; |
OTL | 0:7684b95768c7 | 76 | } |
OTL | 0:7684b95768c7 | 77 | } else //slot is free |
OTL | 0:7684b95768c7 | 78 | return i; |
OTL | 0:7684b95768c7 | 79 | } |
OTL | 0:7684b95768c7 | 80 | return -2; //no free slots |
OTL | 0:7684b95768c7 | 81 | } |
OTL | 0:7684b95768c7 | 82 | |
OTL | 0:7684b95768c7 | 83 | //find the rfcomm socket for dlci |
OTL | 0:7684b95768c7 | 84 | RFCOMMSocket* rfcomm::find_socket(unsigned dlci) { |
OTL | 0:7684b95768c7 | 85 | for (int i = 0; i < MAX_RFCOMM_SCKTS; i++) { |
OTL | 0:7684b95768c7 | 86 | if (sckts[i] != 0) { //socket is in use |
OTL | 0:7684b95768c7 | 87 | RFCOMMSocket *s = (RFCOMMSocket*)GetSocketInternal(sckts[i]); |
OTL | 0:7684b95768c7 | 88 | if (s==0) { |
OTL | 0:7684b95768c7 | 89 | printf("find_socket: socket %d not found\n", sckts[i]); |
OTL | 0:7684b95768c7 | 90 | continue; |
OTL | 0:7684b95768c7 | 91 | } |
OTL | 0:7684b95768c7 | 92 | if (s->dlci == dlci) { |
OTL | 0:7684b95768c7 | 93 | return s; |
OTL | 0:7684b95768c7 | 94 | } |
OTL | 0:7684b95768c7 | 95 | } |
OTL | 0:7684b95768c7 | 96 | } |
OTL | 0:7684b95768c7 | 97 | printf("RFCOMMSocket for dlci %d was not found!\n", dlci); |
OTL | 0:7684b95768c7 | 98 | return 0; //socket not found |
OTL | 0:7684b95768c7 | 99 | } |
OTL | 0:7684b95768c7 | 100 | |
OTL | 0:7684b95768c7 | 101 | //send a PN command to all sockets waiting to be opened |
OTL | 0:7684b95768c7 | 102 | void rfcomm::initChannels(int socket) { |
OTL | 0:7684b95768c7 | 103 | for (int i = 0; i < MAX_RFCOMM_SCKTS; i++) { |
OTL | 0:7684b95768c7 | 104 | if (sckts[i] != 0) { //socket is in use |
OTL | 0:7684b95768c7 | 105 | RFCOMMSocket *s = (RFCOMMSocket*)GetSocketInternal(sckts[i]); |
OTL | 0:7684b95768c7 | 106 | if (s==0) { |
OTL | 0:7684b95768c7 | 107 | printf("initChannels: socket %d not found\n", sckts[i]); |
OTL | 0:7684b95768c7 | 108 | continue; |
OTL | 0:7684b95768c7 | 109 | } |
OTL | 0:7684b95768c7 | 110 | if (s->State == SocketState_Opening) { |
OTL | 0:7684b95768c7 | 111 | printf("Sending PN for DLCI %d on socket %d\n", s->dlci, sckts[i]); |
OTL | 0:7684b95768c7 | 112 | _bt_rfcomm_send_uih_pn_command(socket, 1, s->dlci, maxframesize); |
OTL | 0:7684b95768c7 | 113 | s->State = SocketState_L2CAP_Config_wait; |
OTL | 0:7684b95768c7 | 114 | } |
OTL | 0:7684b95768c7 | 115 | } |
OTL | 0:7684b95768c7 | 116 | } |
OTL | 0:7684b95768c7 | 117 | } |
OTL | 0:7684b95768c7 | 118 | |
OTL | 0:7684b95768c7 | 119 | unsigned rfcomm::release_channel(unsigned dlci) { |
OTL | 0:7684b95768c7 | 120 | int n = 0; |
OTL | 0:7684b95768c7 | 121 | for (int i = 0; i < MAX_RFCOMM_SCKTS; i++) { |
OTL | 0:7684b95768c7 | 122 | if (sckts[i] != 0) { //socket is in use |
OTL | 0:7684b95768c7 | 123 | RFCOMMSocket *s = (RFCOMMSocket*)GetSocketInternal(sckts[i]); |
OTL | 0:7684b95768c7 | 124 | if (s==0) { |
OTL | 0:7684b95768c7 | 125 | printf("Release: socket for dlci %d not found\n", dlci); |
OTL | 0:7684b95768c7 | 126 | continue; |
OTL | 0:7684b95768c7 | 127 | } |
OTL | 0:7684b95768c7 | 128 | if (s->dlci == dlci) |
OTL | 0:7684b95768c7 | 129 | sckts[i] = 0; |
OTL | 0:7684b95768c7 | 130 | else |
OTL | 0:7684b95768c7 | 131 | n++; |
OTL | 0:7684b95768c7 | 132 | } |
OTL | 0:7684b95768c7 | 133 | } |
OTL | 0:7684b95768c7 | 134 | return n; |
OTL | 0:7684b95768c7 | 135 | } |
OTL | 0:7684b95768c7 | 136 | |
OTL | 0:7684b95768c7 | 137 | int rfcomm::Send(SocketInternal *sock, const u8* data, int len) {//also see if credits need to be send |
OTL | 0:7684b95768c7 | 138 | RFCOMMSocket *s = (RFCOMMSocket*)sock; |
OTL | 0:7684b95768c7 | 139 | char credits = 0; |
OTL | 0:7684b95768c7 | 140 | char control = BT_RFCOMM_UIH; |
OTL | 0:7684b95768c7 | 141 | if (len + 14 > maxframesize) //hci/l2cap header =8, rfcomm header ~ 6 |
OTL | 0:7684b95768c7 | 142 | printf("Error! packetsize = %d, maxframesize = %d\n", len, maxframesize); |
OTL | 0:7684b95768c7 | 143 | if (s->peer_credits == 0) {//peer is low on credits |
OTL | 0:7684b95768c7 | 144 | credits = NR_CREDITS; |
OTL | 0:7684b95768c7 | 145 | control = BT_RFCOMM_UIH_PF; |
OTL | 0:7684b95768c7 | 146 | s->peer_credits += NR_CREDITS;//so provide some more |
OTL | 0:7684b95768c7 | 147 | } |
OTL | 0:7684b95768c7 | 148 | unsigned char address = (1 << 0) | (initiator << 1) | (s->dlci << 2); |
OTL | 0:7684b95768c7 | 149 | if (s->my_credits) { |
OTL | 0:7684b95768c7 | 150 | s->my_credits--; |
OTL | 0:7684b95768c7 | 151 | return rfcomm_send_packet(_l2cap, address, control, credits, data, len); |
OTL | 0:7684b95768c7 | 152 | } else |
OTL | 0:7684b95768c7 | 153 | return rfcomm_send_packet(_l2cap, address, control, credits, data, 0);//send an empty packet when credits run out |
OTL | 0:7684b95768c7 | 154 | } |
OTL | 0:7684b95768c7 | 155 | |
OTL | 0:7684b95768c7 | 156 | int rfcomm::Close(SocketInternal* sock) { |
OTL | 0:7684b95768c7 | 157 | RFCOMMSocket *s = (RFCOMMSocket*)sock; |
OTL | 0:7684b95768c7 | 158 | int id = s->dlci; |
OTL | 0:7684b95768c7 | 159 | printf("Closing rfcomm dlci %d state=%d\n", id, s->State); |
OTL | 0:7684b95768c7 | 160 | Disconnect(s); |
OTL | 0:7684b95768c7 | 161 | int n = release_channel(id); |
OTL | 0:7684b95768c7 | 162 | printf("%d channels are still open\n", n); |
OTL | 0:7684b95768c7 | 163 | if (n == 0) {//all rfcomm channels are closed |
OTL | 0:7684b95768c7 | 164 | printf("....L2CAP must be closed as well\n"); |
OTL | 0:7684b95768c7 | 165 | rfcomm_send_packet(_l2cap, (1 << 0) | (initiator << 1), BT_RFCOMM_DISC, 0, 0, 0); //close dlci0 |
OTL | 0:7684b95768c7 | 166 | Socket_Close(_l2cap); |
OTL | 0:7684b95768c7 | 167 | _l2cap = 0; //return rfcomm to the pool |
OTL | 0:7684b95768c7 | 168 | } |
OTL | 0:7684b95768c7 | 169 | return 0; |
OTL | 0:7684b95768c7 | 170 | } |
OTL | 0:7684b95768c7 | 171 | |
OTL | 0:7684b95768c7 | 172 | int rfcomm::SetOpt(SocketInternal *sock, int so, int* data, int len) { |
OTL | 0:7684b95768c7 | 173 | switch (so) { |
OTL | 0:7684b95768c7 | 174 | case SO_RECBUF: |
OTL | 0:7684b95768c7 | 175 | case SO_SNDBUF: |
OTL | 0:7684b95768c7 | 176 | maxframesize = *data; //pointless because setting takes effect only before socket is opened (before PN) |
OTL | 0:7684b95768c7 | 177 | break; |
OTL | 0:7684b95768c7 | 178 | default: return NOPROTOOPT; |
OTL | 0:7684b95768c7 | 179 | } |
OTL | 0:7684b95768c7 | 180 | return 0; |
OTL | 0:7684b95768c7 | 181 | } |
OTL | 0:7684b95768c7 | 182 | |
OTL | 0:7684b95768c7 | 183 | int rfcomm::GetOpt(SocketInternal *sock, int so, int* data, int len) { |
OTL | 0:7684b95768c7 | 184 | switch (so) { |
OTL | 0:7684b95768c7 | 185 | case SO_RECBUF: |
OTL | 0:7684b95768c7 | 186 | case SO_SNDBUF: |
OTL | 0:7684b95768c7 | 187 | if (len >= sizeof(int)) |
OTL | 0:7684b95768c7 | 188 | *data = maxframesize; |
OTL | 0:7684b95768c7 | 189 | break; |
OTL | 0:7684b95768c7 | 190 | default: return NOPROTOOPT; |
OTL | 0:7684b95768c7 | 191 | } |
OTL | 0:7684b95768c7 | 192 | return 0; |
OTL | 0:7684b95768c7 | 193 | } |
OTL | 0:7684b95768c7 | 194 | |
OTL | 0:7684b95768c7 | 195 | int rfcomm::Disconnect(RFCOMMSocket *s) { |
OTL | 0:7684b95768c7 | 196 | unsigned char address = (1 << 0) | (initiator << 1) | (s->dlci << 2); |
OTL | 0:7684b95768c7 | 197 | return rfcomm_send_packet(_l2cap, address, BT_RFCOMM_DISC, 0, 0, 0); |
OTL | 0:7684b95768c7 | 198 | } |
OTL | 0:7684b95768c7 | 199 | |
OTL | 0:7684b95768c7 | 200 | //expect this to be called with socket type=SOCKET_RFCOM and addr->psm = channel and addr->bdaddr is the BT addr |
OTL | 0:7684b95768c7 | 201 | //of the device to connect to. |
OTL | 0:7684b95768c7 | 202 | //eg. Socket_Open(SOCKET_RFCOM, rfcommaddr(bdaddr, chan), receiver_func, appl_obj); |
OTL | 0:7684b95768c7 | 203 | int rfcomm::Open(SocketInternal* sock, SocketAddrHdr* addr) { |
OTL | 0:7684b95768c7 | 204 | int ch = ((L2CAPAddr*)addr)->psm;//abused psm for channel ID |
OTL | 0:7684b95768c7 | 205 | RFCOMMSocket *s = (RFCOMMSocket*)sock; |
OTL | 0:7684b95768c7 | 206 | int slot = find_slot(ch); |
OTL | 0:7684b95768c7 | 207 | if (slot < 0) return 0; |
OTL | 0:7684b95768c7 | 208 | sckts[slot] = s->ID; |
OTL | 0:7684b95768c7 | 209 | s->serdevice = this; |
OTL | 0:7684b95768c7 | 210 | s->State = SocketState_Opening; |
OTL | 0:7684b95768c7 | 211 | |
OTL | 0:7684b95768c7 | 212 | if (_l2cap == 0) { //no rfcomm -> l2cap connection yet |
OTL | 0:7684b95768c7 | 213 | printf("Need to open L2CAP channel first before opening RFCOMM channel %d\n", s->dlci); |
OTL | 0:7684b95768c7 | 214 | ((L2CAPAddr*)addr)->psm = L2CAP_PSM_RFCOMM;//open the l2cap channel and the rfcomm_ch channel |
OTL | 0:7684b95768c7 | 215 | initiator = 1; |
OTL | 0:7684b95768c7 | 216 | s->dlci = (ch<<1)|!initiator; |
OTL | 0:7684b95768c7 | 217 | _l2cap = Socket_Open(SOCKET_L2CAP, addr, OnRfCommControl, this);//this is the socket between the RFCOMM and the L2CAP layer |
OTL | 0:7684b95768c7 | 218 | if (_l2cap) |
OTL | 0:7684b95768c7 | 219 | printf("Successfully opened L2CAP channel on socket %d\n", _l2cap); |
OTL | 0:7684b95768c7 | 220 | else { |
OTL | 0:7684b95768c7 | 221 | printf("Opening L2CAP channel failed\n"); |
OTL | 0:7684b95768c7 | 222 | sckts[slot] = 0; |
OTL | 0:7684b95768c7 | 223 | s->State = SocketState_Closed; |
OTL | 0:7684b95768c7 | 224 | return 0; |
OTL | 0:7684b95768c7 | 225 | } |
OTL | 0:7684b95768c7 | 226 | } else {//bypass the l2cap channel creation |
OTL | 0:7684b95768c7 | 227 | s->dlci = (ch<<1)|!initiator; |
OTL | 0:7684b95768c7 | 228 | _bt_rfcomm_send_uih_pn_command(_l2cap, initiator, s->dlci, maxframesize); |
OTL | 0:7684b95768c7 | 229 | s->State = SocketState_L2CAP_Config_wait; |
OTL | 0:7684b95768c7 | 230 | } |
OTL | 0:7684b95768c7 | 231 | return s->ID; //return the application unique socket nr. |
OTL | 0:7684b95768c7 | 232 | } |
OTL | 0:7684b95768c7 | 233 | |
OTL | 0:7684b95768c7 | 234 | int rfcomm::set_remote_port_parameters(unsigned char dlci, port_settings *p) { |
OTL | 0:7684b95768c7 | 235 | _bt_rfcomm_send_uih_rpn_cmd(_l2cap, initiator, dlci, p); |
OTL | 0:7684b95768c7 | 236 | return 0; |
OTL | 0:7684b95768c7 | 237 | } |
OTL | 0:7684b95768c7 | 238 | |
OTL | 0:7684b95768c7 | 239 | //socket is an L2CAP socket and state is the state of the L2CAP socket, not the RFCOMM socket |
OTL | 0:7684b95768c7 | 240 | void rfcomm::OnRfCommControl(int socket, SocketState state, const u8* data, int len, void* userData) { |
OTL | 0:7684b95768c7 | 241 | int packet_processed = 0; |
OTL | 0:7684b95768c7 | 242 | rfcomm* self = (rfcomm*)userData; |
OTL | 0:7684b95768c7 | 243 | const u8 initiator = self->initiator; |
OTL | 0:7684b95768c7 | 244 | printf("\x1B[%dm", 32); //debug: set a different colour |
OTL | 0:7684b95768c7 | 245 | printf("OnRfCommControl sock = %d, state = %d, length = %d\n", socket, state, len); |
OTL | 0:7684b95768c7 | 246 | |
OTL | 0:7684b95768c7 | 247 | if (len == 0) {//client only |
OTL | 0:7684b95768c7 | 248 | if (state==SocketState_Open) {//callback after change to 'open', the rfcomm->l2cap channel is now open |
OTL | 0:7684b95768c7 | 249 | _bt_rfcomm_send_sabm(socket, initiator, 0); //setup the rfcomm control channel dlci==0 |
OTL | 0:7684b95768c7 | 250 | return; |
OTL | 0:7684b95768c7 | 251 | } |
OTL | 0:7684b95768c7 | 252 | return; //or other states to handle, e.g. Closing or Closed |
OTL | 0:7684b95768c7 | 253 | } |
OTL | 0:7684b95768c7 | 254 | //we have data, so parse the header |
OTL | 0:7684b95768c7 | 255 | const u8 &addr = data[0]; |
OTL | 0:7684b95768c7 | 256 | u8 dlci = addr>>2; |
OTL | 0:7684b95768c7 | 257 | const u8 &control = data[1]; |
OTL | 0:7684b95768c7 | 258 | u16 length = data[2]>>1; |
OTL | 0:7684b95768c7 | 259 | const u8 *payload = data+3; |
OTL | 0:7684b95768c7 | 260 | const u8 *pFCS = data+len-1; //expected position of the CRC |
OTL | 0:7684b95768c7 | 261 | if (!(data[2]&1)) { //two byte length |
OTL | 0:7684b95768c7 | 262 | length += ((unsigned)data[3])<<7; |
OTL | 0:7684b95768c7 | 263 | payload++; |
OTL | 0:7684b95768c7 | 264 | } |
OTL | 0:7684b95768c7 | 265 | u8 credits = 0; |
OTL | 0:7684b95768c7 | 266 | if (control == BT_RFCOMM_UIH_PF)//this packet carries credits |
OTL | 0:7684b95768c7 | 267 | credits = *(payload++); |
OTL | 0:7684b95768c7 | 268 | //sanity check |
OTL | 0:7684b95768c7 | 269 | if (payload+length != pFCS) |
OTL | 0:7684b95768c7 | 270 | printf("RFCOMM size mismatch, expected %d payload bytes, got %d\n", length, pFCS-payload); |
OTL | 0:7684b95768c7 | 271 | |
OTL | 0:7684b95768c7 | 272 | if (DEBUG) { |
OTL | 0:7684b95768c7 | 273 | printf("RFCOMM: EA=%d, C/R=%d, D=%d, ch=%d; control=%02X (P/F=%d); length=%d\n", addr&1, (addr>>1)&1, (addr>>2)&1, (addr>>3), control, (control>>4)&1, length); |
OTL | 0:7684b95768c7 | 274 | printfBytes("payload:", payload, length); |
OTL | 0:7684b95768c7 | 275 | } |
OTL | 0:7684b95768c7 | 276 | if (dlci == 0) { //dlci==0 control channel |
OTL | 0:7684b95768c7 | 277 | L2CAPSocket *s = (L2CAPSocket*)GetSocketInternal(socket); |
OTL | 0:7684b95768c7 | 278 | switch (control) { |
OTL | 0:7684b95768c7 | 279 | case BT_RFCOMM_UA:// received 1. message BT_RF_COMM_UA |
OTL | 0:7684b95768c7 | 280 | packet_processed++; |
OTL | 0:7684b95768c7 | 281 | if (s->si.State == SocketState_Closing || s->si.State==SocketState_L2CAP_WaitDisconnect) { //Confirmation of disconnect |
OTL | 0:7684b95768c7 | 282 | printf("Remote side confirmed disconnect for socket %d\n", s->si.ID); |
OTL | 0:7684b95768c7 | 283 | s->si.SetState(SocketState_Closed); |
OTL | 0:7684b95768c7 | 284 | break; |
OTL | 0:7684b95768c7 | 285 | } |
OTL | 0:7684b95768c7 | 286 | printf("Received RFCOMM unnumbered acknowledgement for channel 0 - multiplexer working\n"); |
OTL | 0:7684b95768c7 | 287 | printf("Sending UIH Parameter Negotiation Command from OnRfCommControl\n"); |
OTL | 0:7684b95768c7 | 288 | self->initChannels(socket); |
OTL | 0:7684b95768c7 | 289 | break; |
OTL | 0:7684b95768c7 | 290 | case BT_RFCOMM_UIH:// received UIH Parameter Negotiation Response |
OTL | 0:7684b95768c7 | 291 | switch (payload[0]) { |
OTL | 0:7684b95768c7 | 292 | case BT_RFCOMM_PN_RSP: {//client |
OTL | 0:7684b95768c7 | 293 | packet_processed++; |
OTL | 0:7684b95768c7 | 294 | printf("UIH Parameter Negotiation Response\n"); |
OTL | 0:7684b95768c7 | 295 | printf("Sending SABM #%u\n", payload[2]); |
OTL | 0:7684b95768c7 | 296 | _bt_rfcomm_send_sabm(socket, initiator, payload[2]);//was rfcomm_ch |
OTL | 0:7684b95768c7 | 297 | RFCOMMSocket *r = self->find_socket(payload[2]); |
OTL | 0:7684b95768c7 | 298 | if (r==0) break; |
OTL | 0:7684b95768c7 | 299 | r->my_credits = payload[9]; //initial amount of credits |
OTL | 0:7684b95768c7 | 300 | self->maxframesize = min(self->maxframesize, payload[6] + (payload[7]<<8)); |
OTL | 0:7684b95768c7 | 301 | printf("Max Frame Size = %d, got %d initial credits\n", self->maxframesize, payload[9]); |
OTL | 0:7684b95768c7 | 302 | } |
OTL | 0:7684b95768c7 | 303 | break; |
OTL | 0:7684b95768c7 | 304 | case BT_RFCOMM_PN_CMD: { //remote side sent PN command, mtu and initial credits |
OTL | 0:7684b95768c7 | 305 | packet_processed++; |
OTL | 0:7684b95768c7 | 306 | printf("UIH Parameter Negotiation Indication\n"); |
OTL | 0:7684b95768c7 | 307 | self->maxframesize = min(self->maxframesize, payload[6] + (payload[7]<<8)); |
OTL | 0:7684b95768c7 | 308 | unsigned char cred = payload[9] & 7; |
OTL | 0:7684b95768c7 | 309 | unsigned char _dlci = payload[2]; |
OTL | 0:7684b95768c7 | 310 | |
OTL | 0:7684b95768c7 | 311 | int skt = rfcomm_manager.find_socket(_dlci>>1); |
OTL | 0:7684b95768c7 | 312 | if (skt == 0) { //No-one is listening |
OTL | 0:7684b95768c7 | 313 | printf("No-one is Listening on channel %d\n", _dlci>>1); |
OTL | 0:7684b95768c7 | 314 | rfcomm_send_packet(socket, (_dlci<<2)|1, BT_RFCOMM_DM, 0, 0, 0); |
OTL | 0:7684b95768c7 | 315 | break; |
OTL | 0:7684b95768c7 | 316 | } |
OTL | 0:7684b95768c7 | 317 | RFCOMMSocket *r = (RFCOMMSocket*)GetSocketInternal(skt); |
OTL | 0:7684b95768c7 | 318 | r->my_credits = cred; |
OTL | 0:7684b95768c7 | 319 | r->peer_credits = INITIAL_CREDITS; |
OTL | 0:7684b95768c7 | 320 | unsigned char reply[10]; |
OTL | 0:7684b95768c7 | 321 | memcpy(reply, payload, sizeof(reply)); |
OTL | 0:7684b95768c7 | 322 | reply[0] = BT_RFCOMM_PN_RSP;//[1]=len, [2]=dlci, [4]=priority, [5]=timer(must be 0), [8] retransmissions (must be 0) |
OTL | 0:7684b95768c7 | 323 | reply[3] = payload[3]==0xF0 ? 0xE0 : 0; //support credit based flow control |
OTL | 0:7684b95768c7 | 324 | reply[6] = self->maxframesize; |
OTL | 0:7684b95768c7 | 325 | reply[7] = self->maxframesize>>8; |
OTL | 0:7684b95768c7 | 326 | reply[9] = payload[3]==0xF0 ? r->peer_credits : 0; |
OTL | 0:7684b95768c7 | 327 | printf("Max Frame Size = %d, give %d initial credits\n", self->maxframesize, reply[9]); |
OTL | 0:7684b95768c7 | 328 | rfcomm_send_packet(socket, addr^2, BT_RFCOMM_UIH, 0, reply, sizeof(reply)); |
OTL | 0:7684b95768c7 | 329 | } |
OTL | 0:7684b95768c7 | 330 | break; |
OTL | 0:7684b95768c7 | 331 | case BT_RFCOMM_MSC_CMD: |
OTL | 0:7684b95768c7 | 332 | packet_processed++; |
OTL | 0:7684b95768c7 | 333 | { |
OTL | 0:7684b95768c7 | 334 | printf("Received BT_RFCOMM_MSC_IND\n"); |
OTL | 0:7684b95768c7 | 335 | // fine with this, return the same status and ignore the value, there is no room in the socket to store the value, rfcomm could generate an event |
OTL | 0:7684b95768c7 | 336 | RFCOMMSocket *r = self->find_socket(payload[2]>>2); |
OTL | 0:7684b95768c7 | 337 | if (r==0) break; |
OTL | 0:7684b95768c7 | 338 | unsigned char reply[5]; |
OTL | 0:7684b95768c7 | 339 | memcpy(reply, payload, 5); //keep length, dlci and value(s) |
OTL | 0:7684b95768c7 | 340 | reply[0] = BT_RFCOMM_MSC_RSP; // change command into response |
OTL | 0:7684b95768c7 | 341 | printf("Sending MSC_RSP (%d bytes)\n", length); |
OTL | 0:7684b95768c7 | 342 | rfcomm_send_packet(socket, addr^2, BT_RFCOMM_UIH, 0, reply, (payload[1]>>1)+2); // reply is 4 or 5 bytes |
OTL | 0:7684b95768c7 | 343 | switch (r->State) { |
OTL | 0:7684b95768c7 | 344 | case SocketState_L2CAP_Config_wait: |
OTL | 0:7684b95768c7 | 345 | r->State = SocketState_L2CAP_Config_wait_send; |
OTL | 0:7684b95768c7 | 346 | printf("Sending MSC_CMD\n"); |
OTL | 0:7684b95768c7 | 347 | _bt_rfcomm_send_uih_msc_cmd(socket, initiator, payload[2]>>2, SIGNALS); // ea=1,fc=0,rtc(DSR/DTR)=1,rtr(RTS/CTs)=1,ic(RI)=0,dv(DCD)=1 |
OTL | 0:7684b95768c7 | 348 | r->State = SocketState_L2CAP_Config_wait_rsp; |
OTL | 0:7684b95768c7 | 349 | break; |
OTL | 0:7684b95768c7 | 350 | case SocketState_L2CAP_Config_wait_reqrsp: |
OTL | 0:7684b95768c7 | 351 | r->State = SocketState_L2CAP_Config_wait_rsp; |
OTL | 0:7684b95768c7 | 352 | break; |
OTL | 0:7684b95768c7 | 353 | case SocketState_L2CAP_Config_wait_req: |
OTL | 0:7684b95768c7 | 354 | r->SetState(SocketState_Open); |
OTL | 0:7684b95768c7 | 355 | break; |
OTL | 0:7684b95768c7 | 356 | case SocketState_Open: |
OTL | 0:7684b95768c7 | 357 | //inform port adaptation layer |
OTL | 0:7684b95768c7 | 358 | printf("Received MSC IND in state Open for dlci 0x%02x\n", payload[2]>>2); |
OTL | 0:7684b95768c7 | 359 | break; |
OTL | 0:7684b95768c7 | 360 | default: |
OTL | 0:7684b95768c7 | 361 | printf("Received MSC IND in state %d for dlci 0x%02x\n", r->State, payload[2]>>2); |
OTL | 0:7684b95768c7 | 362 | } |
OTL | 0:7684b95768c7 | 363 | } |
OTL | 0:7684b95768c7 | 364 | break; |
OTL | 0:7684b95768c7 | 365 | case BT_RFCOMM_MSC_RSP: |
OTL | 0:7684b95768c7 | 366 | packet_processed++; |
OTL | 0:7684b95768c7 | 367 | { |
OTL | 0:7684b95768c7 | 368 | RFCOMMSocket *r = self->find_socket(payload[2]>>2); |
OTL | 0:7684b95768c7 | 369 | if (r==0) break; |
OTL | 0:7684b95768c7 | 370 | if (r->State == SocketState_L2CAP_Config_wait_reqrsp) |
OTL | 0:7684b95768c7 | 371 | r->State = SocketState_L2CAP_Config_wait_req; |
OTL | 0:7684b95768c7 | 372 | else if (r->State == SocketState_L2CAP_Config_wait_rsp) |
OTL | 0:7684b95768c7 | 373 | r->SetState(SocketState_Open); |
OTL | 0:7684b95768c7 | 374 | else |
OTL | 0:7684b95768c7 | 375 | printf("Received MSC confirmation in state %d for dlci 0x%02x\n", r->State, payload[2]>>2); |
OTL | 0:7684b95768c7 | 376 | } |
OTL | 0:7684b95768c7 | 377 | break; |
OTL | 0:7684b95768c7 | 378 | case BT_RFCOMM_RPN_CMD: |
OTL | 0:7684b95768c7 | 379 | packet_processed++; |
OTL | 0:7684b95768c7 | 380 | //accept and ignore all settings |
OTL | 0:7684b95768c7 | 381 | unsigned char reply[10]; |
OTL | 0:7684b95768c7 | 382 | memcpy(reply, payload, length); //keep length, dlci and value(s) |
OTL | 0:7684b95768c7 | 383 | reply[0] = BT_RFCOMM_RPN_RSP; // change command into response |
OTL | 0:7684b95768c7 | 384 | printf("Responding to RPN indication (%d bytes)\n", length); |
OTL | 0:7684b95768c7 | 385 | rfcomm_send_packet(socket, addr^2, BT_RFCOMM_UIH, 0, reply, length); |
OTL | 0:7684b95768c7 | 386 | break; |
OTL | 0:7684b95768c7 | 387 | case BT_RFCOMM_RPN_RSP: |
OTL | 0:7684b95768c7 | 388 | packet_processed++; |
OTL | 0:7684b95768c7 | 389 | //ignore a response |
OTL | 0:7684b95768c7 | 390 | printf("Received RPN confirmation\n"); |
OTL | 0:7684b95768c7 | 391 | break; |
OTL | 0:7684b95768c7 | 392 | default: |
OTL | 0:7684b95768c7 | 393 | printf("Unsupported multiplexer frame, type=%02XH\n", data[3]); |
OTL | 0:7684b95768c7 | 394 | } |
OTL | 0:7684b95768c7 | 395 | break; |
OTL | 0:7684b95768c7 | 396 | case BT_RFCOMM_DISC: |
OTL | 0:7684b95768c7 | 397 | printf("Remote site actively disconnected from DLCI0\n"); |
OTL | 0:7684b95768c7 | 398 | rfcomm_send_packet(socket, addr|2, BT_RFCOMM_UA, 0, 0, 0);//confirm disconnection |
OTL | 0:7684b95768c7 | 399 | //intentional fall through |
OTL | 0:7684b95768c7 | 400 | case BT_RFCOMM_DM: |
OTL | 0:7684b95768c7 | 401 | packet_processed++; |
OTL | 0:7684b95768c7 | 402 | printf("Remote side refused connection on DLCI0\n"); |
OTL | 0:7684b95768c7 | 403 | self->_l2cap = Socket_Close(socket); |
OTL | 0:7684b95768c7 | 404 | break; |
OTL | 0:7684b95768c7 | 405 | case BT_RFCOMM_SABM: |
OTL | 0:7684b95768c7 | 406 | packet_processed++; |
OTL | 0:7684b95768c7 | 407 | printf("Remote site is seeking connection on DLCI0\n"); //respond with UA |
OTL | 0:7684b95768c7 | 408 | rfcomm_send_packet(socket, addr|2, BT_RFCOMM_UA, 0, 0, 0);//confirm connection |
OTL | 0:7684b95768c7 | 409 | break; |
OTL | 0:7684b95768c7 | 410 | default: |
OTL | 0:7684b95768c7 | 411 | printf("Unexpected RFCOMM cmd %02XH for address %02XH, length=%d\n", control, addr, length); |
OTL | 0:7684b95768c7 | 412 | } |
OTL | 0:7684b95768c7 | 413 | } else { //data is for one of the serial sockets |
OTL | 0:7684b95768c7 | 414 | RFCOMMSocket *s = 0; |
OTL | 0:7684b95768c7 | 415 | if (control == BT_RFCOMM_SABM) { //req. for conn on this dlci |
OTL | 0:7684b95768c7 | 416 | //cannot call self->rfcomm::find_socket because it has no socket yet |
OTL | 0:7684b95768c7 | 417 | int skt = rfcomm_manager.find_socket(dlci>>1); //find the server socket |
OTL | 0:7684b95768c7 | 418 | s = (RFCOMMSocket*)GetSocketInternal(skt);//the listening socket |
OTL | 0:7684b95768c7 | 419 | if (s) {//move the listening socket to the appropriate rfcomm |
OTL | 0:7684b95768c7 | 420 | int slot = self->find_slot(dlci>>1); |
OTL | 0:7684b95768c7 | 421 | if (slot < 0) { |
OTL | 0:7684b95768c7 | 422 | printf("RFCOMM Channel %d is not free on rfcomm with l2cap socket %d\n", dlci>>1, self->_l2cap); |
OTL | 0:7684b95768c7 | 423 | return; |
OTL | 0:7684b95768c7 | 424 | } |
OTL | 0:7684b95768c7 | 425 | s->serdevice = self; //bind the socket to this refcomm entity |
OTL | 0:7684b95768c7 | 426 | self->sckts[slot] = skt; |
OTL | 0:7684b95768c7 | 427 | rfcomm_manager.remove_socket(skt); |
OTL | 0:7684b95768c7 | 428 | } else { |
OTL | 0:7684b95768c7 | 429 | printf("Couln't find a listening socket for dlci %d\n", dlci); |
OTL | 0:7684b95768c7 | 430 | return; |
OTL | 0:7684b95768c7 | 431 | } |
OTL | 0:7684b95768c7 | 432 | } else |
OTL | 0:7684b95768c7 | 433 | s = self->find_socket(dlci); |
OTL | 0:7684b95768c7 | 434 | if (s==0){ |
OTL | 0:7684b95768c7 | 435 | printf("DLCI %d not found\n", dlci); |
OTL | 0:7684b95768c7 | 436 | return; |
OTL | 0:7684b95768c7 | 437 | } |
OTL | 0:7684b95768c7 | 438 | switch (control) { |
OTL | 0:7684b95768c7 | 439 | case BT_RFCOMM_SABM: |
OTL | 0:7684b95768c7 | 440 | packet_processed++; |
OTL | 0:7684b95768c7 | 441 | rfcomm_send_packet(socket, addr|2, BT_RFCOMM_UA, 0, 0, 0);//confirm connection |
OTL | 0:7684b95768c7 | 442 | s->State = SocketState_L2CAP_Config_wait; //wait for msc cmd |
OTL | 0:7684b95768c7 | 443 | #ifdef TAKE_INITIATIVE |
OTL | 0:7684b95768c7 | 444 | printf("Sending MSC_CMD\n"); |
OTL | 0:7684b95768c7 | 445 | _bt_rfcomm_send_uih_msc_cmd(socket, initiator, dlci, 0x8d); // ea=1,fc=0,rtc(DSR/DTR)=1,rtr(RTS/CTs)=1,ic(RI)=0,dv(DCD)=1 |
OTL | 0:7684b95768c7 | 446 | s->State = SocketState_L2CAP_Config_wait_reqrsp; |
OTL | 0:7684b95768c7 | 447 | #endif |
OTL | 0:7684b95768c7 | 448 | break; |
OTL | 0:7684b95768c7 | 449 | case BT_RFCOMM_UA:// received 2. message BT_RF_COMM_UA |
OTL | 0:7684b95768c7 | 450 | packet_processed++; |
OTL | 0:7684b95768c7 | 451 | if (s->State == SocketState_Closing) { //Confirmation of disconnect |
OTL | 0:7684b95768c7 | 452 | printf("Remote side confirmed disconnect for socket %d\n", s->ID); |
OTL | 0:7684b95768c7 | 453 | s->SetState(SocketState_Closed); |
OTL | 0:7684b95768c7 | 454 | break; |
OTL | 0:7684b95768c7 | 455 | } |
OTL | 0:7684b95768c7 | 456 | printf("Received RFCOMM unnumbered acknowledgement for dlci %u - channel opened\n", dlci); |
OTL | 0:7684b95768c7 | 457 | if (s->State == SocketState_L2CAP_Config_wait) { |
OTL | 0:7684b95768c7 | 458 | printf("Sending MSC CMD\n"); |
OTL | 0:7684b95768c7 | 459 | _bt_rfcomm_send_uih_msc_cmd(socket, initiator, dlci, 0x8d); // ea=1,fc=0,rtc(DSR/DTR)=1,rtr(RTS/CTs)=1,ic(RI)=0,dv(DCD)=1 |
OTL | 0:7684b95768c7 | 460 | s->State = SocketState_L2CAP_Config_wait_reqrsp; |
OTL | 0:7684b95768c7 | 461 | } |
OTL | 0:7684b95768c7 | 462 | break; |
OTL | 0:7684b95768c7 | 463 | case BT_RFCOMM_UIH_PF: //user data with credits |
OTL | 0:7684b95768c7 | 464 | printf("Got %u credits\n", credits); |
OTL | 0:7684b95768c7 | 465 | s->my_credits += credits; |
OTL | 0:7684b95768c7 | 466 | //intentional fall-through |
OTL | 0:7684b95768c7 | 467 | case BT_RFCOMM_UIH: //user data |
OTL | 0:7684b95768c7 | 468 | packet_processed++; |
OTL | 0:7684b95768c7 | 469 | if (DEBUG) { |
OTL | 0:7684b95768c7 | 470 | printf("RX: address %02x, control %02x: ", addr, control); |
OTL | 0:7684b95768c7 | 471 | printHex( payload, length); |
OTL | 0:7684b95768c7 | 472 | } |
OTL | 0:7684b95768c7 | 473 | if (length) { |
OTL | 0:7684b95768c7 | 474 | s->peer_credits--; |
OTL | 0:7684b95768c7 | 475 | s->Recv(payload, length); |
OTL | 0:7684b95768c7 | 476 | } else |
OTL | 0:7684b95768c7 | 477 | printf("Received empty packet\n"); |
OTL | 0:7684b95768c7 | 478 | if (length == 0 || s->peer_credits == 0) {//send credits when peer runs out |
OTL | 0:7684b95768c7 | 479 | //char ini = !(dlci & 1); |
OTL | 0:7684b95768c7 | 480 | unsigned char address = (1 << 0) | (initiator << 1) | (dlci << 2); |
OTL | 0:7684b95768c7 | 481 | printf("send %d credits to dlci %d\n", NR_CREDITS, addr>>2); |
OTL | 0:7684b95768c7 | 482 | rfcomm_send_packet(socket, address, BT_RFCOMM_UIH_PF, NR_CREDITS, NULL, 0); |
OTL | 0:7684b95768c7 | 483 | s->peer_credits += NR_CREDITS; |
OTL | 0:7684b95768c7 | 484 | } |
OTL | 0:7684b95768c7 | 485 | break; |
OTL | 0:7684b95768c7 | 486 | case BT_RFCOMM_DISC: |
OTL | 0:7684b95768c7 | 487 | packet_processed++; |
OTL | 0:7684b95768c7 | 488 | printf("Received DISC IND for dlci %d\n", dlci); |
OTL | 0:7684b95768c7 | 489 | rfcomm_send_packet(socket, addr, BT_RFCOMM_UA, 0, 0, 0);//confirm disconnection |
OTL | 0:7684b95768c7 | 490 | s->SetState(SocketState_Closed); |
OTL | 0:7684b95768c7 | 491 | break; |
OTL | 0:7684b95768c7 | 492 | case BT_RFCOMM_DM: |
OTL | 0:7684b95768c7 | 493 | case BT_RFCOMM_DM_PF: |
OTL | 0:7684b95768c7 | 494 | packet_processed++; |
OTL | 0:7684b95768c7 | 495 | printf("Received DM IND (%02X) for dlci %d\n", control, dlci); |
OTL | 0:7684b95768c7 | 496 | s->SetState(SocketState_Closed); |
OTL | 0:7684b95768c7 | 497 | break; |
OTL | 0:7684b95768c7 | 498 | default: |
OTL | 0:7684b95768c7 | 499 | printf("Unexpected RFCOMM cmd %02XH for address %02XH, length=%d\n", control, addr, length); |
OTL | 0:7684b95768c7 | 500 | } |
OTL | 0:7684b95768c7 | 501 | } |
OTL | 0:7684b95768c7 | 502 | |
OTL | 0:7684b95768c7 | 503 | if (!packet_processed) { |
OTL | 0:7684b95768c7 | 504 | // just dump data for now |
OTL | 0:7684b95768c7 | 505 | printf("??: address %02x, control %02x: ", data[0], data[1]); |
OTL | 0:7684b95768c7 | 506 | printHex( data, len ); |
OTL | 0:7684b95768c7 | 507 | } |
OTL | 0:7684b95768c7 | 508 | printf("\x1B[%dm", 0);//reset terminal colour |
OTL | 0:7684b95768c7 | 509 | } |
OTL | 0:7684b95768c7 | 510 | |
OTL | 0:7684b95768c7 | 511 | //should make the functions below member functions |
OTL | 0:7684b95768c7 | 512 | |
OTL | 0:7684b95768c7 | 513 | /** |
OTL | 0:7684b95768c7 | 514 | * @param credits - only used for RFCOMM flow control in UIH wiht P/F = 1 |
OTL | 0:7684b95768c7 | 515 | */ |
OTL | 0:7684b95768c7 | 516 | /* Questionable optimisation. When OFFSET==8 a buffer is (dynamically) allocated that provides space for the lower |
OTL | 0:7684b95768c7 | 517 | layer headers, this reduces copying to, and allocation of, a lower layer buffer. However, all other layers using |
OTL | 0:7684b95768c7 | 518 | HCI/L2CAP must do the same. |
OTL | 0:7684b95768c7 | 519 | */ |
OTL | 0:7684b95768c7 | 520 | #define OFFSET 8 |
OTL | 0:7684b95768c7 | 521 | |
OTL | 0:7684b95768c7 | 522 | int rfcomm_send_packet(uint16_t source_cid, uint8_t address, uint8_t control, uint8_t credits, const uint8_t *data, uint16_t len) { |
OTL | 0:7684b95768c7 | 523 | |
OTL | 0:7684b95768c7 | 524 | uint16_t pos = OFFSET; |
OTL | 0:7684b95768c7 | 525 | uint8_t crc_fields = 3; |
OTL | 0:7684b95768c7 | 526 | |
OTL | 0:7684b95768c7 | 527 | #if OFFSET == 8 |
OTL | 0:7684b95768c7 | 528 | uint8_t* rfcomm_out_buffer = new uint8_t[OFFSET+len+6]; |
OTL | 0:7684b95768c7 | 529 | #else |
OTL | 0:7684b95768c7 | 530 | static uint8_t rfcomm_out_buffer[MAXFRAMESIZE+6];//seems a bit big as default max framesize is 127 |
OTL | 0:7684b95768c7 | 531 | #endif |
OTL | 0:7684b95768c7 | 532 | rfcomm_out_buffer[pos++] = address; |
OTL | 0:7684b95768c7 | 533 | rfcomm_out_buffer[pos++] = control; |
OTL | 0:7684b95768c7 | 534 | |
OTL | 0:7684b95768c7 | 535 | // length field can be 1 or 2 octets |
OTL | 0:7684b95768c7 | 536 | if (len < 128) { |
OTL | 0:7684b95768c7 | 537 | rfcomm_out_buffer[pos++] = (len << 1)| 1; // bits 0-6 |
OTL | 0:7684b95768c7 | 538 | } else { |
OTL | 0:7684b95768c7 | 539 | rfcomm_out_buffer[pos++] = (len & 0x7f) << 1; // bits 0-6 |
OTL | 0:7684b95768c7 | 540 | rfcomm_out_buffer[pos++] = len >> 7; // bits 7-14 |
OTL | 0:7684b95768c7 | 541 | crc_fields++; |
OTL | 0:7684b95768c7 | 542 | } |
OTL | 0:7684b95768c7 | 543 | |
OTL | 0:7684b95768c7 | 544 | // add credits for UIH frames when PF bit is set |
OTL | 0:7684b95768c7 | 545 | if (control == BT_RFCOMM_UIH_PF) { |
OTL | 0:7684b95768c7 | 546 | rfcomm_out_buffer[pos++] = credits; |
OTL | 0:7684b95768c7 | 547 | } |
OTL | 0:7684b95768c7 | 548 | |
OTL | 0:7684b95768c7 | 549 | // copy actual data |
OTL | 0:7684b95768c7 | 550 | memcpy(&rfcomm_out_buffer[pos], data, len); |
OTL | 0:7684b95768c7 | 551 | pos += len; |
OTL | 0:7684b95768c7 | 552 | |
OTL | 0:7684b95768c7 | 553 | // UIH frames only calc FCS over address + control (5.1.1) |
OTL | 0:7684b95768c7 | 554 | if ((control & 0xef) == BT_RFCOMM_UIH) { |
OTL | 0:7684b95768c7 | 555 | crc_fields = 2; |
OTL | 0:7684b95768c7 | 556 | } |
OTL | 0:7684b95768c7 | 557 | rfcomm_out_buffer[pos++] = crc8_calc(rfcomm_out_buffer+OFFSET, crc_fields); // calc fcs |
OTL | 0:7684b95768c7 | 558 | int retval = Socket_Send( source_cid, rfcomm_out_buffer, pos); |
OTL | 0:7684b95768c7 | 559 | #if OFFSET == 8 |
OTL | 0:7684b95768c7 | 560 | delete[] rfcomm_out_buffer; |
OTL | 0:7684b95768c7 | 561 | #endif |
OTL | 0:7684b95768c7 | 562 | if (retval <= 0) |
OTL | 0:7684b95768c7 | 563 | return retval; |
OTL | 0:7684b95768c7 | 564 | return len;//return the size of the payload |
OTL | 0:7684b95768c7 | 565 | } |
OTL | 0:7684b95768c7 | 566 | |
OTL | 0:7684b95768c7 | 567 | void _bt_rfcomm_send_sabm(uint16_t source_cid, uint8_t initiator, uint8_t dlci) { |
OTL | 0:7684b95768c7 | 568 | uint8_t address = (1 << 0) | (initiator << 1) | (dlci << 2); |
OTL | 0:7684b95768c7 | 569 | rfcomm_send_packet(source_cid, address, BT_RFCOMM_SABM, 0, NULL, 0); |
OTL | 0:7684b95768c7 | 570 | } |
OTL | 0:7684b95768c7 | 571 | |
OTL | 0:7684b95768c7 | 572 | void _bt_rfcomm_send_uih_pn_command(uint16_t source_cid, uint8_t initiator, uint8_t dlci, uint16_t max_frame_size) { |
OTL | 0:7684b95768c7 | 573 | uint8_t payload[10]; |
OTL | 0:7684b95768c7 | 574 | uint8_t address = (1 << 0) | (initiator << 1); // EA and C/R bit set - always server channel 0 |
OTL | 0:7684b95768c7 | 575 | uint8_t pos = 0; |
OTL | 0:7684b95768c7 | 576 | payload[pos++] = BT_RFCOMM_PN_CMD; |
OTL | 0:7684b95768c7 | 577 | payload[pos++] = 8 << 1 | 1; // len |
OTL | 0:7684b95768c7 | 578 | payload[pos++] = dlci; |
OTL | 0:7684b95768c7 | 579 | payload[pos++] = 0xf0; // pre defined for Bluetooth, see 5.5.3 of TS 07.10 Adaption for RFCOMM |
OTL | 0:7684b95768c7 | 580 | payload[pos++] = 0; // priority |
OTL | 0:7684b95768c7 | 581 | payload[pos++] = 0; // max 60 seconds ack |
OTL | 0:7684b95768c7 | 582 | payload[pos++] = max_frame_size & 0xff; // max framesize low |
OTL | 0:7684b95768c7 | 583 | payload[pos++] = max_frame_size >> 8; // max framesize high |
OTL | 0:7684b95768c7 | 584 | payload[pos++] = 0x00; // number of retransmissions |
OTL | 0:7684b95768c7 | 585 | payload[pos++] = INITIAL_CREDITS; // unused error recovery window |
OTL | 0:7684b95768c7 | 586 | rfcomm_send_packet(source_cid, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); |
OTL | 0:7684b95768c7 | 587 | } |
OTL | 0:7684b95768c7 | 588 | |
OTL | 0:7684b95768c7 | 589 | void _bt_rfcomm_send_uih_data(uint16_t source_cid, uint8_t initiator, uint8_t channel, uint8_t *data, uint16_t len) { |
OTL | 0:7684b95768c7 | 590 | uint8_t address = (1 << 0) | (initiator << 1) | (!initiator << 2) | (channel << 3); |
OTL | 0:7684b95768c7 | 591 | rfcomm_send_packet(source_cid, address, BT_RFCOMM_UIH, 0, data, len); |
OTL | 0:7684b95768c7 | 592 | } |
OTL | 0:7684b95768c7 | 593 | |
OTL | 0:7684b95768c7 | 594 | void _bt_rfcomm_send_uih_msc_cmd(uint16_t source_cid, uint8_t initiator, uint8_t dlci, uint8_t signals) { |
OTL | 0:7684b95768c7 | 595 | uint8_t address = (1 << 0) | (initiator << 1); // EA and C/R bit set - always server channel 0 |
OTL | 0:7684b95768c7 | 596 | uint8_t payload[5]; |
OTL | 0:7684b95768c7 | 597 | uint8_t pos = 0; |
OTL | 0:7684b95768c7 | 598 | payload[pos++] = BT_RFCOMM_MSC_CMD; |
OTL | 0:7684b95768c7 | 599 | payload[pos++] = 2 << 1 | 1; // len, should be adapted when adding break byte |
OTL | 0:7684b95768c7 | 600 | payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); //C/R is always 1 |
OTL | 0:7684b95768c7 | 601 | payload[pos++] = signals; |
OTL | 0:7684b95768c7 | 602 | // payload[pos++] = brk; |
OTL | 0:7684b95768c7 | 603 | rfcomm_send_packet(source_cid, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); |
OTL | 0:7684b95768c7 | 604 | } |
OTL | 0:7684b95768c7 | 605 | |
OTL | 0:7684b95768c7 | 606 | void _bt_rfcomm_send_uih_rpn_cmd(uint16_t source_cid, uint8_t initiator, uint8_t dlci, port_settings *val) { |
OTL | 0:7684b95768c7 | 607 | uint8_t address = (1 << 0) | (initiator << 1); // EA and C/R bit set - always server channel 0 |
OTL | 0:7684b95768c7 | 608 | uint8_t payload[sizeof(port_settings)+3]; |
OTL | 0:7684b95768c7 | 609 | uint8_t pos = 0; |
OTL | 0:7684b95768c7 | 610 | payload[pos++] = BT_RFCOMM_RPN_CMD;//type |
OTL | 0:7684b95768c7 | 611 | if (val) { |
OTL | 0:7684b95768c7 | 612 | payload[pos++] = ((1+sizeof(port_settings)) << 1) | 1; // len |
OTL | 0:7684b95768c7 | 613 | payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); |
OTL | 0:7684b95768c7 | 614 | memcpy(payload+pos, (char*)val, sizeof(port_settings)); |
OTL | 0:7684b95768c7 | 615 | pos += sizeof(port_settings); |
OTL | 0:7684b95768c7 | 616 | } else { |
OTL | 0:7684b95768c7 | 617 | payload[pos++] = (1 << 1) | 1; // len |
OTL | 0:7684b95768c7 | 618 | payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); |
OTL | 0:7684b95768c7 | 619 | } |
OTL | 0:7684b95768c7 | 620 | rfcomm_send_packet(source_cid, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); |
OTL | 0:7684b95768c7 | 621 | } |
OTL | 0:7684b95768c7 | 622 | |
OTL | 0:7684b95768c7 | 623 | int set_remote_port_parameters(int socket, port_settings *p) { |
OTL | 0:7684b95768c7 | 624 | RFCOMMSocket* si = (RFCOMMSocket*)GetSocketInternal(socket);//gets the RFCOMM socket |
OTL | 0:7684b95768c7 | 625 | if (!si || si->ID != socket) |
OTL | 0:7684b95768c7 | 626 | return ERR_SOCKET_NOT_FOUND; |
OTL | 0:7684b95768c7 | 627 | return si->serdevice->set_remote_port_parameters(si->dlci, p); |
OTL | 0:7684b95768c7 | 628 | } |
OTL | 0:7684b95768c7 | 629 | |
OTL | 0:7684b95768c7 | 630 | |
OTL | 0:7684b95768c7 | 631 | /* |
OTL | 0:7684b95768c7 | 632 | * CRC (reversed crc) lookup table as calculated by the table generator in ETSI TS 101 369 V6.3.0. |
OTL | 0:7684b95768c7 | 633 | */ |
OTL | 0:7684b95768c7 | 634 | static const uint8_t crc8table[256] = { /* reversed, 8-bit, poly=0x07 */ |
OTL | 0:7684b95768c7 | 635 | 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75, 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B, |
OTL | 0:7684b95768c7 | 636 | 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69, 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67, |
OTL | 0:7684b95768c7 | 637 | 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D, 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43, |
OTL | 0:7684b95768c7 | 638 | 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51, 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F, |
OTL | 0:7684b95768c7 | 639 | 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05, 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B, |
OTL | 0:7684b95768c7 | 640 | 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19, 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17, |
OTL | 0:7684b95768c7 | 641 | 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D, 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33, |
OTL | 0:7684b95768c7 | 642 | 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21, 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F, |
OTL | 0:7684b95768c7 | 643 | 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95, 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B, |
OTL | 0:7684b95768c7 | 644 | 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89, 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87, |
OTL | 0:7684b95768c7 | 645 | 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD, 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3, |
OTL | 0:7684b95768c7 | 646 | 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1, 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF, |
OTL | 0:7684b95768c7 | 647 | 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5, 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB, |
OTL | 0:7684b95768c7 | 648 | 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9, 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7, |
OTL | 0:7684b95768c7 | 649 | 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD, 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3, |
OTL | 0:7684b95768c7 | 650 | 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1, 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF |
OTL | 0:7684b95768c7 | 651 | }; |
OTL | 0:7684b95768c7 | 652 | |
OTL | 0:7684b95768c7 | 653 | #define CRC8_INIT 0xFF // Initial FCS value |
OTL | 0:7684b95768c7 | 654 | #define CRC8_OK 0xCF // Good final FCS value |
OTL | 0:7684b95768c7 | 655 | /*-----------------------------------------------------------------------------------*/ |
OTL | 0:7684b95768c7 | 656 | uint8_t crc8(uint8_t *data, uint16_t len) { |
OTL | 0:7684b95768c7 | 657 | uint16_t count; |
OTL | 0:7684b95768c7 | 658 | uint8_t crc = CRC8_INIT; |
OTL | 0:7684b95768c7 | 659 | for (count = 0; count < len; count++) |
OTL | 0:7684b95768c7 | 660 | crc = crc8table[crc ^ data[count]]; |
OTL | 0:7684b95768c7 | 661 | return crc; |
OTL | 0:7684b95768c7 | 662 | } |
OTL | 0:7684b95768c7 | 663 | |
OTL | 0:7684b95768c7 | 664 | /*-----------------------------------------------------------------------------------*/ |
OTL | 0:7684b95768c7 | 665 | uint8_t crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum) { |
OTL | 0:7684b95768c7 | 666 | uint8_t crc; |
OTL | 0:7684b95768c7 | 667 | |
OTL | 0:7684b95768c7 | 668 | crc = crc8(data, len); |
OTL | 0:7684b95768c7 | 669 | |
OTL | 0:7684b95768c7 | 670 | crc = crc8table[crc ^ check_sum]; |
OTL | 0:7684b95768c7 | 671 | if (crc == CRC8_OK) |
OTL | 0:7684b95768c7 | 672 | return 0; /* Valid */ |
OTL | 0:7684b95768c7 | 673 | else |
OTL | 0:7684b95768c7 | 674 | return 1; /* Failed */ |
OTL | 0:7684b95768c7 | 675 | |
OTL | 0:7684b95768c7 | 676 | } |
OTL | 0:7684b95768c7 | 677 | |
OTL | 0:7684b95768c7 | 678 | /*-----------------------------------------------------------------------------------*/ |
OTL | 0:7684b95768c7 | 679 | uint8_t crc8_calc(uint8_t *data, uint16_t len) { |
OTL | 0:7684b95768c7 | 680 | /* Ones complement */ |
OTL | 0:7684b95768c7 | 681 | return 0xFF - crc8(data, len); |
OTL | 0:7684b95768c7 | 682 | } |