forked from u-blox/ubxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
u_short_range.h
466 lines (407 loc) · 18.4 KB
/
u_short_range.h
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* 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.
*/
#ifndef _U_SHORT_RANGE_H_
#define _U_SHORT_RANGE_H_
/* Only header files representing a direct and unavoidable
* dependency between the API of this module and the API
* of another module should be included here; otherwise
* please keep #includes to your .c files. */
#include "u_device.h"
#include "u_at_client.h"
#include "u_short_range_module_type.h"
/** \addtogroup _short-range __Short Range
* @{
*/
/** @file
* @brief This header file defines the ShortRange APIs. These APIs are not
* intended to be called directly, they are called only via the ble/wifi
* APIs. The ShortRange APIs are NOT generally thread-safe: the ble/wifi
* APIs add thread safety by calling uShortRangeLock()/uShortRangeUnlock()
* where appropriate.
*/
#ifdef __cplusplus
extern "C" {
#endif
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
#ifndef U_SHORT_RANGE_AT_BUFFER_LENGTH_BYTES
/** The buffer length required in the AT client by the ShortRange driver.
*/
# define U_SHORT_RANGE_AT_BUFFER_LENGTH_BYTES 4000
#endif
#ifndef U_SHORT_RANGE_UART_BUFFER_LENGTH_BYTES
/** UART buffer length. UART characters are placed in this buffer
* on arrival. EDM parser then consumes from this buffer.
*/
# define U_SHORT_RANGE_UART_BUFFER_LENGTH_BYTES 1024
#endif
#ifndef U_SHORT_RANGE_UART_BAUD_RATE
/** The default baud rate to communicate with short range module.
*/
# define U_SHORT_RANGE_UART_BAUD_RATE 115200
#endif
/** Bluetooth address length.
*/
#define U_SHORT_RANGE_BT_ADDRESS_LENGTH 6
/** IPv4 address length.
*/
#define U_SHORT_RANGE_IPv4_ADDRESS_LENGTH 4
/** IPv6 address length.
*/
#define U_SHORT_RANGE_IPv6_ADDRESS_LENGTH 16
/** Module/Device serial number length.
*/
#define U_SHORT_RANGE_SERIAL_NUMBER_LENGTH 20
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/** Error codes specific to short range.
*/
typedef enum {
U_SHORT_RANGE_ERROR_FORCE_32_BIT = 0x7FFFFFFF, /**< Force this enum to be 32 bit as it can be
used as a size also. */
U_SHORT_RANGE_ERROR_AT = U_ERROR_SHORT_RANGE_MAX, /**< -4096 if #U_ERROR_BASE is 0. */
U_SHORT_RANGE_ERROR_NOT_CONFIGURED = U_ERROR_SHORT_RANGE_MAX - 1, /**< -4097 if #U_ERROR_BASE is 0. */
U_SHORT_RANGE_ERROR_VALUE_OUT_OF_RANGE = U_ERROR_SHORT_RANGE_MAX - 2, /**< -4098 if #U_ERROR_BASE is 0. */
U_SHORT_RANGE_ERROR_INVALID_MODE = U_ERROR_SHORT_RANGE_MAX - 3, /**< -4099 if# U_ERROR_BASE is 0. */
U_SHORT_RANGE_ERROR_NOT_DETECTED = U_ERROR_SHORT_RANGE_MAX - 4, /**< -4100 if #U_ERROR_BASE is 0. */
U_SHORT_RANGE_ERROR_WRONG_TYPE = U_ERROR_SHORT_RANGE_MAX - 5, /**< -4101 if #U_ERROR_BASE is 0. */
U_SHORT_RANGE_ERROR_INIT_UART = U_ERROR_SHORT_RANGE_MAX - 6, /**< -4102 if #U_ERROR_BASE is 0. */
U_SHORT_RANGE_ERROR_INIT_EDM = U_ERROR_SHORT_RANGE_MAX - 7, /**< -4103 if #U_ERROR_BASE is 0. */
U_SHORT_RANGE_ERROR_INIT_ATCLIENT = U_ERROR_SHORT_RANGE_MAX - 8, /**< -4104 if #U_ERROR_BASE is 0. */
U_SHORT_RANGE_ERROR_INIT_INTERNAL = U_ERROR_SHORT_RANGE_MAX - 9, /**< -4105 if #U_ERROR_BASE is 0. */
} uShortRangeErrorCode_t;
typedef enum {
U_SHORT_RANGE_SERVER_DISABLED = 0,
U_SHORT_RANGE_SERVER_TCP = 1,
U_SHORT_RANGE_SERVER_UDP = 2,
U_SHORT_RANGE_SERVER_SPP = 3,
U_SHORT_RANGE_SERVER_DUN = 4,
U_SHORT_RANGE_SERVER_UUID = 5,
U_SHORT_RANGE_SERVER_SPS = 6,
U_SHORT_RANGE_SERVER_ATP = 7
} uShortRangeServerType_t;
typedef enum {
U_SHORT_RANGE_EVENT_CONNECTED,
U_SHORT_RANGE_EVENT_DISCONNECTED
} uShortRangeConnectionEventType_t;
typedef enum {
U_SHORT_RANGE_CONNECTION_TYPE_INVALID = -1,
U_SHORT_RANGE_CONNECTION_TYPE_BT = 0,
U_SHORT_RANGE_CONNECTION_TYPE_IP,
U_SHORT_RANGE_CONNECTION_TYPE_MQTT
} uShortRangeConnectionType_t;
typedef enum {
U_SHORT_RANGE_IP_PROTOCOL_TCP,
U_SHORT_RANGE_IP_PROTOCOL_UDP,
U_SHORT_RANGE_IP_PROTOCOL_MQTT
} uShortRangeIpProtocol_t;
typedef enum {
U_SHORT_RANGE_BT_PROFILE_SPP,
U_SHORT_RANGE_BT_PROFILE_DUN,
U_SHORT_RANGE_BT_PROFILE_SPS
} uShortRangeBtProfile_t;
typedef struct {
uShortRangeIpProtocol_t protocol;
uint8_t remoteAddress[U_SHORT_RANGE_IPv4_ADDRESS_LENGTH];
uint16_t remotePort;
uint8_t localAddress[U_SHORT_RANGE_IPv4_ADDRESS_LENGTH];
uint16_t localPort;
} uShortRangeConnectionIpv4_t;
typedef struct {
uShortRangeIpProtocol_t protocol;
uint8_t remoteAddress[U_SHORT_RANGE_IPv6_ADDRESS_LENGTH];
uint16_t remotePort;
uint8_t localAddress[U_SHORT_RANGE_IPv6_ADDRESS_LENGTH];
uint16_t localPort;
} uShortRangeConnectionIpv6_t;
typedef enum {
U_SHORT_RANGE_CONNECTION_IPv4,
U_SHORT_RANGE_CONNECTION_IPv6
} uShortRangeIpVersion_t;
// Enable anonymous unions inclusion for ARM compiler
#ifdef __arm__
// Stop GCC complaining
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma anon_unions
#pragma GCC diagnostic pop
#endif
typedef struct {
uShortRangeIpVersion_t type;
union {
uShortRangeConnectionIpv4_t ipv4;
uShortRangeConnectionIpv6_t ipv6;
};
} uShortRangeConnectDataIp_t;
typedef struct {
uShortRangeBtProfile_t profile;
uint8_t address[U_SHORT_RANGE_BT_ADDRESS_LENGTH];
uint16_t framesize;
} uShortRangeConnectDataBt_t;
typedef void (*uShortRangeBtConnectionStatusCallback_t)(uDeviceHandle_t devHandle,
int32_t connHandle,
uShortRangeConnectionEventType_t eventType,
uShortRangeConnectDataBt_t *pConnectData,
void *pCallbackParameter);
typedef void (*uShortRangeIpConnectionStatusCallback_t)(uDeviceHandle_t devHandle,
int32_t connHandle,
uShortRangeConnectionEventType_t eventType,
uShortRangeConnectDataIp_t *pConnectData,
void *pCallbackParameter);
//lint -esym(754, uShortRangeModuleInfo_t::supportsBtClassic) Suppress not referenced
typedef struct {
int32_t moduleType;
const char *pName;
bool supportsBle;
bool supportsBtClassic;
bool supportsWifi;
} uShortRangeModuleInfo_t;
typedef struct {
int32_t uartPort;
int32_t baudRate;
int32_t pinTx;
int32_t pinRx;
int32_t pinCts;
int32_t pinRts;
const char *pPrefix;
} uShortRangeUartConfig_t;
/* ----------------------------------------------------------------
* FUNCTIONS
* -------------------------------------------------------------- */
/** Initialise the short range driver. If the driver is already
* initialised then this function returns immediately.
*
* @return zero on success or negative error code on failure.
*/
int32_t uShortRangeInit();
/** Shut-down the short range driver. All short range instances
* will be removed internally with calls to uShortRangeRemove().
*/
void uShortRangeDeinit();
/** Locks the short range mutex.
* MUST be called before any of the below functions are!
* Will wait for the mutex if already locked.
*
* @return zero on success or negative error code on failure.
*/
int32_t uShortRangeLock();
/** Unlocks the short range mutex.
*
* @return zero on success or negative error code on failure.
*/
int32_t uShortRangeUnlock();
/** Open UART for a short range module, reboots the module and configures it for EDM stream handling.
*
* @param moduleType the short range module type.
* @param[in] pUartConfig the UART configuration to be used.
* if a short range instance has already been added
* for this pUartConfig an error will be returned.
* @param restart if true - module is restarted.
* @param[out] pDevHandle a pointer to the output handle. Will only be set on success.
* @return zero on success or negative error code on failure.
*/
int32_t uShortRangeOpenUart(uShortRangeModuleType_t moduleType,
const uShortRangeUartConfig_t *pUartConfig,
bool restart, uDeviceHandle_t *pDevHandle);
/** Closes and disconnects all associated handles, such as UART and EDM, for the short range instance
*
* @param devHandle the short range device handle to close.
*/
void uShortRangeClose(uDeviceHandle_t devHandle);
/** Detect the module connected to the handle. Will attempt to change the mode on
* the module to communicate with it. No change to UART configuration is done,
* so even if this fails, as last attempt to recover, it could work to re-init
* the UART on a different baud rate. This sould recover that module if another
* rate than the default one has been used.
*
* @param devHandle the short range device handle.
* @return Module on success, #U_SHORT_RANGE_MODULE_TYPE_INVALID
* on failure.
*/
uShortRangeModuleType_t uShortRangeDetectModule(uDeviceHandle_t devHandle);
/** Sends "AT" to the short range module on which it should repond with "OK"
* but takes no action. This checks that the module is ready to respond to commands.
*
* @param devHandle the short range device handle.
* @return zero on success or negative error code
* on failure.
*/
int32_t uShortRangeAttention(uDeviceHandle_t devHandle);
/** Set a callback for Bluetooth connection status.
*
* @param devHandle the short range device handle.
* @param[in] pCallback callback function.
* @param[in] pCallbackParameter parameter included with the callback.
* @return zero on success or negative error code
* on failure.
*/
int32_t uShortRangeSetBtConnectionStatusCallback(uDeviceHandle_t devHandle,
uShortRangeBtConnectionStatusCallback_t pCallback,
void *pCallbackParameter);
/** Set a callback for IP connection status.
*
* @param devHandle the short range device handle.
* @param[in] pCallback callback function.
* @param[in] pCallbackParameter parameter included with the callback.
* @return zero on success or negative error code
* on failure.
*/
int32_t uShortRangeSetIpConnectionStatusCallback(uDeviceHandle_t devHandle,
uShortRangeIpConnectionStatusCallback_t pCallback,
void *pCallbackParameter);
/** Set a callback for MQTT connection status.
*
* @param devHandle the short range device handle.
* @param[in] pCallback callback function.
* @param[in] pCallbackParameter parameter included with the callback.
* @return zero on success or negative error code
* on failure.
*/
int32_t uShortRangeSetMqttConnectionStatusCallback(uDeviceHandle_t devHandle,
uShortRangeIpConnectionStatusCallback_t pCallback,
void *pCallbackParameter);
/** Get the handle of the AT client used by the given
* short range instance.
*
* @param devHandle the short range device handle.
* @param[out] pAtHandle a place to put the AT client handle.
* @return zero on success else negative error code.
*/
int32_t uShortRangeAtClientHandleGet(uDeviceHandle_t devHandle,
uAtClientHandle_t *pAtHandle);
const uShortRangeModuleInfo_t *uShortRangeGetModuleInfo(int32_t moduleId);
/** Get the firmware version string from the short-range module.
*
* @param devHandle the short range device handle.
* @param[out] pStr a pointer to size bytes of storage into which
* the firmware version string will be copied.
* Room should be allowed for a null terminator,
* which will be added to terminate the string.
* This pointer cannot be NULL.
* @param size the number of bytes available at pStr, including
* room for a null terminator. Must be greater
* than zero.
* @return on success, the number of characters copied into
* pStr NOT including the terminator (as strlen()
* would return), on failure negative error code.
*/
int32_t uShortRangeGetFirmwareVersionStr(uDeviceHandle_t devHandle,
char *pStr, size_t size);
/** Check if a module type supports BLE
*
* @param moduleType the short range module type.
* @return true if moduleType supports BLE, false otherwise.
*/
bool uShortRangeSupportsBle(uShortRangeModuleType_t moduleType);
/** Check if a module type supports WiFi
*
* @param moduleType the short range module type.
* @return true if moduleType supports WiFi, false otherwise.
*/
bool uShortRangeSupportsWifi(uShortRangeModuleType_t moduleType);
/** Get the serial number of the module.
*
* @param devHandle the short range device handle.
* @param[out] pSerialNumber a pointer to storage of atleast
* #U_SHORT_RANGE_SERIAL_NUMBER_LENGTH bytes
* in which the product serial number will be stored;
* cannot be NULL.
* @return the length of the string copied into
* pSerialNumber (as strlen() would
* return) or negative error code.
*/
int32_t uShortRangeGetSerialNumber(uDeviceHandle_t devHandle, char *pSerialNumber);
/** Get the edm stream handle corresponding to short range handle.
*
* @param devHandle the short range device handle.
* @return an edm stream handle on success
* else negative error code.
*/
int32_t uShortRangeGetEdmStreamHandle(uDeviceHandle_t devHandle);
/** Get the UART handle corresponding to short range handle.
*
* @param devHandle the short range device handle.
* @return an UART handle on success
* else negative error code.
*/
int32_t uShortRangeGetUartHandle(uDeviceHandle_t devHandle);
/** Sets new UART baudrate for a short range module.
*
* VERY IMPORTANT: this function internally calls uShortRangeClose()
* and then uShortRangeOpenUart(), which will result in any callbacks
* that have been configured with uShortRangeSetIpConnectionStatusCallback(),
* uShortRangeSetIpConnectionStatusCallback() or
* uShortRangeSetMqttConnectionStatusCallback() being forgotten and
* any handles returned by uShortRangeAtClientHandleGet() and
* uShortRangeGetUartHandle() becoming invalid.
* Since uShortRangeClose()/uShortRangeOpenUart() also open and close
* the EDM stream, any settings there, including the EDM stream handle,
* will also be re-created.
*
* @param[in,out] pDevHandle a pointer to a short range device handle that has been
* opened with uShortRangeOpenUart(). If the reconfiguration
* of the UART settings succeeds a new device handle will
* be allocated and returned via this parameter.
* @param[in] pUartConfig the new UART configuration to be used.
* @return 0 on success or negative error code on failure.
*/
int32_t uShortRangeSetBaudrate(uDeviceHandle_t *pDevHandle,
const uShortRangeUartConfig_t *pUartConfig);
/** Configure a GPIO of a shortrange module.
*
* @param devHandle the handle of the shortrange device handle.
* @param gpioId the GPIO ID to configure (often the same as pin number).
* @param isOutput the direction, set to true for an output, false for
* an input.
* @param level the initial level to set, only used if isOutput
* is true; 0 for low or non-zero for high.
* @return zero on success else negative error code.
*/
int32_t uShortRangeGpioConfig(uDeviceHandle_t devHandle, int32_t gpioId, bool isOutput,
int32_t level);
/** Set the state of a GPIO of a shortrange module.
*
* @param devHandle the handle of the shortrange device handle.
* @param gpioId the GPIO ID to set (often the same as pin number).
* @param level the level to set, 0 for low or non-zero for high.
* @return zero on success else negative error code.
*/
int32_t uShortRangeGpioSet(uDeviceHandle_t devHandle, int32_t gpioId, int32_t level);
/** Resets the module settings to default values of a short-range module.
*
* @param pinResetToDefaults the pin of this MCU that MUST BE CONNECTED TO
* the DSR pin of the module. Note: the AT manual
* says that the module will be looking for the
* reset sequence on the "DTR line", but the
* "DTR line", the output from this MCU, which may
* be on this MCU's DTR pin, is conventionally
* connected to the DSR _pin_ of the short-range
* module; all very confusing. To be clear:
*
* THIS IS THE MCU PIN THAT IS CONNECTED TO THE
* **DSR** PIN OF THE SHORT-RANGE MODULE.
*
* @return zero on success else negative error code.
*/
int32_t uShortRangeResetToDefaultSettings(int32_t pinResetToDefaults);
#ifdef __cplusplus
}
#endif
/** @}*/
#endif // _U_SHORT_RANGE_H_
// End of file