主導機 mbed用のプログラムです

Dependencies:   mbed

Fork of F3RC_syudou_master by F3RC1班

Committer:
hirokimineshita
Date:
Thu Apr 30 05:59:05 2015 +0000
Revision:
0:736c76a75def
2015/04/30 PM 2 upload

Who changed what in which revision?

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