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