Description: type mini keyboard and display LCD
L2CAP.cpp@0:e5197e77ab57, 26 Feb 2011 (annotated)
- Committer:
- Date:
- Sat Feb 26 07:24:32 2011 +0000
- Revision:
- 0:e5197e77ab57
rev.1
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 0:e5197e77ab57 | 1 | /* | |
| 0:e5197e77ab57 | 2 | Copyright (c) 2010 Peter Barrett | |
| 0:e5197e77ab57 | 3 | ||
| 0:e5197e77ab57 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 0:e5197e77ab57 | 5 | of this software and associated documentation files (the "Software"), to deal | |
| 0:e5197e77ab57 | 6 | in the Software without restriction, including without limitation the rights | |
| 0:e5197e77ab57 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 0:e5197e77ab57 | 8 | copies of the Software, and to permit persons to whom the Software is | |
| 0:e5197e77ab57 | 9 | furnished to do so, subject to the following conditions: | |
| 0:e5197e77ab57 | 10 | ||
| 0:e5197e77ab57 | 11 | The above copyright notice and this permission notice shall be included in | |
| 0:e5197e77ab57 | 12 | all copies or substantial portions of the Software. | |
| 0:e5197e77ab57 | 13 | ||
| 0:e5197e77ab57 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 0:e5197e77ab57 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 0:e5197e77ab57 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 0:e5197e77ab57 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 0:e5197e77ab57 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 0:e5197e77ab57 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 0:e5197e77ab57 | 20 | THE SOFTWARE. | |
| 0:e5197e77ab57 | 21 | */ | |
| 0:e5197e77ab57 | 22 | ||
| 0:e5197e77ab57 | 23 | ||
| 0:e5197e77ab57 | 24 | #include <stdio.h> | |
| 0:e5197e77ab57 | 25 | #include <stdlib.h> | |
| 0:e5197e77ab57 | 26 | #include <stdio.h> | |
| 0:e5197e77ab57 | 27 | #include <string.h> | |
| 0:e5197e77ab57 | 28 | ||
| 0:e5197e77ab57 | 29 | #include "Utils.h" | |
| 0:e5197e77ab57 | 30 | #include "hci.h" | |
| 0:e5197e77ab57 | 31 | ||
| 0:e5197e77ab57 | 32 | #define L2CAP_COMMAND_REJ 0x01 | |
| 0:e5197e77ab57 | 33 | #define L2CAP_CONN_REQ 0x02 | |
| 0:e5197e77ab57 | 34 | #define L2CAP_CONN_RSP 0x03 | |
| 0:e5197e77ab57 | 35 | #define L2CAP_CONF_REQ 0x04 | |
| 0:e5197e77ab57 | 36 | #define L2CAP_CONF_RSP 0x05 | |
| 0:e5197e77ab57 | 37 | #define L2CAP_DISCONN_REQ 0x06 | |
| 0:e5197e77ab57 | 38 | #define L2CAP_DISCONN_RSP 0x07 | |
| 0:e5197e77ab57 | 39 | #define L2CAP_ECHO_REQ 0x08 | |
| 0:e5197e77ab57 | 40 | #define L2CAP_ECHO_RSP 0x09 | |
| 0:e5197e77ab57 | 41 | #define L2CAP_INFO_REQ 0x0a | |
| 0:e5197e77ab57 | 42 | #define L2CAP_INFO_RSP 0x0b | |
| 0:e5197e77ab57 | 43 | ||
| 0:e5197e77ab57 | 44 | ||
| 0:e5197e77ab57 | 45 | /* L2CAP command codes */ | |
| 0:e5197e77ab57 | 46 | const char* L2CAP_ComandCodeStr(int c) | |
| 0:e5197e77ab57 | 47 | { | |
| 0:e5197e77ab57 | 48 | switch (c) | |
| 0:e5197e77ab57 | 49 | { | |
| 0:e5197e77ab57 | 50 | case L2CAP_COMMAND_REJ: return "L2CAP_COMMAND_REJ"; | |
| 0:e5197e77ab57 | 51 | case L2CAP_CONN_REQ: return "L2CAP_CONN_REQ"; | |
| 0:e5197e77ab57 | 52 | case L2CAP_CONN_RSP: return "L2CAP_CONN_RSP"; | |
| 0:e5197e77ab57 | 53 | case L2CAP_CONF_REQ: return "L2CAP_CONF_REQ"; | |
| 0:e5197e77ab57 | 54 | case L2CAP_CONF_RSP: return "L2CAP_CONF_RSP"; | |
| 0:e5197e77ab57 | 55 | case L2CAP_DISCONN_REQ: return "L2CAP_DISCONN_REQ"; | |
| 0:e5197e77ab57 | 56 | case L2CAP_DISCONN_RSP: return "L2CAP_DISCONN_RSP"; | |
| 0:e5197e77ab57 | 57 | case L2CAP_ECHO_REQ: return "L2CAP_ECHO_REQ"; | |
| 0:e5197e77ab57 | 58 | case L2CAP_ECHO_RSP: return "L2CAP_ECHO_RSP"; | |
| 0:e5197e77ab57 | 59 | case L2CAP_INFO_REQ: return "L2CAP_INFO_REQ"; | |
| 0:e5197e77ab57 | 60 | case L2CAP_INFO_RSP: return "L2CAP_INFO_RSP"; | |
| 0:e5197e77ab57 | 61 | } | |
| 0:e5197e77ab57 | 62 | return "unknown"; | |
| 0:e5197e77ab57 | 63 | } | |
| 0:e5197e77ab57 | 64 | ||
| 0:e5197e77ab57 | 65 | typedef struct | |
| 0:e5197e77ab57 | 66 | { | |
| 0:e5197e77ab57 | 67 | u16 handle; | |
| 0:e5197e77ab57 | 68 | u16 length; // total | |
| 0:e5197e77ab57 | 69 | u16 l2capLength; // length -4 | |
| 0:e5197e77ab57 | 70 | u16 cid; // Signaling packet CID = 1 | |
| 0:e5197e77ab57 | 71 | u8 data[64]; // Largest thing to send!!! todo | |
| 0:e5197e77ab57 | 72 | } L2CAPData; | |
| 0:e5197e77ab57 | 73 | ||
| 0:e5197e77ab57 | 74 | typedef struct | |
| 0:e5197e77ab57 | 75 | { | |
| 0:e5197e77ab57 | 76 | u16 handle; | |
| 0:e5197e77ab57 | 77 | u16 length; // total | |
| 0:e5197e77ab57 | 78 | u16 l2capLength; // length -4 | |
| 0:e5197e77ab57 | 79 | u16 cid; // Signaling packet CID = 1 | |
| 0:e5197e77ab57 | 80 | ||
| 0:e5197e77ab57 | 81 | // Payload | |
| 0:e5197e77ab57 | 82 | u8 cmd; // | |
| 0:e5197e77ab57 | 83 | u8 id; | |
| 0:e5197e77ab57 | 84 | u16 cmdLength; // total-8 | |
| 0:e5197e77ab57 | 85 | u16 params[4]; // Params | |
| 0:e5197e77ab57 | 86 | } L2CAPCmd; | |
| 0:e5197e77ab57 | 87 | ||
| 0:e5197e77ab57 | 88 | // | |
| 0:e5197e77ab57 | 89 | void BTDevice::Init() | |
| 0:e5197e77ab57 | 90 | { | |
| 0:e5197e77ab57 | 91 | memset(&_info,0,sizeof(inquiry_info)); | |
| 0:e5197e77ab57 | 92 | _handle = 0; | |
| 0:e5197e77ab57 | 93 | _name[0] = 0; | |
| 0:e5197e77ab57 | 94 | _state = 0; | |
| 0:e5197e77ab57 | 95 | } | |
| 0:e5197e77ab57 | 96 | ||
| 0:e5197e77ab57 | 97 | // virtual SocketHandler | |
| 0:e5197e77ab57 | 98 | int BTDevice::Open(SocketInternal* sock, SocketAddrHdr* addr) | |
| 0:e5197e77ab57 | 99 | { | |
| 0:e5197e77ab57 | 100 | L2CAPSocket* s = (L2CAPSocket*)sock; | |
| 0:e5197e77ab57 | 101 | L2CAPAddr* a = (L2CAPAddr*)addr; | |
| 0:e5197e77ab57 | 102 | s->scid = 0x40 + sock->ID-1; // are these reserved? | |
| 0:e5197e77ab57 | 103 | s->dcid = 0; | |
| 0:e5197e77ab57 | 104 | Connect(s->scid,a->psm); | |
| 0:e5197e77ab57 | 105 | return sock->ID; | |
| 0:e5197e77ab57 | 106 | } | |
| 0:e5197e77ab57 | 107 | ||
| 0:e5197e77ab57 | 108 | // virtual SocketHandler | |
| 0:e5197e77ab57 | 109 | int BTDevice::Send(SocketInternal* sock, const u8* data, int len) | |
| 0:e5197e77ab57 | 110 | { | |
| 0:e5197e77ab57 | 111 | L2CAPData d; | |
| 0:e5197e77ab57 | 112 | L2CAPSocket* s = (L2CAPSocket*)sock; | |
| 0:e5197e77ab57 | 113 | ||
| 0:e5197e77ab57 | 114 | d.handle = _handle | 0x2000; | |
| 0:e5197e77ab57 | 115 | d.length = 4 + len; | |
| 0:e5197e77ab57 | 116 | d.l2capLength = len; | |
| 0:e5197e77ab57 | 117 | d.cid = s->dcid; | |
| 0:e5197e77ab57 | 118 | ||
| 0:e5197e77ab57 | 119 | if (len > 64) | |
| 0:e5197e77ab57 | 120 | return -1; | |
| 0:e5197e77ab57 | 121 | memcpy(d.data,data,len); | |
| 0:e5197e77ab57 | 122 | return Send((u8*)&d,len+8); | |
| 0:e5197e77ab57 | 123 | } | |
| 0:e5197e77ab57 | 124 | ||
| 0:e5197e77ab57 | 125 | // virtual SocketHandler | |
| 0:e5197e77ab57 | 126 | int BTDevice::Close(SocketInternal* sock) | |
| 0:e5197e77ab57 | 127 | { | |
| 0:e5197e77ab57 | 128 | printf("L2CAP close %d\n",sock->ID); | |
| 0:e5197e77ab57 | 129 | L2CAPSocket* s = (L2CAPSocket*)sock; | |
| 0:e5197e77ab57 | 130 | return Disconnect(s->scid,s->dcid); | |
| 0:e5197e77ab57 | 131 | } | |
| 0:e5197e77ab57 | 132 | ||
| 0:e5197e77ab57 | 133 | L2CAPSocket* BTDevice::SCIDToSocket(int scid) | |
| 0:e5197e77ab57 | 134 | { | |
| 0:e5197e77ab57 | 135 | return (L2CAPSocket*)GetSocketInternal(scid-0x40+1); | |
| 0:e5197e77ab57 | 136 | } | |
| 0:e5197e77ab57 | 137 | ||
| 0:e5197e77ab57 | 138 | int BTDevice::Send(const u8* data, int len) | |
| 0:e5197e77ab57 | 139 | { | |
| 0:e5197e77ab57 | 140 | _transport->ACLSend(data,len); | |
| 0:e5197e77ab57 | 141 | return 0; | |
| 0:e5197e77ab57 | 142 | } | |
| 0:e5197e77ab57 | 143 | ||
| 0:e5197e77ab57 | 144 | int BTDevice::Send(u8 c, u8 id, u16* params, int count) | |
| 0:e5197e77ab57 | 145 | { | |
| 0:e5197e77ab57 | 146 | L2CAPCmd cmd; | |
| 0:e5197e77ab57 | 147 | cmd.handle = _handle | 0x2000; | |
| 0:e5197e77ab57 | 148 | cmd.length = 8 + count*2; | |
| 0:e5197e77ab57 | 149 | ||
| 0:e5197e77ab57 | 150 | cmd.l2capLength = cmd.length-4; | |
| 0:e5197e77ab57 | 151 | cmd.cid = 1; // Signaling packet | |
| 0:e5197e77ab57 | 152 | ||
| 0:e5197e77ab57 | 153 | cmd.cmd = c; | |
| 0:e5197e77ab57 | 154 | cmd.id = id; | |
| 0:e5197e77ab57 | 155 | cmd.cmdLength = count*2; | |
| 0:e5197e77ab57 | 156 | for (int i = 0; i < count; i++) | |
| 0:e5197e77ab57 | 157 | cmd.params[i] = params[i]; | |
| 0:e5197e77ab57 | 158 | return Send((u8*)&cmd,cmd.length+4); | |
| 0:e5197e77ab57 | 159 | } | |
| 0:e5197e77ab57 | 160 | ||
| 0:e5197e77ab57 | 161 | int BTDevice::Connect(int scid, int psm) | |
| 0:e5197e77ab57 | 162 | { | |
| 0:e5197e77ab57 | 163 | u16 p[2]; | |
| 0:e5197e77ab57 | 164 | p[0] = psm; | |
| 0:e5197e77ab57 | 165 | p[1] = scid; | |
| 0:e5197e77ab57 | 166 | return Send(L2CAP_CONN_REQ,_txid++,p,2); | |
| 0:e5197e77ab57 | 167 | } | |
| 0:e5197e77ab57 | 168 | ||
| 0:e5197e77ab57 | 169 | int BTDevice::Disconnect(int scid, int dcid) | |
| 0:e5197e77ab57 | 170 | { | |
| 0:e5197e77ab57 | 171 | u16 p[2]; | |
| 0:e5197e77ab57 | 172 | p[0] = dcid; | |
| 0:e5197e77ab57 | 173 | p[1] = scid; | |
| 0:e5197e77ab57 | 174 | return Send(L2CAP_DISCONN_REQ,_txid++,p,2); | |
| 0:e5197e77ab57 | 175 | } | |
| 0:e5197e77ab57 | 176 | ||
| 0:e5197e77ab57 | 177 | int BTDevice::ConfigureRequest(int dcid) | |
| 0:e5197e77ab57 | 178 | { | |
| 0:e5197e77ab57 | 179 | u16 p[4]; | |
| 0:e5197e77ab57 | 180 | p[0] = dcid; | |
| 0:e5197e77ab57 | 181 | p[1] = 0; | |
| 0:e5197e77ab57 | 182 | p[2] = 0x0201; // Options | |
| 0:e5197e77ab57 | 183 | p[3] = 0x02A0; // 672 | |
| 0:e5197e77ab57 | 184 | return Send(L2CAP_CONF_REQ,_txid++,p,4); | |
| 0:e5197e77ab57 | 185 | } | |
| 0:e5197e77ab57 | 186 | ||
| 0:e5197e77ab57 | 187 | int BTDevice::ConfigureResponse(u8 rxid, int dcid) | |
| 0:e5197e77ab57 | 188 | { | |
| 0:e5197e77ab57 | 189 | u16 p[3]; | |
| 0:e5197e77ab57 | 190 | p[0] = dcid; | |
| 0:e5197e77ab57 | 191 | p[1] = 0; | |
| 0:e5197e77ab57 | 192 | p[2] = 0; | |
| 0:e5197e77ab57 | 193 | return Send(L2CAP_CONF_RSP,rxid,p,3); | |
| 0:e5197e77ab57 | 194 | } | |
| 0:e5197e77ab57 | 195 | ||
| 0:e5197e77ab57 | 196 | int BTDevice::DisconnectResponse(u8 rxid, int scid, int dcid) | |
| 0:e5197e77ab57 | 197 | { | |
| 0:e5197e77ab57 | 198 | u16 p[2]; | |
| 0:e5197e77ab57 | 199 | p[0] = dcid; | |
| 0:e5197e77ab57 | 200 | p[1] = scid; | |
| 0:e5197e77ab57 | 201 | return Send(L2CAP_DISCONN_RSP,rxid,p,2); | |
| 0:e5197e77ab57 | 202 | } | |
| 0:e5197e77ab57 | 203 | ||
| 0:e5197e77ab57 | 204 | void BTDevice::Control(const u8* data, int len) | |
| 0:e5197e77ab57 | 205 | { | |
| 0:e5197e77ab57 | 206 | int cc = data[8]; | |
| 0:e5197e77ab57 | 207 | printf(L2CAP_ComandCodeStr(cc)); | |
| 0:e5197e77ab57 | 208 | int result = LE16(data+16); | |
| 0:e5197e77ab57 | 209 | printf(" Result %d\n",result); | |
| 0:e5197e77ab57 | 210 | switch (cc) | |
| 0:e5197e77ab57 | 211 | { | |
| 0:e5197e77ab57 | 212 | case L2CAP_COMMAND_REJ: | |
| 0:e5197e77ab57 | 213 | break; | |
| 0:e5197e77ab57 | 214 | case L2CAP_CONN_REQ: | |
| 0:e5197e77ab57 | 215 | break; | |
| 0:e5197e77ab57 | 216 | ||
| 0:e5197e77ab57 | 217 | // Response to our initial connect from Remote | |
| 0:e5197e77ab57 | 218 | case L2CAP_CONN_RSP: | |
| 0:e5197e77ab57 | 219 | { | |
| 0:e5197e77ab57 | 220 | if (result == 0) | |
| 0:e5197e77ab57 | 221 | { | |
| 0:e5197e77ab57 | 222 | int dcid = LE16(data+12); | |
| 0:e5197e77ab57 | 223 | int scid = LE16(data+14); | |
| 0:e5197e77ab57 | 224 | L2CAPSocket* s = SCIDToSocket(scid); | |
| 0:e5197e77ab57 | 225 | if (s) | |
| 0:e5197e77ab57 | 226 | { | |
| 0:e5197e77ab57 | 227 | s->dcid = dcid; | |
| 0:e5197e77ab57 | 228 | ConfigureRequest(dcid); | |
| 0:e5197e77ab57 | 229 | } | |
| 0:e5197e77ab57 | 230 | } else | |
| 0:e5197e77ab57 | 231 | printf("Connect failed?\n"); | |
| 0:e5197e77ab57 | 232 | } | |
| 0:e5197e77ab57 | 233 | break; | |
| 0:e5197e77ab57 | 234 | ||
| 0:e5197e77ab57 | 235 | case L2CAP_CONF_RSP: | |
| 0:e5197e77ab57 | 236 | { | |
| 0:e5197e77ab57 | 237 | int scid = LE16(data+12); | |
| 0:e5197e77ab57 | 238 | SocketInternal* s = (SocketInternal*)SCIDToSocket(scid); | |
| 0:e5197e77ab57 | 239 | if (s) | |
| 0:e5197e77ab57 | 240 | s->SetState(SocketState_Open); | |
| 0:e5197e77ab57 | 241 | } | |
| 0:e5197e77ab57 | 242 | break; | |
| 0:e5197e77ab57 | 243 | ||
| 0:e5197e77ab57 | 244 | case L2CAP_CONF_REQ: | |
| 0:e5197e77ab57 | 245 | { | |
| 0:e5197e77ab57 | 246 | int scid = LE16(data+12); | |
| 0:e5197e77ab57 | 247 | L2CAPSocket* s = SCIDToSocket(scid); | |
| 0:e5197e77ab57 | 248 | if (s) | |
| 0:e5197e77ab57 | 249 | ConfigureResponse(data[9],s->dcid); | |
| 0:e5197e77ab57 | 250 | } | |
| 0:e5197e77ab57 | 251 | break; | |
| 0:e5197e77ab57 | 252 | } | |
| 0:e5197e77ab57 | 253 | } | |
| 0:e5197e77ab57 | 254 | ||
| 0:e5197e77ab57 | 255 | void BTDevice::ACLRecv(const u8* data, int len) | |
| 0:e5197e77ab57 | 256 | { | |
| 0:e5197e77ab57 | 257 | // printfBytes("L2CP",data,16); | |
| 0:e5197e77ab57 | 258 | int handle = LE16(data); | |
| 0:e5197e77ab57 | 259 | if (handle != (0x2000 | _handle)) | |
| 0:e5197e77ab57 | 260 | return; | |
| 0:e5197e77ab57 | 261 | ||
| 0:e5197e77ab57 | 262 | int cid = LE16(data+6); | |
| 0:e5197e77ab57 | 263 | if (cid == 1) | |
| 0:e5197e77ab57 | 264 | { | |
| 0:e5197e77ab57 | 265 | Control(data,len); | |
| 0:e5197e77ab57 | 266 | return; | |
| 0:e5197e77ab57 | 267 | } | |
| 0:e5197e77ab57 | 268 | ||
| 0:e5197e77ab57 | 269 | SocketInternal* s = (SocketInternal*)SCIDToSocket(cid); | |
| 0:e5197e77ab57 | 270 | if (s) | |
| 0:e5197e77ab57 | 271 | s->Recv(data+8,LE16(data+2)-4); | |
| 0:e5197e77ab57 | 272 | else | |
| 0:e5197e77ab57 | 273 | printf("Bad event cid %d\n",cid); | |
| 0:e5197e77ab57 | 274 | } |

