Skip to content

Commit

Permalink
component/bt: Add API: get cod & set cod
Browse files Browse the repository at this point in the history
In response to: #1565
  • Loading branch information
blueMoodBHD committed Apr 9, 2018
1 parent 1a302cb commit 789edc0
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 5 deletions.
37 changes: 37 additions & 0 deletions components/bt/bluedroid/api/esp_gap_bt_api.c
Expand Up @@ -141,4 +141,41 @@ uint8_t *esp_bt_gap_resolve_eir_data(uint8_t *eir, esp_bt_eir_type_t type, uint8

return BTM_CheckEirData(eir, type, length);
}

esp_err_t esp_bt_gap_set_cod(esp_bt_cod_t cod, esp_bt_cod_mode_t mode)
{
btc_msg_t msg;
btc_gap_bt_args_t arg;

if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
return ESP_ERR_INVALID_STATE;
}

switch (mode) {
case ESP_BT_SET_COD_MAJOR_MINOR:
case ESP_BT_SET_COD_SERVICE_CLASS:
case ESP_BT_CLR_COD_SERVICE_CLASS:
case ESP_BT_SET_COD_ALL:
case ESP_BT_INIT_COD:
break;
default:
return ESP_ERR_INVALID_ARG;
break;
}

msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_ACT_SET_COD;

arg.set_cod.mode = mode;
memcpy(&arg.set_cod.cod, &cod, sizeof(esp_bt_cod_t));
return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}


esp_err_t esp_bt_gap_get_cod(esp_bt_cod_t *cod)
{
return btc_gap_bt_get_cod(cod);
}

#endif /* #if BTC_GAP_BT_INCLUDED == TRUE */
56 changes: 51 additions & 5 deletions components/bt/bluedroid/api/include/esp_gap_bt_api.h
Expand Up @@ -23,6 +23,23 @@
extern "C" {
#endif

/// Class of device
typedef struct {
uint32_t reserved_2: 2; /*!< undefined */
uint32_t minor: 6; /*!< minor class */
uint32_t major: 5; /*!< major class */
uint32_t service: 11; /*!< service class */
uint32_t reserved_8: 8; /*!< undefined */
} esp_bt_cod_t;

/// class of device settings
typedef enum {
ESP_BT_SET_COD_MAJOR_MINOR = 0x01, /*!< overwrite major, minor class */
ESP_BT_SET_COD_SERVICE_CLASS = 0x02, /*!< set the bits in the input, the current bit will remain */
ESP_BT_CLR_COD_SERVICE_CLASS = 0x04, /*!< clear the bits in the input, others will remain */
ESP_BT_SET_COD_ALL = 0x08, /*!< overwrite major, minor, set the bits in service class */
ESP_BT_INIT_COD = 0x0a, /*!< overwrite major, minor, and service class */
} esp_bt_cod_mode_t;

/// Discoverability and Connectability mode
typedef enum {
Expand Down Expand Up @@ -174,6 +191,7 @@ typedef union {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
esp_bt_status_t stat; /*!< service search status */
} rmt_srvc_rec; /*!< specific service record from remote device parameter struct */

} esp_bt_gap_cb_param_t;

/**
Expand Down Expand Up @@ -258,7 +276,7 @@ esp_err_t esp_bt_gap_register_callback(esp_bt_gap_cb_t callback);
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_ARG: if argument invalid
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode);
Expand All @@ -274,7 +292,7 @@ esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode);
*
* @return
* - ESP_OK : Succeed
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_ARG: if invalid parameters are provided
* - ESP_FAIL: others
*/
Expand All @@ -286,7 +304,7 @@ esp_err_t esp_bt_gap_start_discovery(esp_bt_inq_mode_t mode, uint8_t inq_len, ui
*
* @return
* - ESP_OK : Succeed
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_cancel_discovery(void);
Expand All @@ -297,7 +315,7 @@ esp_err_t esp_bt_gap_cancel_discovery(void);
*
* @return
* - ESP_OK : Succeed
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_get_remote_services(esp_bd_addr_t remote_bda);
Expand All @@ -309,7 +327,7 @@ esp_err_t esp_bt_gap_get_remote_services(esp_bd_addr_t remote_bda);
* esp_bt_gap_cb_t will is called with ESP_BT_GAP_RMT_SRVC_REC_EVT after service discovery ends
* @return
* - ESP_OK : Succeed
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_get_remote_service_record(esp_bd_addr_t remote_bda, esp_bt_uuid_t *uuid);
Expand All @@ -326,6 +344,34 @@ esp_err_t esp_bt_gap_get_remote_service_record(esp_bd_addr_t remote_bda, esp_bt_
*/
uint8_t *esp_bt_gap_resolve_eir_data(uint8_t *eir, esp_bt_eir_type_t type, uint8_t *length);

/**
* @brief This function is called to set class of device.
* esp_bt_gap_cb_t will is called with ESP_BT_GAP_SET_COD_EVT after set COD ends
* Some profile have special restrictions on class of device,
* changes may cause these profile do not work
*
* @param[in] cod - class of device
* @param[in] mode - setting mode
*
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_ARG: if param is invalid
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_set_cod(esp_bt_cod_t cod, esp_bt_cod_mode_t mode);

/**
* @brief This function is called to get class of device.
*
* @param[out] cod - class of device
*
* @return
* - ESP_OK : Succeed
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_get_cod(esp_bt_cod_t *cod);

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions components/bt/bluedroid/bta/include/utl.h
Expand Up @@ -136,6 +136,21 @@ extern void utl_freebuf(void **p);
*******************************************************************************/
extern BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd);

/*******************************************************************************
**
** Function utl_get_device_class
**
** Description This function get the local Device Class.
**
** Parameters:
** p_cod - Pointer to the device class to get to
**
**
** Returns TRUE if successful, Otherwise FALSE
**
*******************************************************************************/
extern BOOLEAN utl_get_device_class(tBTA_UTL_COD *p_cod);

/*******************************************************************************
**
** Function utl_isintstr
Expand Down
31 changes: 31 additions & 0 deletions components/bt/bluedroid/bta/sys/utl.c
Expand Up @@ -235,6 +235,37 @@ BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd)
return FALSE;
}

/*******************************************************************************
**
** Function utl_get_device_class
**
** Description This function get the local Device Class.
**
** Parameters:
** p_cod - Pointer to the device class to get to
**
**
** Returns TRUE if successful, Otherwise FALSE
**
*******************************************************************************/
BOOLEAN utl_get_device_class(tBTA_UTL_COD *p_cod)
{
UINT8 *dev;
UINT16 service;
UINT8 minor, major;

dev = BTM_ReadDeviceClass();
BTM_COD_SERVICE_CLASS( service, dev );
BTM_COD_MINOR_CLASS(minor, dev );
BTM_COD_MAJOR_CLASS(major, dev );

p_cod->minor = minor;
p_cod->major = major;
p_cod->service = service;

return TRUE;
}

/*******************************************************************************
**
** Function utl_isintstr
Expand Down
31 changes: 31 additions & 0 deletions components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c
Expand Up @@ -567,6 +567,33 @@ static void search_services_copy_cb(btc_msg_t *msg, void *p_dest, void *p_src)
}
}

static void btc_gap_bt_set_cod(btc_gap_bt_args_t *arg)
{
tBTA_UTL_COD p_cod;
esp_bt_cod_t *cod = &(arg->set_cod.cod);
p_cod.minor = cod->minor << 2;
p_cod.major = cod->major;
p_cod.service = cod->service << 5;
bool ret = utl_set_device_class(&p_cod, arg->set_cod.mode);
if (!ret){
LOG_ERROR("%s set class of device failed!",__func__);
}
}

esp_err_t btc_gap_bt_get_cod(esp_bt_cod_t *cod)
{
tBTA_UTL_COD p_cod;
bool ret = utl_get_device_class(&p_cod);
if (!ret){
LOG_ERROR("%s get class of device failed!",__func__);
return ESP_BT_STATUS_FAIL;
}
cod->minor = p_cod.minor >> 2;
cod->major = p_cod.major;
cod->service = p_cod.service >> 5;
return ESP_BT_STATUS_SUCCESS;
}

void btc_gap_bt_call_handler(btc_msg_t *msg)
{
btc_gap_bt_args_t *arg = (btc_gap_bt_args_t *)msg->arg;
Expand Down Expand Up @@ -604,6 +631,10 @@ void btc_gap_bt_call_handler(btc_msg_t *msg)
btc_gap_bt_search_service_record(msg->aid, msg->arg);
break;
}
case BTC_GAP_BT_ACT_SET_COD: {
btc_gap_bt_set_cod(msg->arg);
break;
}
default:
break;
}
Expand Down
10 changes: 10 additions & 0 deletions components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h
Expand Up @@ -19,6 +19,7 @@
#include "esp_bt_defs.h"
#include "esp_gap_bt_api.h"
#include "btc_task.h"
#include "utl.h"

#if (BTC_GAP_BT_INCLUDED == TRUE)

Expand All @@ -32,6 +33,7 @@ typedef enum {
BTC_GAP_BT_ACT_SEARCH_SERVICES,
BTC_GAP_BT_ACT_GET_REMOTE_SERVICE_RECORD,
BTC_GAP_BT_ACT_SEARCH_SERVICE_RECORD,
BTC_GAP_BT_ACT_SET_COD,
} btc_gap_bt_act_t;

/* btc_bt_gap_args_t */
Expand All @@ -56,12 +58,20 @@ typedef union {
bt_bdaddr_t bda;
esp_bt_uuid_t uuid;
} get_rmt_srv_rcd;

// BTC_GAP_BT_ACT_SET_COD
struct set_cod_args {
esp_bt_cod_t cod;
esp_bt_cod_mode_t mode;
} set_cod;

} btc_gap_bt_args_t;

void btc_gap_bt_call_handler(btc_msg_t *msg);

void btc_gap_bt_busy_level_updated(uint8_t bl_flags);

esp_err_t btc_gap_bt_get_cod(esp_bt_cod_t *cod);
#endif /* #if BTC_GAP_BT_INCLUDED */

#endif /* __BTC_GAP_BT_H__ */

0 comments on commit 789edc0

Please sign in to comment.