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 nickel_bluetooth action #152

Merged
merged 4 commits into from
Dec 15, 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
7 changes: 7 additions & 0 deletions res/doc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
# nickel_misc - other stuff which isn't significant enough for its own category
# nickel_open - opens a view
# nickel_wifi - controls wifi (note: it doesn't wait for it to connect or disconnect, neither does it check for success)
# nickel_bluetooth - controls bluetooth
# nickel_orientation - controls screen orientation
# (devices without an orientation sensor may need to use the kobopatch patch "Allow rotation on all devices" or set [DeveloperSettings] ForceAllowLandscape=true)
# (devices with an orientation sensor don't need to do anything, but can set the config setting to make this work on all views)
Expand Down Expand Up @@ -147,6 +148,12 @@
# reboot (4.13.12638+)
# sleep (4.13.12638+)
# skip - the number of actions to skip, or -1 to skip all remaining ones (i.e. end the chain)
# nickel_bluetooth - one of:
# enable (4.34.20097+)
# disable (4.34.20097+)
# toggle (4.34.20097+)
# check (4.34.20097+)
# scan (4.34.20097+)
#
# chain_success:<action>:<arg>
# chain_failure:<action>:<arg>
Expand Down
1 change: 1 addition & 0 deletions src/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void nm_action_result_free(nm_action_result_t *res);
X(nickel_misc) \
X(nickel_open) \
X(nickel_wifi) \
X(nickel_bluetooth) \
X(nickel_orientation) \
X(power) \
X(skip)
Expand Down
70 changes: 70 additions & 0 deletions src/action_cc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef void WirelessWorkflowManager;
typedef void StatusBarView;
typedef void MoreController;
typedef void MainWindowController;
typedef void BluetoothManager;

#define NM_ACT_SYM(var, sym) reinterpret_cast<void*&>(var) = dlsym(RTLD_DEFAULT, sym)
#define NM_ACT_XSYM(var, symb, err) do { \
Expand Down Expand Up @@ -896,3 +897,72 @@ NM_ACTION_(cmd_output) {
? nm_action_result_silent()
: nm_action_result_msg("%s", qPrintable(Qt::convertFromPlainText(out, Qt::WhiteSpacePre)));
}

NM_ACTION_(nickel_bluetooth) {
enum BLUETOOTH_ACTION {
ENABLE = 0b00001,
DISABLE = 0b00010,
TOGGLE = 0b00100,
CHECK = 0b01000,
SCAN = 0b10000
};

int action = 0;
if (!strcmp(arg, "enable")) action |= ENABLE;
else if (!strcmp(arg, "disable")) action |= DISABLE;
else if (!strcmp(arg, "toggle")) action |= TOGGLE;
else if (!strcmp(arg, "check")) action |= CHECK;
else if (!strcmp(arg, "scan")) action |= SCAN;
else
NM_ERR_RET(nullptr, "unknown nickel_bluetooth action '%s'", arg);

//libnickel 4.34.20097 * _ZN16BluetoothManager14sharedInstanceEv
BluetoothManager *(*BluetoothManager_sharedInstance)();
NM_ACT_XSYM(BluetoothManager_sharedInstance, "_ZN16BluetoothManager14sharedInstanceEv", "could not dlsym BluetoothManager::sharedInstance");

//libnickel 4.34.20097 * _ZNK16BluetoothManager2upEv
uint (*BluetoothManager_up)(BluetoothManager *);
NM_ACT_XSYM(BluetoothManager_up, "_ZNK16BluetoothManager2upEv", "could not dlsym BluetoothManager::up");

//libnickel 4.34.20097 * _ZN16BluetoothManager2onEv
void (*BluetoothManager_on)(BluetoothManager *);
NM_ACT_XSYM(BluetoothManager_on, "_ZN16BluetoothManager2onEv", "could not dlsym BluetoothManager::on");

//libnickel 4.34.20097 * _ZN16BluetoothManager4scanEv
void (*BluetoothManager_scan)(BluetoothManager *);
NM_ACT_XSYM(BluetoothManager_scan, "_ZN16BluetoothManager4scanEv", "could not dlsym BluetoothManager::BluetoothManager::scanEv");

//libnickel 4.34.20097 * _ZN16BluetoothManager8stopScanEv
void (*BluetoothManager_stopScan)(BluetoothManager *);
NM_ACT_XSYM(BluetoothManager_stopScan, "_ZN16BluetoothManager8stopScanEv", "could not dlsym BluetoothManager::stopScan");

//libnickel 4.34.20097 * _ZN16BluetoothManager3offEv
void (*BluetoothManager_off)(BluetoothManager *);
NM_ACT_XSYM(BluetoothManager_off, "_ZN16BluetoothManager3offEv", "could not dlsym BluetoothManager::off");

BluetoothManager *btm = BluetoothManager_sharedInstance();
NM_CHECK(nullptr, btm, "could not get shared bluetooth manager pointer");

uint isUp = BluetoothManager_up(btm);
if (action & TOGGLE)
action = (action & ~TOGGLE) | (isUp ? DISABLE : ENABLE);

switch (action) {
case CHECK:
return nm_action_result_toast("Bluetooth is %s.", isUp ? "on" : "off");
case ENABLE:
BluetoothManager_on(btm);
BluetoothManager_scan(btm);
return nm_action_result_toast("Bluetooth turned on.");
case DISABLE:
BluetoothManager_stopScan(btm);
BluetoothManager_off(btm);
return nm_action_result_toast("Bluetooth turned off.");
case SCAN:
BluetoothManager_scan(btm);
return nm_action_result_toast("Bluetooth scan initiated.");
default:
NM_ERR_RET(nullptr, "unknown nickel_bluetooth action '%s'", arg);
break;
}
}