FakeSMCKey *FakeSMCDevice::addKeyWithHandler(const char *name, const char *type, unsigned char size, IOService *handler)
{
    IORecursiveLockLock(device_lock);

    FakeSMCKey* key;
	if ((key = getKey(name))) {
		HWSensorsErrorLog("key %s already handled", name);
        if (key->getHandler() != NULL) {
            // TODO: check priority?
            HWSensorsErrorLog("key %s already handled", name);
            key = 0;
        }
        else {
            key->setType(type);
            key->setSize(size);
            key->setHandler(handler);
        }
        
	}
    else {
    
        FakeSMCDebugLog("adding key %s with handler, type: %s, size: %d", name, type, size);
        
        if ((key = FakeSMCKey::withHandler(name, type, size, handler))) {
            keys->setObject(key);
            updateKeyCounterKey();
        }
	}
    IORecursiveLockUnlock(device_lock);
    
    if (!key)
        HWSensorsErrorLog("failed to create key %s", name);
        
	return key;
}
FakeSMCKey *FakeSMCKeyStore::addKeyWithHandler(const char *name, const char *type, unsigned char size, FakeSMCKeyHandler *handler)
{
    FakeSMCKey *key = 0;
    
    KEYSLOCK;
    
    if ((key = getKey(name))) {
        
        FakeSMCKeyHandler *existedHandler = key->getHandler();
        
        if (handler->getProbeScore() < existedHandler->getProbeScore()) {
            HWSensorsErrorLog("key %s already handled with prioritized handler %s", name, existedHandler ? existedHandler->getName() : "*Unreferenced*");
            key = 0;
        }
        else {
            HWSensorsInfoLog("key %s handler %s has been replaced with new prioritized handler %s", name, existedHandler ? existedHandler->getName() : "*Unreferenced*", handler ? handler->getName() : "*Unreferenced*");
            
            key->setType(type);
            key->setSize(size);
            key->setHandler(handler);
        }
    }
    else {
        
        HWSensorsDebugLog("adding key %s with handler, type: %s, size: %d", name, type, size);
        
        if ((key = FakeSMCKey::withHandler(name, type, size, handler))) {
            ////KEYSLOCK;
            keys->setObject(key);
            ////KEYSUNLOCK;
            updateKeyCounterKey();
        }
    }
    
    KEYSUNLOCK;
    
    if (!key)
        HWSensorsErrorLog("addKeyWithHandler: failed to create key %s", name);
    
    return key;
}
FakeSMCKey *FakeSMCDevice::addKeyWithHandler(const char *name, const char *type, unsigned char size, IOService *handler)
{
    KEYSLOCK;
    
    FakeSMCKey *key;
    if ((key = getKey(name))) {
        
        IOService *existedHandler = key->getHandler();
        
        if (getHandlingPriority(handler) < getHandlingPriority(existedHandler)) {
            HWSensorsErrorLog("key %s already handled with prioritized handler %s", name, existedHandler ? existedHandler->getName() : "*Unreferenced*");
            key = 0;
        }
        else {
            HWSensorsInfoLog("key %s handler %s has been replaced with new prioritized handler %s", name, existedHandler ? existedHandler->getName() : "*Unreferenced*", handler ? handler->getName() : "*Unreferenced*");
            
            key->setType(type);
            key->setSize(size);
            key->setHandler(handler);
        }
    }
    else {
        
        FakeSMCDebugLog("adding key %s with handler, type: %s, size: %d", name, type, size);
        
        if ((key = FakeSMCKey::withHandler(name, type, size, handler))) {
            keys->setObject(key);
            updateKeyCounterKey();
        }
        else {
            HWSensorsErrorLog("failed to create key %s", name);
        }
    }
    KEYSUNLOCK;
    
    return key;
}