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