const std::string WifiActionPower::NAME = "power";
-WifiActionPower::WifiActionPower(wifi_manager_h wifiHandle)
- : WifiAction(NAME), oldVal(false)
+WifiActionPower::WifiActionPower()
+ : WifiAction(NAME), cb(NULL), cbData(NULL), oldVal(false)
{
- handle = wifiHandle;
-}
-
-void WifiActionPower::activateCB(wifi_manager_error_e result, void *user_data)
-{
- INFO("wifi Activated");
- if (WIFI_MANAGER_ERROR_NONE != result) {
- const std::string &errStr = ((WifiActionPower*)user_data)->strErr(result);
- ERR("activateCB() Fail(%d, %s)", result, errStr.c_str());
- }
-}
-
-void WifiActionPower::deactivateCB(wifi_manager_error_e result, void *user_data)
-{
- INFO("wifi Deactivated");
- if (WIFI_MANAGER_ERROR_NONE != result) {
- const std::string &errStr = ((WifiActionPower*)user_data)->strErr(result);
- ERR("deactivateCB() Fail(%d, %s)", result, errStr.c_str());
- }
}
int WifiActionPower::set(bool val)
return MODES_ERROR_NONE;
}
+
+int WifiActionPower::setChangedCallback(valueChangedCB callback, void *userData)
+{
+ RETV_IF(NULL == callback, MODES_ERROR_INVALID_PARAMETER);
+
+ int ret = wifi_manager_set_device_state_changed_cb(handle, wifiStateChangedCB, this);
+ if (WIFI_MANAGER_ERROR_NONE != ret) {
+ ERR("wifi_manager_set_device_state_changed_cb() Fail(%s)", strErr(ret).c_str());
+ return MODES_ERROR_SYSTEM;
+ }
+
+ cb = callback;
+ cbData = userData;
+
+ DBG("WifiActionPower setChangedCallback() Success");
+ return MODES_ERROR_NONE;
+}
+int WifiActionPower::unSetChangedCallback(valueChangedCB callback, void *userData)
+{
+ RETV_IF(NULL == callback, MODES_ERROR_INVALID_PARAMETER);
+
+ int ret = wifi_manager_unset_device_state_changed_cb(handle);
+ if (WIFI_MANAGER_ERROR_NONE != ret) {
+ ERR("wifi_manager_unset_device_state_changed_cb() Fail(%s)", strErr(ret).c_str());
+ return MODES_ERROR_SYSTEM;
+ }
+
+ cb = NULL;
+ cbData = NULL;
+
+ DBG("WifiActionPower unSetChangedCallback() Success");
+ return MODES_ERROR_NONE;
+}
+
+void WifiActionPower::activateCB(wifi_manager_error_e result, void *user_data)
+{
+ INFO("wifi Activated");
+ if (WIFI_MANAGER_ERROR_NONE != result) {
+ const std::string &errStr = ((WifiActionPower*)user_data)->strErr(result);
+ ERR("activateCB() Fail(%d, %s)", result, errStr.c_str());
+ }
+}
+
+void WifiActionPower::deactivateCB(wifi_manager_error_e result, void *user_data)
+{
+ INFO("wifi Deactivated");
+ if (WIFI_MANAGER_ERROR_NONE != result) {
+ const std::string &errStr = ((WifiActionPower*)user_data)->strErr(result);
+ ERR("deactivateCB() Fail(%d, %s)", result, errStr.c_str());
+ }
+}
+
+void WifiActionPower::wifiStateChangedCB(wifi_manager_device_state_e state, void *user_data)
+{
+ WifiActionPower *action = (WifiActionPower*)user_data;
+
+ RET_IF(NULL == user_data);
+
+ bool changedVal = action->oldVal;
+ if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED)
+ changedVal = true;
+ else if (state == WIFI_MANAGER_DEVICE_STATE_DEACTIVATED)
+ changedVal = false;
+ else
+ ERR("Unknown state(%d)", state);
+
+ INFO("state:%d", state);
+
+ if (changedVal != action->oldVal) {
+ valueChangedCB cb = action->cb;
+ void *cbData = action->cbData;
+ //TODO: revise after revising the modes(because of piAction free)
+ action->unSetChangedCallback(action->cb, action->cbData);
+ cb(cbData);
+ }
+}
class WifiActionPower : public WifiAction {
public:
static const std::string NAME;
- WifiActionPower(wifi_manager_h handle);
+ WifiActionPower();
int set(bool val) override;
int undo() override;
std::string serialize() override;
int parse(const std::string &archive) override;
+ int setChangedCallback(valueChangedCB callback, void *userData);
+ int unSetChangedCallback(valueChangedCB callback, void *userData);
+
+ valueChangedCB cb;
+ void *cbData;
private:
static void activateCB(wifi_manager_error_e result, void *user_data);
static void deactivateCB(wifi_manager_error_e result, void *user_data);
+ static void wifiStateChangedCB(wifi_manager_device_state_e state, void * user_data);
bool oldVal;
};