Add function Device::isMethodSupported()

This function both checks if a method is supported and also "upmask" a method if
it was masked previously.
This commit is contained in:
Micke Prag 2012-11-02 16:18:27 +01:00
parent 35dbc0a980
commit 2368b6e564
2 changed files with 30 additions and 15 deletions

View file

@ -148,23 +148,11 @@ int Device::doAction(int action, unsigned char data, Controller *controller) {
return TELLSTICK_ERROR_CONFIG_SYNTAX; return TELLSTICK_ERROR_CONFIG_SYNTAX;
} }
// Try to determine if we need to call another method due to masking // Try to determine if we need to call another method due to masking
int methods = p->methods(); int method = this->isMethodSupported(action);
if ((action & methods) == 0) { if (method <= 0) {
// Loop all methods an see if any method masks to this one
for(int i = 1; i <= methods; i <<= 1) {
if ((i & methods) == 0) {
continue;
}
if (this->maskUnsupportedMethods(i, action)) {
action = i;
break;
}
}
}
if ((action & methods) == 0) {
return TELLSTICK_ERROR_METHOD_NOT_SUPPORTED; return TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
} }
std::string code = p->getStringForMethod(action, data, controller); std::string code = p->getStringForMethod(method, data, controller);
if (code == "") { if (code == "") {
return TELLSTICK_ERROR_METHOD_NOT_SUPPORTED; return TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
} }
@ -189,6 +177,32 @@ int Device::doAction(int action, unsigned char data, Controller *controller) {
return controller->send(code); return controller->send(code);
} }
int Device::isMethodSupported(int method) const {
Protocol *p = this->retrieveProtocol();
if (!p) {
// Syntax error in configuration, no such protocol
return TELLSTICK_ERROR_CONFIG_SYNTAX;
}
// Try to determine if we need to call another method due to masking
int methods = p->methods();
if ((method & methods) == 0) {
// Loop all methods an see if any method masks to this one
for(int i = 1; i <= methods; i <<= 1) {
if ((i & methods) == 0) {
continue;
}
if (this->maskUnsupportedMethods(i, method)) {
method = i;
break;
}
}
}
if ((method & methods) == 0) {
return TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
}
return method;
}
Protocol* Device::retrieveProtocol() const { Protocol* Device::retrieveProtocol() const {
if (d->protocol) { if (d->protocol) {
return d->protocol; return d->protocol;

View file

@ -19,6 +19,7 @@ public:
~Device(void); ~Device(void);
int doAction(int action, unsigned char data, Controller *controller); int doAction(int action, unsigned char data, Controller *controller);
int isMethodSupported(int method) const;
std::wstring getStateValue(); std::wstring getStateValue();
int getLastSentCommand(int methodsSupported); int getLastSentCommand(int methodsSupported);
int getMethods() const; int getMethods() const;