-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathble_client.c
252 lines (216 loc) · 8.13 KB
/
ble_client.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
/*
* Copyright (c) 2015, Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <string.h>
#include "cfw/cfw.h"
#include "cfw/cfw_debug.h"
#include "cfw/cfw_messages.h"
#include "cfw/cfw_internal.h"
#include "cfw/cfw_service.h"
#include "cfw_platform.h"
#include "infra/time.h"
#include "infra/factory_data.h"
#include "infra/version.h"
#include "curie_factory_data.h"
#include "portable.h"
#include "uart.h"
#include "ipc_uart_ns16550.h"
#include "infra/ipc_uart.h"
#include "ble_client.h"
#include "platform.h"
#include "infra/log.h"
// APP callback
static ble_client_connect_event_cb_t ble_client_connect_event_cb = NULL;
static void *ble_client_connect_event_param;
static ble_client_disconnect_event_cb_t ble_client_disconnect_event_cb = NULL;
static void *ble_client_disconnect_event_param;
static ble_client_update_param_event_cb_t ble_client_update_param_event_cb = NULL;
static void *ble_client_update_param_event_param;
#define NIBBLE_TO_CHAR(n) \
((n) >= 0xA ? ('A' + (n) - 0xA) : ('0' + (n)))
#define BYTE_TO_STR(s, byte) \
do { \
*s++ = NIBBLE_TO_CHAR(byte >> 4); \
*s++ = NIBBLE_TO_CHAR(byte & 0xF); \
}while(0)
#ifdef __cplusplus
extern "C" {
#endif
static bool initialized = false;
inline void ble_cfw_init() {
if (!initialized) {
ble_cfw_service_init(BLE_SERVICE_ID, cfw_get_service_queue());
initialized = true;
}
}
static void on_connected(bt_conn_t *conn, uint8_t err)
{
if (ble_client_connect_event_cb)
{
ble_client_connect_event_cb(conn, err, ble_client_connect_event_param);
}
}
static void on_disconnected(bt_conn_t *conn, uint8_t reason)
{
if (ble_client_disconnect_event_cb)
{
ble_client_disconnect_event_cb(conn, reason, ble_client_disconnect_event_param);
}
}
static void on_le_param_updated(bt_conn_t *conn, uint16_t interval,
uint16_t latency, uint16_t timeout)
{
if (ble_client_update_param_event_cb)
{
ble_client_update_param_event_cb (conn,
interval,
latency,
timeout,
ble_client_update_param_event_param);
}
}
static struct bt_conn_cb conn_callbacks = {
.connected = on_connected,
.disconnected = on_disconnected,
.le_param_updated = on_le_param_updated
};
void ble_client_get_mac_address(bt_addr_le_t *bda)
{
struct curie_oem_data *p_oem = NULL;
unsigned i;
/* Set the MAC address defined in Factory Data (if provided)
* Otherwise, the device will default to a static random address */
if (bda) {
bda->type = BLE_DEVICE_ADDR_INVALID;
if (!strncmp((char*)global_factory_data->oem_data.magic, FACTORY_DATA_MAGIC, 4)) {
p_oem = (struct curie_oem_data *) &global_factory_data->oem_data.project_data;
if (p_oem->bt_mac_address_type < 2) {
bda->type = p_oem->bt_mac_address_type;
for (i = 0; i < BLE_ADDR_LEN; i++)
bda->val[i] = p_oem->bt_address[BLE_ADDR_LEN - 1 - i];
}
}
}
}
void ble_client_get_factory_config(bt_addr_le_t *bda, char *name)
{
struct curie_oem_data *p_oem = NULL;
ble_client_get_mac_address(bda);
/* Set a default name if one has not been specified */
if (name) {
// Need to check in the OTP if there is some board name set
// If yes, let's read it, otherwise let's keep the default
// name set in BLE_DEVICE_NAME_DEFAULT_PREFIX
const struct customer_data* otp_data_ptr = (struct customer_data*)(FACTORY_DATA_ADDR + 0x200);
char *suffix;
// checking the presence of key patterns
if ((otp_data_ptr->patternKeyStart == PATTERN_KEY_START) &&
(otp_data_ptr->patternKeyEnd == PATTERN_KEY_END))
{
// The board name is with OTP ar programmed
uint8_t len = otp_data_ptr->board_name_len;
// We need to reserve 5 bytes for '-' and 4 last MAC address in ASCII
if (len > BLE_MAX_DEVICE_NAME - 5) len = BLE_MAX_DEVICE_NAME - 5;
strncpy(name, (const char *)otp_data_ptr->board_name, len);
suffix = name + len;
}
else
{
// There is no board name in the OTP area
suffix = name + strlen(BLE_DEVICE_NAME_DEFAULT_PREFIX);
strcpy(name, BLE_DEVICE_NAME_DEFAULT_PREFIX);
}
// Adding the four last digits of MAC address separated by '-' sufix
if (bda && bda->type != BLE_DEVICE_ADDR_INVALID)
{
*suffix++ = '-';
p_oem = (struct curie_oem_data *) &global_factory_data->oem_data.project_data;
BYTE_TO_STR(suffix, p_oem->bt_address[4]);
BYTE_TO_STR(suffix, p_oem->bt_address[5]);
*suffix = 0; /* NULL-terminate the string. Note the macro BYTE_TO_STR
automatically move the pointer */
}
else
{
/* This code segment will be only reached if Curie module was not
provisioned properly with a BLE MAC address*/
*suffix++ = 0; /* NULL-terminate the string */
}
}
}
void ble_client_init(ble_client_connect_event_cb_t connect_cb, void* connect_param,
ble_client_disconnect_event_cb_t disconnect_cb, void* disconnect_param,
ble_client_update_param_event_cb_t update_param_cb, void* update_param_param)
{
//uint32_t delay_until;
pr_info(LOG_MODULE_BLE, "%s", __FUNCTION__);
ble_client_connect_event_cb = connect_cb;
ble_client_connect_event_param = connect_param;
ble_client_disconnect_event_cb = disconnect_cb;
ble_client_disconnect_event_param = disconnect_param;
ble_client_update_param_event_cb = update_param_cb;
ble_client_update_param_event_param = update_param_param;
bt_conn_cb_register(&conn_callbacks);
return;
}
BLE_STATUS_T errorno_to_ble_status(int err)
{
BLE_STATUS_T err_code;
err = 0 - err;
switch(err) {
case 0:
err_code = BLE_STATUS_SUCCESS;
break;
case EIO:
err_code = BLE_STATUS_WRONG_STATE;
break;
case EBUSY:
err_code = BLE_STATUS_TIMEOUT;
break;
case EFBIG:
case ENOTSUP:
err_code = BLE_STATUS_NOT_SUPPORTED;
break;
case EPERM:
case EACCES:
err_code = BLE_STATUS_NOT_ALLOWED;
break;
case ENOMEM: // No memeory
err_code = BLE_STATUS_NO_MEMORY;
break;
default:
err_code = BLE_STATUS_ERROR;
break;
}
return err_code;
}
#ifdef __cplusplus
}
#endif