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 
73  void disconnect();
80  operator bool() {
81  return connected;
82  }
84  bool connected;
85 
91  int available(void);
92 
94  void flush(void) {
95  send();
96  };
101  int peek(void);
106  int read(void);
107 
108 #if defined(ARDUINO) && ARDUINO >=100
109 
114  size_t write(uint8_t data);
121  size_t write(const uint8_t* data, size_t size);
123 #if !defined(RBL_NRF51822)
124  using Print::write;
125 #endif
126 #else
127 
131  void write(uint8_t data);
137  void write(const uint8_t* data, size_t size);
138 #endif
139 
141  void discard(void);
147  void send(void);
150 protected:
156  void ACLData(uint8_t* ACLData);
158  void Run();
160  void Reset();
166  void onInit();
169 private:
170  /* Set true when a channel is created */
171  bool SDPConnected;
172  bool RFCOMMConnected;
173 
174  /* Variables used by L2CAP state machines */
175  uint8_t l2cap_sdp_state;
176  uint8_t l2cap_rfcomm_state;
177 
178  uint8_t l2capoutbuf[BULK_MAXPKTSIZE]; // General purpose buffer for l2cap out data
179  uint8_t rfcommbuf[10]; // Buffer for RFCOMM Commands
180 
181  /* L2CAP Channels */
182  uint8_t sdp_scid[2]; // L2CAP source CID for SDP
183  uint8_t sdp_dcid[2]; // 0x0050
184  uint8_t rfcomm_scid[2]; // L2CAP source CID for RFCOMM
185  uint8_t rfcomm_dcid[2]; // 0x0051
186 
187  /* RFCOMM Variables */
188  uint8_t rfcommChannel;
189  uint8_t rfcommChannelConnection; // This is the channel the SPP channel will be running at
190  uint8_t rfcommDirection;
191  uint8_t rfcommCommandResponse;
192  uint8_t rfcommChannelType;
193  uint8_t rfcommPfBit;
194 
195  uint32_t timer;
196  bool waitForLastCommand;
197  bool creditSent;
198 
199  uint8_t rfcommDataBuffer[100]; // Create a 100 sized buffer for incoming data
200  uint8_t sppOutputBuffer[100]; // Create a 100 sized buffer for outgoing SPP data
201  uint8_t sppIndex;
202  uint8_t rfcommAvailable;
203 
204  bool firstMessage; // Used to see if it's the first SDP request received
205  uint8_t bytesRead; // Counter to see when it's time to send more credit
206 
207  /* State machines */
208  void SDP_task(); // SDP state machine
209  void RFCOMM_task(); // RFCOMM state machine
210 
211  /* SDP Commands */
212  void SDP_Command(uint8_t *data, uint8_t nbytes);
213  void serviceNotSupported(uint8_t transactionIDHigh, uint8_t transactionIDLow);
214  void serialPortResponse1(uint8_t transactionIDHigh, uint8_t transactionIDLow);
215  void serialPortResponse2(uint8_t transactionIDHigh, uint8_t transactionIDLow);
216  void l2capResponse1(uint8_t transactionIDHigh, uint8_t transactionIDLow);
217  void l2capResponse2(uint8_t transactionIDHigh, uint8_t transactionIDLow);
218 
219  /* RFCOMM Commands */
220  void RFCOMM_Command(uint8_t *data, uint8_t nbytes);
221  void sendRfcomm(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t *data, uint8_t length);
222  void sendRfcommCredit(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t credit);
223  uint8_t calcFcs(uint8_t *data);
224  bool checkFcs(uint8_t *data, uint8_t fcs);
225  uint8_t crc(uint8_t *data);
226 };
227 #endif
size_t write(uint8_t data)
Definition: SPP.cpp:742
void onInit()
Definition: SPP.cpp:433
SPP(BTD *p, const char *name="Arduino", const char *pin="0000")
Definition: SPP.cpp:45
Definition: BTD.h:201
void flush(void)
Definition: SPP.h:94
bool connected
Definition: SPP.h:84
int read(void)
Definition: SPP.cpp:811
void Run()
Definition: SPP.cpp:423
void Reset()
Definition: SPP.cpp:60
int available(void)
Definition: SPP.cpp:797
void discard(void)
Definition: SPP.cpp:801
#define BULK_MAXPKTSIZE
Definition: BTD.h:37
void disconnect()
Definition: SPP.cpp:72
Definition: SPP.h:61
void send(void)
Definition: SPP.cpp:769
void ACLData(uint8_t *ACLData)
Definition: SPP.cpp:84
int peek(void)
Definition: SPP.cpp:805