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 "host/ble_hs_adv.h"
22 #include "ble_hs_priv.h"
23
24 #define BLE_IBEACON_MFG_DATA_SIZE 25
25
26 /**
27 * Configures the device to advertise iBeacons.
28 *
29 * @param uuid The 128-bit UUID to advertise.
30 * @param major The major version number to include in
31 * iBeacons.
32 * @param minor The minor version number to include in
33 * iBeacons.
34 * @param measured_power The Measured Power (RSSI value at 1 Meter).
35 *
36 * @return 0 on success;
37 * BLE_HS_EBUSY if advertising is in progress;
38 * Other nonzero on failure.
39 */
40 int
ble_ibeacon_set_adv_data(void * uuid128,uint16_t major,uint16_t minor,int8_t measured_power)41 ble_ibeacon_set_adv_data(void *uuid128, uint16_t major,
42 uint16_t minor, int8_t measured_power)
43 {
44 struct ble_hs_adv_fields fields;
45 uint8_t buf[BLE_IBEACON_MFG_DATA_SIZE];
46 int rc;
47
48 /** Company identifier (Apple). */
49 buf[0] = 0x4c;
50 buf[1] = 0x00;
51
52 /** iBeacon indicator. */
53 buf[2] = 0x02;
54 buf[3] = 0x15;
55
56 /** UUID. */
57 memcpy(buf + 4, uuid128, 16);
58
59 /** Version number. */
60 put_be16(buf + 20, major);
61 put_be16(buf + 22, minor);
62
63 /* Measured Power ranging data (Calibrated tx power at 1 meters). */
64 if (measured_power < -126 || measured_power > 20) {
65 return BLE_HS_EINVAL;
66 }
67 buf[24] = measured_power;
68
69 memset(&fields, 0, sizeof fields);
70 fields.mfg_data = buf;
71 fields.mfg_data_len = sizeof buf;
72
73 /* Advertise two flags:
74 * o Discoverability in forthcoming advertisement (general)
75 * o BLE-only (BR/EDR unsupported).
76 */
77 fields.flags = BLE_HS_ADV_F_DISC_GEN |
78 BLE_HS_ADV_F_BREDR_UNSUP;
79
80 rc = ble_gap_adv_set_fields(&fields);
81 return rc;
82 }
83