This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
btnled.gatt.server.sb
404 lines (334 loc) · 15.1 KB
/
btnled.gatt.server.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright (c) 2014, 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
//
// This is a sample app for demonstration purpose only
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filenname ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// BleLedDemo.sb
// -- Created to demonstrating the following:
// - Press a button on the BL600 dev board and it lights an LED on the BL620 dev board.
// - Press a button on the BL620 dev board and it lights an LED on the BL600 dev board
//
// Values: 0x00 -> Both LEDs off
// 0x01 -> LED 0 on, LED 1 off
// 0x02 -> LED 1 on, LED 0 off
// 0x03 -> Both LEDs on
//
// ####################################
// # #
// # Search for TODO to make changes #
// # #
// ####################################
//
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
#define ENABLE_DEBUG_PRINTS 1
//Laird Technologies 128 bit Base UUID
#define LT_BASE_UUID "\56\9a\00\00\b8\7f\49\0c\92\cb\11\ba\5e\a5\16\7c\"
//Button Led Service 16 bit UUID
#define LED_SVC_UUID 0x1902
//GATT Client (e.g. BL620) LED Controller Characteristic UUID
#define CLILEDS_CHAR_UUID 0x2030
//GATT Server (e.g. BL600) LED Controller Characteristic UUID
#define SERLEDS_CHAR_UUID 0x2031
//Will advertise this name
#define DEVICENAME "Laird Btn LED Demo - BL600"
//Version of this app in Device Info Service
#define SWREV "1.0"
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//GPIO Defines
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define GPIO_BTN0 16
#define GPIO_BTN1 17
#define GPIO_LED0 18
#define GPIO_LED1 19
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Can ignore the rest of the following #defines for now
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define DEVICENAME_WRITABLE 1
//BLE_APPEARANCE_GENERIC_TAG
#define APPEARANCE 512
#define MANF_NAME "Laird Technologies"
#define MODELSTR "BL600"
#define SERIALNUM "12345"
#define HWREV "HwRev1.0"
#define MAX_DEVNAME_CHRS_IN_ADVERT 0
//DiscoverableMode (2==General)
#define DISCOVERY_MODE 2
//Advertise type (0==Connectable, 2==Not Connectable)
#define ADVERT_TYPE 0
//Advertising interval -- higher the number, lower the current consumption
//Recommended range 50 to 10000
#define ADV_INTERVAL_MS 250
//Advertise timeout (0==forever)
#define ADV_TIMEOUT_MS 0
//Minimum acceptable connection interval (0.5 seconds)
#define MIN_CONN_INTERVAL 7500
//Maximum acceptable connection interval (1 second).
#define MAX_CONN_INTERVAL 100000
//Slave latency -- number of conn events that can be missed
//Effective Conn interval will be (SLAVE_LATENCY+1)*ACTUAL_CONN_INTERVAL
#define SLAVE_LATENCY 0
//Connection supervisory timeout (4 seconds) - max 32 seconds
#define CONN_SUP_TIMEOUT 4000000
//DiscoverableMode
#define DISCOVERY_MODE 2
//EvBleMsg ID for a BLE disconnection event
#define BLE_DISCONNECTED 1
#define AD_128BIT_UUID 0x21
//******************************************************************************
// Register Error Handler as early as possible
//******************************************************************************
sub HandlerOnErr()
if (ENABLE_DEBUG_PRINTS!=0) then
print "\n OnErr - ";GetLastError();"\n"
endif
endsub
onerror next HandlerOnErr
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
dim rc //Resultcode
dim dn$ //Device name
dim prAddr$ //Peer mac address (not required for this app) hence empty
dim hSvc //Service handler
dim hCliLeds //GATT Client LEDs characteristic handle
dim hSerLeds //GATT Server LEDs characteristic handle
dim hSvcUuid //Btn LED service UUID handle
dim nTxBufs //Number of free tx buffers for notifications
//******************************************************************************
// Initialise Global Variable
//******************************************************************************
dn$=DEVICENAME
prAddr$=""
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
//------------------------------------------------------------------------------
// For debugging
// --- Format: ln=ABCD
// AB : Subroutine/Function number
// CD : Result code number
//------------------------------------------------------------------------------
Sub AssertRC(rc,ln)
if rc!=0 then
if (ENABLE_DEBUG_PRINTS!=0) then
print "\nFail :";integer.h' rc;" at tag ";ln
endif
endif
EndSub
//------------------------------------------------------------------------------
// Delay using a while loop
//------------------------------------------------------------------------------
Sub Delay(ms)
dim i : i = GetTickCount()
while GetTickSince(i)<ms
endwhile
EndSub
//------------------------------------------------------------------------------
// Initialise service and characteristics
//------------------------------------------------------------------------------
Sub CreateSvc()
dim bseUuid$ : bseUuid$=LT_BASE_UUID
//Laird Technologies Base UUID handle
dim hBseUuid : hBseUuid=BleHandleUuid128(bseUuid$)
//LED service UUID handle
hSvcUuid=BleHandleUuidSibling(hBseUuid,LED_SVC_UUID)
dim dta$ : dta$="\00"
dim rc2,rc3
//Create Btn LED Service
rc=BleServiceNew(1,hSvcUuid,hSvc)
AssertRC(rc,0600)
//Initialise GATT Client LEDs Characteristic - Notifyable, Readable
rc=BleCharNew(0x22, BleHandleUuidSibling(hBseUuid,(CLILEDS_CHAR_UUID)), BleAttrMetaData(1,0,2,1,rc2), BleAttrMetaData(1,1,1,1,rc3),0)
AssertRC(rc,155) : AssertRC(rc2,1552)
rc=BleCharCommit(hSvc,dta$,hCliLeds)
AssertRC(rc,157)
//Initialise GATT Server LEDs Characteristic - Read/Write
rc=BleCharNew(0x0A, BleHandleUuidSibling(hBseUuid,(SERLEDS_CHAR_UUID)), BleAttrMetaData(1,1,2,1,rc2),0,0)
AssertRC(rc,0603) : AssertRC(rc2,0601) : AssertRC(rc3,0601)
rc=BleCharCommit(hSvc,dta$,hSerLeds)
AssertRC(rc,0604)
//Commit service to the GATT table
rc=BleServiceCommit(hSvc)
EndSub
//------------------------------------------------------------------------------
// Initialise advert reports
//------------------------------------------------------------------------------
Sub MakeAdvRpts()
dim advRpt$, scnRpt$
//Initialise the advert report
rc = BleAdvRptInit(advRpt$, 2, 0, 0)
AssertRC(rc,0703)
//Initialise scan report
rc=BleScanRptInit(scnRpt$)
//Add local name to scan report
rc=BleAdvRptAppendAD(scnRpt$,0x09,dn$)
AssertRC(rc,179)
//Add led svc uuid to advert report
rc=BleAdvRptAddUuid128(advRpt$, hSvcUuid)
//Commit the reports to stack
rc = BleAdvRptsCommit(advRpt$,scnRpt$)
AssertRC(rc,0900)
EndSub
//------------------------------------------------------------------------------
// Start advertising
//------------------------------------------------------------------------------
Sub StartAdvertising()
rc = BleAdvertStart(ADVERT_TYPE, prAddr$, ADV_INTERVAL_MS, ADV_TIMEOUT_MS,0)
AssertRC(rc,197)
print "\n-- Advertising"
EndSub
//------------------------------------------------------------------------------
// Initialise GPIOS
//------------------------------------------------------------------------------
Sub InitGpios()
rc=gpiosetfunc(GPIO_BTN0,1,2) //Button 0 digital input with weak pull up resistor
AssertRC(rc,188)
rc=gpiosetfunc(GPIO_BTN1,1,2) //Button 1 digital input with weak pull up resistor
AssertRC(rc,188)
rc=GpioSetFunc(GPIO_LED0,2,0) //Sets sio18 (LED0) as a digital out
AssertRC(rc,190)
GpioWrite(GPIO_LED0,0)
rc=GpioSetFunc(GPIO_LED1,2,0) //Sets sio19 (LED1) as a digital out
AssertRC(rc,192)
GpioWrite(GPIO_LED1,0)
EndSub
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
Sub OnStartup()
print "\n\n*****************************************************************************************"
print "\n** BleLedDemo.sb **\n"
print "\n*****************************************************************************************"
print "\n Notifiable characteristic (0x2031) which notifies when button is pressed or depressed"
print "\n Writeable characteristic (0x2030) to control on board LEDs:"
print "\n * Bit 0 controls LED 1, Bit 1 controls LED 0"
print "\n * Set bit to turn LED on, clear bit to turn LED off"
print "\n * e.g. 0x11 turns both LEDs on, 0x10 turns on LED 0 and turns off LED 1\n**"
print "\n*****************************************************************************************"
print "\n*****************************************************************************************\n"
CreateSvc()
//Initialise the Gap Service
rc=BleGapSvcInit(dn$,DEVICENAME_WRITABLE,APPEARANCE,MIN_CONN_INTERVAL,MAX_CONN_INTERVAL,CONN_SUP_TIMEOUT,SLAVE_LATENCY)
AssertRC(rc,234)
MakeAdvRpts()
InitGpios()
StartAdvertising()
EndSub
//******************************************************************************
// Handler definitions
//******************************************************************************
//------------------------------------------------------------------------------
// New Characteristic value
//------------------------------------------------------------------------------
Function HndlrCharVal(charHndl, offset, len)
dim i,s$
//Get characteristic value
rc=BleCharValueRead(charHndl, s$)
print "\nNew value received from client:";s$
//write values to LEDs
GpioWrite(GPIO_LED0,StrGetChr(s$,0))
GpioWrite(GPIO_LED1,StrGetChr(s$,1))
EndFunc 1
//------------------------------------------------------------------------------
// Ble event handler
//------------------------------------------------------------------------------
Function HndlrBleMsg(ByVal nMsgId, ByVal connHndl)
if nMsgID==1 then
print "\n\n-- Disconnected from client\n"
elseif nMsgID==0 then
print "\n-- Connected to client"
endif
EndFunc 1
//------------------------------------------------------------------------------
// Button 0 transition handler
//------------------------------------------------------------------------------
Function HndlrGpio()
OnEvent EvGpioChan0 disable
OnEvent EvGpioChan1 disable
Delay(150)
dim val$
rc=StrSetChr(val$, !GpioRead(GPIO_BTN1), 1)
AssertRC(rc,1203)
rc=StrSetChr(val$, !GpioRead(GPIO_BTN0), 0)
AssertRC(rc,1203)
print "\n server button update:";val$
if nTxBufs > 0 then
//GATT Server indicates the value to the GATT client
//In other words, "Turn on your LED"
rc=BleCharValueIndicate(hCliLeds,val$)
AssertRC(rc,1203)
nTxBufs = nTxBufs-1
endif
OnEvent EvGpioChan0 call HndlrGpio
OnEvent EvGpioChan1 call HndlrGpio
EndFunc 1
//------------------------------------------------------------------------------
// CCCD written
//------------------------------------------------------------------------------
Function HndlrCccd(charHndl, val)
if charHndl == hCliLeds then
print "\n CCCD:";val
if val==2 then
rc=GpioBindEvent(0,GPIO_BTN0,2) //Binds a gpio transition high or low on button 0 (SIO16) to event 0
AssertRC(rc,309)
rc=GpioBindEvent(1,GPIO_BTN1,2) //Binds a gpio transition high or low on button 1 (SIO17) to event 1
AssertRC(rc,311)
print "\n Indications enabled"
nTxBufs = nTxBufs + 1
else
rc=GpioUnbindEvent(0)
rc=GpioUnbindEvent(1)
print "\n Indications disabled"
endif
endif
EndFunc 1
//------------------------------------------------------------------------------
// Disconnection
//------------------------------------------------------------------------------
Function HndlrDiscon(hConn, rsn)
StartAdvertising()
EndFunc 1
//------------------------------------------------------------------------------
// Called when indicate ack'd
//------------------------------------------------------------------------------
Function HndlrCharHvc(hChar)
nTxBufs = nTxBufs + 1
EndFunc 1
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
OnStartup()
OnEvent EvCharVal call HndlrCharVal
OnEvent EvBleMsg call HndlrBleMsg
OnEvent EvCharCccd call HndlrCccd
OnEvent EvGpioChan0 call HndlrGpio
OnEvent EvGpioChan1 call HndlrGpio
OnEvent EvDiscon call HndlrDiscon
OnEvent EvCharHvc call HndlrCharHvc
//------------------------------------------------------------------------------
// Wait for a synchronous event.
// An application can have multiple <WaitEvent> statements
//------------------------------------------------------------------------------
WaitEvent