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

Implement connection manager. #1031

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
9 changes: 7 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ function processArguments() {
DEVICES="$DEVICES uhk-80-left uhk-80-right uhk-dongle"
shift
;;
clean|setup|update|build|make|flash|flashUsb|shell|release)
clean|setup|update|build|make|flash|shell|release)
ACTIONS="$ACTIONS $1"
TARGET_TMUX_SESSION=$BUILD_SESSION_NAME
shift
;;
flashUsb)
ACTIONS="$ACTIONS make $1"
TARGET_TMUX_SESSION=$BUILD_SESSION_NAME
shift
;;
addrline)
shift
ADDR=$1
Expand Down Expand Up @@ -374,7 +379,7 @@ function run() {
if [ `echo $DEVICES | wc -w` -gt 1 ]
then
runPerDevice
elif [ `echo $DEVICES | wc -w` -eq 0 ]
elif [ `echo $DEVICES | wc -w` -le 1 ]
then
performActions $DEVICES
else
Expand Down
7 changes: 7 additions & 0 deletions device/prj.conf.overlays/nrf_shared.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ CONFIG_BT_SMP=y

CONFIG_BT_L2CAP_TX_BUF_COUNT=5

# Todo: place these where they belong
# config for right half host switching
CONFIG_BT_MAX_CONN=4
CONFIG_BT_CTLR_SDC_PERIPHERAL_COUNT=3
CONFIG_BT_FILTER_ACCEPT_LIST=y


# negotiate larger MTU for NUS
CONFIG_BT_USER_DATA_LEN_UPDATE=y
CONFIG_BT_BUF_ACL_RX_SIZE=251
Expand Down
1 change: 1 addition & 0 deletions device/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if(NOT CONFIG_BOARD_UHK_60_RIGHT)
bt_conn.c
bt_manager.c
bt_pair.c
connections.c
debug_eventloop_timing.c
device_state.c
flash.c
Expand Down
53 changes: 47 additions & 6 deletions device/src/bt_advertise.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include "bt_advertise.h"
#include <bluetooth/services/nus.h>
#include <zephyr/bluetooth/gatt.h>
#include "bt_conn.h"
#include "connections.h"
#include "device.h"
#include "event_scheduler.h"
#include "bt_scan.h"

#undef DEVICE_NAME
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
Expand Down Expand Up @@ -38,8 +42,24 @@ static const struct bt_data sdHid[] = {SD_HID_DATA};

static const struct bt_data sdNusHid[] = {SD_NUS_DATA SD_HID_DATA};

void BtAdvertise_Start(uint8_t adv_type)
static struct bt_le_adv_param advertisementParams[] = BT_LE_ADV_CONN;

static void setFilters() {
bt_le_filter_accept_list_clear();
if (DEVICE_IS_UHK80_RIGHT) {
if (BtConn_UnusedPeripheralConnectionCount() <= 1 && SelectedHostConnectionId != ConnectionId_Invalid) {
bt_le_filter_accept_list_add(&HostConnection(SelectedHostConnectionId)->bleAddress);
advertisementParams->options = BT_LE_ADV_OPT_FILTER_CONN;
} else {
advertisementParams->options = BT_LE_ADV_OPT_NONE;
}
}
}

uint8_t BtAdvertise_Start(uint8_t adv_type)
{
setFilters();

int err;
const char *adv_type_string;
if (adv_type == (ADVERTISE_NUS | ADVERTISE_HID)) {
Expand All @@ -54,33 +74,54 @@ void BtAdvertise_Start(uint8_t adv_type)
err = bt_le_adv_start(BT_LE_ADV_CONN, adHid, ARRAY_SIZE(adHid), sdHid, ARRAY_SIZE(sdHid));
} else {
printk("Attempted to start advertising without any type! Ignoring.\n");
return;
return 0;
}

if (err == 0) {
printk("%s advertising successfully started\n", adv_type_string);
return 0;
} else if (err == -EALREADY) {
printk("%s advertising continued\n", adv_type_string);
return 0;
} else {
printk("%s advertising failed to start (err %d)\n", adv_type_string, err);
printk("%s advertising failed to start (err %d), free connections: %d\n", adv_type_string, err, BtConn_UnusedPeripheralConnectionCount());
return err;
}
}

void BtAdvertise_Stop() {
int err = bt_le_adv_stop();
if (err) {
printk("Advertising failed to stop (err %d)\n", err);
} else {
printk("Advertising successfully stopped\n");
}
}

static uint8_t connectedHidCount() {
uint8_t connectedHids = 0;
for (uint8_t peerId = PeerIdFirstHost; peerId <= PeerIdLastHost; peerId++) {
if (Peers[peerId].conn && Connections_Type(Peers[peerId].connectionId) == ConnectionType_BtHid) {
connectedHids++;
}
}
return connectedHids;
}

uint8_t BtAdvertise_Type() {
switch (DEVICE_ID) {
case DeviceId_Uhk80_Left:
return ADVERTISE_NUS;
case DeviceId_Uhk80_Right:
return ADVERTISE_NUS | ADVERTISE_HID;
if (BtConn_UnusedPeripheralConnectionCount() > 0) {
if (connectedHidCount() > 0) {
return ADVERTISE_NUS;
} else {
return ADVERTISE_NUS | ADVERTISE_HID;
}
} else {
printk("Current slot count %d, not advertising\n", BtConn_UnusedPeripheralConnectionCount());
BtConn_ListCurrentConnections();
return 0;
}
case DeviceId_Uhk_Dongle:
return 0;
default:
Expand Down
2 changes: 1 addition & 1 deletion device/src/bt_advertise.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// Functions:

void BtAdvertise_Start(uint8_t adv_type);
uint8_t BtAdvertise_Start(uint8_t adv_type);
void BtAdvertise_Stop();
uint8_t BtAdvertise_Type();

Expand Down
Loading