1 /*
2  * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <protocomm.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /**
16  * BLE device name cannot be larger than this value
17  * 31 bytes (max scan response size) - 1 byte (length) - 1 byte (type) = 29 bytes
18  */
19 #define MAX_BLE_DEVNAME_LEN 29
20 #define BLE_UUID128_VAL_LENGTH  16
21  /**
22  * Theoretically, the limit for max manufacturer length remains same as BLE
23  * device name i.e. 31 bytes (max scan response size) - 1 byte (length) - 1
24  * byte (type) = 29 bytes
25  * However, manufacturer data goes along with BLE device name in scan response.
26  * So, it is important to understand the actual length should be smaller than
27  * (29 - (BLE device name length) - 2). */
28 #define MAX_BLE_MANUFACTURER_DATA_LEN 29
29 
30 /**
31  * @brief   This structure maps handler required by protocomm layer to
32  *          UUIDs which are used to uniquely identify BLE characteristics
33  *          from a smartphone or a similar client device.
34  */
35 typedef struct name_uuid {
36     /**
37      * Name of the handler, which is passed to protocomm layer
38      */
39     const char *name;
40 
41     /**
42      * UUID to be assigned to the BLE characteristic which is
43      * mapped to the handler
44      */
45     uint16_t uuid;
46 } protocomm_ble_name_uuid_t;
47 
48 /**
49  * @brief   Config parameters for protocomm BLE service
50  */
51 typedef struct protocomm_ble_config {
52     /**
53      * BLE device name being broadcast at the time of provisioning
54      */
55     char         device_name[MAX_BLE_DEVNAME_LEN];
56 
57     /**
58      * 128 bit UUID of the provisioning service
59      */
60     uint8_t      service_uuid[BLE_UUID128_VAL_LENGTH];
61 
62     /**
63      * BLE device manufacturer data pointer in advertisement
64      */
65     uint8_t      *manufacturer_data;
66 
67     /**
68      * BLE device manufacturer data length in advertisement
69      */
70     ssize_t      manufacturer_data_len;
71 
72     /**
73      * Number of entries in the Name-UUID lookup table
74      */
75     ssize_t      nu_lookup_count;
76 
77     /**
78      * Pointer to the Name-UUID lookup table
79      */
80     protocomm_ble_name_uuid_t *nu_lookup;
81 
82     /* BLE bonding */
83     unsigned ble_bonding:1;
84 
85 } protocomm_ble_config_t;
86 
87 /**
88  * @brief   Start Bluetooth Low Energy based transport layer for provisioning
89  *
90  * Initialize and start required BLE service for provisioning. This includes
91  * the initialization for characteristics/service for BLE.
92  *
93  * @param[in] pc        Protocomm instance pointer obtained from protocomm_new()
94  * @param[in] config    Pointer to config structure for initializing BLE
95  *
96  * @return
97  *  - ESP_OK : Success
98  *  - ESP_FAIL : Simple BLE start error
99  *  - ESP_ERR_NO_MEM : Error allocating memory for internal resources
100  *  - ESP_ERR_INVALID_STATE : Error in ble config
101  *  - ESP_ERR_INVALID_ARG : Null arguments
102  */
103 esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *config);
104 
105 /**
106  * @brief   Stop Bluetooth Low Energy based transport layer for provisioning
107  *
108  * Stops service/task responsible for BLE based interactions for provisioning
109  *
110  * @note    You might want to optionally reclaim memory from Bluetooth.
111  *          Refer to the documentation of `esp_bt_mem_release` in that case.
112  *
113  * @param[in] pc    Same protocomm instance that was passed to protocomm_ble_start()
114  *
115  * @return
116  *  - ESP_OK : Success
117  *  - ESP_FAIL : Simple BLE stop error
118  *  - ESP_ERR_INVALID_ARG : Null / incorrect protocomm instance
119  */
120 esp_err_t protocomm_ble_stop(protocomm_t *pc);
121 
122 #ifdef __cplusplus
123 }
124 #endif
125