![](/media/cache/profiles/83bdd5a91e556ca3b57aafab83ee8ef5.50x50_q85.jpg)
for PS3 Controller Special thanks to robo8080 http://blog.goo.ne.jp/roboz80/e/10e7bf38d3a63b996ca2894e9fb5e3b6
Dependencies: FatFileSystem mbed
Fork of PS3_BlueUSB by
hci.cpp@0:99a111b75cb4, 2011-04-26 (annotated)
- Committer:
- BartJanssens
- Date:
- Tue Apr 26 16:09:17 2011 +0000
- Revision:
- 0:99a111b75cb4
- Child:
- 1:469717149e71
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BartJanssens | 0:99a111b75cb4 | 1 | /* |
BartJanssens | 0:99a111b75cb4 | 2 | Copyright (c) 2010 Peter Barrett |
BartJanssens | 0:99a111b75cb4 | 3 | |
BartJanssens | 0:99a111b75cb4 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
BartJanssens | 0:99a111b75cb4 | 5 | of this software and associated documentation files (the "Software"), to deal |
BartJanssens | 0:99a111b75cb4 | 6 | in the Software without restriction, including without limitation the rights |
BartJanssens | 0:99a111b75cb4 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
BartJanssens | 0:99a111b75cb4 | 8 | copies of the Software, and to permit persons to whom the Software is |
BartJanssens | 0:99a111b75cb4 | 9 | furnished to do so, subject to the following conditions: |
BartJanssens | 0:99a111b75cb4 | 10 | |
BartJanssens | 0:99a111b75cb4 | 11 | The above copyright notice and this permission notice shall be included in |
BartJanssens | 0:99a111b75cb4 | 12 | all copies or substantial portions of the Software. |
BartJanssens | 0:99a111b75cb4 | 13 | |
BartJanssens | 0:99a111b75cb4 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
BartJanssens | 0:99a111b75cb4 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
BartJanssens | 0:99a111b75cb4 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
BartJanssens | 0:99a111b75cb4 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
BartJanssens | 0:99a111b75cb4 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
BartJanssens | 0:99a111b75cb4 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
BartJanssens | 0:99a111b75cb4 | 20 | THE SOFTWARE. |
BartJanssens | 0:99a111b75cb4 | 21 | */ |
BartJanssens | 0:99a111b75cb4 | 22 | |
BartJanssens | 0:99a111b75cb4 | 23 | /* |
BartJanssens | 0:99a111b75cb4 | 24 | Tue Apr 26 2011 Bart Janssens: added HCI connection accept |
BartJanssens | 0:99a111b75cb4 | 25 | */ |
BartJanssens | 0:99a111b75cb4 | 26 | |
BartJanssens | 0:99a111b75cb4 | 27 | #include <stdio.h> |
BartJanssens | 0:99a111b75cb4 | 28 | #include <stdlib.h> |
BartJanssens | 0:99a111b75cb4 | 29 | #include <stdio.h> |
BartJanssens | 0:99a111b75cb4 | 30 | #include <string.h> |
BartJanssens | 0:99a111b75cb4 | 31 | |
BartJanssens | 0:99a111b75cb4 | 32 | #include "Utils.h" |
BartJanssens | 0:99a111b75cb4 | 33 | #include "hci.h" |
BartJanssens | 0:99a111b75cb4 | 34 | #include "hci_private.h" |
BartJanssens | 0:99a111b75cb4 | 35 | |
BartJanssens | 0:99a111b75cb4 | 36 | enum hci_callback_evt |
BartJanssens | 0:99a111b75cb4 | 37 | { |
BartJanssens | 0:99a111b75cb4 | 38 | NONE, |
BartJanssens | 0:99a111b75cb4 | 39 | CONNECT, |
BartJanssens | 0:99a111b75cb4 | 40 | DISCONECT, |
BartJanssens | 0:99a111b75cb4 | 41 | INQUIRYRESULT |
BartJanssens | 0:99a111b75cb4 | 42 | }; |
BartJanssens | 0:99a111b75cb4 | 43 | |
BartJanssens | 0:99a111b75cb4 | 44 | #define MAX_BLUETOOTH_ADAPTERS 1 |
BartJanssens | 0:99a111b75cb4 | 45 | |
BartJanssens | 0:99a111b75cb4 | 46 | enum StateMask { |
BartJanssens | 0:99a111b75cb4 | 47 | MASK_RESET = 1, |
BartJanssens | 0:99a111b75cb4 | 48 | MASK_READ_BUFFER_SIZE = 2, |
BartJanssens | 0:99a111b75cb4 | 49 | MASK_READ_BD_ADDR = 4, |
BartJanssens | 0:99a111b75cb4 | 50 | MASK_INITED = 8, |
BartJanssens | 0:99a111b75cb4 | 51 | MASK_INQUIRY = 16, |
BartJanssens | 0:99a111b75cb4 | 52 | MASK_REMOTE_NAME = 32, |
BartJanssens | 0:99a111b75cb4 | 53 | MASK_CREATE_CONNECTION = 64 |
BartJanssens | 0:99a111b75cb4 | 54 | }; |
BartJanssens | 0:99a111b75cb4 | 55 | |
BartJanssens | 0:99a111b75cb4 | 56 | int HCI::Open(HCITransport* transport, HCICallback callback) |
BartJanssens | 0:99a111b75cb4 | 57 | { |
BartJanssens | 0:99a111b75cb4 | 58 | _transport = transport; |
BartJanssens | 0:99a111b75cb4 | 59 | _transport->Set(this); |
BartJanssens | 0:99a111b75cb4 | 60 | _callback = callback; |
BartJanssens | 0:99a111b75cb4 | 61 | _state = 0; |
BartJanssens | 0:99a111b75cb4 | 62 | for (int i = 0; i < MAX_BTDEVICES; i++) |
BartJanssens | 0:99a111b75cb4 | 63 | { |
BartJanssens | 0:99a111b75cb4 | 64 | _devices[i].Init(); |
BartJanssens | 0:99a111b75cb4 | 65 | _devices[i]._transport = transport; |
BartJanssens | 0:99a111b75cb4 | 66 | } |
BartJanssens | 0:99a111b75cb4 | 67 | return SendCmd(HCI_OP_RESET); |
BartJanssens | 0:99a111b75cb4 | 68 | } |
BartJanssens | 0:99a111b75cb4 | 69 | |
BartJanssens | 0:99a111b75cb4 | 70 | void printf(const BD_ADDR* addr); |
BartJanssens | 0:99a111b75cb4 | 71 | |
BartJanssens | 0:99a111b75cb4 | 72 | BTDevice* HCI::Find(const BD_ADDR* addr) |
BartJanssens | 0:99a111b75cb4 | 73 | { |
BartJanssens | 0:99a111b75cb4 | 74 | for (int i = 0; i < MAX_BTDEVICES; i++) |
BartJanssens | 0:99a111b75cb4 | 75 | if (_devices[i]._state != 0 && memcmp(addr,&_devices[i]._info.bdaddr,6) == 0) |
BartJanssens | 0:99a111b75cb4 | 76 | return &_devices[i]; |
BartJanssens | 0:99a111b75cb4 | 77 | return 0; |
BartJanssens | 0:99a111b75cb4 | 78 | } |
BartJanssens | 0:99a111b75cb4 | 79 | |
BartJanssens | 0:99a111b75cb4 | 80 | BTDevice* HCI::Find(int handle) |
BartJanssens | 0:99a111b75cb4 | 81 | { |
BartJanssens | 0:99a111b75cb4 | 82 | for (int i = 0; i < MAX_BTDEVICES; i++) |
BartJanssens | 0:99a111b75cb4 | 83 | if (_devices[i]._state != 0 && handle == _devices[i]._handle) |
BartJanssens | 0:99a111b75cb4 | 84 | return &_devices[i]; |
BartJanssens | 0:99a111b75cb4 | 85 | return 0; |
BartJanssens | 0:99a111b75cb4 | 86 | } |
BartJanssens | 0:99a111b75cb4 | 87 | // |
BartJanssens | 0:99a111b75cb4 | 88 | bool HCI::Busy() |
BartJanssens | 0:99a111b75cb4 | 89 | { |
BartJanssens | 0:99a111b75cb4 | 90 | return (_state & (MASK_INQUIRY | MASK_REMOTE_NAME | MASK_CREATE_CONNECTION)) != 0; |
BartJanssens | 0:99a111b75cb4 | 91 | } |
BartJanssens | 0:99a111b75cb4 | 92 | |
BartJanssens | 0:99a111b75cb4 | 93 | int HCI::Inquiry(int duration) |
BartJanssens | 0:99a111b75cb4 | 94 | { |
BartJanssens | 0:99a111b75cb4 | 95 | _state |= MASK_INQUIRY; |
BartJanssens | 0:99a111b75cb4 | 96 | u8 buf[5]; |
BartJanssens | 0:99a111b75cb4 | 97 | buf[0] = 0x33; |
BartJanssens | 0:99a111b75cb4 | 98 | buf[1] = 0x8B; |
BartJanssens | 0:99a111b75cb4 | 99 | buf[2] = 0x9E; |
BartJanssens | 0:99a111b75cb4 | 100 | buf[3] = duration; |
BartJanssens | 0:99a111b75cb4 | 101 | buf[4] = 5; // 5 results |
BartJanssens | 0:99a111b75cb4 | 102 | SendCmd(HCI_OP_INQUIRY,buf,sizeof(buf)); |
BartJanssens | 0:99a111b75cb4 | 103 | return 0; |
BartJanssens | 0:99a111b75cb4 | 104 | } |
BartJanssens | 0:99a111b75cb4 | 105 | |
BartJanssens | 0:99a111b75cb4 | 106 | int HCI::WriteScanEnable() |
BartJanssens | 0:99a111b75cb4 | 107 | { |
BartJanssens | 0:99a111b75cb4 | 108 | |
BartJanssens | 0:99a111b75cb4 | 109 | u8 buf[2]; |
BartJanssens | 0:99a111b75cb4 | 110 | buf[0] = 0x03; |
BartJanssens | 0:99a111b75cb4 | 111 | buf[1] = 0x01; |
BartJanssens | 0:99a111b75cb4 | 112 | |
BartJanssens | 0:99a111b75cb4 | 113 | SendCmd(HCI_OP_WRITE_SCAN_ENABLE,buf,sizeof(buf)); |
BartJanssens | 0:99a111b75cb4 | 114 | return 0; |
BartJanssens | 0:99a111b75cb4 | 115 | } |
BartJanssens | 0:99a111b75cb4 | 116 | |
BartJanssens | 0:99a111b75cb4 | 117 | int HCI::AcceptConnection(const BD_ADDR* addr) |
BartJanssens | 0:99a111b75cb4 | 118 | { |
BartJanssens | 0:99a111b75cb4 | 119 | u8 buf[6+4]; |
BartJanssens | 0:99a111b75cb4 | 120 | memset(buf,0,sizeof(buf)); |
BartJanssens | 0:99a111b75cb4 | 121 | memcpy(buf,addr,6); |
BartJanssens | 0:99a111b75cb4 | 122 | buf[7] = 0; |
BartJanssens | 0:99a111b75cb4 | 123 | |
BartJanssens | 0:99a111b75cb4 | 124 | SendCmd(HCI_OP_ACCEPT_CONN_REQ,buf,sizeof(buf)); |
BartJanssens | 0:99a111b75cb4 | 125 | return 0; |
BartJanssens | 0:99a111b75cb4 | 126 | } |
BartJanssens | 0:99a111b75cb4 | 127 | |
BartJanssens | 0:99a111b75cb4 | 128 | int HCI::SendCmd(int cmd, const u8* params, int len) |
BartJanssens | 0:99a111b75cb4 | 129 | { |
BartJanssens | 0:99a111b75cb4 | 130 | u8 b[32]; |
BartJanssens | 0:99a111b75cb4 | 131 | b[0] = cmd; |
BartJanssens | 0:99a111b75cb4 | 132 | b[1] = (cmd >> 8); |
BartJanssens | 0:99a111b75cb4 | 133 | b[2] = len; |
BartJanssens | 0:99a111b75cb4 | 134 | if (params) |
BartJanssens | 0:99a111b75cb4 | 135 | memcpy(b+3,params,len); |
BartJanssens | 0:99a111b75cb4 | 136 | _transport->HCISend(b,len+3); |
BartJanssens | 0:99a111b75cb4 | 137 | return 0; |
BartJanssens | 0:99a111b75cb4 | 138 | } |
BartJanssens | 0:99a111b75cb4 | 139 | |
BartJanssens | 0:99a111b75cb4 | 140 | void HCI::OnCommandComplete(int cmd, const u8* data, int len) |
BartJanssens | 0:99a111b75cb4 | 141 | { |
BartJanssens | 0:99a111b75cb4 | 142 | // printf("%04X %s",cmd,CmdStr(cmd)); |
BartJanssens | 0:99a111b75cb4 | 143 | if (len < 0) |
BartJanssens | 0:99a111b75cb4 | 144 | return; |
BartJanssens | 0:99a111b75cb4 | 145 | //printfBytes(" complete",data,min(16,len)); |
BartJanssens | 0:99a111b75cb4 | 146 | |
BartJanssens | 0:99a111b75cb4 | 147 | switch (cmd) |
BartJanssens | 0:99a111b75cb4 | 148 | { |
BartJanssens | 0:99a111b75cb4 | 149 | // Init phase 0 |
BartJanssens | 0:99a111b75cb4 | 150 | case HCI_OP_RESET: // Reset done, init chain to HCI_OP_READ_LOCAL_NAME |
BartJanssens | 0:99a111b75cb4 | 151 | SendCmd(HCI_OP_READ_BUFFER_SIZE); |
BartJanssens | 0:99a111b75cb4 | 152 | _state |= MASK_RESET; |
BartJanssens | 0:99a111b75cb4 | 153 | break; |
BartJanssens | 0:99a111b75cb4 | 154 | |
BartJanssens | 0:99a111b75cb4 | 155 | // Init phase 1 |
BartJanssens | 0:99a111b75cb4 | 156 | case HCI_OP_READ_BUFFER_SIZE: |
BartJanssens | 0:99a111b75cb4 | 157 | _acl_mtu = LE16(data); |
BartJanssens | 0:99a111b75cb4 | 158 | _sco_mtu = data[2]; |
BartJanssens | 0:99a111b75cb4 | 159 | _acl_max_pkt = LE16(data+3); |
BartJanssens | 0:99a111b75cb4 | 160 | _sco_max_pkt = LE16(data+5); |
BartJanssens | 0:99a111b75cb4 | 161 | SendCmd(HCI_OP_READ_BD_ADDR); |
BartJanssens | 0:99a111b75cb4 | 162 | _state |= MASK_READ_BUFFER_SIZE; |
BartJanssens | 0:99a111b75cb4 | 163 | break; |
BartJanssens | 0:99a111b75cb4 | 164 | |
BartJanssens | 0:99a111b75cb4 | 165 | // Init phase 2 |
BartJanssens | 0:99a111b75cb4 | 166 | case HCI_OP_READ_BD_ADDR: |
BartJanssens | 0:99a111b75cb4 | 167 | _localAddr = *((BD_ADDR*)data); // Local Address |
BartJanssens | 0:99a111b75cb4 | 168 | printf("Local Address: "); |
BartJanssens | 0:99a111b75cb4 | 169 | printf(&_localAddr); |
BartJanssens | 0:99a111b75cb4 | 170 | printf("\r\n"); |
BartJanssens | 0:99a111b75cb4 | 171 | |
BartJanssens | 0:99a111b75cb4 | 172 | _state |= MASK_READ_BD_ADDR; |
BartJanssens | 0:99a111b75cb4 | 173 | _state |= MASK_INITED; |
BartJanssens | 0:99a111b75cb4 | 174 | Callback(CALLBACK_READY,data,6); |
BartJanssens | 0:99a111b75cb4 | 175 | break; |
BartJanssens | 0:99a111b75cb4 | 176 | |
BartJanssens | 0:99a111b75cb4 | 177 | // 0CXX |
BartJanssens | 0:99a111b75cb4 | 178 | case HCI_OP_READ_LOCAL_NAME: |
BartJanssens | 0:99a111b75cb4 | 179 | break; |
BartJanssens | 0:99a111b75cb4 | 180 | |
BartJanssens | 0:99a111b75cb4 | 181 | case HCI_OP_READ_LOCAL_VERSION: |
BartJanssens | 0:99a111b75cb4 | 182 | // params |
BartJanssens | 0:99a111b75cb4 | 183 | //SendCmd(HCI_OP_READ_LOCAL_NAME); |
BartJanssens | 0:99a111b75cb4 | 184 | break; |
BartJanssens | 0:99a111b75cb4 | 185 | |
BartJanssens | 0:99a111b75cb4 | 186 | case HCI_OP_READ_LOCAL_COMMANDS: |
BartJanssens | 0:99a111b75cb4 | 187 | break; |
BartJanssens | 0:99a111b75cb4 | 188 | |
BartJanssens | 0:99a111b75cb4 | 189 | case HCI_OP_READ_LOCAL_FEATURES: |
BartJanssens | 0:99a111b75cb4 | 190 | //SendCmd(HCI_OP_READ_LOCAL_VERSION); |
BartJanssens | 0:99a111b75cb4 | 191 | break; |
BartJanssens | 0:99a111b75cb4 | 192 | |
BartJanssens | 0:99a111b75cb4 | 193 | case HCI_OP_READ_LOCAL_EXT_FEATURES: |
BartJanssens | 0:99a111b75cb4 | 194 | break; |
BartJanssens | 0:99a111b75cb4 | 195 | |
BartJanssens | 0:99a111b75cb4 | 196 | case HCI_OP_PIN_CODE_REPLY: |
BartJanssens | 0:99a111b75cb4 | 197 | printf("Got pin reply\r\n"); |
BartJanssens | 0:99a111b75cb4 | 198 | break; |
BartJanssens | 0:99a111b75cb4 | 199 | |
BartJanssens | 0:99a111b75cb4 | 200 | default: |
BartJanssens | 0:99a111b75cb4 | 201 | //printf("Unrecognized Command %04X\r\n",cmd); |
BartJanssens | 0:99a111b75cb4 | 202 | break; |
BartJanssens | 0:99a111b75cb4 | 203 | } |
BartJanssens | 0:99a111b75cb4 | 204 | } |
BartJanssens | 0:99a111b75cb4 | 205 | |
BartJanssens | 0:99a111b75cb4 | 206 | void HCI::Callback(HCI_CALLBACK_EVENT c, const u8* data, int len) |
BartJanssens | 0:99a111b75cb4 | 207 | { |
BartJanssens | 0:99a111b75cb4 | 208 | _callback(this,c,data,len); |
BartJanssens | 0:99a111b75cb4 | 209 | } |
BartJanssens | 0:99a111b75cb4 | 210 | |
BartJanssens | 0:99a111b75cb4 | 211 | int HCI::RemoteNameRequest(const BD_ADDR* addr) |
BartJanssens | 0:99a111b75cb4 | 212 | { |
BartJanssens | 0:99a111b75cb4 | 213 | _state |= MASK_REMOTE_NAME; |
BartJanssens | 0:99a111b75cb4 | 214 | u8 buf[6+4]; |
BartJanssens | 0:99a111b75cb4 | 215 | memset(buf,0,sizeof(buf)); |
BartJanssens | 0:99a111b75cb4 | 216 | memcpy(buf,addr,6); |
BartJanssens | 0:99a111b75cb4 | 217 | buf[7] = 1; |
BartJanssens | 0:99a111b75cb4 | 218 | return SendCmd(HCI_OP_REMOTE_NAME_REQ,buf,sizeof(buf)); |
BartJanssens | 0:99a111b75cb4 | 219 | } |
BartJanssens | 0:99a111b75cb4 | 220 | |
BartJanssens | 0:99a111b75cb4 | 221 | int HCI::CreateConnection(const BD_ADDR* remoteAddr) |
BartJanssens | 0:99a111b75cb4 | 222 | { |
BartJanssens | 0:99a111b75cb4 | 223 | _state |= MASK_CREATE_CONNECTION; |
BartJanssens | 0:99a111b75cb4 | 224 | u8 buf[6+7]; |
BartJanssens | 0:99a111b75cb4 | 225 | memset(buf,0,sizeof(buf)); |
BartJanssens | 0:99a111b75cb4 | 226 | memcpy(buf,remoteAddr,6); |
BartJanssens | 0:99a111b75cb4 | 227 | buf[6] = 0x18; // DM1,DH1 |
BartJanssens | 0:99a111b75cb4 | 228 | buf[7] = 0xCC; // DM3, DH3, DM5, DH5 |
BartJanssens | 0:99a111b75cb4 | 229 | buf[8] = 1; // Page Repetition R1 |
BartJanssens | 0:99a111b75cb4 | 230 | return SendCmd(HCI_OP_CREATE_CONN,buf,sizeof(buf)); |
BartJanssens | 0:99a111b75cb4 | 231 | } |
BartJanssens | 0:99a111b75cb4 | 232 | |
BartJanssens | 0:99a111b75cb4 | 233 | int HCI::Disconnect(const BD_ADDR* bdaddr) |
BartJanssens | 0:99a111b75cb4 | 234 | { |
BartJanssens | 0:99a111b75cb4 | 235 | BTDevice* d = Find(bdaddr); |
BartJanssens | 0:99a111b75cb4 | 236 | if (!d) |
BartJanssens | 0:99a111b75cb4 | 237 | return ERR_HCI_DEVICE_NOT_FOUND; |
BartJanssens | 0:99a111b75cb4 | 238 | int handle = d->_handle; |
BartJanssens | 0:99a111b75cb4 | 239 | printf("Disconnect from %d\r\n",handle); |
BartJanssens | 0:99a111b75cb4 | 240 | _state |= MASK_CREATE_CONNECTION; |
BartJanssens | 0:99a111b75cb4 | 241 | u8 buf[3]; |
BartJanssens | 0:99a111b75cb4 | 242 | buf[0] = handle; |
BartJanssens | 0:99a111b75cb4 | 243 | buf[1] = (handle >> 8); |
BartJanssens | 0:99a111b75cb4 | 244 | buf[2] = 0x13; |
BartJanssens | 0:99a111b75cb4 | 245 | return SendCmd(HCI_OP_DISCONNECT,buf,sizeof(buf)); |
BartJanssens | 0:99a111b75cb4 | 246 | } |
BartJanssens | 0:99a111b75cb4 | 247 | |
BartJanssens | 0:99a111b75cb4 | 248 | void HCI::DisconnectComplete(int handle) |
BartJanssens | 0:99a111b75cb4 | 249 | { |
BartJanssens | 0:99a111b75cb4 | 250 | BTDevice* d = Find(handle); |
BartJanssens | 0:99a111b75cb4 | 251 | if (!d) |
BartJanssens | 0:99a111b75cb4 | 252 | return; |
BartJanssens | 0:99a111b75cb4 | 253 | d->_handle = 0; |
BartJanssens | 0:99a111b75cb4 | 254 | } |
BartJanssens | 0:99a111b75cb4 | 255 | |
BartJanssens | 0:99a111b75cb4 | 256 | int HCI::DisconnectAll() |
BartJanssens | 0:99a111b75cb4 | 257 | { |
BartJanssens | 0:99a111b75cb4 | 258 | BTDevice* devs[8]; |
BartJanssens | 0:99a111b75cb4 | 259 | int count = GetDevices(devs,8); |
BartJanssens | 0:99a111b75cb4 | 260 | for (int i = 0; i < count; i++) |
BartJanssens | 0:99a111b75cb4 | 261 | Disconnect(&devs[i]->_info.bdaddr); |
BartJanssens | 0:99a111b75cb4 | 262 | return 0; |
BartJanssens | 0:99a111b75cb4 | 263 | } |
BartJanssens | 0:99a111b75cb4 | 264 | |
BartJanssens | 0:99a111b75cb4 | 265 | int HCI::PinCodeReply(const u8* data) |
BartJanssens | 0:99a111b75cb4 | 266 | { |
BartJanssens | 0:99a111b75cb4 | 267 | u8 b[6+1+16]; |
BartJanssens | 0:99a111b75cb4 | 268 | memset(b,0,sizeof(b)); |
BartJanssens | 0:99a111b75cb4 | 269 | memcpy(b,data,6); |
BartJanssens | 0:99a111b75cb4 | 270 | b[6] = 4; |
BartJanssens | 0:99a111b75cb4 | 271 | b[7] = '0'; |
BartJanssens | 0:99a111b75cb4 | 272 | b[8] = '0'; |
BartJanssens | 0:99a111b75cb4 | 273 | b[9] = '0'; |
BartJanssens | 0:99a111b75cb4 | 274 | b[10] = '0'; |
BartJanssens | 0:99a111b75cb4 | 275 | return SendCmd(HCI_OP_PIN_CODE_REPLY,b,sizeof(b)); |
BartJanssens | 0:99a111b75cb4 | 276 | } |
BartJanssens | 0:99a111b75cb4 | 277 | |
BartJanssens | 0:99a111b75cb4 | 278 | void HCI::InquiryResult(const inquiry_info* info) |
BartJanssens | 0:99a111b75cb4 | 279 | { |
BartJanssens | 0:99a111b75cb4 | 280 | BTDevice* bt = Find(&info->bdaddr); |
BartJanssens | 0:99a111b75cb4 | 281 | if (!bt) // new device |
BartJanssens | 0:99a111b75cb4 | 282 | { |
BartJanssens | 0:99a111b75cb4 | 283 | for (int i = 0; i < MAX_BTDEVICES; i++) |
BartJanssens | 0:99a111b75cb4 | 284 | { |
BartJanssens | 0:99a111b75cb4 | 285 | if (_devices[i]._state == 0) |
BartJanssens | 0:99a111b75cb4 | 286 | { |
BartJanssens | 0:99a111b75cb4 | 287 | bt = _devices + i; |
BartJanssens | 0:99a111b75cb4 | 288 | bt->_state = 1; |
BartJanssens | 0:99a111b75cb4 | 289 | break; |
BartJanssens | 0:99a111b75cb4 | 290 | } |
BartJanssens | 0:99a111b75cb4 | 291 | } |
BartJanssens | 0:99a111b75cb4 | 292 | if (!bt) |
BartJanssens | 0:99a111b75cb4 | 293 | { |
BartJanssens | 0:99a111b75cb4 | 294 | printf("HCI::InquiryResult too many devices\r\n"); |
BartJanssens | 0:99a111b75cb4 | 295 | return; // Too many devices! |
BartJanssens | 0:99a111b75cb4 | 296 | } |
BartJanssens | 0:99a111b75cb4 | 297 | } |
BartJanssens | 0:99a111b75cb4 | 298 | |
BartJanssens | 0:99a111b75cb4 | 299 | bt->_info = *info; |
BartJanssens | 0:99a111b75cb4 | 300 | } |
BartJanssens | 0:99a111b75cb4 | 301 | |
BartJanssens | 0:99a111b75cb4 | 302 | int HCI::GetDevices(BTDevice** devices, int maxDevices) |
BartJanssens | 0:99a111b75cb4 | 303 | { |
BartJanssens | 0:99a111b75cb4 | 304 | int j = 0; |
BartJanssens | 0:99a111b75cb4 | 305 | for (int i = 0; i < MAX_BTDEVICES; i++) |
BartJanssens | 0:99a111b75cb4 | 306 | { |
BartJanssens | 0:99a111b75cb4 | 307 | if (_devices[i]._state != 0) |
BartJanssens | 0:99a111b75cb4 | 308 | { |
BartJanssens | 0:99a111b75cb4 | 309 | devices[j++] = _devices + i; |
BartJanssens | 0:99a111b75cb4 | 310 | if (j == maxDevices) |
BartJanssens | 0:99a111b75cb4 | 311 | break; |
BartJanssens | 0:99a111b75cb4 | 312 | } |
BartJanssens | 0:99a111b75cb4 | 313 | } |
BartJanssens | 0:99a111b75cb4 | 314 | return j; |
BartJanssens | 0:99a111b75cb4 | 315 | } |
BartJanssens | 0:99a111b75cb4 | 316 | |
BartJanssens | 0:99a111b75cb4 | 317 | void HCI::RemoteName(const BD_ADDR* addr, const char* name) |
BartJanssens | 0:99a111b75cb4 | 318 | { |
BartJanssens | 0:99a111b75cb4 | 319 | BTDevice* d = Find(addr); |
BartJanssens | 0:99a111b75cb4 | 320 | if (d) |
BartJanssens | 0:99a111b75cb4 | 321 | { |
BartJanssens | 0:99a111b75cb4 | 322 | strncpy(d->_name,name,sizeof(d->_name)-1); |
BartJanssens | 0:99a111b75cb4 | 323 | d->_name[sizeof(d->_name)-1] = 0; |
BartJanssens | 0:99a111b75cb4 | 324 | } |
BartJanssens | 0:99a111b75cb4 | 325 | } |
BartJanssens | 0:99a111b75cb4 | 326 | |
BartJanssens | 0:99a111b75cb4 | 327 | void HCI::ConnectComplete(const connection_info* info) |
BartJanssens | 0:99a111b75cb4 | 328 | { |
BartJanssens | 0:99a111b75cb4 | 329 | BTDevice* d = Find(&info->bdaddr); |
BartJanssens | 0:99a111b75cb4 | 330 | if (!d) |
BartJanssens | 0:99a111b75cb4 | 331 | return; |
BartJanssens | 0:99a111b75cb4 | 332 | if (info->status == 0) |
BartJanssens | 0:99a111b75cb4 | 333 | { |
BartJanssens | 0:99a111b75cb4 | 334 | d->_handle = info->handle; |
BartJanssens | 0:99a111b75cb4 | 335 | printf("Connected on %04X\r\n",info->handle); |
BartJanssens | 0:99a111b75cb4 | 336 | } else |
BartJanssens | 0:99a111b75cb4 | 337 | printf("Connection failed with %d\r\n",info->status); |
BartJanssens | 0:99a111b75cb4 | 338 | } |
BartJanssens | 0:99a111b75cb4 | 339 | |
BartJanssens | 0:99a111b75cb4 | 340 | void HCI::ConnectRequest(const request_info* info) |
BartJanssens | 0:99a111b75cb4 | 341 | { |
BartJanssens | 0:99a111b75cb4 | 342 | BTDevice* bt = Find(&info->bdaddr); |
BartJanssens | 0:99a111b75cb4 | 343 | if (!bt) // new device |
BartJanssens | 0:99a111b75cb4 | 344 | { |
BartJanssens | 0:99a111b75cb4 | 345 | for (int i = 0; i < MAX_BTDEVICES; i++) |
BartJanssens | 0:99a111b75cb4 | 346 | { |
BartJanssens | 0:99a111b75cb4 | 347 | if (_devices[i]._state == 0) |
BartJanssens | 0:99a111b75cb4 | 348 | { |
BartJanssens | 0:99a111b75cb4 | 349 | bt = _devices + i; |
BartJanssens | 0:99a111b75cb4 | 350 | bt->_state = 1; |
BartJanssens | 0:99a111b75cb4 | 351 | break; |
BartJanssens | 0:99a111b75cb4 | 352 | } |
BartJanssens | 0:99a111b75cb4 | 353 | } |
BartJanssens | 0:99a111b75cb4 | 354 | if (!bt) |
BartJanssens | 0:99a111b75cb4 | 355 | { |
BartJanssens | 0:99a111b75cb4 | 356 | printf("HCI::ConnectRequest too many devices\r\n"); |
BartJanssens | 0:99a111b75cb4 | 357 | return; // Too many devices! |
BartJanssens | 0:99a111b75cb4 | 358 | } |
BartJanssens | 0:99a111b75cb4 | 359 | } |
BartJanssens | 0:99a111b75cb4 | 360 | bt->_info.bdaddr = info->bdaddr; |
BartJanssens | 0:99a111b75cb4 | 361 | memcpy(bt->_info.dev_class,info->dev_class,3); |
BartJanssens | 0:99a111b75cb4 | 362 | AcceptConnection(&info->bdaddr); |
BartJanssens | 0:99a111b75cb4 | 363 | } |
BartJanssens | 0:99a111b75cb4 | 364 | |
BartJanssens | 0:99a111b75cb4 | 365 | void HCI::HCIRecv(const u8* data, int len) |
BartJanssens | 0:99a111b75cb4 | 366 | { |
BartJanssens | 0:99a111b75cb4 | 367 | //printfBytes(EvtStr(data[0]),data,len); |
BartJanssens | 0:99a111b75cb4 | 368 | //printfBytes(EvtStr(data[0]),data,min(len,16)); |
BartJanssens | 0:99a111b75cb4 | 369 | //printf("HCI Event %02X\r\n",data[0]); |
BartJanssens | 0:99a111b75cb4 | 370 | switch (data[0]) |
BartJanssens | 0:99a111b75cb4 | 371 | { |
BartJanssens | 0:99a111b75cb4 | 372 | case HCI_EV_INQUIRY_COMPLETE: |
BartJanssens | 0:99a111b75cb4 | 373 | printfBytes("Inquiry Complete",data,data[1]); |
BartJanssens | 0:99a111b75cb4 | 374 | _state &= ~MASK_INQUIRY; |
BartJanssens | 0:99a111b75cb4 | 375 | Callback(CALLBACK_INQUIRY_DONE,0,0); |
BartJanssens | 0:99a111b75cb4 | 376 | WriteScanEnable(); |
BartJanssens | 0:99a111b75cb4 | 377 | break; |
BartJanssens | 0:99a111b75cb4 | 378 | |
BartJanssens | 0:99a111b75cb4 | 379 | case HCI_EV_INQUIRY_RESULT: |
BartJanssens | 0:99a111b75cb4 | 380 | { |
BartJanssens | 0:99a111b75cb4 | 381 | const u8* end = data[1] + data + 2; |
BartJanssens | 0:99a111b75cb4 | 382 | data += 3; |
BartJanssens | 0:99a111b75cb4 | 383 | while (data < end) |
BartJanssens | 0:99a111b75cb4 | 384 | { |
BartJanssens | 0:99a111b75cb4 | 385 | inquiry_info align; |
BartJanssens | 0:99a111b75cb4 | 386 | memcpy(&align,data,sizeof(inquiry_info)); |
BartJanssens | 0:99a111b75cb4 | 387 | InquiryResult(&align); |
BartJanssens | 0:99a111b75cb4 | 388 | Callback(CALLBACK_INQUIRY_RESULT,(u8*)&align,sizeof(inquiry_info)); |
BartJanssens | 0:99a111b75cb4 | 389 | data += 14; |
BartJanssens | 0:99a111b75cb4 | 390 | } |
BartJanssens | 0:99a111b75cb4 | 391 | } |
BartJanssens | 0:99a111b75cb4 | 392 | break; |
BartJanssens | 0:99a111b75cb4 | 393 | |
BartJanssens | 0:99a111b75cb4 | 394 | case HCI_EV_CONN_COMPLETE: |
BartJanssens | 0:99a111b75cb4 | 395 | _state &= ~MASK_CREATE_CONNECTION; |
BartJanssens | 0:99a111b75cb4 | 396 | { |
BartJanssens | 0:99a111b75cb4 | 397 | connection_info align; |
BartJanssens | 0:99a111b75cb4 | 398 | memcpy(&align,data+2,sizeof(connection_info)); |
BartJanssens | 0:99a111b75cb4 | 399 | ConnectComplete(&align); |
BartJanssens | 0:99a111b75cb4 | 400 | Callback(CALLBACK_CONNECTION_COMPLETE,(u8*)&align,sizeof(connection_info)); |
BartJanssens | 0:99a111b75cb4 | 401 | } |
BartJanssens | 0:99a111b75cb4 | 402 | break; |
BartJanssens | 0:99a111b75cb4 | 403 | |
BartJanssens | 0:99a111b75cb4 | 404 | case HCI_EV_CONN_REQUEST: |
BartJanssens | 0:99a111b75cb4 | 405 | { |
BartJanssens | 0:99a111b75cb4 | 406 | request_info align; |
BartJanssens | 0:99a111b75cb4 | 407 | memcpy(&align,data+2,sizeof(request_info)); |
BartJanssens | 0:99a111b75cb4 | 408 | ConnectRequest(&align); |
BartJanssens | 0:99a111b75cb4 | 409 | } |
BartJanssens | 0:99a111b75cb4 | 410 | break; |
BartJanssens | 0:99a111b75cb4 | 411 | |
BartJanssens | 0:99a111b75cb4 | 412 | case HCI_EV_DISCONN_COMPLETE: |
BartJanssens | 0:99a111b75cb4 | 413 | DisconnectComplete(LE16(data+3)); |
BartJanssens | 0:99a111b75cb4 | 414 | break; |
BartJanssens | 0:99a111b75cb4 | 415 | |
BartJanssens | 0:99a111b75cb4 | 416 | case HCI_EV_REMOTE_NAME: |
BartJanssens | 0:99a111b75cb4 | 417 | { |
BartJanssens | 0:99a111b75cb4 | 418 | BD_ADDR* addr = (BD_ADDR*)(data+3); |
BartJanssens | 0:99a111b75cb4 | 419 | const char* name = (const char*)(data + 9); |
BartJanssens | 0:99a111b75cb4 | 420 | RemoteName(addr,name); |
BartJanssens | 0:99a111b75cb4 | 421 | } |
BartJanssens | 0:99a111b75cb4 | 422 | Callback(CALLBACK_REMOTE_NAME,data+3,LE16(data+1)); // addr is in here too |
BartJanssens | 0:99a111b75cb4 | 423 | _state &= ~MASK_REMOTE_NAME; |
BartJanssens | 0:99a111b75cb4 | 424 | break; |
BartJanssens | 0:99a111b75cb4 | 425 | |
BartJanssens | 0:99a111b75cb4 | 426 | case HCI_EV_CMD_STATUS: |
BartJanssens | 0:99a111b75cb4 | 427 | { |
BartJanssens | 0:99a111b75cb4 | 428 | const char* errs = HCIErrStr(data[2]); |
BartJanssens | 0:99a111b75cb4 | 429 | printf("Status %s %s\r\n",CmdStr(LE16(data+4)),errs); |
BartJanssens | 0:99a111b75cb4 | 430 | } |
BartJanssens | 0:99a111b75cb4 | 431 | break; |
BartJanssens | 0:99a111b75cb4 | 432 | |
BartJanssens | 0:99a111b75cb4 | 433 | case HCI_EV_CMD_COMPLETE: |
BartJanssens | 0:99a111b75cb4 | 434 | OnCommandComplete(data[3] | (data[4] << 8),data+6,data[1]-4); |
BartJanssens | 0:99a111b75cb4 | 435 | break; |
BartJanssens | 0:99a111b75cb4 | 436 | |
BartJanssens | 0:99a111b75cb4 | 437 | case HCI_EV_PIN_CODE_REQ: |
BartJanssens | 0:99a111b75cb4 | 438 | PinCodeReply(data+2); |
BartJanssens | 0:99a111b75cb4 | 439 | break; |
BartJanssens | 0:99a111b75cb4 | 440 | |
BartJanssens | 0:99a111b75cb4 | 441 | case HCI_EV_LINK_KEY_REQ: |
BartJanssens | 0:99a111b75cb4 | 442 | SendCmd(HCI_OP_LINK_KEY_NEG_REPLY,data+2,6); |
BartJanssens | 0:99a111b75cb4 | 443 | break; |
BartJanssens | 0:99a111b75cb4 | 444 | |
BartJanssens | 0:99a111b75cb4 | 445 | default: |
BartJanssens | 0:99a111b75cb4 | 446 | ; |
BartJanssens | 0:99a111b75cb4 | 447 | // printfBytes("HCI Event:",data,data[1]+2); |
BartJanssens | 0:99a111b75cb4 | 448 | } |
BartJanssens | 0:99a111b75cb4 | 449 | } |
BartJanssens | 0:99a111b75cb4 | 450 | |
BartJanssens | 0:99a111b75cb4 | 451 | int HCI::Open(SocketInternal* sock, SocketAddrHdr* addr) |
BartJanssens | 0:99a111b75cb4 | 452 | { |
BartJanssens | 0:99a111b75cb4 | 453 | L2CAPSocket* l2capsock = (L2CAPSocket*)sock; |
BartJanssens | 0:99a111b75cb4 | 454 | L2CAPAddr* l2capaddr = (L2CAPAddr*)addr; |
BartJanssens | 0:99a111b75cb4 | 455 | BTDevice* bt = Find(&l2capaddr->bdaddr); |
BartJanssens | 0:99a111b75cb4 | 456 | if (!bt) |
BartJanssens | 0:99a111b75cb4 | 457 | { |
BartJanssens | 0:99a111b75cb4 | 458 | printf("Can't open l2cap %d on ",l2capaddr->psm); |
BartJanssens | 0:99a111b75cb4 | 459 | printf(&l2capaddr->bdaddr); |
BartJanssens | 0:99a111b75cb4 | 460 | printf("\r\n"); |
BartJanssens | 0:99a111b75cb4 | 461 | return ERR_HCI_DEVICE_NOT_FOUND; |
BartJanssens | 0:99a111b75cb4 | 462 | } |
BartJanssens | 0:99a111b75cb4 | 463 | l2capsock->btdevice = bt; |
BartJanssens | 0:99a111b75cb4 | 464 | return bt->Open(sock,addr); |
BartJanssens | 0:99a111b75cb4 | 465 | } |
BartJanssens | 0:99a111b75cb4 | 466 | |
BartJanssens | 0:99a111b75cb4 | 467 | int HCI::Create(SocketInternal* sock, SocketAddrHdr* addr) |
BartJanssens | 0:99a111b75cb4 | 468 | { |
BartJanssens | 0:99a111b75cb4 | 469 | L2CAPSocket* l2capsock = (L2CAPSocket*)sock; |
BartJanssens | 0:99a111b75cb4 | 470 | L2CAPAddr* l2capaddr = (L2CAPAddr*)addr; |
BartJanssens | 0:99a111b75cb4 | 471 | BTDevice* bt = Find(&l2capaddr->bdaddr); |
BartJanssens | 0:99a111b75cb4 | 472 | if (!bt) |
BartJanssens | 0:99a111b75cb4 | 473 | { |
BartJanssens | 0:99a111b75cb4 | 474 | printf("HCI Create: Can't open l2cap on "); |
BartJanssens | 0:99a111b75cb4 | 475 | printf(&l2capaddr->bdaddr); |
BartJanssens | 0:99a111b75cb4 | 476 | printf("\r\n"); |
BartJanssens | 0:99a111b75cb4 | 477 | return ERR_HCI_DEVICE_NOT_FOUND; |
BartJanssens | 0:99a111b75cb4 | 478 | } |
BartJanssens | 0:99a111b75cb4 | 479 | l2capsock->btdevice = bt; |
BartJanssens | 0:99a111b75cb4 | 480 | //return 0; |
BartJanssens | 0:99a111b75cb4 | 481 | return bt->Create(sock,addr); |
BartJanssens | 0:99a111b75cb4 | 482 | } |
BartJanssens | 0:99a111b75cb4 | 483 | |
BartJanssens | 0:99a111b75cb4 | 484 | |
BartJanssens | 0:99a111b75cb4 | 485 | int HCI::Accept(SocketInternal* sock, SocketAddrHdr* addr) |
BartJanssens | 0:99a111b75cb4 | 486 | { |
BartJanssens | 0:99a111b75cb4 | 487 | |
BartJanssens | 0:99a111b75cb4 | 488 | printf("Call to HCI Accept \r\n"); |
BartJanssens | 0:99a111b75cb4 | 489 | |
BartJanssens | 0:99a111b75cb4 | 490 | L2CAPSocket* l2capsock = (L2CAPSocket*)sock; |
BartJanssens | 0:99a111b75cb4 | 491 | L2CAPAddr* l2capaddr = (L2CAPAddr*)addr; |
BartJanssens | 0:99a111b75cb4 | 492 | BTDevice* bt = Find(&l2capaddr->bdaddr); |
BartJanssens | 0:99a111b75cb4 | 493 | if (!bt) |
BartJanssens | 0:99a111b75cb4 | 494 | { |
BartJanssens | 0:99a111b75cb4 | 495 | //printf("Can't open l2cap %d on ",l2capaddr->psm); |
BartJanssens | 0:99a111b75cb4 | 496 | printf("HCI Accept: Can't open l2cap on "); |
BartJanssens | 0:99a111b75cb4 | 497 | printf(&l2capaddr->bdaddr); |
BartJanssens | 0:99a111b75cb4 | 498 | printf("\r\n"); |
BartJanssens | 0:99a111b75cb4 | 499 | return ERR_HCI_DEVICE_NOT_FOUND; |
BartJanssens | 0:99a111b75cb4 | 500 | } |
BartJanssens | 0:99a111b75cb4 | 501 | //l2capsock->btdevice = bt; |
BartJanssens | 0:99a111b75cb4 | 502 | return bt->Open(sock,addr); |
BartJanssens | 0:99a111b75cb4 | 503 | |
BartJanssens | 0:99a111b75cb4 | 504 | } |
BartJanssens | 0:99a111b75cb4 | 505 | |
BartJanssens | 0:99a111b75cb4 | 506 | /** |
BartJanssens | 0:99a111b75cb4 | 507 | SocketInternal* HCI::Create(SocketInternal* sock) |
BartJanssens | 0:99a111b75cb4 | 508 | { |
BartJanssens | 0:99a111b75cb4 | 509 | return sock; |
BartJanssens | 0:99a111b75cb4 | 510 | } |
BartJanssens | 0:99a111b75cb4 | 511 | **/ |
BartJanssens | 0:99a111b75cb4 | 512 | |
BartJanssens | 0:99a111b75cb4 | 513 | |
BartJanssens | 0:99a111b75cb4 | 514 | int HCI::Send(SocketInternal* sock, const u8* data, int len) |
BartJanssens | 0:99a111b75cb4 | 515 | { |
BartJanssens | 0:99a111b75cb4 | 516 | L2CAPSocket* l2capsock = (L2CAPSocket*)sock; |
BartJanssens | 0:99a111b75cb4 | 517 | return l2capsock->btdevice->Send(sock,data,len); // Pointless double dispatch |
BartJanssens | 0:99a111b75cb4 | 518 | } |
BartJanssens | 0:99a111b75cb4 | 519 | |
BartJanssens | 0:99a111b75cb4 | 520 | int HCI::Close(SocketInternal* sock) |
BartJanssens | 0:99a111b75cb4 | 521 | { |
BartJanssens | 0:99a111b75cb4 | 522 | L2CAPSocket* l2capsock = (L2CAPSocket*)sock; |
BartJanssens | 0:99a111b75cb4 | 523 | return l2capsock->btdevice->Close(sock); // Pointless double dispatch |
BartJanssens | 0:99a111b75cb4 | 524 | } |
BartJanssens | 0:99a111b75cb4 | 525 | |
BartJanssens | 0:99a111b75cb4 | 526 | void HCI::ACLRecv(const u8* data, int len) |
BartJanssens | 0:99a111b75cb4 | 527 | { |
BartJanssens | 0:99a111b75cb4 | 528 | int handle = LE16(data); |
BartJanssens | 0:99a111b75cb4 | 529 | BD_ADDR* addr; |
BartJanssens | 0:99a111b75cb4 | 530 | BTDevice* d = Find(handle & 0x0FFF); |
BartJanssens | 0:99a111b75cb4 | 531 | addr = &d->_info.bdaddr; |
BartJanssens | 0:99a111b75cb4 | 532 | |
BartJanssens | 0:99a111b75cb4 | 533 | if (d) |
BartJanssens | 0:99a111b75cb4 | 534 | d->ACLRecv(addr,data,len); |
BartJanssens | 0:99a111b75cb4 | 535 | } |
BartJanssens | 0:99a111b75cb4 | 536 | |
BartJanssens | 0:99a111b75cb4 | 537 | //=================================================================== |
BartJanssens | 0:99a111b75cb4 | 538 | //=================================================================== |