USB Host Shield 2.0
Loading...
Searching...
No Matches
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 RFCOMM */
24#define RFCOMM_SABM 0x2F
25#define RFCOMM_UA 0x63
26#define RFCOMM_UIH 0xEF
27//#define RFCOMM_DM 0x0F
28#define RFCOMM_DISC 0x43
29
30#define extendAddress 0x01 // Always 1
31
32// Multiplexer message types
33#define BT_RFCOMM_PN_CMD 0x83
34#define BT_RFCOMM_PN_RSP 0x81
35#define BT_RFCOMM_MSC_CMD 0xE3
36#define BT_RFCOMM_MSC_RSP 0xE1
37#define BT_RFCOMM_RPN_CMD 0x93
38#define BT_RFCOMM_RPN_RSP 0x91
39/*
40#define BT_RFCOMM_TEST_CMD 0x23
41#define BT_RFCOMM_TEST_RSP 0x21
42#define BT_RFCOMM_FCON_CMD 0xA3
43#define BT_RFCOMM_FCON_RSP 0xA1
44#define BT_RFCOMM_FCOFF_CMD 0x63
45#define BT_RFCOMM_FCOFF_RSP 0x61
46#define BT_RFCOMM_RLS_CMD 0x53
47#define BT_RFCOMM_RLS_RSP 0x51
48#define BT_RFCOMM_NSC_RSP 0x11
49 */
50
55class SPP : public BluetoothService, public Stream {
56public:
63 SPP(BTD *p, const char *name = "Arduino", const char *pin = "0000");
64
67 void disconnect();
74 operator bool() {
75 return connected;
76 }
79
85 int available(void);
86
88 void flush(void) {
89 send();
90 };
95 int peek(void);
100 int read(void);
101
102#if defined(ARDUINO) && ARDUINO >=100
108 size_t write(uint8_t data);
115 size_t write(const uint8_t* data, size_t size);
117#if !defined(RBL_NRF51822) && !defined(NRF52_SERIES)
118 using Print::write;
119#endif
120#else
125 void write(uint8_t data);
131 void write(const uint8_t* data, size_t size);
132#endif
133
135 void discard(void);
141 void send(void);
144protected:
150 void ACLData(uint8_t* ACLData);
152 void Run();
154 void Reset();
160 void onInit();
163private:
164 /* Set true when a channel is created */
165 bool SDPConnected;
166 bool RFCOMMConnected;
167
168 /* Variables used by L2CAP state machines */
169 uint8_t l2cap_sdp_state;
170 uint8_t l2cap_rfcomm_state;
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
181 /* RFCOMM Variables */
182 uint8_t rfcommChannel;
183 uint8_t rfcommChannelConnection; // This is the channel the SPP channel will be running at
184 uint8_t rfcommDirection;
185 uint8_t rfcommCommandResponse;
186 uint8_t rfcommChannelType;
187 uint8_t rfcommPfBit;
188
189 uint32_t timer;
190 bool waitForLastCommand;
191 bool creditSent;
192
193 uint8_t rfcommDataBuffer[100]; // Create a 100 sized buffer for incoming data
194 uint8_t sppOutputBuffer[100]; // Create a 100 sized buffer for outgoing SPP data
195 uint8_t sppIndex;
196 uint8_t rfcommAvailable;
197
198 bool firstMessage; // Used to see if it's the first SDP request received
199 uint8_t bytesRead; // Counter to see when it's time to send more credit
200
201 /* State machines */
202 void SDP_task(); // SDP state machine
203 void RFCOMM_task(); // RFCOMM state machine
204
205 /* SDP Commands */
206 void SDP_Command(uint8_t *data, uint8_t nbytes);
207 void serviceNotSupported(uint8_t transactionIDHigh, uint8_t transactionIDLow);
208 void serialPortResponse1(uint8_t transactionIDHigh, uint8_t transactionIDLow);
209 void serialPortResponse2(uint8_t transactionIDHigh, uint8_t transactionIDLow);
212
213 /* RFCOMM Commands */
214 void RFCOMM_Command(uint8_t *data, uint8_t nbytes);
215 void sendRfcomm(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t *data, uint8_t length);
216 void sendRfcommCredit(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t credit);
217 uint8_t calcFcs(uint8_t *data);
218 bool checkFcs(uint8_t *data, uint8_t fcs);
219 uint8_t crc(uint8_t *data);
220};
221#endif
#define BULK_MAXPKTSIZE
Definition BTD.h:37
Definition BTD.h:222
Definition SPP.h:55
void flush(void)
Definition SPP.h:88
void disconnect()
Definition SPP.cpp:72
void Run()
Definition SPP.cpp:423
int peek(void)
Definition SPP.cpp:805
void discard(void)
Definition SPP.cpp:801
size_t write(uint8_t data)
Definition SPP.cpp:742
int available(void)
Definition SPP.cpp:797
int read(void)
Definition SPP.cpp:811
void send(void)
Definition SPP.cpp:769
void ACLData(uint8_t *ACLData)
Definition SPP.cpp:84
bool connected
Definition SPP.h:78
void Reset()
Definition SPP.cpp:60
void onInit()
Definition SPP.cpp:433