1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include <string.h>
21 #include "securec.h"
22 #include "host/ble_hs_adv.h"
23 #include "ble_hs_priv.h"
24
25 #define BLE_IBEACON_MFG_DATA_SIZE 25
26
27 /**
28 * Configures the device to advertise iBeacons.
29 *
30 * @param uuid The 128-bit UUID to advertise.
31 * @param major The major version number to include in
32 * iBeacons.
33 * @param minor The minor version number to include in
34 * iBeacons.
35 * @param measured_power The Measured Power (RSSI value at 1 Meter).
36 *
37 * @return 0 on success;
38 * BLE_HS_EBUSY if advertising is in progress;
39 * Other nonzero on failure.
40 */
ble_ibeacon_set_adv_data(void * uuid128,uint16_t major,uint16_t minor,int8_t measured_power)41 int ble_ibeacon_set_adv_data(void *uuid128, uint16_t major, uint16_t minor, int8_t measured_power)
42 {
43 struct ble_hs_adv_fields fields;
44 uint8_t buf[BLE_IBEACON_MFG_DATA_SIZE];
45 int rc;
46 /** Company identifier (Apple). */
47 buf[0] = 0x4c;
48 buf[1] = 0x00;
49 /** iBeacon indicator. */
50 buf[2] = 0x02; // 2:array element
51 buf[3] = 0x15; // 3:array element
52 /** UUID. */
53 memcpy_s(buf + 4, sizeof(buf + 4), uuid128, 16); // 4:byte alignment, 16:size
54 /** Version number. */
55 put_be16(buf + 20, major); // 20:byte alignment
56 put_be16(buf + 22, minor); // 22:byte alignment
57
58 /* Measured Power ranging data (Calibrated tx power at 1 meters). */
59 if (measured_power < -126 || measured_power > 20) { // -126:Analyzing conditions, 20:Analyzing conditions
60 return BLE_HS_EINVAL;
61 }
62
63 buf[24] = measured_power; // 24:array element
64 memset_s(&fields, sizeof fields, 0, sizeof fields);
65 fields.mfg_data = buf;
66 fields.mfg_data_len = sizeof buf;
67 /* Advertise two flags:
68 * o Discoverability in forthcoming advertisement (general)
69 * o BLE-only (BR/EDR unsupported).
70 */
71 fields.flags = BLE_HS_ADV_F_DISC_GEN |
72 BLE_HS_ADV_F_BREDR_UNSUP;
73 rc = ble_gap_adv_set_fields(&fields);
74 return rc;
75 }