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