Dependencies:   mbed

Committer:
abe00makoto
Date:
Thu May 26 19:39:37 2011 +0000
Revision:
1:237cfff63ef8
Parent:
0:e939856c1939

        

Who changed what in which revision?

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