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
/
dn.device.name.nv.memory.sb
183 lines (147 loc) · 7.4 KB
/
dn.device.name.nv.memory.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
// 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
//
// dn.device.name.nv.memory.sb
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filenname ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// This sample app advertises with a writeable device name. Once written, the new name is
// saved to non-volatile memory so it can be used upon a power cycle. The GAP service
// is updated, a new advert report is committed to the GATT table and the module starts
// advertising again.
//
//
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
#define BLE_EVBLEMSGID_DEVICENAME_WRITE 21
#define BLE_EVBLEMSGID_DISCONNECT 1
#define DEVNAME_REC_NUM 100
#define BLE_APPEARANCE_GENERIC_KEYRING 576
#define ENABLE_DEBUG_PRINTS 1
#define DEVNAME_WRITEABLE 1
#define BLE_DISCOVERABILITY_GENERAL 2
#define MAX_DEVNAME_CHARS 20
#define ADV_FILTERPOLICY_ANY 0
#define ADV_IND 0
#define ADV_INTERVAL_MS 100
#define ADV_TIMEOUT_MS 0
//Minimum acceptable connection interval (50 ms)
#define MIN_CONN_INTERVAL 50000
//Maximum acceptable connection interval (100 ms).
#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
//EvBleMsg ID for a BLE disconnection event
//******************************************************************************
// Library Import
//******************************************************************************
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
dim rc //Result code
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
//------------------------------------------------------------------------------
// For debugging
// --- rc = result code
// --- ln = line number
//------------------------------------------------------------------------------
Sub AssertRC(rc,ln)
if rc!=0 then
print "\nFail :";integer.h' rc;" at tag ";ln
endif
EndSub
//------------------------------------------------------------------------------
// 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
//------------------------------------------------------------------------------
// Update GAP service, initialise and commit an advert report then start advertising
//------------------------------------------------------------------------------
sub StartAdvertising()
dim advRpt$ //Advert report
dim scnRpt$ //Scan report
dim addr$ //Empty peer address
dim dn$ //Device name
dn$ = BleGetDeviceName$() //Device name to used in advertisements
//Update GAP service with device name set above. Make it writeable
rc=BleGapSvcInit(dn$, DEVNAME_WRITEABLE, BLE_APPEARANCE_GENERIC_KEYRING, MIN_CONN_INTERVAL, MAX_CONN_INTERVAL,CONN_SUP_TIMEOUT,SLAVE_LATENCY)
AssertRC(rc,103)
//Initialise advert report so that new device name will appear in adverts
rc=BleAdvRptInit(advRpt$, BLE_DISCOVERABILITY_GENERAL, 0, MAX_DEVNAME_CHARS)
AssertRC(rc,107)
//Commit advert report and empty scan report to GATT table
rc=BleAdvRptsCommit(advRpt$, scnRpt$)
AssertRC(rc,111)
//Start advertising
rc=BleAdvertStart(ADV_IND, addr$, ADV_INTERVAL_MS, ADV_TIMEOUT_MS, ADV_FILTERPOLICY_ANY)
AssertRC(rc,115)
endsub
//------------------------------------------------------------------------------
// This subroutine gets called first
//------------------------------------------------------------------------------
sub OnStartup()
dim devName$
//Load stored device name from non volatile memory. If not present, use current device name
rc=NvRecordGetEx(DEVNAME_REC_NUM, devName$ , BleGetDeviceName$())
//Update GAP service with device name read above
rc=BleGapSvcInit(devName$, DEVNAME_WRITEABLE, BLE_APPEARANCE_GENERIC_KEYRING, MIN_CONN_INTERVAL, MAX_CONN_INTERVAL,CONN_SUP_TIMEOUT,SLAVE_LATENCY)
AssertRC(rc,129)
StartAdvertising()
endsub
//******************************************************************************
// Handler definitions
//******************************************************************************
Function HndlrBleMsg(ByVal msgID, ByVal hConn)
if msgID == BLE_EVBLEMSGID_DEVICENAME_WRITE then
dim devName$ : devName$ = BleGetDeviceName$() //Device name to be saved
//Write device name to non volatile memory
rc=NvRecordSet(DEVNAME_REC_NUM, devName$)
elseif msgID == BLE_EVBLEMSGID_DISCONNECT then
//Stop advertising so that new name can appear in subsequent adverts after updating the GAP service
rc=BleAdvertStop()
AssertRC(rc,148)
//Call this sub which will update GAP service, initialise and commit new advert report, and start advertising
StartAdvertising()
endif
EndFunc 1
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
OnStartup()
onevent evblemsg call HndlrBleMsg
onerror next HandlerOnErr
//------------------------------------------------------------------------------
// Wait for a synchronous event.
// An application can have multiple <WaitEvent> statements
//------------------------------------------------------------------------------
waitevent