Skip to content

Commit

Permalink
os/board/rtl8730e: add BLE scan parameter setting
Browse files Browse the repository at this point in the history
- added API ble_client_set_scan to set scan interval, scan window and scan type
  • Loading branch information
yeetee179 authored and hs36-kim committed Oct 25, 2024
1 parent 4e85050 commit 0661e5a
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 4 deletions.
9 changes: 9 additions & 0 deletions framework/src/ble_manager/ble_manager_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ ble_result_e ble_manager_conn_param_update(ble_conn_handle *con_handle, ble_conn
}

/* Scanner */
ble_result_e ble_client_set_scan(uint16_t scan_interval, uint16_t scan_window, uint8_t scan_type)
{
blemgr_msg_params param = { 3, {(uint16_t *)&scan_interval, (uint16_t *)&scan_window, (uint8_t *)&scan_type} };
blemgr_msg_s msg = {BLE_CMD_SET_SCAN, BLE_MANAGER_FAIL, (void *)(&param), NULL};
int res = blemgr_post_message(&msg);

RETURN_RESULT(res, msg);
}

ble_result_e ble_client_start_scan(ble_scan_filter *filter, ble_scan_callback_list *callbacks)
{
blemgr_msg_params param = {2, {(void *)filter, (void *)callbacks}};
Expand Down
1 change: 1 addition & 0 deletions framework/src/ble_manager/ble_manager_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ typedef enum {
BLE_CMD_GET_VERSION,

// Scanner
BLE_CMD_SET_SCAN,
BLE_CMD_START_SCAN,
BLE_CMD_STOP_SCAN,
BLE_CMD_WHITELIST_ADD,
Expand Down
11 changes: 11 additions & 0 deletions framework/src/ble_manager/ble_manager_lwnl.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ trble_result_e ble_drv_ioctl(trble_msg_s *dmsg)
}

/*** Scanner(Observer) ***/
trble_result_e ble_drv_set_scan(uint16_t scan_interval, uint16_t scan_window, trble_scan_type scan_type)
{
trble_result_e res = TRBLE_SUCCESS;
lwnl_msg_params msg_data = { 3, {(void *)&scan_interval, (void *)&scan_window, (void *)&scan_type} };
lwnl_msg msg = {BLE_INTF_NAME, {LWNL_REQ_BLE_SET_SCAN}, sizeof(msg_data), (void *)(&msg_data), (void *)&res};
if (_send_msg(&msg) < 0) {
res = TRBLE_FILE_ERROR;
}
return res;
}

trble_result_e ble_drv_start_scan(trble_scan_filter *filter)
{
trble_result_e res = TRBLE_SUCCESS;
Expand Down
9 changes: 9 additions & 0 deletions framework/src/ble_manager/ble_manager_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,15 @@ ble_result_e blemgr_handle_request(blemgr_msg_s *msg)
} break;

// Scanner
case BLE_CMD_SET_SCAN: {
BLE_STATE_CHECK;
blemgr_msg_params *param = (blemgr_msg_params *)msg->param;
uint16_t scan_interval = *(uint16_t *)param->param[0];
uint16_t scan_window = *(uint16_t *)param->param[1];
trble_scan_type scan_type = *(trble_adv_type_e *)param->param[2];
ret = ble_drv_set_scan(scan_interval, scan_window, scan_type);
} break;

case BLE_CMD_START_SCAN: {
BLE_STATE_CHECK;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <tinyara/net/if/ble.h>
#include "stddef.h"
#include <gap_conn_le.h>
#include <gap_scan.h>
#include "ble_tizenrt_scatternet_app.h"
#include "ble_tizenrt_scatternet_link_mgr.h"
#include "ble_tizenrt_central_client_app.h"
Expand Down Expand Up @@ -291,6 +292,46 @@ void scan_stop_cb(void *arg)
} while(new_state.gap_scan_state != GAP_SCAN_STATE_IDLE);
}

trble_result_e rtw_ble_client_set_scan(uint16_t scan_interval, uint16_t scan_window, trble_scan_type scan_type)
{
uint8_t ret = 0;
uint8_t duplicate_opt = GAP_SCAN_FILTER_DUPLICATE_DISABLE;
uint8_t filter_policy = GAP_SCAN_FILTER_ANY;

if (le_scan_set_param(GAP_PARAM_SCAN_MODE, sizeof(scan_type), &scan_type) != GAP_CAUSE_SUCCESS)
{
printf("rtw_ble_client_set_scan set scan type fail!! cause %d\n", ret);
return TRBLE_FAIL;
}

if (le_scan_set_param(GAP_PARAM_SCAN_INTERVAL, sizeof(scan_interval), &scan_interval) != GAP_CAUSE_SUCCESS)
{
printf("rtw_ble_client_set_scan set scan interval fail!! cause %d\n", ret);
return TRBLE_FAIL;
}

if (le_scan_set_param(GAP_PARAM_SCAN_WINDOW, sizeof(scan_window), &scan_window) != GAP_CAUSE_SUCCESS)
{
printf("rtw_ble_client_set_scan set scan window fail!! cause %d\n", ret);
return TRBLE_FAIL;
}

if (le_scan_set_param(GAP_PARAM_SCAN_FILTER_POLICY, sizeof(filter_policy), &filter_policy) != GAP_CAUSE_SUCCESS)
{
printf("rtw_ble_client_set_scan set scan filter policy fail!! cause %d\n", ret);
return TRBLE_FAIL;
}

if (le_scan_set_param(GAP_PARAM_SCAN_FILTER_DUPLICATES, sizeof(duplicate_opt), &duplicate_opt) != GAP_CAUSE_SUCCESS)
{
printf("rtw_ble_client_set_scan set scan filter duplicate fail!! cause %d\n", ret);
return TRBLE_FAIL;
}

return TRBLE_SUCCESS;
}


trble_result_e rtw_ble_client_start_scan_with_filter(trble_scan_filter* scan_parm, bool whitelist_enable)
{
if (client_init_parm == NULL || client_init_parm->trble_device_scanned_cb == NULL || scan_parm == NULL)
Expand Down
7 changes: 7 additions & 0 deletions os/board/rtl8721csm/src/component/os/tizenrt/rtk_blemgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ trble_result_e trble_netmgr_conn_param_update(struct bledev *dev, trble_conn_han
trble_result_e trble_netmgr_ioctl(struct bledev *dev, trble_msg_s *msg);

/*** Scanner(Observer) ***/
trble_result_e trble_netmgr_set_scan(struct bledev *dev, uint16_t scan_interval, uint16_t scan_window, trble_scan_type scan_type);
trble_result_e trble_netmgr_start_scan(struct bledev *dev, trble_scan_filter *filter);
trble_result_e trble_netmgr_stop_scan(struct bledev *dev);
trble_result_e trble_netmgr_scan_whitelist_add(struct bledev *dev, trble_addr *addr);
Expand Down Expand Up @@ -121,6 +122,7 @@ struct trble_ops g_trble_drv_ops = {
trble_netmgr_ioctl,

// Observer
trble_netmgr_set_scan,
trble_netmgr_start_scan,
trble_netmgr_stop_scan,
trble_netmgr_scan_whitelist_add,
Expand Down Expand Up @@ -289,6 +291,11 @@ trble_result_e trble_netmgr_ioctl(struct bledev *dev, trble_msg_s *msg)
}

/*** Scanner(Observer) ***/
trble_result_e trble_netmgr_set_scan(struct bledev *dev, uint16_t scan_interval, uint16_t scan_window, trble_scan_type scan_type)
{
return rtw_ble_client_set_scan(scan_interval, scan_window, scan_type);
}

trble_result_e trble_netmgr_start_scan(struct bledev *dev, trble_scan_filter *filter)
{
return rtw_ble_client_start_scan_with_filter(filter, filter->whitelist_enable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ void scan_stop_cb(void *arg)
} while(new_state.gap_scan_state != GAP_SCAN_STATE_IDLE);
}

trble_result_e rtw_ble_client_set_scan(uint16_t scan_interval, uint16_t scan_window, trble_scan_type scan_type)
{
rtk_bt_le_scan_param_t gap_scan_param;
gap_scan_param.type = scan_type;
gap_scan_param.interval = scan_interval;
gap_scan_param.window = scan_window;
gap_scan_param.own_addr_type = RTK_BT_LE_ADDR_TYPE_PUBLIC;
gap_scan_param.filter_policy = RTK_BT_LE_SCAN_FILTER_ALLOW_ALL;
gap_scan_param.duplicate_opt = RTK_BT_LE_SCAN_DUPLICATE_DISABLE;
if(RTK_BT_OK != rtk_bt_le_gap_set_scan_param(&gap_scan_param))
{
printf("rtw_ble_client_set_scan set scan fail !! \n");
return TRBLE_FAIL;
} else {
printf("rtw_ble_client_set_scan set scan success !! \n");
return TRBLE_SUCCESS;
}
}

trble_result_e rtw_ble_client_start_scan_with_filter(trble_scan_filter* scan_parm, bool whitelist_enable)
{
if (client_init_parm == NULL || client_init_parm->trble_device_scanned_cb == NULL || scan_parm == NULL)
Expand Down
7 changes: 7 additions & 0 deletions os/board/rtl8730e/src/component/os/tizenrt/rtk_blemgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ trble_result_e trble_netmgr_conn_param_update(struct bledev *dev, trble_conn_han
trble_result_e trble_netmgr_ioctl(struct bledev *dev, trble_msg_s *msg);

/*** Scanner(Observer) ***/
trble_result_e trble_netmgr_set_scan(struct bledev *dev, uint16_t scan_interval, uint16_t scan_window, trble_scan_type scan_type);
trble_result_e trble_netmgr_start_scan(struct bledev *dev, trble_scan_filter *filter);
trble_result_e trble_netmgr_stop_scan(struct bledev *dev);
trble_result_e trble_netmgr_scan_whitelist_add(struct bledev *dev, trble_addr *addr);
Expand Down Expand Up @@ -129,6 +130,7 @@ struct trble_ops g_trble_drv_ops = {
trble_netmgr_ioctl,

// Observer
trble_netmgr_set_scan,
trble_netmgr_start_scan,
trble_netmgr_stop_scan,
trble_netmgr_scan_whitelist_add,
Expand Down Expand Up @@ -293,6 +295,11 @@ trble_result_e trble_netmgr_ioctl(struct bledev *dev, trble_msg_s *msg)
}

/*** Scanner(Observer) ***/
trble_result_e trble_netmgr_set_scan(struct bledev *dev, uint16_t scan_interval, uint16_t scan_window, trble_scan_type scan_type)
{
return rtw_ble_client_set_scan(scan_interval, scan_window, scan_type);
}

trble_result_e trble_netmgr_start_scan(struct bledev *dev, trble_scan_filter *filter)
{
return rtw_ble_client_start_scan_with_filter(filter, filter->whitelist_enable);
Expand Down
16 changes: 12 additions & 4 deletions os/include/tinyara/net/if/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ typedef enum {
LWNL_REQ_BLE_IOCTL,

// Scanner
LWNL_REQ_BLE_SET_SCAN,
LWNL_REQ_BLE_START_SCAN,
LWNL_REQ_BLE_STOP_SCAN,
LWNL_REQ_BLE_WHITELIST_ADD,
Expand Down Expand Up @@ -122,8 +123,8 @@ typedef enum {
LWNL_REQ_BLE_SET_ADV_TXPOWER,
LWNL_REQ_BLE_START_ADV,
LWNL_REQ_BLE_STOP_ADV,
LWNL_REQ_BLE_ONE_SHOT_ADV_INIT,
LWNL_REQ_BLE_ONE_SHOT_ADV_DEINIT,
LWNL_REQ_BLE_ONE_SHOT_ADV_INIT,
LWNL_REQ_BLE_ONE_SHOT_ADV_DEINIT,
LWNL_REQ_BLE_ONE_SHOT_ADV,
LWNL_REQ_BLE_CREATE_ADV,
LWNL_REQ_BLE_DELETE_ADV,
Expand Down Expand Up @@ -174,6 +175,11 @@ typedef enum {
TRBLE_SCAN_STARTED,
} trble_scan_state_e;

typedef enum {
TRBLE_SCAN_PASSIVE = 0,
TRBLE_SCAN_ACTIVE,
} trble_scan_type;

typedef enum {
TRBLE_ADV_TYPE_IND,
TRBLE_ADV_TYPE_DIRECT,
Expand Down Expand Up @@ -347,6 +353,7 @@ typedef trble_result_e (*trble_conn_param_update)(struct bledev *dev, trble_conn
typedef trble_result_e (*trble_drv_ioctl)(struct bledev *dev, trble_msg_s *msg);

/*** Scanner(Observer) ***/
typedef trble_result_e (*trble_set_scan)(struct bledev *dev, uint16_t scan_interval, uint16_t scan_window, trble_scan_type scan_type);
typedef trble_result_e (*trble_start_scan)(struct bledev *dev, trble_scan_filter *filter);
typedef trble_result_e (*trble_stop_scan)(struct bledev *dev);
typedef trble_result_e (*trble_scan_whitelist_add)(struct bledev *dev, trble_addr *addr);
Expand Down Expand Up @@ -415,6 +422,7 @@ struct trble_ops {
trble_drv_ioctl drv_ioctl;

/* Scanner(Observer) */
trble_set_scan set_scan;
trble_start_scan start_scan;
trble_stop_scan stop_scan;
trble_scan_whitelist_add whitelist_add;
Expand Down Expand Up @@ -456,8 +464,8 @@ struct trble_ops {
trble_set_adv_txpower set_adv_txpower;
trble_start_adv start_adv;
trble_stop_adv stop_adv;
trble_one_shot_adv_init one_shot_adv_init;
trble_one_shot_adv_deinit one_shot_adv_deinit;
trble_one_shot_adv_init one_shot_adv_init;
trble_one_shot_adv_deinit one_shot_adv_deinit;
trble_one_shot_adv one_shot_adv;
trble_create_multi_adv create_multi_adv;
trble_delete_multi_adv delete_multi_adv;
Expand Down
18 changes: 18 additions & 0 deletions os/net/blemgr/bledev.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ int bledev_handle(struct bledev *dev, lwnl_req cmd, void *data, uint32_t data_le
break;

// Scanner
case LWNL_REQ_BLE_SET_SCAN:
{
uint16_t scan_interval = 0;
uint16_t scan_window = 0;
trble_scan_type scan_type = 0;

lwnl_msg_params param = { 0, };
if (data != NULL) {
memcpy(&param, data, data_len);
} else {
return TRBLE_INVALID_ARGS;
}
scan_interval = *(uint16_t *)param.param[0];
scan_window = *(uint16_t *)param.param[1];
scan_type = *(trble_scan_type *)param.param[2];
TRBLE_DRV_CALL(ret, dev, set_scan, (dev, scan_interval, scan_window, scan_type));
}
break;
case LWNL_REQ_BLE_START_SCAN:
{
/* filter can be NULL */
Expand Down

0 comments on commit 0661e5a

Please sign in to comment.