This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
u_ble_cfg_extmod.c
338 lines (279 loc) · 10.4 KB
/
u_ble_cfg_extmod.c
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
/*
* Copyright 2019-2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Only #includes of u_* and the C standard library are allowed here,
* no platform stuff and no OS stuff. Anything required from
* the platform/OS must be brought in through u_port* to maintain
* portability.
*/
/** @file
* @brief Implementation of the cfg API for ble.
*/
#ifndef U_CFG_BLE_MODULE_INTERNAL
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stdlib.h" // strol(), atoi(), strol(), strtof()
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "u_error_common.h"
#include "u_cfg_sw.h"
#include "u_port_os.h"
#include "u_timeout.h"
#include "u_at_client.h"
#include "u_short_range_module_type.h"
#include "u_short_range.h"
#include "u_short_range_private.h"
#include "u_ble_cfg.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
#define U_BLE_CFG_SERVER_TYPE_SPS 6
#define U_BLE_CFG_MAX_NUM_SERVERS 7
#define U_BLE_CFG_STARTUP_MODE_EDM 2
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
static int32_t getStartupMode(const uAtClientHandle_t atHandle)
{
int32_t modeOrError;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UMSM?");
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UMSM:");
modeOrError = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
uAtClientUnlock(atHandle);
return modeOrError;
}
static int32_t setStartupMode(const uAtClientHandle_t atHandle, int32_t mode)
{
int32_t error;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UMSM=");
uAtClientWriteInt(atHandle, mode);
uAtClientCommandStopReadResponse(atHandle);
error = uAtClientUnlock(atHandle);
return error;
}
static int32_t setBleRole(const uAtClientHandle_t atHandle, int32_t role)
{
int32_t error;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTLE=");
uAtClientWriteInt(atHandle, role);
uAtClientCommandStopReadResponse(atHandle);
error = uAtClientUnlock(atHandle);
return error;
}
static int32_t getServer(const uAtClientHandle_t atHandle, int32_t type)
{
int32_t error;
int32_t found_id = -1;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UDSC");
uAtClientCommandStop(atHandle);
// Loop until we get OK, ERROR or timeout
while (uAtClientResponseStart(atHandle, "+UDSC:") == 0) {
int32_t id = uAtClientReadInt(atHandle);
if (uAtClientReadInt(atHandle) == type) {
found_id = id;
}
}
uAtClientResponseStop(atHandle);
error = uAtClientUnlock(atHandle);
if (error == 0) {
error = found_id;
}
return error;
}
static int32_t disableServer(const uAtClientHandle_t atHandle, int32_t serverId)
{
int32_t error;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UDSC=");
uAtClientWriteInt(atHandle, serverId);
uAtClientWriteInt(atHandle, 0);
uAtClientCommandStopReadResponse(atHandle);
error = uAtClientUnlock(atHandle);
return error;
}
static int32_t setServer(const uAtClientHandle_t atHandle, uShortRangeServerType_t type)
{
int32_t error;
int32_t id = -1;
bool found = false;
uAtClientLock(atHandle);
for (size_t y = 0; (y < U_BLE_CFG_MAX_NUM_SERVERS) &&
!found; y++) {
uAtClientCommandStart(atHandle, "AT+UDSC=");
uAtClientWriteInt(atHandle, (int32_t) y);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UDSC:");
id = uAtClientReadInt(atHandle);
if (uAtClientReadInt(atHandle) == (int32_t) U_SHORT_RANGE_SERVER_DISABLED) {
found = true;
uAtClientResponseStop(atHandle);
break;
}
uAtClientResponseStop(atHandle);
}
error = uAtClientUnlock(atHandle);
if (found && (error == 0)) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UDSC=");
uAtClientWriteInt(atHandle, id);
uAtClientWriteInt(atHandle, (int32_t) type);
uAtClientCommandStopReadResponse(atHandle);
error = uAtClientUnlock(atHandle);
}
return error;
}
static int32_t restart(const uAtClientHandle_t atHandle, bool store)
{
int32_t error = (int32_t) U_ERROR_COMMON_SUCCESS;
if (store) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT&W");
uAtClientCommandStopReadResponse(atHandle);
error = (int32_t)uAtClientUnlock(atHandle);
}
if (error == (int32_t)U_ERROR_COMMON_SUCCESS) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+CPWROFF");
uAtClientCommandStopReadResponse(atHandle);
error = (int32_t)uAtClientUnlock(atHandle);
if (error == (int32_t) U_ERROR_COMMON_SUCCESS) {
uPortTaskBlock(5000);
uAtClientFlush(atHandle);
}
}
return error;
}
static int32_t getSecurityOption(const uAtClientHandle_t atHandle)
{
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTST?");
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UBTST:");
int32_t opt = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
uAtClientUnlock(atHandle);
return opt;
}
static int32_t setSecurityOption(const uAtClientHandle_t atHandle, int32_t opt)
{
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTST=");
uAtClientWriteInt(atHandle, opt);
uAtClientCommandStopReadResponse(atHandle);
return uAtClientUnlock(atHandle);
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS THAT ARE PRIVATE TO BLE EXTMOD
* -------------------------------------------------------------- */
int32_t uBlePrivateGetRole(uDeviceHandle_t devHandle)
{
int32_t roleOrError;
uAtClientHandle_t atHandle;
roleOrError = uShortRangeAtClientHandleGet(devHandle, &atHandle);
if (roleOrError == 0) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTLE?");
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UBTLE:");
roleOrError = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
uAtClientUnlock(atHandle);
}
return roleOrError;
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
int32_t uBleCfgConfigure(uDeviceHandle_t devHandle,
const uBleCfg_t *pCfg)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
uShortRangePrivateInstance_t *pInstance;
uAtClientHandle_t atHandle;
if (pCfg != NULL) {
errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED;
if (uShortRangeLock() == (int32_t) U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
if (pInstance != NULL) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
bool restartNeeded = false;
atHandle = pInstance->atHandle;
int32_t role = uBlePrivateGetRole(devHandle);
if (role != (int32_t) pCfg->role) {
errorCode = setBleRole(atHandle, (int32_t) pCfg->role);
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
restartNeeded = true;
}
}
if (pCfg->spsServer) {
if (getServer(atHandle, U_BLE_CFG_SERVER_TYPE_SPS) < 0) {
errorCode = setServer(atHandle,
(uShortRangeServerType_t) U_BLE_CFG_SERVER_TYPE_SPS);
if (errorCode >= 0) {
restartNeeded = true;
}
}
} else {
int32_t spsServerId = getServer(atHandle, U_BLE_CFG_SERVER_TYPE_SPS);
if (spsServerId >= 0) {
disableServer(atHandle, spsServerId);
restartNeeded = true;
}
}
// ODIN-W2 only supports simple pairing security and
// therefore cannot be configured
if ((errorCode >= 0) &&
pInstance->pModule->moduleType != U_SHORT_RANGE_MODULE_TYPE_ODIN_W2) {
int32_t secOpt = getSecurityOption(atHandle);
if (secOpt != 1) {
errorCode = setSecurityOption(atHandle, 1);
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
restartNeeded = true;
}
}
}
int32_t mode = getStartupMode(atHandle);
if ((errorCode >= 0) &&
(mode != U_BLE_CFG_STARTUP_MODE_EDM)) {
errorCode = setStartupMode(atHandle, U_BLE_CFG_STARTUP_MODE_EDM);
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
restartNeeded = true;
}
}
if (errorCode >= 0 && restartNeeded) {
restart(atHandle, true);
}
}
uShortRangeUnlock();
}
}
return errorCode;
}
#endif
// End of file