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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hci.h Source File

hci.h

00001 /*
00002 Copyright (c) 2010 Peter Barrett
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 #ifndef HCI_H_INCLUDED
00024 #define HCI_H_INCLUDED
00025 
00026 #include "Socket.h"
00027 
00028 #pragma pack(1)
00029 
00030 #define ERR_HCI_DEVICE_NOT_FOUND -300
00031 
00032 class HCI;
00033 class HCITransport;
00034 class BTDevice;
00035 
00036 typedef struct
00037 {
00038     u8  addr[6];
00039 } BD_ADDR;
00040 
00041 typedef struct
00042 {
00043     BD_ADDR bdaddr;
00044     u8  pscan_rep_mode;
00045     u8  pscan_period_mode;
00046     u8  pscan_mode;
00047     u8  dev_class[3];
00048     u16 clock_offset;
00049 } inquiry_info;
00050 
00051 typedef struct
00052 {
00053     BD_ADDR bdaddr;
00054     u8  dev_class[3];
00055     u8  link_type;   
00056 } request_info;
00057 
00058 typedef struct
00059 {
00060     u8  status;
00061     u16 handle;
00062     BD_ADDR bdaddr;
00063     u8  link_type;
00064     u8  encr_mode;
00065 } connection_info;
00066 
00067 //  Address struct for creating L2CAP sockets
00068 typedef struct {
00069     SocketAddrHdr hdr;
00070     BD_ADDR bdaddr;
00071     u16 psm;
00072 } L2CAPAddr;
00073 
00074 #pragma pack(4)
00075 
00076 class BTDevice;
00077 typedef struct
00078 {
00079     public:
00080     SocketInternal si;
00081     BTDevice* btdevice;
00082     u16 scid;
00083     u16 dcid;
00084 } L2CAPSocket;
00085 
00086 #define MAX_HCL_NAME_LENGTH 20  // TODO - BTDevice wants to be a multiple of 4
00087 #define MAX_PORTS 3
00088 
00089 //  BTDevice encapsulates individual device state
00090 //  It provides L2CAP layer sockets
00091 
00092 class BTDevice : public SocketHandler
00093 {
00094     public:
00095     HCITransport* _transport;
00096     inquiry_info  _info;
00097     u16 _handle;     // acl connection handle
00098     u8  _state;      // connection state
00099     u8  _txid;
00100     char   _name[MAX_HCL_NAME_LENGTH];
00101 
00102     void Init();
00103 
00104     BD_ADDR* GetAddress() { return &_info.bdaddr; }
00105 
00106     //  Called from HCI
00107     void ACLRecv(const BD_ADDR* addr, const u8* data, int len);
00108 
00109     // SocketHandler
00110     virtual int Open(SocketInternal* sock, SocketAddrHdr* addr);
00111     virtual int Send(SocketInternal* sock, const u8* data, int len);
00112     virtual int Close(SocketInternal* sock);
00113     // added by Bart Janssens
00114     virtual int Create(SocketInternal* sock, SocketAddrHdr* addr);
00115     virtual int Accept(SocketInternal* sock, SocketAddrHdr* addr);
00116 
00117 private:
00118     L2CAPSocket* SCIDToSocket(int scid);
00119     int Send(const u8* data, int len);
00120     int Send(u8 c, u8 id, u16* params, int count);
00121     int Connect(int scid, int psm);
00122     int Disconnect(int scid, int dcid);
00123     int ConfigureRequest(int dcid);
00124     int ConfigureResponse(u8 rxid, int dcid);
00125     int DisconnectResponse(u8 rxid, int scid, int dcid);
00126     // added by Bart Janssens
00127     int AcceptResponse(u8 rxid, int scid, int dcid);
00128     int RefuseResponse(u8 rxid);
00129     //
00130     void Control(const BD_ADDR* addr, const u8* data, int len);
00131 };
00132 
00133 enum HCI_CALLBACK_EVENT
00134 {
00135     CALLBACK_NONE,
00136     CALLBACK_READY,
00137     CALLBACK_INQUIRY_RESULT,
00138     CALLBACK_INQUIRY_DONE,
00139     CALLBACK_REMOTE_NAME,
00140     CALLBACK_CONNECTION_COMPLETE,
00141     CALLBACK_CONNECTION_FAILED
00142 };
00143 
00144 //  L2CAP Protocol/Service Multiplexor (PSM) values
00145 
00146 #define L2CAP_PSM_ANY                   0x0000  /* Any/Invalid PSM */
00147 #define L2CAP_PSM_SDP                   0x0001  /* Service Discovery Protocol */
00148 #define L2CAP_PSM_RFCOMM                0x0003  /* RFCOMM protocol */
00149 #define L2CAP_PSM_TCP                   0x0005  /* Telephony Control Protocol */
00150 #define L2CAP_PSM_TCS                   0x0007  /* TCS cordless */
00151 #define L2CAP_PSM_BNEP                  0x000f  /* Bluetooth Network Encapsulation Protocol*/
00152 #define L2CAP_PSM_HID_CNTL              0x0011  /* HID Control */
00153 #define L2CAP_PSM_HID_INTR              0x0013  /* HID Interrupt */
00154 #define L2CAP_PSM_ESDP                  0x0015  /* Extended Service Discovery Profile */
00155 #define L2CAP_PSM_AVCTP                 0x0017  /* Audio/Visual Control Transport Protocol */
00156 #define L2CAP_PSM_AVDTP                 0x0019  /* Audio/Visual Distribution */
00157 
00158 /* Connection response results */
00159 #define L2CAP_CONN_SUCCESS 0x0000
00160 #define L2CAP_CONN_PND 0x0001
00161 #define L2CAP_CONN_REF_PSM 0x0002
00162 #define L2CAP_CONN_REF_SEC 0x0003
00163 #define L2CAP_CONN_REF_RES 0x0004
00164 #define L2CAP_CONN_CFG_TO 0x0005 /* Implementation specific result */
00165 
00166 
00167 
00168 //  Callback from inquiry
00169 typedef int (*HCICallback)(HCI* hci, HCI_CALLBACK_EVENT evt, const u8* data, int len);
00170 
00171 #define MAX_BTDEVICES 8
00172 
00173 class HCITransport;
00174 class HCI : public SocketHandler
00175 {
00176     HCITransport* _transport;
00177     HCICallback _callback;
00178     BD_ADDR  _localAddr;
00179 
00180     BTDevice _devices[MAX_BTDEVICES];
00181     int _deviceCount;
00182 
00183     int _acl_mtu;
00184     int _acl_max_pkt;
00185     int _sco_mtu;
00186     int _sco_max_pkt;
00187 
00188     int _state;
00189 
00190     public:
00191 
00192     //  Open a local adapter
00193     int Open(HCITransport* transport, HCICallback callback);
00194 
00195     //  Return list of discovered addreses
00196     int GetDevices(BTDevice** devices, int maxDevices);
00197 
00198     //  Lookup a device by address or handle
00199     BTDevice* Find(const BD_ADDR* addr);
00200     BTDevice* Find(int handle);
00201 
00202     //  Disconnect from a remote device
00203     int Disconnect(const BD_ADDR* addr);
00204     int DisconnectAll();
00205 
00206     //  see what devies are in the system
00207     int Inquiry(int duration = 10);
00208     
00209     // added by Bart Janssens
00210     int WriteScanEnable();
00211     
00212     int AcceptConnection(const BD_ADDR* addr);
00213 
00214     //  get a name, delivered in callback
00215     int RemoteNameRequest(const BD_ADDR* addr);
00216 
00217     //  Connect to a remote device
00218     int CreateConnection(const BD_ADDR* remoteAddr);
00219 
00220     bool Busy();
00221 
00222     //  called from transport
00223     void HCIRecv(const u8* data, int len);
00224 
00225     //  called from transport
00226     void ACLRecv(const u8* data, int len);
00227 
00228     //  SocketHandler methods for maintaining L2CAP sockets
00229     virtual int Open(SocketInternal* sock, SocketAddrHdr* addr);
00230     virtual int Send(SocketInternal* sock, const u8* data, int len);
00231     virtual int Close(SocketInternal* sock);
00232     // added by Bart Janssens
00233     virtual int Create(SocketInternal* sock, SocketAddrHdr* addr);
00234     virtual int Accept(SocketInternal* sock, SocketAddrHdr* addr);
00235 
00236     private:
00237     void    InquiryResult(const inquiry_info* info);
00238     void    RemoteName(const BD_ADDR* addr, const char* name);
00239     void    ConnectComplete(const connection_info* info);
00240     void    ConnectRequest(const request_info* info);
00241     void    DisconnectComplete(int handle);
00242     int     SendCmd(int cmd, const u8* params = 0, int len = 0);
00243     void    OnCommandComplete(int cmd, const u8* data, int len);
00244     void    Callback(HCI_CALLBACK_EVENT c, const u8* data, int len);
00245     int     PinCodeReply(const u8* data);
00246 };
00247 
00248 class HCITransport
00249 {
00250 protected:
00251     HCI* _target;
00252 public:
00253     void Set(HCI* target) { _target = target; };
00254     virtual void HCISend(const u8* data, int len) = 0;
00255     virtual void ACLSend(const u8* data, int len) = 0;
00256 };
00257 
00258 #endif