![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is the project for the Old Model Robots for OU's Dr. Davis's Configurable Robots Research. This is being published so future robots can be set up easily.
Dependencies: FatFileSystem MCP3008 Motor PinDetect QTR_8A SRF05 SSD1308_128x64_I2C mbed
blueusb/hci.h@0:bcad524c1856, 2017-11-01 (annotated)
- Committer:
- DrewSchaef
- Date:
- Wed Nov 01 15:57:59 2017 +0000
- Revision:
- 0:bcad524c1856
Published the project to allow access for future work on the Configurable Robots Research Project(s).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DrewSchaef | 0:bcad524c1856 | 1 | /* |
DrewSchaef | 0:bcad524c1856 | 2 | Copyright (c) 2010 Peter Barrett |
DrewSchaef | 0:bcad524c1856 | 3 | |
DrewSchaef | 0:bcad524c1856 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
DrewSchaef | 0:bcad524c1856 | 5 | of this software and associated documentation files (the "Software"), to deal |
DrewSchaef | 0:bcad524c1856 | 6 | in the Software without restriction, including without limitation the rights |
DrewSchaef | 0:bcad524c1856 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
DrewSchaef | 0:bcad524c1856 | 8 | copies of the Software, and to permit persons to whom the Software is |
DrewSchaef | 0:bcad524c1856 | 9 | furnished to do so, subject to the following conditions: |
DrewSchaef | 0:bcad524c1856 | 10 | |
DrewSchaef | 0:bcad524c1856 | 11 | The above copyright notice and this permission notice shall be included in |
DrewSchaef | 0:bcad524c1856 | 12 | all copies or substantial portions of the Software. |
DrewSchaef | 0:bcad524c1856 | 13 | |
DrewSchaef | 0:bcad524c1856 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
DrewSchaef | 0:bcad524c1856 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
DrewSchaef | 0:bcad524c1856 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
DrewSchaef | 0:bcad524c1856 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
DrewSchaef | 0:bcad524c1856 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
DrewSchaef | 0:bcad524c1856 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
DrewSchaef | 0:bcad524c1856 | 20 | THE SOFTWARE. |
DrewSchaef | 0:bcad524c1856 | 21 | */ |
DrewSchaef | 0:bcad524c1856 | 22 | |
DrewSchaef | 0:bcad524c1856 | 23 | #ifndef HCI_H_INCLUDED |
DrewSchaef | 0:bcad524c1856 | 24 | #define HCI_H_INCLUDED |
DrewSchaef | 0:bcad524c1856 | 25 | |
DrewSchaef | 0:bcad524c1856 | 26 | #include "Socket.h" |
DrewSchaef | 0:bcad524c1856 | 27 | |
DrewSchaef | 0:bcad524c1856 | 28 | #pragma pack(1) |
DrewSchaef | 0:bcad524c1856 | 29 | |
DrewSchaef | 0:bcad524c1856 | 30 | #define ERR_HCI_DEVICE_NOT_FOUND -300 |
DrewSchaef | 0:bcad524c1856 | 31 | |
DrewSchaef | 0:bcad524c1856 | 32 | class HCI; |
DrewSchaef | 0:bcad524c1856 | 33 | class HCITransport; |
DrewSchaef | 0:bcad524c1856 | 34 | class BTDevice; |
DrewSchaef | 0:bcad524c1856 | 35 | |
DrewSchaef | 0:bcad524c1856 | 36 | typedef struct |
DrewSchaef | 0:bcad524c1856 | 37 | { |
DrewSchaef | 0:bcad524c1856 | 38 | u8 addr[6]; |
DrewSchaef | 0:bcad524c1856 | 39 | } BD_ADDR; |
DrewSchaef | 0:bcad524c1856 | 40 | |
DrewSchaef | 0:bcad524c1856 | 41 | typedef struct |
DrewSchaef | 0:bcad524c1856 | 42 | { |
DrewSchaef | 0:bcad524c1856 | 43 | BD_ADDR bdaddr; |
DrewSchaef | 0:bcad524c1856 | 44 | u8 pscan_rep_mode; |
DrewSchaef | 0:bcad524c1856 | 45 | u8 pscan_period_mode; |
DrewSchaef | 0:bcad524c1856 | 46 | u8 pscan_mode; |
DrewSchaef | 0:bcad524c1856 | 47 | u8 dev_class[3]; |
DrewSchaef | 0:bcad524c1856 | 48 | u16 clock_offset; |
DrewSchaef | 0:bcad524c1856 | 49 | } inquiry_info; |
DrewSchaef | 0:bcad524c1856 | 50 | |
DrewSchaef | 0:bcad524c1856 | 51 | typedef struct |
DrewSchaef | 0:bcad524c1856 | 52 | { |
DrewSchaef | 0:bcad524c1856 | 53 | u8 status; |
DrewSchaef | 0:bcad524c1856 | 54 | u16 handle; |
DrewSchaef | 0:bcad524c1856 | 55 | BD_ADDR bdaddr; |
DrewSchaef | 0:bcad524c1856 | 56 | u8 link_type; |
DrewSchaef | 0:bcad524c1856 | 57 | u8 encr_mode; |
DrewSchaef | 0:bcad524c1856 | 58 | } connection_info; |
DrewSchaef | 0:bcad524c1856 | 59 | |
DrewSchaef | 0:bcad524c1856 | 60 | // Address struct for creating L2CAP sockets |
DrewSchaef | 0:bcad524c1856 | 61 | typedef struct { |
DrewSchaef | 0:bcad524c1856 | 62 | SocketAddrHdr hdr; |
DrewSchaef | 0:bcad524c1856 | 63 | BD_ADDR bdaddr; |
DrewSchaef | 0:bcad524c1856 | 64 | u16 psm; |
DrewSchaef | 0:bcad524c1856 | 65 | } L2CAPAddr; |
DrewSchaef | 0:bcad524c1856 | 66 | |
DrewSchaef | 0:bcad524c1856 | 67 | #pragma pack(4) |
DrewSchaef | 0:bcad524c1856 | 68 | |
DrewSchaef | 0:bcad524c1856 | 69 | class BTDevice; |
DrewSchaef | 0:bcad524c1856 | 70 | typedef struct |
DrewSchaef | 0:bcad524c1856 | 71 | { |
DrewSchaef | 0:bcad524c1856 | 72 | public: |
DrewSchaef | 0:bcad524c1856 | 73 | SocketInternal si; |
DrewSchaef | 0:bcad524c1856 | 74 | BTDevice* btdevice; |
DrewSchaef | 0:bcad524c1856 | 75 | u16 scid; |
DrewSchaef | 0:bcad524c1856 | 76 | u16 dcid; |
DrewSchaef | 0:bcad524c1856 | 77 | } L2CAPSocket; |
DrewSchaef | 0:bcad524c1856 | 78 | |
DrewSchaef | 0:bcad524c1856 | 79 | #define MAX_HCL_NAME_LENGTH 20 // TODO - BTDevice wants to be a multiple of 4 |
DrewSchaef | 0:bcad524c1856 | 80 | |
DrewSchaef | 0:bcad524c1856 | 81 | // BTDevice encapsulates individual device state |
DrewSchaef | 0:bcad524c1856 | 82 | // It provides L2CAP layer sockets |
DrewSchaef | 0:bcad524c1856 | 83 | |
DrewSchaef | 0:bcad524c1856 | 84 | class BTDevice : public SocketHandler |
DrewSchaef | 0:bcad524c1856 | 85 | { |
DrewSchaef | 0:bcad524c1856 | 86 | public: |
DrewSchaef | 0:bcad524c1856 | 87 | HCITransport* _transport; |
DrewSchaef | 0:bcad524c1856 | 88 | inquiry_info _info; |
DrewSchaef | 0:bcad524c1856 | 89 | u16 _handle; // acl connection handle |
DrewSchaef | 0:bcad524c1856 | 90 | u8 _state; // connection state |
DrewSchaef | 0:bcad524c1856 | 91 | u8 _txid; |
DrewSchaef | 0:bcad524c1856 | 92 | char _name[MAX_HCL_NAME_LENGTH]; |
DrewSchaef | 0:bcad524c1856 | 93 | |
DrewSchaef | 0:bcad524c1856 | 94 | void Init(); |
DrewSchaef | 0:bcad524c1856 | 95 | |
DrewSchaef | 0:bcad524c1856 | 96 | BD_ADDR* GetAddress() { return &_info.bdaddr; } |
DrewSchaef | 0:bcad524c1856 | 97 | |
DrewSchaef | 0:bcad524c1856 | 98 | // Called from HCI |
DrewSchaef | 0:bcad524c1856 | 99 | void ACLRecv(const u8* data, int len); |
DrewSchaef | 0:bcad524c1856 | 100 | |
DrewSchaef | 0:bcad524c1856 | 101 | // SocketHandler |
DrewSchaef | 0:bcad524c1856 | 102 | virtual int Open(SocketInternal* sock, SocketAddrHdr* addr); |
DrewSchaef | 0:bcad524c1856 | 103 | virtual int Send(SocketInternal* sock, const u8* data, int len); |
DrewSchaef | 0:bcad524c1856 | 104 | virtual int Close(SocketInternal* sock); |
DrewSchaef | 0:bcad524c1856 | 105 | |
DrewSchaef | 0:bcad524c1856 | 106 | private: |
DrewSchaef | 0:bcad524c1856 | 107 | L2CAPSocket* SCIDToSocket(int scid); |
DrewSchaef | 0:bcad524c1856 | 108 | int Send(const u8* data, int len); |
DrewSchaef | 0:bcad524c1856 | 109 | int Send(u8 c, u8 id, u16* params, int count); |
DrewSchaef | 0:bcad524c1856 | 110 | int Connect(int scid, int psm); |
DrewSchaef | 0:bcad524c1856 | 111 | int Disconnect(int scid, int dcid); |
DrewSchaef | 0:bcad524c1856 | 112 | int ConfigureRequest(int dcid); |
DrewSchaef | 0:bcad524c1856 | 113 | int ConfigureResponse(u8 rxid, int dcid); |
DrewSchaef | 0:bcad524c1856 | 114 | int DisconnectResponse(u8 rxid, int scid, int dcid); |
DrewSchaef | 0:bcad524c1856 | 115 | void Control(const u8* data, int len); |
DrewSchaef | 0:bcad524c1856 | 116 | }; |
DrewSchaef | 0:bcad524c1856 | 117 | |
DrewSchaef | 0:bcad524c1856 | 118 | enum HCI_CALLBACK_EVENT |
DrewSchaef | 0:bcad524c1856 | 119 | { |
DrewSchaef | 0:bcad524c1856 | 120 | CALLBACK_NONE, |
DrewSchaef | 0:bcad524c1856 | 121 | CALLBACK_READY, |
DrewSchaef | 0:bcad524c1856 | 122 | CALLBACK_INQUIRY_RESULT, |
DrewSchaef | 0:bcad524c1856 | 123 | CALLBACK_INQUIRY_DONE, |
DrewSchaef | 0:bcad524c1856 | 124 | CALLBACK_REMOTE_NAME, |
DrewSchaef | 0:bcad524c1856 | 125 | CALLBACK_CONNECTION_COMPLETE, |
DrewSchaef | 0:bcad524c1856 | 126 | CALLBACK_CONNECTION_FAILED |
DrewSchaef | 0:bcad524c1856 | 127 | }; |
DrewSchaef | 0:bcad524c1856 | 128 | |
DrewSchaef | 0:bcad524c1856 | 129 | // L2CAP Protocol/Service Multiplexor (PSM) values |
DrewSchaef | 0:bcad524c1856 | 130 | |
DrewSchaef | 0:bcad524c1856 | 131 | #define L2CAP_PSM_ANY 0x0000 /* Any/Invalid PSM */ |
DrewSchaef | 0:bcad524c1856 | 132 | #define L2CAP_PSM_SDP 0x0001 /* Service Discovery Protocol */ |
DrewSchaef | 0:bcad524c1856 | 133 | #define L2CAP_PSM_RFCOMM 0x0003 /* RFCOMM protocol */ |
DrewSchaef | 0:bcad524c1856 | 134 | #define L2CAP_PSM_TCP 0x0005 /* Telephony Control Protocol */ |
DrewSchaef | 0:bcad524c1856 | 135 | #define L2CAP_PSM_TCS 0x0007 /* TCS cordless */ |
DrewSchaef | 0:bcad524c1856 | 136 | #define L2CAP_PSM_BNEP 0x000f /* Bluetooth Network Encapsulation Protocol*/ |
DrewSchaef | 0:bcad524c1856 | 137 | #define L2CAP_PSM_HID_CNTL 0x0011 /* HID Control */ |
DrewSchaef | 0:bcad524c1856 | 138 | #define L2CAP_PSM_HID_INTR 0x0013 /* HID Interrupt */ |
DrewSchaef | 0:bcad524c1856 | 139 | #define L2CAP_PSM_ESDP 0x0015 /* Extended Service Discovery Profile */ |
DrewSchaef | 0:bcad524c1856 | 140 | #define L2CAP_PSM_AVCTP 0x0017 /* Audio/Visual Control Transport Protocol */ |
DrewSchaef | 0:bcad524c1856 | 141 | #define L2CAP_PSM_AVDTP 0x0019 /* Audio/Visual Distribution */ |
DrewSchaef | 0:bcad524c1856 | 142 | |
DrewSchaef | 0:bcad524c1856 | 143 | // Callback from inquiry |
DrewSchaef | 0:bcad524c1856 | 144 | typedef int (*HCICallback)(HCI* hci, HCI_CALLBACK_EVENT evt, const u8* data, int len); |
DrewSchaef | 0:bcad524c1856 | 145 | |
DrewSchaef | 0:bcad524c1856 | 146 | #define MAX_BTDEVICES 8 |
DrewSchaef | 0:bcad524c1856 | 147 | |
DrewSchaef | 0:bcad524c1856 | 148 | class HCITransport; |
DrewSchaef | 0:bcad524c1856 | 149 | class HCI : public SocketHandler |
DrewSchaef | 0:bcad524c1856 | 150 | { |
DrewSchaef | 0:bcad524c1856 | 151 | HCITransport* _transport; |
DrewSchaef | 0:bcad524c1856 | 152 | HCICallback _callback; |
DrewSchaef | 0:bcad524c1856 | 153 | BD_ADDR _localAddr; |
DrewSchaef | 0:bcad524c1856 | 154 | |
DrewSchaef | 0:bcad524c1856 | 155 | BTDevice _devices[MAX_BTDEVICES]; |
DrewSchaef | 0:bcad524c1856 | 156 | int _deviceCount; |
DrewSchaef | 0:bcad524c1856 | 157 | |
DrewSchaef | 0:bcad524c1856 | 158 | int _acl_mtu; |
DrewSchaef | 0:bcad524c1856 | 159 | int _acl_max_pkt; |
DrewSchaef | 0:bcad524c1856 | 160 | int _sco_mtu; |
DrewSchaef | 0:bcad524c1856 | 161 | int _sco_max_pkt; |
DrewSchaef | 0:bcad524c1856 | 162 | |
DrewSchaef | 0:bcad524c1856 | 163 | int _state; |
DrewSchaef | 0:bcad524c1856 | 164 | |
DrewSchaef | 0:bcad524c1856 | 165 | public: |
DrewSchaef | 0:bcad524c1856 | 166 | |
DrewSchaef | 0:bcad524c1856 | 167 | // Open a local adapter |
DrewSchaef | 0:bcad524c1856 | 168 | int Open(HCITransport* transport, HCICallback callback); |
DrewSchaef | 0:bcad524c1856 | 169 | |
DrewSchaef | 0:bcad524c1856 | 170 | // Return list of discovered addreses |
DrewSchaef | 0:bcad524c1856 | 171 | int GetDevices(BTDevice** devices, int maxDevices); |
DrewSchaef | 0:bcad524c1856 | 172 | |
DrewSchaef | 0:bcad524c1856 | 173 | // Lookup a device by address or handle |
DrewSchaef | 0:bcad524c1856 | 174 | BTDevice* Find(const BD_ADDR* addr); |
DrewSchaef | 0:bcad524c1856 | 175 | BTDevice* Find(int handle); |
DrewSchaef | 0:bcad524c1856 | 176 | |
DrewSchaef | 0:bcad524c1856 | 177 | // Disconnect from a remote device |
DrewSchaef | 0:bcad524c1856 | 178 | int Disconnect(const BD_ADDR* addr); |
DrewSchaef | 0:bcad524c1856 | 179 | int DisconnectAll(); |
DrewSchaef | 0:bcad524c1856 | 180 | |
DrewSchaef | 0:bcad524c1856 | 181 | // see what devies are in the system |
DrewSchaef | 0:bcad524c1856 | 182 | int Inquiry(int duration = 10); |
DrewSchaef | 0:bcad524c1856 | 183 | |
DrewSchaef | 0:bcad524c1856 | 184 | // get a name, delivered in callback |
DrewSchaef | 0:bcad524c1856 | 185 | int RemoteNameRequest(const BD_ADDR* addr); |
DrewSchaef | 0:bcad524c1856 | 186 | |
DrewSchaef | 0:bcad524c1856 | 187 | // Connect to a remote device |
DrewSchaef | 0:bcad524c1856 | 188 | int CreateConnection(const BD_ADDR* remoteAddr); |
DrewSchaef | 0:bcad524c1856 | 189 | |
DrewSchaef | 0:bcad524c1856 | 190 | bool Busy(); |
DrewSchaef | 0:bcad524c1856 | 191 | |
DrewSchaef | 0:bcad524c1856 | 192 | // called from transport |
DrewSchaef | 0:bcad524c1856 | 193 | void HCIRecv(const u8* data, int len); |
DrewSchaef | 0:bcad524c1856 | 194 | |
DrewSchaef | 0:bcad524c1856 | 195 | // called from transport |
DrewSchaef | 0:bcad524c1856 | 196 | void ACLRecv(const u8* data, int len); |
DrewSchaef | 0:bcad524c1856 | 197 | |
DrewSchaef | 0:bcad524c1856 | 198 | // SocketHandler methods for maintaining L2CAP sockets |
DrewSchaef | 0:bcad524c1856 | 199 | virtual int Open(SocketInternal* sock, SocketAddrHdr* addr); |
DrewSchaef | 0:bcad524c1856 | 200 | virtual int Send(SocketInternal* sock, const u8* data, int len); |
DrewSchaef | 0:bcad524c1856 | 201 | virtual int Close(SocketInternal* sock); |
DrewSchaef | 0:bcad524c1856 | 202 | |
DrewSchaef | 0:bcad524c1856 | 203 | private: |
DrewSchaef | 0:bcad524c1856 | 204 | void InquiryResult(const inquiry_info* info); |
DrewSchaef | 0:bcad524c1856 | 205 | void RemoteName(const BD_ADDR* addr, const char* name); |
DrewSchaef | 0:bcad524c1856 | 206 | void ConnectComplete(const connection_info* info); |
DrewSchaef | 0:bcad524c1856 | 207 | void DisconnectComplete(int handle); |
DrewSchaef | 0:bcad524c1856 | 208 | int SendCmd(int cmd, const u8* params = 0, int len = 0); |
DrewSchaef | 0:bcad524c1856 | 209 | void OnCommandComplete(int cmd, const u8* data, int len); |
DrewSchaef | 0:bcad524c1856 | 210 | void Callback(HCI_CALLBACK_EVENT c, const u8* data, int len); |
DrewSchaef | 0:bcad524c1856 | 211 | int PinCodeReply(const u8* data); |
DrewSchaef | 0:bcad524c1856 | 212 | }; |
DrewSchaef | 0:bcad524c1856 | 213 | |
DrewSchaef | 0:bcad524c1856 | 214 | class HCITransport |
DrewSchaef | 0:bcad524c1856 | 215 | { |
DrewSchaef | 0:bcad524c1856 | 216 | protected: |
DrewSchaef | 0:bcad524c1856 | 217 | HCI* _target; |
DrewSchaef | 0:bcad524c1856 | 218 | public: |
DrewSchaef | 0:bcad524c1856 | 219 | void Set(HCI* target) { _target = target; }; |
DrewSchaef | 0:bcad524c1856 | 220 | virtual void HCISend(const u8* data, int len) = 0; |
DrewSchaef | 0:bcad524c1856 | 221 | virtual void ACLSend(const u8* data, int len) = 0; |
DrewSchaef | 0:bcad524c1856 | 222 | }; |
DrewSchaef | 0:bcad524c1856 | 223 | |
DrewSchaef | 0:bcad524c1856 | 224 | #endif |