BlackOneとAndroidの連携デモプログラム AndroidAccessoryを改造してBlackOneとAndroidが連携できるようにしました。 サポートしているのは、デモアプリの ”Buttons” B1-SW1, B2-SW2, B3-SW3 ”LED2” RGB-LED のみです。 LCDに表示するイメージをマイクロSDカードに入れてLCDのソケットに挿入しておく必要があります。 イメージは、320X240ドットで”\Image”という名前のフォルダの直下に”10.jpg”という名前で入れてください。

Dependencies:   TextLCD mbed

Committer:
techand
Date:
Fri Dec 23 04:33:33 2011 +0000
Revision:
0:7b556109fd46

        

Who changed what in which revision?

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