yoshinari kou / Mbed 2 deprecated BlueUSB_debug

Dependencies:   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 //bool SetLocalName_complite = false;
00033 
00034 class HCI;
00035 class HCITransport;
00036 class BTDevice;
00037 
00038 typedef struct
00039 {
00040     u8  addr[6];
00041 } BD_ADDR;
00042 
00043 typedef struct
00044 {
00045     BD_ADDR bdaddr;
00046     u8  pscan_rep_mode;
00047     u8  pscan_period_mode;
00048     u8  pscan_mode;
00049     u8  dev_class[3];
00050     u16 clock_offset;
00051 } inquiry_info;
00052 
00053 typedef struct
00054 {
00055     u8  status;
00056     u16 handle;
00057     BD_ADDR bdaddr;
00058     u8  link_type;
00059     u8  encr_mode;
00060 } connection_info;
00061 
00062 //  Address struct for creating L2CAP sockets
00063 typedef struct {
00064     SocketAddrHdr hdr;
00065     BD_ADDR bdaddr;
00066     u16 psm;
00067 } L2CAPAddr;
00068 
00069 #pragma pack(4)
00070 
00071 class BTDevice;
00072 typedef struct
00073 {
00074     public:
00075     SocketInternal si;
00076     BTDevice* btdevice;
00077     u16 scid;
00078     u16 dcid;
00079 } L2CAPSocket;
00080 
00081 #define MAX_HCL_NAME_LENGTH 20  // TODO - BTDevice wants to be a multiple of 4
00082 
00083 //  BTDevice encapsulates individual device state
00084 //  It provides L2CAP layer sockets
00085 
00086 class BTDevice : public SocketHandler
00087 {
00088     public:
00089     HCITransport* _transport;
00090     inquiry_info  _info;
00091     u16 _handle;     // acl connection handle
00092     u8  _state;      // connection state
00093     u8  _txid;
00094     char   _name[MAX_HCL_NAME_LENGTH];
00095 
00096     void Init();
00097 
00098     BD_ADDR* GetAddress() { return &_info.bdaddr; }
00099 
00100     //  Called from HCI
00101     void ACLRecv(const u8* data, int len);
00102 
00103     // SocketHandler
00104     virtual int Open(SocketInternal* sock, SocketAddrHdr* addr);
00105     virtual int Send(SocketInternal* sock, const u8* data, int len);
00106     virtual int Close(SocketInternal* sock);
00107 
00108 private:
00109     L2CAPSocket* SCIDToSocket(int scid);
00110     int Send(const u8* data, int len);
00111     int Send(u8 c, u8 id, u16* params, int count);
00112     int Connect(int scid, int psm);
00113     int Disconnect(int scid, int dcid);
00114     int ConfigureRequest(int dcid);
00115     int ConfigureResponse(u8 rxid, int dcid);
00116     int DisconnectResponse(u8 rxid, int scid, int dcid);
00117     void Control(const u8* data, int len);
00118 };
00119 
00120 enum HCI_CALLBACK_EVENT
00121 {
00122     CALLBACK_NONE,
00123     CALLBACK_READY,
00124     CALLBACK_INQUIRY_RESULT,
00125     CALLBACK_INQUIRY_DONE,
00126     CALLBACK_REMOTE_NAME,
00127     CALLBACK_CONNECTION_COMPLETE,
00128     CALLBACK_CONNECTION_FAILED,
00129     CALLBACK_SET_LOCAL_NAME
00130 };
00131 
00132 //  L2CAP Protocol/Service Multiplexor (PSM) values
00133 
00134 #define L2CAP_PSM_ANY                   0x0000  /* Any/Invalid PSM */
00135 #define L2CAP_PSM_SDP                   0x0001  /* Service Discovery Protocol */
00136 #define L2CAP_PSM_RFCOMM                0x0003  /* RFCOMM protocol */
00137 #define L2CAP_PSM_TCP                   0x0005  /* Telephony Control Protocol */
00138 #define L2CAP_PSM_TCS                   0x0007  /* TCS cordless */
00139 #define L2CAP_PSM_BNEP                  0x000f  /* Bluetooth Network Encapsulation Protocol*/
00140 #define L2CAP_PSM_HID_CNTL              0x0011  /* HID Control */
00141 #define L2CAP_PSM_HID_INTR              0x0013  /* HID Interrupt */
00142 #define L2CAP_PSM_ESDP                  0x0015  /* Extended Service Discovery Profile */
00143 #define L2CAP_PSM_AVCTP                 0x0017  /* Audio/Visual Control Transport Protocol */
00144 #define L2CAP_PSM_AVDTP                 0x0019  /* Audio/Visual Distribution */
00145 
00146 //  Callback from inquiry
00147 typedef int (*HCICallback)(HCI* hci, HCI_CALLBACK_EVENT evt, const u8* data, int len);
00148 
00149 #define MAX_BTDEVICES 8
00150 
00151 class HCITransport;
00152 class HCI : public SocketHandler
00153 {
00154     HCITransport* _transport;
00155     HCICallback _callback;
00156     BD_ADDR  _localAddr;
00157 
00158     BTDevice _devices[MAX_BTDEVICES];
00159     int _deviceCount;
00160 
00161     int _acl_mtu;
00162     int _acl_max_pkt;
00163     int _sco_mtu;
00164     int _sco_max_pkt;
00165 
00166     int _state;
00167 
00168     public:
00169 
00170     //  Open a local adapter
00171     int Open(HCITransport* transport, HCICallback callback);
00172 
00173     //  Return list of discovered addreses
00174     int GetDevices(BTDevice** devices, int maxDevices);
00175 
00176     //  Lookup a device by address or handle
00177     BTDevice* Find(const BD_ADDR* addr);
00178     BTDevice* Find(int handle);
00179 
00180     //  Disconnect from a remote device
00181     int Disconnect(const BD_ADDR* addr);
00182     int DisconnectAll();
00183 
00184 //-----y.k-----
00185     void Setlocalname(void);
00186     void Setdevice(void);
00187     void Scanenable(void);
00188 //-----y.k-----
00189     
00190     //  see what devies are in the system
00191     int Inquiry(int duration = 10);
00192 
00193     //  get a name, delivered in callback
00194     int RemoteNameRequest(const BD_ADDR* addr);
00195 
00196     //  Connect to a remote device
00197     int CreateConnection(const BD_ADDR* remoteAddr);
00198 
00199     bool Busy();
00200 
00201     //  called from transport
00202     void HCIRecv(const u8* data, int len);
00203 
00204     //  called from transport
00205     void ACLRecv(const u8* data, int len);
00206 
00207     //  SocketHandler methods for maintaining L2CAP sockets
00208     virtual int Open(SocketInternal* sock, SocketAddrHdr* addr);
00209     virtual int Send(SocketInternal* sock, const u8* data, int len);
00210     virtual int Close(SocketInternal* sock);
00211     
00212     bool SetLocalName_complite;
00213 
00214     private:
00215     void    InquiryResult(const inquiry_info* info);
00216     void    RemoteName(const BD_ADDR* addr, const char* name);
00217     void    ConnectComplete(const connection_info* info);
00218     void    DisconnectComplete(int handle);
00219     int     SendCmd(int cmd, const u8* params = 0, int len = 0);
00220     void    OnCommandComplete(int cmd, const u8* data, int len);
00221     void    Callback(HCI_CALLBACK_EVENT c, const u8* data, int len);
00222     int     PinCodeReply(const u8* data);
00223 };
00224 
00225 class HCITransport
00226 {
00227 protected:
00228     HCI* _target;
00229 public:
00230     void Set(HCI* target) { _target = target; };
00231     virtual void HCISend(const u8* data, int len) = 0;
00232     virtual void ACLSend(const u8* data, int len) = 0;
00233 };
00234 
00235 #endif