USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
SPP.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
2 
3  This software may be distributed and modified under the terms of the GNU
4  General Public License version 2 (GPL2) as published by the Free Software
5  Foundation and appearing in the file GPL2.TXT included in the packaging of
6  this file. Please note that GPL2 Section 2[b] requires that all works based
7  on this software must also be made publicly available under the terms of
8  the GPL2 ("Copyleft").
9 
10  Contact information
11  -------------------
12 
13  Kristian Lauszus, TKJ Electronics
14  Web : http://www.tkjelectronics.com
15  e-mail : kristianl@tkjelectronics.com
16  */
17 
18 #ifndef _spp_h_
19 #define _spp_h_
20 
21 #include "BTD.h"
22 
23 /* Used for SDP */
24 #define SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST_PDU 0x06 // See the RFCOMM specs
25 #define SDP_SERVICE_SEARCH_ATTRIBUTE_RESPONSE_PDU 0x07 // See the RFCOMM specs
26 #define SERIALPORT_UUID 0x1101 // See http://www.bluetooth.org/Technical/AssignedNumbers/service_discovery.htm
27 #define L2CAP_UUID 0x0100
28 
29 /* Used for RFCOMM */
30 #define RFCOMM_SABM 0x2F
31 #define RFCOMM_UA 0x63
32 #define RFCOMM_UIH 0xEF
33 //#define RFCOMM_DM 0x0F
34 #define RFCOMM_DISC 0x43
35 
36 #define extendAddress 0x01 // Always 1
37 
38 // Multiplexer message types
39 #define BT_RFCOMM_PN_CMD 0x83
40 #define BT_RFCOMM_PN_RSP 0x81
41 #define BT_RFCOMM_MSC_CMD 0xE3
42 #define BT_RFCOMM_MSC_RSP 0xE1
43 #define BT_RFCOMM_RPN_CMD 0x93
44 #define BT_RFCOMM_RPN_RSP 0x91
45 /*
46 #define BT_RFCOMM_TEST_CMD 0x23
47 #define BT_RFCOMM_TEST_RSP 0x21
48 #define BT_RFCOMM_FCON_CMD 0xA3
49 #define BT_RFCOMM_FCON_RSP 0xA1
50 #define BT_RFCOMM_FCOFF_CMD 0x63
51 #define BT_RFCOMM_FCOFF_RSP 0x61
52 #define BT_RFCOMM_RLS_CMD 0x53
53 #define BT_RFCOMM_RLS_RSP 0x51
54 #define BT_RFCOMM_NSC_RSP 0x11
55  */
56 
61 class SPP : public BluetoothService, public Stream {
62 public:
69  SPP(BTD *p, const char *name = "Arduino", const char *pin = "0000");
70 
75  operator bool() {
76  return connected;
77  }
79  bool connected;
80 
86  virtual void ACLData(uint8_t* ACLData);
88  virtual void Run();
90  virtual void Reset();
92  virtual void disconnect();
100  virtual int available(void);
101 
103  virtual void flush(void) {
104  send();
105  };
110  virtual int peek(void);
115  virtual int read(void);
116 
117 #if defined(ARDUINO) && ARDUINO >=100
118 
123  virtual size_t write(uint8_t data);
130  virtual size_t write(const uint8_t* data, size_t size);
132  using Print::write;
133 #else
134 
138  virtual void write(uint8_t data);
144  virtual void write(const uint8_t* data, size_t size);
145 #endif
146 
148  void discard(void);
154  void send(void);
157 private:
158  /* Bluetooth dongle library pointer */
159  BTD *pBtd;
160 
161  /* Set true when a channel is created */
162  bool SDPConnected;
163  bool RFCOMMConnected;
164 
165  uint16_t hci_handle; // The HCI Handle for the connection
166 
167  /* Variables used by L2CAP state machines */
168  uint8_t l2cap_sdp_state;
169  uint8_t l2cap_rfcomm_state;
170  uint32_t l2cap_event_flag; // l2cap flags of received Bluetooth events
171 
172  uint8_t l2capoutbuf[BULK_MAXPKTSIZE]; // General purpose buffer for l2cap out data
173  uint8_t rfcommbuf[10]; // Buffer for RFCOMM Commands
174 
175  /* L2CAP Channels */
176  uint8_t sdp_scid[2]; // L2CAP source CID for SDP
177  uint8_t sdp_dcid[2]; // 0x0050
178  uint8_t rfcomm_scid[2]; // L2CAP source CID for RFCOMM
179  uint8_t rfcomm_dcid[2]; // 0x0051
180  uint8_t identifier; // Identifier for command
181 
182  /* RFCOMM Variables */
183  uint8_t rfcommChannel;
184  uint8_t rfcommChannelConnection; // This is the channel the SPP channel will be running at
185  uint8_t rfcommDirection;
186  uint8_t rfcommCommandResponse;
187  uint8_t rfcommChannelType;
188  uint8_t rfcommPfBit;
189 
190  unsigned long timer;
191  bool waitForLastCommand;
192  bool creditSent;
193 
194  uint8_t rfcommDataBuffer[100]; // Create a 100 sized buffer for incoming data
195  uint8_t sppOutputBuffer[100]; // Create a 100 sized buffer for outgoing SPP data
196  uint8_t sppIndex;
197  uint8_t rfcommAvailable;
198 
199  bool firstMessage; // Used to see if it's the first SDP request received
200  uint8_t bytesRead; // Counter to see when it's time to send more credit
201 
202  /* State machines */
203  void SDP_task(); // SDP state machine
204  void RFCOMM_task(); // RFCOMM state machine
205 
206  /* SDP Commands */
207  void SDP_Command(uint8_t *data, uint8_t nbytes);
208  void serviceNotSupported(uint8_t transactionIDHigh, uint8_t transactionIDLow);
209  void serialPortResponse1(uint8_t transactionIDHigh, uint8_t transactionIDLow);
210  void serialPortResponse2(uint8_t transactionIDHigh, uint8_t transactionIDLow);
211  void l2capResponse1(uint8_t transactionIDHigh, uint8_t transactionIDLow);
212  void l2capResponse2(uint8_t transactionIDHigh, uint8_t transactionIDLow);
213 
214  /* RFCOMM Commands */
215  void RFCOMM_Command(uint8_t *data, uint8_t nbytes);
216  void sendRfcomm(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t *data, uint8_t length);
217  void sendRfcommCredit(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t credit);
218  uint8_t calcFcs(uint8_t *data);
219  bool checkFcs(uint8_t *data, uint8_t fcs);
220  uint8_t crc(uint8_t *data);
221 };
222 #endif
virtual size_t write(uint8_t data)
Definition: SPP.cpp:728
SPP(BTD *p, const char *name="Arduino", const char *pin="0000")
Definition: SPP.cpp:45
Definition: BTD.h:230
bool connected
Definition: SPP.h:79
virtual int read(void)
Definition: SPP.cpp:797
virtual void Run()
Definition: SPP.cpp:428
virtual void Reset()
Definition: SPP.cpp:63
virtual void flush(void)
Definition: SPP.h:103
virtual int available(void)
Definition: SPP.cpp:783
void discard(void)
Definition: SPP.cpp:787
#define BULK_MAXPKTSIZE
Definition: BTD.h:33
virtual void disconnect()
Definition: SPP.cpp:74
Definition: SPP.h:61
void send(void)
Definition: SPP.cpp:755
virtual void ACLData(uint8_t *ACLData)
Definition: SPP.cpp:86
virtual int peek(void)
Definition: SPP.cpp:791