Skip to content
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

Move music control to Zbus and remove #ifdef #126

Merged
merged 3 commits into from
Nov 20, 2023
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
60 changes: 11 additions & 49 deletions app/src/applications/music_control/music_control_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
#include <zsw_clock.h>
#include <zephyr/zbus/zbus.h>

#ifdef CONFIG_BLE_USES_IOS
#include "ble/ble_ams.h"
#endif

#include "music_control_ui.h"
#include "ble/ble_comm.h"
#include "events/ble_data_event.h"
#include "events/music_event.h"
#include "managers/zsw_app_manager.h"

// Functions needed for all applications
Expand All @@ -22,6 +19,8 @@ static void zbus_ble_comm_data_callback(const struct zbus_channel *chan);
static void handle_update_ui(struct k_work *item);

ZBUS_CHAN_DECLARE(ble_comm_data_chan);

ZBUS_CHAN_DECLARE(music_control_data_chan);
ZBUS_LISTENER_DEFINE(music_app_ble_comm_lis, zbus_ble_comm_data_callback);

static K_WORK_DEFINE(update_ui_work, handle_update_ui);
Expand Down Expand Up @@ -59,53 +58,16 @@ static void music_control_app_stop(void)

static void on_music_ui_evt_music(music_control_ui_evt_type_t evt_type)
{
uint8_t buf[50];
int msg_len = 0;

#if defined(CONFIG_BLE_USES_GADGETBRIDGE)

switch (evt_type) {
case MUSIC_CONTROL_UI_CLOSE:
zsw_app_manager_app_close_request(&app);
break;
case MUSIC_CONTROL_UI_PLAY:
msg_len = snprintf(buf, sizeof(buf), "{\"t\":\"music\", \"n\": %s} \n", "play");
break;
case MUSIC_CONTROL_UI_PAUSE:
msg_len = snprintf(buf, sizeof(buf), "{\"t\":\"music\", \"n\": %s} \n", "pause");
break;
case MUSIC_CONTROL_UI_NEXT_TRACK:
msg_len = snprintf(buf, sizeof(buf), "{\"t\":\"music\", \"n\": %s} \n", "next");
break;
case MUSIC_CONTROL_UI_PREV_TRACK:
msg_len = snprintf(buf, sizeof(buf), "{\"t\":\"music\", \"n\": %s} \n", "previous");
break;
}
if (msg_len > 0) {
ble_comm_send(buf, msg_len);
}

#elif defined(CONFIG_BLE_USES_IOS)

switch (evt_type) {
case MUSIC_CONTROL_UI_CLOSE:
zsw_app_manager_app_close_request(&app);
break;
case MUSIC_CONTROL_UI_PLAY:
ble_ams_play_pause();
break;
case MUSIC_CONTROL_UI_PAUSE:
ble_ams_play_pause();
break;
case MUSIC_CONTROL_UI_NEXT_TRACK:
ble_ams_next_track();
break;
case MUSIC_CONTROL_UI_PREV_TRACK:
ble_ams_previous_track();
break;
if (evt_type == MUSIC_CONTROL_UI_CLOSE) {
zsw_app_manager_app_close_request(&app);
} else {
struct music_event music_event = {
.control_type = evt_type,
};

zbus_chan_pub(&music_control_data_chan, &music_event, K_MSEC(50));
}

#endif // CONFIG_BLE_USES_IOS
}

static void zbus_ble_comm_data_callback(const struct zbus_channel *chan)
Expand Down
35 changes: 34 additions & 1 deletion app/src/ble/ble_ams.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "ble/ble_ams.h"
#include "ble/ble_comm.h"
#include "events/ble_data_event.h"
#include "events/music_event.h"

LOG_MODULE_REGISTER(ble_ams, LOG_LEVEL_INF);

Expand Down Expand Up @@ -57,10 +58,42 @@ static struct bt_conn *current_conn;

static void ble_ams_delayed_write_handle(struct k_work *item);
static void ams_discover_retry_handle(struct k_work *item);
static void music_control_event_callback(const struct zbus_channel *chan);

ZBUS_CHAN_DECLARE(ble_comm_data_chan);

ZBUS_CHAN_DECLARE(music_control_data_chan);
ZBUS_OBS_DECLARE(ios_music_control_lis);
ZBUS_LISTENER_DEFINE(ios_music_control_lis, music_control_event_callback);

K_WORK_DELAYABLE_DEFINE(ams_gatt_discover_retry, ams_discover_retry_handle);
K_WORK_DELAYABLE_DEFINE(ble_ams_delayed_write, ble_ams_delayed_write_handle);
ZBUS_CHAN_DECLARE(ble_comm_data_chan);

static void music_control_event_callback(const struct zbus_channel *chan)
{
const struct music_event *event = zbus_chan_const_msg(chan);

switch (event->control_type) {
case MUSIC_CONTROL_UI_PLAY:
ble_ams_play_pause();
break;
case MUSIC_CONTROL_UI_PAUSE:
ble_ams_play_pause();
break;
case MUSIC_CONTROL_UI_NEXT_TRACK:
ble_ams_next_track();
break;
case MUSIC_CONTROL_UI_PREV_TRACK:
ble_ams_previous_track();
break;

case MUSIC_CONTROL_UI_CLOSE:
default:
// Nothing to do
break;
}

}

static void notify_rc_cb(struct bt_ams_client *ams_c,
const uint8_t *data, size_t len)
Expand Down
36 changes: 36 additions & 0 deletions app/src/ble/ble_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ble/ble_comm.h"
#include "ble/ble_transport.h"
#include "events/ble_data_event.h"
#include "events/music_event.h"

#ifdef CONFIG_BT_AMS_CLIENT
#include <bluetooth/services/ams_client.h>
Expand Down Expand Up @@ -65,6 +66,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason);
static void param_updated(struct bt_conn *conn, uint16_t interval, uint16_t latency, uint16_t timeout);
static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data, uint16_t len);
static void update_conn_interval_handler(struct k_work *item);
static void music_control_event_callback(const struct zbus_channel *chan);

BT_CONN_CB_DEFINE(conn_callbacks) = {
.connected = connected,
Expand All @@ -91,6 +93,9 @@ static const struct bt_data ad_nus[] = {

K_WORK_DELAYABLE_DEFINE(conn_interval_work, update_conn_interval_handler);

ZBUS_CHAN_DECLARE(music_control_data_chan);
ZBUS_LISTENER_DEFINE(android_music_control_lis, music_control_event_callback);

static struct bt_conn *current_conn;
static uint32_t max_send_len;
static uint8_t receive_buf[MAX_GB_PACKET_LENGTH];
Expand All @@ -102,6 +107,37 @@ static on_data_cb_t data_parsed_cb;

static int pairing_enabled;

static void music_control_event_callback(const struct zbus_channel *chan)
{
const struct music_event *event = zbus_chan_const_msg(chan);

uint8_t buf[50];
int msg_len = 0;

switch (event->control_type) {
case MUSIC_CONTROL_UI_PLAY:
msg_len = snprintf(buf, sizeof(buf), "{\"t\":\"music\", \"n\": %s} \n", "play");
break;
case MUSIC_CONTROL_UI_PAUSE:
msg_len = snprintf(buf, sizeof(buf), "{\"t\":\"music\", \"n\": %s} \n", "pause");
break;
case MUSIC_CONTROL_UI_NEXT_TRACK:
msg_len = snprintf(buf, sizeof(buf), "{\"t\":\"music\", \"n\": %s} \n", "next");
break;
case MUSIC_CONTROL_UI_PREV_TRACK:
msg_len = snprintf(buf, sizeof(buf), "{\"t\":\"music\", \"n\": %s} \n", "previous");
break;
case MUSIC_CONTROL_UI_CLOSE:
default:
// Nothing to do
break;
}
if (msg_len > 0) {
ble_comm_send(buf, msg_len);
}

}

static struct ble_transport_cb ble_transport_callbacks = {
.data_receive = bt_receive_cb,
};
Expand Down
10 changes: 10 additions & 0 deletions app/src/events/music_event.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "music_event.h"
#include <zephyr/zbus/zbus.h>

ZBUS_CHAN_DEFINE(music_control_data_chan,
struct music_event,
NULL,
NULL,
ZBUS_OBSERVERS(android_music_control_lis),
ZBUS_MSG_INIT()
);
7 changes: 7 additions & 0 deletions app/src/events/music_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "music_control/music_control_ui.h"

struct music_event {
music_control_ui_evt_type_t control_type;
};