Skip to content

AVRCP: add avrcp metadata feature #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ if(CONFIG_BLUETOOTH)
list(APPEND CSRCS
${BLUETOOTH_DIR}/feature/src/system_bluetooth_bt_a2dpsink_impl.c)
endif()
if(CONFIG_BLUETOOTH_AVRCP_CONTROL)
list(APPEND CSRCS
${BLUETOOTH_DIR}/feature/src/system_bluetooth_bt_avrcpcontrol.c)
list(APPEND CSRCS
${BLUETOOTH_DIR}/feature/src/system_bluetooth_bt_avrcpcontrol_impl.c)
endif()
endif()

nuttx_add_library(libbluetooth STATIC)
Expand Down Expand Up @@ -519,6 +525,10 @@ if(CONFIG_BLUETOOTH)
list(APPEND JIDL_PATHS
${BLUETOOTH_DIR}/feature/jdil/bluetooth_a2dpsink.jidl)
endif()
if(CONFIG_BLUETOOTH_AVRCP_CONTROL)
list(APPEND JIDL_PATHS
${BLUETOOTH_DIR}/feature/jdil/bluetooth_avrcpcontrol.jidl)
endif()

nuttx_add_jidl(
TARGET
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ ifeq ($(CONFIG_BLUETOOTH_A2DP_SINK), y)
CSRCS += feature/src/system_bluetooth_bt_a2dpsink.c
CSRCS += feature/src/system_bluetooth_bt_a2dpsink_impl.c
endif
ifeq ($(CONFIG_BLUETOOTH_AVRCP_CONTROL), y)
CSRCS += feature/src/system_bluetooth_bt_avrcpcontrol.c
CSRCS += feature/src/system_bluetooth_bt_avrcpcontrol_impl.c
endif

depend::
@python3 $(APPDIR)/frameworks/runtimes/feature/tools/jidl/jsongensource.py \
Expand All @@ -356,6 +360,11 @@ ifeq ($(CONFIG_BLUETOOTH_A2DP_SINK), y)
$(APPDIR)/frameworks/connectivity/bluetooth/feature/jidl/bluetooth_bt_a2dpsink.jidl -out-dir \
$(APPDIR)/frameworks/connectivity/bluetooth/feature/src -header system_bluetooth_bt_a2dpsink.h -source system_bluetooth_bt_a2dpsink.c
endif
ifeq ($(CONFIG_BLUETOOTH_AVRCP_CONTROL), y)
@python3 $(APPDIR)/frameworks/runtimes/feature/tools/jidl/jsongensource.py \
$(APPDIR)/frameworks/connectivity/bluetooth/feature/jidl/bluetooth_bt_avrcpcontrol.jidl -out-dir \
$(APPDIR)/frameworks/connectivity/bluetooth/feature/src -header system_bluetooth_bt_avrcpcontrol.h -source system_bluetooth_bt_avrcpcontrol.c
endif

endif

Expand Down
8 changes: 8 additions & 0 deletions feature/include/feature_bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ typedef enum {
ON_DISCOVERY_RESULT,
ON_BOND_STATE_CHANGE,
A2DPSINK_ON_CONNECT_STATE_CHANGE,
AVRCPCONTROL_ELEMENT_ATTRIBUTE_CALLBACK,
} feature_bluetooth_callback_t;

typedef enum {
FEATURE_BLUETOOTH,
FEATURE_BLUETOOTH_BT,
FEATURE_BLUETOOTH_A2DPSINK,
FEATURE_BLUETOOTH_AVRCPCONTROL,
} feature_bluetooth_feature_type_t;

typedef struct {
Expand All @@ -64,6 +66,11 @@ typedef struct {
FtCallbackId a2dp_sink_connection_state_cb_id;
} feature_bluetooth_a2dp_sink_callbacks_t;

typedef struct {
FeatureInstanceHandle* feature_ins;
FtCallbackId avrcp_control_element_attribute_cb_id;
} feature_bluetooth_avrcp_control_callbacks_t;

typedef struct {
FtCallbackId feature_callback_id;
void* feature;
Expand All @@ -75,6 +82,7 @@ typedef struct {
bt_list_t* feature_bluetooth_callbacks;
bt_list_t* feature_bluetooth_bt_callbacks;
bt_list_t* feature_a2dp_sink_callbacks;
bt_list_t* feature_avrcp_control_callbacks;
uint32_t created_features;
} feature_bluetooth_features_info_t;

Expand Down
27 changes: 27 additions & 0 deletions feature/jidl/bluetooth_bt_avrcpcontrol.jidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module [email protected]

callback startGetElementAttributeSuccess()
callback startGetElementAttributeFail(string data, int code)
callback startGetElementAttributeComplete()
struct StartGetElementAttributeParams {
string deviceId
callback startGetElementAttributeSuccess success
callback startGetElementAttributeFail fail
callback startGetElementAttributeComplete complete
}
void startGetElementAttribute(StartGetElementAttributeParams params)

struct attr_info_t {
int attrId
int chrSet
string text
}

struct OnElementAttributeData {
string deviceId
int attrsCount
attr_info_t[] attrs;
}

callback ElementAttributeCallback(OnElementAttributeData data)
property ElementAttributeCallback onElementattribute
142 changes: 142 additions & 0 deletions feature/src/feature_bluetooth_callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/
#include "bt_a2dp_sink.h"
#include "bt_adapter.h"
#include "bt_avrcp_control.h"
#include "bt_list.h"
#include "feature_bluetooth.h"
#include "feature_exports.h"
#include "feature_log.h"
#include "system_bluetooth.h"
#include "system_bluetooth_bt.h"
#include "system_bluetooth_bt_a2dpsink.h"
#include "system_bluetooth_bt_avrcpcontrol.h"
#include "uv.h"

#define REMOVE_CALLBACK(feature_callback, callback_type) \
Expand Down Expand Up @@ -85,6 +87,16 @@ static bool get_callback_a2dp_sink(void* data, void* feature_ins)
return callbacks->feature_ins == feature_ins;
}

static bool get_callback_avrcp_control(void* data, void* feature_ins)
{
feature_bluetooth_avrcp_control_callbacks_t* callbacks = (feature_bluetooth_avrcp_control_callbacks_t*)data;
if (!callbacks) {
return false;
}

return callbacks->feature_ins == feature_ins;
}

static void free_feature_callback(bt_list_t* callbacks, FeatureInstanceHandle handle, bt_list_find_cb find_func)
{
void* data;
Expand Down Expand Up @@ -136,6 +148,18 @@ static void free_feature_bluetooth_a2dp_sink_node(void* node)
free(feature_callback);
}

static void free_feature_bluetooth_avrcp_control_node(void* node)
{
feature_bluetooth_avrcp_control_callbacks_t* feature_callback = (feature_bluetooth_avrcp_control_callbacks_t*)node;

if (!feature_callback) {
return;
}

REMOVE_CALLBACK(feature_callback, avrcp_control_element_attribute_cb_id);
free(feature_callback);
}

static void on_adapter_state_changed_cb(void* cookie, bt_adapter_state_t state)
{
bt_instance_t* bt_ins = (bt_instance_t*)cookie;
Expand Down Expand Up @@ -437,6 +461,92 @@ static const a2dp_sink_callbacks_t a2dp_sink_cbs = {
a2dp_sink_connection_state_cb,
};

system_bluetooth_bt_avrcpcontrol_attr_info_t* get_attr_info(avrcp_element_attr_val_t* attr)
{
system_bluetooth_bt_avrcpcontrol_attr_info_t* attr_info = system_bluetooth_bt_avrcpcontrolMallocattr_info_t();
attr_info->attrId = attr->attr_id;
attr_info->chrSet = attr->chr_set;
attr_info->text = StringToFtString((char*)attr->text);
return attr_info;
}

static void avrcp_control_get_element_attribute_cb(void* cookie, bt_address_t* addr, uint8_t attrs_count, avrcp_element_attr_val_t* attrs)
{
int attr_index;
bt_instance_t* bt_ins = (bt_instance_t*)cookie;
feature_bluetooth_features_info_t* features_callbacks;
bt_list_t* callbacks;
bt_list_node_t* node;

FEATURE_LOG_DEBUG("avrcp control element attribute cb");
if (!bt_ins) {
return;
}

features_callbacks = (feature_bluetooth_features_info_t*)bt_ins->context;
if (!features_callbacks) {
return;
}

uv_mutex_lock(&features_callbacks->mutex);
callbacks = features_callbacks->feature_avrcp_control_callbacks;
if (!callbacks) {
uv_mutex_unlock(&features_callbacks->mutex);
return;
}

node = bt_list_head(callbacks);
if (!node) {
uv_mutex_unlock(&features_callbacks->mutex);
return;
}

while (node) {
feature_bluetooth_avrcp_control_callbacks_t* feature_callback;
system_bluetooth_bt_avrcpcontrol_OnElementAttributeData* data;
FtArray* attributes;

char addr_str[BT_ADDR_STR_LENGTH] = { 0 };

feature_callback = (feature_bluetooth_avrcp_control_callbacks_t*)bt_list_node(node);
if (!feature_callback) {
break;
}

FEATURE_LOG_DEBUG("feature:%p, callbackId:%d", feature_callback->feature_ins, feature_callback->avrcp_control_element_attribute_cb_id);
bt_addr_ba2str(addr, addr_str);
data = system_bluetooth_bt_avrcpcontrolMallocOnElementAttributeData();
attributes = system_bluetooth_bt_avrcpcontrol_malloc_attr_info_t_struct_type_array();

if (!data || !attributes) {
continue;
}

data->deviceId = StringToFtString(addr_str);
data->attrsCount = attrs_count;
attributes->_size = attrs_count;
attributes->_element = malloc(attributes->_size * sizeof(struct Attribute*));
if (!attributes->_element) {
continue;
}
for (attr_index = 0; attr_index < attributes->_size; attr_index++) {
system_bluetooth_bt_avrcpcontrol_attr_info_t* attr_info = get_attr_info(&attrs[attr_index]);
((system_bluetooth_bt_avrcpcontrol_attr_info_t**)attributes->_element)[attr_index] = attr_info;
}

data->attrs = attributes;

feature_bluetooth_post_task(feature_callback->feature_ins, feature_callback->avrcp_control_element_attribute_cb_id, data);
node = bt_list_next(callbacks, node);
}
uv_mutex_unlock(&features_callbacks->mutex);
}

static const avrcp_control_callbacks_t avrcp_control_cbs = {
.size = sizeof(avrcp_control_cbs),
.get_element_attribute_cb = avrcp_control_get_element_attribute_cb,
};

void feature_bluetooth_add_feature_callback(FeatureInstanceHandle handle, feature_bluetooth_feature_type_t feature_type)
{
bt_instance_t* bt_ins;
Expand Down Expand Up @@ -465,6 +575,11 @@ void feature_bluetooth_add_feature_callback(FeatureInstanceHandle handle, featur
case FEATURE_BLUETOOTH_A2DPSINK:
add_feature_callback(features_callbacks->feature_a2dp_sink_callbacks, feature_bluetooth_a2dp_sink_callbacks_t, handle);
break;
#endif
#ifdef CONFIG_BLUETOOTH_AVRCP_CONTROL
case FEATURE_BLUETOOTH_AVRCPCONTROL:
add_feature_callback(features_callbacks->feature_avrcp_control_callbacks, feature_bluetooth_avrcp_control_callbacks_t, handle);
break;
#endif
default:
break;
Expand Down Expand Up @@ -499,6 +614,11 @@ void feature_bluetooth_free_feature_callback(FeatureInstanceHandle handle, featu
case FEATURE_BLUETOOTH_A2DPSINK:
free_feature_callback(features_callbacks->feature_a2dp_sink_callbacks, handle, get_callback_a2dp_sink);
break;
#endif
#ifdef CONFIG_BLUETOOTH_AVRCP_CONTROL
case FEATURE_BLUETOOTH_AVRCPCONTROL:
free_feature_callback(features_callbacks->feature_avrcp_control_callbacks, handle, get_callback_avrcp_control);
break;
#endif
default:
break;
Expand Down Expand Up @@ -536,6 +656,11 @@ void feature_bluetooth_set_feature_callback(FeatureInstanceHandle handle, FtCall
case A2DPSINK_ON_CONNECT_STATE_CHANGE:
set_feature_callback(features_callbacks->feature_a2dp_sink_callbacks, feature_bluetooth_a2dp_sink_callbacks_t, get_callback_a2dp_sink, handle, callback_id, a2dp_sink_connection_state_cb_id);
break;
#endif
#ifdef CONFIG_BLUETOOTH_AVRCP_CONTROL
case AVRCPCONTROL_ELEMENT_ATTRIBUTE_CALLBACK:
set_feature_callback(features_callbacks->feature_avrcp_control_callbacks, feature_bluetooth_avrcp_control_callbacks_t, get_callback_avrcp_control, handle, callback_id, avrcp_control_element_attribute_cb_id);
break;
#endif
default:
break;
Expand Down Expand Up @@ -575,6 +700,11 @@ FtCallbackId feature_bluetooth_get_feature_callback(FeatureInstanceHandle handle
case A2DPSINK_ON_CONNECT_STATE_CHANGE:
get_feature_callback(features_callbacks->feature_a2dp_sink_callbacks, feature_bluetooth_a2dp_sink_callbacks_t, get_callback_a2dp_sink, handle, callback_id, a2dp_sink_connection_state_cb_id);
break;
#endif
#ifdef CONFIG_BLUETOOTH_AVRCP_CONTROL
case AVRCPCONTROL_ELEMENT_ATTRIBUTE_CALLBACK:
get_feature_callback(features_callbacks->feature_avrcp_control_callbacks, feature_bluetooth_avrcp_control_callbacks_t, get_callback_avrcp_control, handle, callback_id, avrcp_control_element_attribute_cb_id);
break;
#endif
default:
break;
Expand Down Expand Up @@ -607,6 +737,11 @@ void feature_bluetooth_callback_init(bt_instance_t* bt_ins)
bt_ins->a2dp_sink_cookie = bt_a2dp_sink_register_callbacks(bt_ins, &a2dp_sink_cbs);
#endif

#ifdef CONFIG_BLUETOOTH_AVRCP_CONTROL
features_callbacks->feature_avrcp_control_callbacks = bt_list_new(free_feature_bluetooth_avrcp_control_node);
bt_ins->avrcp_control_cookie = bt_avrcp_control_register_callbacks(bt_ins, &avrcp_control_cbs);
#endif

bt_ins->context = features_callbacks;
}

Expand All @@ -629,11 +764,18 @@ void feature_bluetooth_callback_uninit(bt_instance_t* bt_ins)
bt_a2dp_sink_unregister_callbacks(bt_ins, bt_ins->a2dp_sink_cookie);
#endif

#ifdef CONFIG_BLUETOOTH_AVRCP_CONTROL
bt_avrcp_control_unregister_callbacks(bt_ins, bt_ins->avrcp_control_cookie);
#endif

uv_mutex_lock(&features_callbacks->mutex);
bt_list_free(features_callbacks->feature_bluetooth_callbacks);
bt_list_free(features_callbacks->feature_bluetooth_bt_callbacks);
#ifdef CONFIG_BLUETOOTH_A2DP_SINK
bt_list_free(features_callbacks->feature_a2dp_sink_callbacks);
#endif
#ifdef CONFIG_BLUETOOTH_AVRCP_CONTROL
bt_list_free(features_callbacks->feature_avrcp_control_callbacks);
#endif
uv_mutex_unlock(&features_callbacks->mutex);

Expand Down
3 changes: 3 additions & 0 deletions feature/src/feature_bluetooth_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ void feature_bluetooth_post_task(FeatureInstanceHandle handle, FtCallbackId call

char* StringToFtString(const char* str)
{
if (!str) {
return NULL;
}
int len = strlen(str);
char* ftStr = (char*)FeatureMalloc(len + 1, FT_CHAR);
strcpy(ftStr, str);
Expand Down
Loading
Loading