Skip to content

Commit 789edc0

Browse files
committed
component/bt: Add API: get cod & set cod
In response to: #1565
1 parent 1a302cb commit 789edc0

File tree

6 files changed

+175
-5
lines changed

6 files changed

+175
-5
lines changed

components/bt/bluedroid/api/esp_gap_bt_api.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,41 @@ uint8_t *esp_bt_gap_resolve_eir_data(uint8_t *eir, esp_bt_eir_type_t type, uint8
141141

142142
return BTM_CheckEirData(eir, type, length);
143143
}
144+
145+
esp_err_t esp_bt_gap_set_cod(esp_bt_cod_t cod, esp_bt_cod_mode_t mode)
146+
{
147+
btc_msg_t msg;
148+
btc_gap_bt_args_t arg;
149+
150+
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
151+
return ESP_ERR_INVALID_STATE;
152+
}
153+
154+
switch (mode) {
155+
case ESP_BT_SET_COD_MAJOR_MINOR:
156+
case ESP_BT_SET_COD_SERVICE_CLASS:
157+
case ESP_BT_CLR_COD_SERVICE_CLASS:
158+
case ESP_BT_SET_COD_ALL:
159+
case ESP_BT_INIT_COD:
160+
break;
161+
default:
162+
return ESP_ERR_INVALID_ARG;
163+
break;
164+
}
165+
166+
msg.sig = BTC_SIG_API_CALL;
167+
msg.pid = BTC_PID_GAP_BT;
168+
msg.act = BTC_GAP_BT_ACT_SET_COD;
169+
170+
arg.set_cod.mode = mode;
171+
memcpy(&arg.set_cod.cod, &cod, sizeof(esp_bt_cod_t));
172+
return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
173+
}
174+
175+
176+
esp_err_t esp_bt_gap_get_cod(esp_bt_cod_t *cod)
177+
{
178+
return btc_gap_bt_get_cod(cod);
179+
}
180+
144181
#endif /* #if BTC_GAP_BT_INCLUDED == TRUE */

components/bt/bluedroid/api/include/esp_gap_bt_api.h

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@
2323
extern "C" {
2424
#endif
2525

26+
/// Class of device
27+
typedef struct {
28+
uint32_t reserved_2: 2; /*!< undefined */
29+
uint32_t minor: 6; /*!< minor class */
30+
uint32_t major: 5; /*!< major class */
31+
uint32_t service: 11; /*!< service class */
32+
uint32_t reserved_8: 8; /*!< undefined */
33+
} esp_bt_cod_t;
34+
35+
/// class of device settings
36+
typedef enum {
37+
ESP_BT_SET_COD_MAJOR_MINOR = 0x01, /*!< overwrite major, minor class */
38+
ESP_BT_SET_COD_SERVICE_CLASS = 0x02, /*!< set the bits in the input, the current bit will remain */
39+
ESP_BT_CLR_COD_SERVICE_CLASS = 0x04, /*!< clear the bits in the input, others will remain */
40+
ESP_BT_SET_COD_ALL = 0x08, /*!< overwrite major, minor, set the bits in service class */
41+
ESP_BT_INIT_COD = 0x0a, /*!< overwrite major, minor, and service class */
42+
} esp_bt_cod_mode_t;
2643

2744
/// Discoverability and Connectability mode
2845
typedef enum {
@@ -174,6 +191,7 @@ typedef union {
174191
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
175192
esp_bt_status_t stat; /*!< service search status */
176193
} rmt_srvc_rec; /*!< specific service record from remote device parameter struct */
194+
177195
} esp_bt_gap_cb_param_t;
178196

179197
/**
@@ -258,7 +276,7 @@ esp_err_t esp_bt_gap_register_callback(esp_bt_gap_cb_t callback);
258276
* @return
259277
* - ESP_OK : Succeed
260278
* - ESP_ERR_INVALID_ARG: if argument invalid
261-
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
279+
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
262280
* - ESP_FAIL: others
263281
*/
264282
esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode);
@@ -274,7 +292,7 @@ esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode);
274292
*
275293
* @return
276294
* - ESP_OK : Succeed
277-
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
295+
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
278296
* - ESP_ERR_INVALID_ARG: if invalid parameters are provided
279297
* - ESP_FAIL: others
280298
*/
@@ -286,7 +304,7 @@ esp_err_t esp_bt_gap_start_discovery(esp_bt_inq_mode_t mode, uint8_t inq_len, ui
286304
*
287305
* @return
288306
* - ESP_OK : Succeed
289-
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
307+
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
290308
* - ESP_FAIL: others
291309
*/
292310
esp_err_t esp_bt_gap_cancel_discovery(void);
@@ -297,7 +315,7 @@ esp_err_t esp_bt_gap_cancel_discovery(void);
297315
*
298316
* @return
299317
* - ESP_OK : Succeed
300-
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
318+
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
301319
* - ESP_FAIL: others
302320
*/
303321
esp_err_t esp_bt_gap_get_remote_services(esp_bd_addr_t remote_bda);
@@ -309,7 +327,7 @@ esp_err_t esp_bt_gap_get_remote_services(esp_bd_addr_t remote_bda);
309327
* esp_bt_gap_cb_t will is called with ESP_BT_GAP_RMT_SRVC_REC_EVT after service discovery ends
310328
* @return
311329
* - ESP_OK : Succeed
312-
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
330+
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
313331
* - ESP_FAIL: others
314332
*/
315333
esp_err_t esp_bt_gap_get_remote_service_record(esp_bd_addr_t remote_bda, esp_bt_uuid_t *uuid);
@@ -326,6 +344,34 @@ esp_err_t esp_bt_gap_get_remote_service_record(esp_bd_addr_t remote_bda, esp_bt_
326344
*/
327345
uint8_t *esp_bt_gap_resolve_eir_data(uint8_t *eir, esp_bt_eir_type_t type, uint8_t *length);
328346

347+
/**
348+
* @brief This function is called to set class of device.
349+
* esp_bt_gap_cb_t will is called with ESP_BT_GAP_SET_COD_EVT after set COD ends
350+
* Some profile have special restrictions on class of device,
351+
* changes may cause these profile do not work
352+
*
353+
* @param[in] cod - class of device
354+
* @param[in] mode - setting mode
355+
*
356+
* @return
357+
* - ESP_OK : Succeed
358+
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
359+
* - ESP_ERR_INVALID_ARG: if param is invalid
360+
* - ESP_FAIL: others
361+
*/
362+
esp_err_t esp_bt_gap_set_cod(esp_bt_cod_t cod, esp_bt_cod_mode_t mode);
363+
364+
/**
365+
* @brief This function is called to get class of device.
366+
*
367+
* @param[out] cod - class of device
368+
*
369+
* @return
370+
* - ESP_OK : Succeed
371+
* - ESP_FAIL: others
372+
*/
373+
esp_err_t esp_bt_gap_get_cod(esp_bt_cod_t *cod);
374+
329375
#ifdef __cplusplus
330376
}
331377
#endif

components/bt/bluedroid/bta/include/utl.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ extern void utl_freebuf(void **p);
136136
*******************************************************************************/
137137
extern BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd);
138138

139+
/*******************************************************************************
140+
**
141+
** Function utl_get_device_class
142+
**
143+
** Description This function get the local Device Class.
144+
**
145+
** Parameters:
146+
** p_cod - Pointer to the device class to get to
147+
**
148+
**
149+
** Returns TRUE if successful, Otherwise FALSE
150+
**
151+
*******************************************************************************/
152+
extern BOOLEAN utl_get_device_class(tBTA_UTL_COD *p_cod);
153+
139154
/*******************************************************************************
140155
**
141156
** Function utl_isintstr

components/bt/bluedroid/bta/sys/utl.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,37 @@ BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd)
235235
return FALSE;
236236
}
237237

238+
/*******************************************************************************
239+
**
240+
** Function utl_get_device_class
241+
**
242+
** Description This function get the local Device Class.
243+
**
244+
** Parameters:
245+
** p_cod - Pointer to the device class to get to
246+
**
247+
**
248+
** Returns TRUE if successful, Otherwise FALSE
249+
**
250+
*******************************************************************************/
251+
BOOLEAN utl_get_device_class(tBTA_UTL_COD *p_cod)
252+
{
253+
UINT8 *dev;
254+
UINT16 service;
255+
UINT8 minor, major;
256+
257+
dev = BTM_ReadDeviceClass();
258+
BTM_COD_SERVICE_CLASS( service, dev );
259+
BTM_COD_MINOR_CLASS(minor, dev );
260+
BTM_COD_MAJOR_CLASS(major, dev );
261+
262+
p_cod->minor = minor;
263+
p_cod->major = major;
264+
p_cod->service = service;
265+
266+
return TRUE;
267+
}
268+
238269
/*******************************************************************************
239270
**
240271
** Function utl_isintstr

components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,33 @@ static void search_services_copy_cb(btc_msg_t *msg, void *p_dest, void *p_src)
567567
}
568568
}
569569

570+
static void btc_gap_bt_set_cod(btc_gap_bt_args_t *arg)
571+
{
572+
tBTA_UTL_COD p_cod;
573+
esp_bt_cod_t *cod = &(arg->set_cod.cod);
574+
p_cod.minor = cod->minor << 2;
575+
p_cod.major = cod->major;
576+
p_cod.service = cod->service << 5;
577+
bool ret = utl_set_device_class(&p_cod, arg->set_cod.mode);
578+
if (!ret){
579+
LOG_ERROR("%s set class of device failed!",__func__);
580+
}
581+
}
582+
583+
esp_err_t btc_gap_bt_get_cod(esp_bt_cod_t *cod)
584+
{
585+
tBTA_UTL_COD p_cod;
586+
bool ret = utl_get_device_class(&p_cod);
587+
if (!ret){
588+
LOG_ERROR("%s get class of device failed!",__func__);
589+
return ESP_BT_STATUS_FAIL;
590+
}
591+
cod->minor = p_cod.minor >> 2;
592+
cod->major = p_cod.major;
593+
cod->service = p_cod.service >> 5;
594+
return ESP_BT_STATUS_SUCCESS;
595+
}
596+
570597
void btc_gap_bt_call_handler(btc_msg_t *msg)
571598
{
572599
btc_gap_bt_args_t *arg = (btc_gap_bt_args_t *)msg->arg;
@@ -604,6 +631,10 @@ void btc_gap_bt_call_handler(btc_msg_t *msg)
604631
btc_gap_bt_search_service_record(msg->aid, msg->arg);
605632
break;
606633
}
634+
case BTC_GAP_BT_ACT_SET_COD: {
635+
btc_gap_bt_set_cod(msg->arg);
636+
break;
637+
}
607638
default:
608639
break;
609640
}

components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "esp_bt_defs.h"
2020
#include "esp_gap_bt_api.h"
2121
#include "btc_task.h"
22+
#include "utl.h"
2223

2324
#if (BTC_GAP_BT_INCLUDED == TRUE)
2425

@@ -32,6 +33,7 @@ typedef enum {
3233
BTC_GAP_BT_ACT_SEARCH_SERVICES,
3334
BTC_GAP_BT_ACT_GET_REMOTE_SERVICE_RECORD,
3435
BTC_GAP_BT_ACT_SEARCH_SERVICE_RECORD,
36+
BTC_GAP_BT_ACT_SET_COD,
3537
} btc_gap_bt_act_t;
3638

3739
/* btc_bt_gap_args_t */
@@ -56,12 +58,20 @@ typedef union {
5658
bt_bdaddr_t bda;
5759
esp_bt_uuid_t uuid;
5860
} get_rmt_srv_rcd;
61+
62+
// BTC_GAP_BT_ACT_SET_COD
63+
struct set_cod_args {
64+
esp_bt_cod_t cod;
65+
esp_bt_cod_mode_t mode;
66+
} set_cod;
67+
5968
} btc_gap_bt_args_t;
6069

6170
void btc_gap_bt_call_handler(btc_msg_t *msg);
6271

6372
void btc_gap_bt_busy_level_updated(uint8_t bl_flags);
6473

74+
esp_err_t btc_gap_bt_get_cod(esp_bt_cod_t *cod);
6575
#endif /* #if BTC_GAP_BT_INCLUDED */
6676

6777
#endif /* __BTC_GAP_BT_H__ */

0 commit comments

Comments
 (0)