This repository was archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsmartZ.BL620.sb
More file actions
386 lines (316 loc) · 14.6 KB
/
Copy pathsmartZ.BL620.sb
File metadata and controls
386 lines (316 loc) · 14.6 KB
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
// 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
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filenname ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//
// smartZ.BL620
//
//
// This app provides for a command line interface to the BL620 devkit over the UART.
// The commands are designed to mimic the behaviour of BlueZ commands used in Linux.
//
// When you run the app you will get a -------> command prompt to allow you to submit
// commands as follows:-
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// hcitool dev Display local devices
// hcitool con Display active connections
// hcitool leadv Start LE advertisement
// hcitool noleadv Stop LE advertisement
// hcitool lescan Start LE scan
// hcitool lecc Create a LE Connection
// hcitool ledc Disconnect a LE Connection
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// gatttool --primary Primary Service Discovery
// gatttool --characteristics Characteristics Discovery
// gatttool --char-read Characteristics Value/Descriptor Read
// gatttool --char-write Characteristics Value Write Without Response
// gatttool --char-desc Characteristics Descriptor Discovery
// gatttool --listen Listen for notifications and indications
//
// Options:
// -b, --device=MAC Specify remote Bluetooth address
// -l, --sec-level Set security level. Default: low
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// hciconfig letpl [level] Get/Set LE transmit power level
// hciconfig version Display version information
// hciconfig leadv Enable LE advertising
// hciconfig noleadv Disable LE advertising
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//******************************************************************************
// Definitions
//******************************************************************************
// Number of connections
#define NUM_OF_CONNS 8
// BLE EVENT MSG IDs
#define BLE_EVBLEMSGID_CONNECT 0 //msgCtx = connection handle
#define BLE_EVBLEMSGID_DISCONNECT 1 //msgCtx = connection handle
// SMARTZ COMMAND IDs:
#define SMARTZ_COMMAND_NONE 0
// SMARTZ COMMAND IDs: HCITOOL
#define SMARTZ_COMMAND_HCITOOL_DEV 1
#define SMARTZ_COMMAND_HCITOOL_INQ 2
#define SMARTZ_COMMAND_HCITOOL_SCAN 3
#define SMARTZ_COMMAND_HCITOOL_NAME 4
#define SMARTZ_COMMAND_HCITOOL_CON 5
#define SMARTZ_COMMAND_HCITOOL_PAIR 6
#define SMARTZ_COMMAND_HCITOOL_UNPAIR 7
#define SMARTZ_COMMAND_HCITOOL_LESCAN 8
#define SMARTZ_COMMAND_HCITOOL_LEADV 9
#define SMARTZ_COMMAND_HCITOOL_LENOADV 10
#define SMARTZ_COMMAND_HCITOOL_LECC 11
#define SMARTZ_COMMAND_HCITOOL_LEDC 12
// SMARTZ COMMAND IDs: GATTTOOL
#define SMARTZ_COMMAND_GATTTOOL_PRIMARY 21
#define SMARTZ_COMMAND_GATTTOOL_CHARACTERISTICS 22
#define SMARTZ_COMMAND_GATTTOOL_CHAR_READ 23
#define SMARTZ_COMMAND_GATTTOOL_CHAR_WRITE 24
#define SMARTZ_COMMAND_GATTTOOL_CHAR_DESC 25
#define SMARTZ_COMMAND_GATTTOOL_LISTEN 26
// SMARTZ COMMAND IDs: RFCOMM
#define SMARTZ_COMMAND_RFCOMM_CONNECT 31
// APP RUN MODES
#define NORMAL_MODE 0
#define GATTTOOL_LISTEN 2
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
//Global variables
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dim rc //Response code
dim stRsp$ //Uart rx data is stored here
dim pr$ //Command prompt
dim urtcmd$ //CMD line from uart
dim tkn$,tlen //Used by command parser
dim hc[NUM_OF_CONNS+1] //Contains connection handles
dim conns //Number of connections
dim name$ //Local device name
dim addr$ //Address of device being connected to
dim command : command = 0 //Currently active smartZ command
dim conHdl //Global connection handle of the current connection being serviced
//Global variables used for the hciconfig command
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dim letpl : letpl = 4 //LE Tx Power level in dBm
dim mjVersion : mjVersion=1 //Major version number
dim mnVersion : mnVersion=1 //Minor version number
//Globale variables used for gatttool command
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dim gattLsn : gattLsn = 0 //If Lsn = 1, notifications will be shown
dim chrHdl //Global characteristic handle of the current characteristic being serviced
dim valHdl //Global characteristic value handle of the current characteristic value handle being serviced
dim secPair //If 1, it means sec-level is medium or high and therefore should pair
//******************************************************************************
// Initialise Global Variable
//******************************************************************************
pr$ = "------->"
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
//==============================================================================
// Display the error message
//==============================================================================
sub AssertResCode(resCode)
if rc !=0 then
print "error: ";integer.h' rc;"\n"
endif
endsub
//==============================================================================
// Add a delay of t seconds
//==============================================================================
sub delay(t as integer)
t = t*1000
DIM tm
tm = gettickcount()
while (gettickcount()-tm) < t
//Wait for t*1000 milliseconds
endwhile
endsub
//==============================================================================
// Initialise all connection handles
//==============================================================================
sub InitConnHandles()
dim z
for z=0 to (NUM_OF_CONNS)
hc[z]=-1
next
endsub
//==============================================================================
// Add the connection handle to the list of connection handles
//==============================================================================
function AcqConnHandle(hConn)
dim z
for z=1 to (NUM_OF_CONNS)
if hc[z] == -1 then
hc[z]=hConn
exitfunc z
endif
next
endfunc 0
//==============================================================================
// Release the connection handle from the array of conn handles
//==============================================================================
function RelConnHandle(hConn)
dim z
for z=1 to (NUM_OF_CONNS)
if hc[z] == hConn then
hc[z]=-1
exitfunc z
endif
next
endfunc 0
//==============================================================================
//------------------------------------------------------------------------------
//** #include the correct library file for the smartBASIC module being used
//------------------------------------------------------------------------------
#include "lib\smartZ.hcitool.BL620.sblib"
#include "lib\smartZ.gatttool.sblib"
#include "lib\smartZ.hciconfig.BL620.sblib"
//==============================================================================
// Initialise the Bluetooth device
// Set the device to be discoverable, connectible, pairable,
// Set the inquiry type to be general, inquiry mode to be extended
//==============================================================================
sub InitBtDevice()
dim nHandle
//Set device name
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Get the device's bdaddr and display it in hex format
name$ = SYSINFO$(4)
name$ = right$(name$, 6)
name$ = StrHexize$(name$)
//Set the name of the local Bluetooth device to be smartZ+<bdaddr>
name$ = "smartZ-" + name$
AssertResCode(rc)
endsub
//==============================================================================
// This is the first subroutine called when the program starts
//==============================================================================
sub Initialise()
//Set device to be discoverable, connectible, and pairable
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InitBtDevice()
//Initialise connection handles
InitConnHandles()
//Display the following lines as soon as the program starts
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
print "\n-----------------------\n"
print " smartZ v";mjVersion;".";mnVersion;"\n"
print "-----------------------\n"
print "\nType \22help\22 or \22<command> help\22\n"
print pr$
endsub
//==============================================================================
// Display the Help menu
//==============================================================================
sub _help()
print "Available Commands: \n\n"
print " help\n"
print " hcitool\n"
print " gatttool\n"
print " hciconfig\n"
print " quit/exit\n"
print "\n"
print "For more information on the usage of each command use:\n"
print "<command> --help\n\n"
endsub
//==============================================================================
// Extract the received UART command
//==============================================================================
function OnUartCmd() as integer
//Get first token
tlen = ExtractStrToken(urtcmd$,tkn$)
if strcmp(tkn$,"hcitool")==0 then
_hci()
elseif strcmp(tkn$,"gatttool")==0 then
_gatt()
elseif strcmp(tkn$,"hciconfig")==0 then
_hciCfg()
elseif strcmp(tkn$, "help")==0 then
_help()
elseif strcmp(tkn$,"exit")==0 || strcmp(tkn$,"quit")==0 then
//Exit this application
reset(0)
elseif strcmp(tkn$,"")==0 then
//Received carriage return. Do nothing
else
print tkn$;": command not found\n"
endif
print pr$
endfunc 1
//******************************************************************************
// Handler definitions
//******************************************************************************
//==============================================================================
// This handler is called when data has arrived through the UART
//==============================================================================
function HandlerUartRxCmd() as integer
dim nMatch
//Read UART data until CR
nMatch=UartReadMatch(stRsp$,13)
if nMatch!=0 then
//CR exists in the input buffer
urtcmd$ = strsplitleft$(stRsp$, nMatch)
//Process the received string
exitfunc OnUartCmd()
endif
endfunc 1
//==============================================================================
// Called after SendMsgApp is called. Used to switch between command, rfcomm, and
// gatttool listen modes
//==============================================================================
function SetMode(msgID, cmd) as integer
if cmd == NORMAL_MODE then
//If normal mode, UART data will be processed normally
OnEvent EVUARTRX call HandlerUartRxCmd
elseif cmd == GATTTOOL_LISTEN then
//If gatttool mode, UART data will be discarded as terminal
//is used for displaying notifications/indications
print "\nListening for notifications/indications. For hangup, type \22stop\22\n"
OnEvent EVUARTRX call HandlerUartRxLsn
endif
endfunc 1
//******************************************************************************
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
//------------------------------------------------------------------------------
// Enable synchronous event handlers
//------------------------------------------------------------------------------
//Data received over the UART. Process the data accordingly
OnEvent EVUARTRX call HandlerUartRxCmd
//Event recieved to switch between normal, rfcomm, and gatttool mode
OnEvent EVMSGAPP call SetMode
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//Initialise the device
Initialise()
//------------------------------------------------------------------------------
// Wait for a synchronous event.
//------------------------------------------------------------------------------
WaitEvent