From 87aafe5899a23a9e5a9a69e21f7aa41302a07ce5 Mon Sep 17 00:00:00 2001 From: Patrick Gaskin Date: Tue, 16 Jun 2020 19:12:21 -0400 Subject: [PATCH] Implemented nickel_wifi action (closes #25) (#46) --- res/doc | 7 ++++++ src/action.h | 1 + src/action_cc.cc | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/res/doc b/res/doc index 4f18261..653d559 100644 --- a/res/doc +++ b/res/doc @@ -31,6 +31,7 @@ # nickel_extras - opens one of the beta features # 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) # power - gracefully controls the power state # skip - skips a number of actions after the current one (mainly useful for more complex conditional chains) (this action will not update the success/failure flag) # the argument passed to the action: @@ -79,6 +80,12 @@ # reading_life:words - My Words # store:overdrive - OverDrive (4.10.11655+) (note: if you don't have an active OverDrive account, it will give you a "Network Error") # store:search - Search +# nickel_wifi - one of: +# autoconnect - attempts to enable and connect to wifi (similar to what happens when you open a link) +# autoconnect_silent - attempts to connect to wifi in the background (does nothing if wifi is disabled, the battery is low, is already connected, or there aren't any known networks in range) (no errors are shown) (similar to what happens when you turn on the Kobo) +# enable - enables WiFi (but doesn't necessarily connect to it) +# disable - disables WiFi (but doesn't necessarily connect to it) +# toggle - disables WiFi (but doesn't necessarily connect to it) # power - one of: # shutdown (4.13.12638+) # reboot (4.13.12638+) diff --git a/src/action.h b/src/action.h index 5f97eff..7873342 100644 --- a/src/action.h +++ b/src/action.h @@ -45,6 +45,7 @@ void nm_action_result_free(nm_action_result_t *res); X(nickel_extras) \ X(nickel_misc) \ X(nickel_open) \ + X(nickel_wifi) \ X(power) \ X(skip) diff --git a/src/action_cc.cc b/src/action_cc.cc index d9a4f1f..3084f09 100644 --- a/src/action_cc.cc +++ b/src/action_cc.cc @@ -50,6 +50,7 @@ typedef void PlugWorkflowManager; typedef void BrowserWorkflowManager; typedef void N3SettingsExtrasController; typedef void N3PowerWorkflowManager; +typedef void WirelessWorkflowManager; NM_ACTION_(nickel_open) { #define NM_ERR_RET nullptr @@ -451,6 +452,62 @@ NM_ACTION_(power) { #undef NM_ERR_RET } +NM_ACTION_(nickel_wifi) { + #define NM_ERR_RET nullptr + + //libnickel 4.6 * _ZN23WirelessWorkflowManager14sharedInstanceEv + WirelessWorkflowManager *(*WirelessWorkflowManager_sharedInstance)(); + reinterpret_cast(WirelessWorkflowManager_sharedInstance) = dlsym(RTLD_DEFAULT, "_ZN23WirelessWorkflowManager14sharedInstanceEv"); + NM_ASSERT(WirelessWorkflowManager_sharedInstance, "could not dlsym WirelessWorkflowManager::sharedInstance"); + + WirelessWorkflowManager *wfm = WirelessWorkflowManager_sharedInstance(); + NM_ASSERT(wfm, "could not get shared wireless manager pointer"); + + if (!strcmp(arg, "autoconnect")) { + //libnickel 4.6 * _ZN23WirelessWorkflowManager15connectWirelessEbb + void (*WirelessWorkflowManager_connectWireless)(WirelessWorkflowManager*, bool, bool); // I haven't looked into what the params are for, so I'm just using what the browser uses when opening it + reinterpret_cast(WirelessWorkflowManager_connectWireless) = dlsym(RTLD_DEFAULT, "_ZN23WirelessWorkflowManager15connectWirelessEbb"); + NM_ASSERT(WirelessWorkflowManager_connectWireless, "could not dlsym WirelessWorkflowManager::connectWireless"); + + WirelessWorkflowManager_connectWireless(wfm, true, false); + } else if (!strcmp(arg, "autoconnect_silent")) { + //libnickel 4.6 * _ZN23WirelessWorkflowManager23connectWirelessSilentlyEv + void (*WirelessWorkflowManager_connectWirelessSilently)(WirelessWorkflowManager*); + reinterpret_cast(WirelessWorkflowManager_connectWirelessSilently) = dlsym(RTLD_DEFAULT, "_ZN23WirelessWorkflowManager23connectWirelessSilentlyEv"); + NM_ASSERT(WirelessWorkflowManager_connectWirelessSilently, "could not dlsym WirelessWorkflowManager::connectWirelessSilently"); + + WirelessWorkflowManager_connectWirelessSilently(wfm); + } else if (!strcmp(arg, "enable") || !strcmp(arg, "disable") || !strcmp(arg, "toggle")) { + //libnickel 4.6 * _ZN23WirelessWorkflowManager14isAirplaneModeEv + bool (*WirelessWorkflowManager_isAirplaneMode)(WirelessWorkflowManager*); + reinterpret_cast(WirelessWorkflowManager_isAirplaneMode) = dlsym(RTLD_DEFAULT, "_ZN23WirelessWorkflowManager14isAirplaneModeEv"); + NM_ASSERT(WirelessWorkflowManager_isAirplaneMode, "could not dlsym WirelessWorkflowManager::isAirplaneMode"); + + bool e = WirelessWorkflowManager_isAirplaneMode(wfm); + NM_LOG("wifi disabled: %d", e); + + //libnickel 4.6 * _ZN23WirelessWorkflowManager15setAirplaneModeEb + void (*WirelessWorkflowManager_setAirplaneMode)(WirelessWorkflowManager*, bool); + reinterpret_cast(WirelessWorkflowManager_setAirplaneMode) = dlsym(RTLD_DEFAULT, "_ZN23WirelessWorkflowManager15setAirplaneModeEb"); + NM_ASSERT(WirelessWorkflowManager_setAirplaneMode, "could not dlsym WirelessWorkflowManager::setAirplaneMode"); + + if (!strcmp(arg, "enable")) { + if (e) + WirelessWorkflowManager_setAirplaneMode(wfm, false); + } else if (!strcmp(arg, "disable")) { + if (!e) + WirelessWorkflowManager_setAirplaneMode(wfm, true); + } else if (!strcmp(arg, "toggle")) { + WirelessWorkflowManager_setAirplaneMode(wfm, !e); + } + } else { + NM_RETURN_ERR("unknown wifi action '%s'", arg); + } + + NM_RETURN_OK(nm_action_result_silent()); + #undef NM_ERR_RET +} + NM_ACTION_(cmd_spawn) { #define NM_ERR_RET nullptr char *tmp = strdup(arg); // strsep and strtrim will modify it