手動機アーム、mbed基盤のspiをおくるだけのプログラムです(9/4)

Dependencies:   SPI_master_arm_shudouki mbed

Fork of SPI_master_arm_shudouki2 by F^3 RC 2班

Committer:
yoka06
Date:
Mon Aug 21 08:49:06 2017 +0000
Revision:
0:76d1c7f13415
a

Who changed what in which revision?

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