forked from LairdCP/BL600-Applications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LatencyS.erver.BLE.BL600orBT900.sb
188 lines (178 loc) · 9.12 KB
/
LatencyS.erver.BLE.BL600orBT900.sb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2015, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filenname ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// BLE Latency Tester (Host): This application hosts a simple BLE server and
// forwards values written back to the client device
// as notifications to test the latency of BLE conn-
// -ections with different min/max connection
// intervals.
//
// To use: Simply run the LatencyS.erver.BLE.BL600orBT900.sb application on a
// BL600 or BT900 and put the device's Bluetooth address in the client
// files (LatencyC.lient.BLE.BL620orBT900.sb) global variable section
// (in the 'dim addr$ : addr$ = ""' line), then download and run the
// client on a BL620 or BT900 and it will automatically connect to the
// master device.
// Once connected, the minimum/maximum connection interval can be
// changed from the master device side, and enter can be pressed on the
// client device to run a single loop test.
//
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
#define APP_VERSION "1.00" //Version of the application
#define PreferredMinConnInt 7500 //The default preferred minimum connection interval
#define PreferredMaxConnInt 10000 //The default preferred maximum connection interval
#define ConnIntIncrement 500 //Amount to increment/decrement the connection interval by
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
dim rc //Result code variable
dim addr$ //Address of device to connect to
dim cHndl //Client handle
dim svcID //Service handle
dim charID //Characteristic handle
dim NewConnection //Set to 1 if this is a new connection
dim MinConnInt : MinConnInt = PreferredMinConnInt //
dim MaxConnInt : MaxConnInt = PreferredMaxConnInt //
dim nSuprToutUs //
dim nSlaveLatency //
dim Connected
//******************************************************************************
// Register Error Handler as early as possible
//******************************************************************************
SUB AssertRC(rc, line)
if (rc != 0) then
print "Error, rc = ";integer.h'rc;" on line ";line;"\n"
endif
ENDSUB
//==============================================================================
//Function when a BLE event occurs
//==============================================================================
FUNCTION HndlrBleMsg(nMsgId, nCtx)
dim nIntervalUs
IF (nMsgId == 0) THEN
//Connected - set preferred parameters
cHndl = nCtx
rc = BLEGETCURCONNPARMS(cHndl, nIntervalUs, nSuprToutUs, nSlaveLatency)
PRINT "Connection params: Interval = ";nIntervalUs;", Supervision timeout = ";nSuprToutUs;", Slave latency = ";nSlaveLatency;"\n"
rc = BLESETCURCONNPARMS(cHndl, MinConnInt, MaxConnInt, nSuprToutUs, nSlaveLatency)
print "Slave device connected!\n";
NewConnection = 1
Connected = 1
print "Command keys: 1) decrease minimum connection interval, 2) increase minimum connection interval, 3) decrease maximum connection interval, 4) increase maximum connection interval, 5) Renegotiate connection parameters (update)\n"
ELSEIF (nMsgId == 1) THEN
//Client disconnected, restart advertising
rc = BleAdvertStart(0, addr$, 250, 0, 0)
AssertRC(rc, 5)
Connected = 0
ELSEIF (nMsgId == 14) THEN
//Connection parameter update
rc = BLEGETCURCONNPARMS(cHndl, nIntervalUs, nSuprToutUs, nSlaveLatency)
PRINT "Connection params update: Interval = ";nIntervalUs;", Supervision timeout = ";nSuprToutUs;", Slave latency = ";nSlaveLatency;"\n"
if (nIntervalUs > 30000 && NewConnection == 1) then
//Connection parameters are high, attempt to lower them
rc = BLESETCURCONNPARMS(cHndl, MinConnInt, MaxConnInt, nSuprToutUs, nSlaveLatency)
AssertRC(rc, 6)
NewConnection = 0
endif
ENDIF
ENDFUNC 1
//==============================================================================
//Function called when a characteristic has been written
//==============================================================================
FUNCTION HndlrCharVal(charHandle, offset, len)
rc = BLECHARVALUEREAD(charHandle, addr$)
AssertRC(rc, 7)
rc = BLECHARVALUENOTIFY(charID, addr$)
AssertRC(rc, 8)
ENDFUNC 1
//==============================================================================
//Function called when a characteristic has been written
//==============================================================================
FUNCTION ResetTest()
dim TmpStr$, TmpStr2$, DoUpdate
rc = uartread(TmpStr$)
IF (Connected == 1) THEN
//Connection is active
TmpStr$ = LEFT$(TmpStr$, 1)
TmpStr2$ = "1"
IF (STRCMP(TmpStr$, TmpStr2$) == 0 && MinConnInt > 7500) THEN
//Decrease minimum connection interval
MinConnInt = MinConnInt - ConnIntIncrement
PRINT "Minimum connection interval: ";MinConnInt;"\n"
ENDIF
TmpStr2$ = "2"
IF (STRCMP(TmpStr$, TmpStr2$) == 0 && MinConnInt < 100000 && MinConnInt+ConnIntIncrement < MaxConnInt) THEN
//Increase minimum connection interval
MinConnInt = MinConnInt + ConnIntIncrement
PRINT "Minimum connection interval: ";MinConnInt;"\n"
ENDIF
TmpStr2$ = "3"
IF (STRCMP(TmpStr$, TmpStr2$) == 0 && MaxConnInt > 7500 && MaxConnInt-ConnIntIncrement > MinConnInt) THEN
//Decrease maximum connection interval
MaxConnInt = MaxConnInt - ConnIntIncrement
PRINT "Maximum connection interval: ";MaxConnInt;"\n"
ENDIF
TmpStr2$ = "4"
IF (STRCMP(TmpStr$, TmpStr2$) == 0 && MaxConnInt < 100000) THEN
//Increase maximum connection interval
MaxConnInt = MaxConnInt + ConnIntIncrement
PRINT "Maximum connection interval: ";MaxConnInt;"\n"
ENDIF
TmpStr2$ = "5"
IF (STRCMP(TmpStr$, TmpStr2$) == 0) THEN
//Renegotiate connection parameters
PRINT "Renegotiating...\n"
rc = BLESETCURCONNPARMS(cHndl, MinConnInt, MaxConnInt, nSuprToutUs, nSlaveLatency)
ENDIF
ENDIF
ENDFUNC 1
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
//Create service with one characteristic
addr$ = "a"
rc = BleServiceNew(1, BleHandleUuid16(0x180D), svcID)
AssertRC(rc, 1)
rc = BleCharNew(62, BleHandleUuid16(0x2020), BleAttrMetadata(1,1,20,1,rc), BleAttrMetadata(1,1,2,0,rc), 0)
AssertRC(rc, 2)
rc = BleCharCommit(svcID, addr$, charID)
AssertRC(rc, 3)
rc = BleServiceCommit(svcID)
AssertRC(rc, 4)
//Start advertising
rc = BleAdvertStart(0, addr$, 250, 0, 0)
AssertRC(rc, 5)
//------------------------------------------------------------------------------
// Enable synchronous event handlers
//------------------------------------------------------------------------------
ONEVENT EVBLEMSG CALL HndlrBleMsg
ONEVENT EVCHARVAL CALL HndlrCharVal
ONEVENT EVUARTRX CALL ResetTest
//------------------------------------------------------------------------------
// Wait for a synchronous event.
// An application can have multiple <WaitEvent> statements
//------------------------------------------------------------------------------
WAITEVENT