/
jswrap_bluetooth.h
162 lines (143 loc) · 8.23 KB
/
jswrap_bluetooth.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
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2016 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* Contains JavaScript interface for nRF51/52 bluetooth
* ----------------------------------------------------------------------------
*/
#include "jspin.h"
#include "bluetooth.h"
// ------------------------------------------------------------------------------
typedef enum {
BLETASK_NONE,
BLETASK_REQUEST_DEVICE, ///< Waiting for requestDevice to finish (bleTaskInfo=index of a setTimeout to stop scanning)
BLETASK_CENTRAL_START, // =========================================== Start of central tasks
BLETASK_CONNECT = BLETASK_CENTRAL_START, ///< Connect in central mode (bleTaskInfo=BluetoothRemoteGATTServer)
BLETASK_DISCONNECT, ///< Disconnect from Central (bleTaskInfo=BluetoothRemoteGATTServer)
BLETASK_PRIMARYSERVICE, ///< Find primary service (bleTaskInfo=BluetoothDevice, bleTaskInfo2=populated with list of BluetoothRemoteGATTService)
BLETASK_CHARACTERISTIC, ///< Find characteristics (bleTaskInfo=BluetoothRemoteGATTService, bleTaskInfo2=populated with list of BluetoothRemoteGATTCharacteristic)
BLETASK_CHARACTERISTIC_WRITE, ///< Write to a characteristic (bleTaskInfo=0)
BLETASK_CHARACTERISTIC_READ, ///< Read from a characteristic (bleTaskInfo=BluetoothRemoteGATTCharacteristic)
BLETASK_CHARACTERISTIC_DESC_AND_STARTNOTIFY, ///< Discover descriptors and start notifications (bleTaskInfo=BluetoothRemoteGATTCharacteristic)
BLETASK_CHARACTERISTIC_NOTIFY, ///< Setting whether notifications are on or off (bleTaskInfo=BluetoothRemoteGATTCharacteristic)
BLETASK_BONDING, ///< Try and bond in central mode (bleTaskInfo=BluetoothRemoteGATTServer for central, or 0 for peripheral)
BLETASK_CENTRAL_END = BLETASK_CHARACTERISTIC_NOTIFY, // ============= End of central tasks
#ifdef ESPR_BLUETOOTH_ANCS
BLETASK_ANCS_NOTIF_ATTR, //< Apple Notification Centre notification attributes (bleTaskInfo=0)
BLETASK_ANCS_APP_ATTR, //< Apple Notification Centre app attributes (bleTaskInfo=appId string)
BLETASK_AMS_ATTR, //< Apple Media Service track info request (bleTaskInfo=0)
BLETASK_CTS_GET_TIME, //< CMS get current time
#endif
} BleTask;
#ifdef ESPR_BLUETOOTH_ANCS
#define BLETASK_STRINGS "NONE\0REQ_DEV\0CONNECT\0DISCONNECT\0SERVICE\0CHAR\0CHAR_WR\0CHAR_RD\0CHAR_DESC_NOTIFY\nCHAR_NOTIFY\0BOND\0ANCS_NOTIF_ATTR\0ANCS_APP_ATTR\0AMS_ATTR\0"
#else
#define BLETASK_STRINGS "NONE\0REQ_DEV\0CONNECT\0DISCONNECT\0SERVICE\0CHAR\0CHAR_WR\0CHAR_RD\0CHAR_DESC_NOTIFY\nCHAR_NOTIFY\0BOND\0"
#endif
// Is this task related to BLE central mode?
#define BLETASK_IS_CENTRAL(x) ((x)>=BLETASK_CENTRAL_START && ((x)<=BLETASK_CENTRAL_END))
#ifdef ESPR_BLUETOOTH_ANCS
#define BLETASK_IS_ANCS(x) ((x)==BLETASK_ANCS_NOTIF_ATTR || ((x)==BLETASK_ANCS_APP_ATTR))
#define BLETASK_IS_AMS(x) ((x)==BLETASK_AMS_ATTR)
#endif
extern JsVar *bleTaskInfo; // info related to the current task
extern JsVar *bleTaskInfo2; // info related to the current task
const char *bleGetTaskString(BleTask task);
bool bleInTask(BleTask task);
BleTask bleGetCurrentTask();
bool bleNewTask(BleTask task, JsVar *taskInfo);
void bleCompleteTaskSuccess(BleTask task, JsVar *data);
void bleCompleteTaskSuccessAndUnLock(BleTask task, JsVar *data);
void bleCompleteTaskFail(BleTask task, JsVar *data);
void bleCompleteTaskFailAndUnLock(BleTask task, JsVar *data);
void bleSwitchTask(BleTask task);
#if CENTRAL_LINK_COUNT>0
// Set the currently active GATT server based on the index in m_central_conn_handles
void bleSetActiveBluetoothGattServer(int idx, JsVar *var);
// Get the currently active GATT server based on the index in m_central_conn_handles (the return value needs unlocking)
JsVar *bleGetActiveBluetoothGattServer(int idx);
uint16_t jswrap_ble_BluetoothRemoteGATTServer_getHandle(JsVar *parent);
uint16_t jswrap_ble_BluetoothDevice_getHandle(JsVar *parent);
uint16_t jswrap_ble_BluetoothRemoteGATTService_getHandle(JsVar *parent);
uint16_t jswrap_ble_BluetoothRemoteGATTCharacteristic_getHandle(JsVar *parent);
#endif
// ------------------------------------------------------------------------------
void jswrap_ble_init();
bool jswrap_ble_idle();
void jswrap_ble_kill();
// Used to dump bluetooth initialisation info for 'dump'
void jswrap_ble_dumpBluetoothInitialisation(vcbprintf_callback user_callback, void *user_data);
/** Reconfigure the softdevice (on init or after restart) to have all the services/advertising we need */
void jswrap_ble_reconfigure_softdevice();
// ------------------------------------------------------------------------------
void jswrap_ble_disconnect();
void jswrap_ble_sleep();
void jswrap_ble_wake();
void jswrap_ble_restart(JsVar *callback);
void jswrap_ble_eraseBonds();
JsVar *jswrap_ble_getAddress();
void jswrap_ble_setAddress(JsVar *address);
JsVar *jswrap_ble_resolveAddress(JsVar *address);
/// Used by bluetooth.c internally when it needs to set up advertising at first
JsVar *jswrap_ble_getCurrentAdvertisingData();
JsVarFloat jswrap_ble_getBattery();
void jswrap_ble_setAdvertising(JsVar *data, JsVar *options);
JsVar *jswrap_ble_getAdvertisingData(JsVar *data, JsVar *options);
void jswrap_ble_setScanResponse(JsVar *data);
void jswrap_ble_setServices(JsVar *data, JsVar *options);
void jswrap_ble_updateServices(JsVar *data);
void jswrap_ble_setScan(JsVar *callback, JsVar *options);
JsVar *jswrap_ble_filterDevices(JsVar *devices, JsVar *filters);
void jswrap_ble_findDevices(JsVar *callback, JsVar *options);
void jswrap_ble_setRSSIHandler(JsVar *callback);
void jswrap_ble_setTxPower(JsVarInt pwr);
void jswrap_ble_setLowPowerConnection(bool lowPower);
void jswrap_nfc_URL(JsVar *url);
void jswrap_nfc_pair(JsVar *key);
void jswrap_nfc_androidApp(JsVar *appName);
void jswrap_nfc_raw(JsVar *payload);
JsVar *jswrap_nfc_start(JsVar *payload);
void jswrap_nfc_stop();
void jswrap_nfc_send(JsVar *payload);
// BLE_HIDS_ENABLED
void jswrap_ble_sendHIDReport(JsVar *data, JsVar *callback);
// if ESPR_BLUETOOTH_ANCS
bool jswrap_ble_ancsIsActive();
void jswrap_ble_ancsAction(int uid, bool isPositive);
JsVar *jswrap_ble_ancsGetNotificationInfo(JsVarInt uid);
JsVar *jswrap_ble_ancsGetAppInfo(JsVar *appId);
bool jswrap_ble_amsIsActive();
JsVar *jswrap_ble_amsGetPlayerInfo(JsVar *id);
JsVar *jswrap_ble_amsGetTrackInfo(JsVar *id);
void jswrap_ble_amsCommand(JsVar *id);
bool jswrap_ble_ctsIsActive();
JsVar *jswrap_ble_ctsGetTime();
JsVar *jswrap_ble_requestDevice(JsVar *options);
JsVar *jswrap_ble_connect(JsVar *mac, JsVar *options);
void jswrap_ble_setWhitelist(bool whitelist);
void jswrap_ble_setConnectionInterval(JsVar *interval);
void jswrap_ble_setSecurity(JsVar *options);
JsVar *jswrap_ble_getSecurityStatus(JsVar *parent);
JsVar *jswrap_ble_startBonding(bool forceRePair);
JsVar *jswrap_BluetoothDevice_gatt(JsVar *parent);
void jswrap_ble_BluetoothDevice_sendPasskey(JsVar *parent, JsVar *passkeyVar);
JsVar *jswrap_ble_BluetoothRemoteGATTServer_connect(JsVar *parent, JsVar *options);
JsVar *jswrap_BluetoothRemoteGATTServer_disconnect(JsVar *parent);
JsVar *jswrap_ble_BluetoothRemoteGATTServer_startBonding(JsVar *parent, bool forceRePair);
JsVar *jswrap_ble_BluetoothRemoteGATTServer_getSecurityStatus(JsVar *parent);
JsVar *jswrap_BluetoothRemoteGATTServer_getPrimaryService(JsVar *parent, JsVar *service);
JsVar *jswrap_BluetoothRemoteGATTServer_getPrimaryServices(JsVar *parent);
void jswrap_BluetoothRemoteGATTServer_setRSSIHandler(JsVar *parent, JsVar *callback);
JsVar *jswrap_BluetoothRemoteGATTService_getCharacteristic(JsVar *parent, JsVar *characteristic);
JsVar *jswrap_BluetoothRemoteGATTService_getCharacteristics(JsVar *parent);
JsVar *jswrap_ble_BluetoothRemoteGATTCharacteristic_writeValue(JsVar *characteristic, JsVar *data);
JsVar *jswrap_ble_BluetoothRemoteGATTCharacteristic_readValue(JsVar *characteristic);
JsVar *jswrap_ble_BluetoothRemoteGATTCharacteristic_startNotifications(JsVar *characteristic);
JsVar *jswrap_ble_BluetoothRemoteGATTCharacteristic_stopNotifications(JsVar *characteristic);
void jswrap_ble_powerusage(JsVar *devices);