FakeSMCKey *FakeSMCKey::withHandler(const char *aName, const char *aType, unsigned char aSize, IOService *aHandler)
{
    FakeSMCKey *me = new FakeSMCKey;
	
    if (me && !me->init(aName, aType, aSize, 0, aHandler)) {
        me->release();
        return 0;
    }
	
    return me;
}
FakeSMCKey *FakeSMCKey::withValue(const char *aName, const char *aType, unsigned char aSize, const void *aValue)
{
    FakeSMCKey *me = new FakeSMCKey;
	
    if (me && !me->init(aName, aType, aSize, aValue)) {
        me->release();
        return 0;
    }
	
    return me;
}
Beispiel #3
0
/**
 *  Synchronized method to copy sensor value to specified buffer
 *
 *  @param key   Key name
 *  @param value Buffer to copy value to. Buffer should be already allocated with proper size to fit key value
 *
 *  @return True on success False otherwise
 */
bool FakeSMCPlugin::getKeyValue(const char *key, void *value)
{
    LOCK;

    FakeSMCKey *smcKey = keyStore->getKey(key);

    if (smcKey) {
        memcpy(value, smcKey->getValue(), smcKey->getSize());
    }

    UNLOCK;

    return smcKey != NULL;
}
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;
}
FakeSMCKey *FakeSMCDevice::addKeyWithHandler(const char *name, const char *type, unsigned char size, IOService *handler)
{
    KEYSLOCK;
    
    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();
        }
	}
    KEYSUNLOCK;
    
    if (!key)
        HWSensorsErrorLog("failed to create key %s", name);
    
	return key;
}
IOReturn FakeSMCKeyStoreUserClient::externalMethod(uint32_t selector, IOExternalMethodArguments* arguments, IOExternalMethodDispatch * dispatch, OSObject * target, void * reference )
{
	IOReturn result = kIOReturnError;

	if (keyStore == NULL || isInactive()) {
		result = kIOReturnNotAttached;
	}
	else if (!keyStore->isOpen(this)) {
		result = kIOReturnNotOpen;
	}

    SYNCLOCK;

    switch (selector) {
        case KERNEL_INDEX_SMC: {

            SMCKeyData_t *input = (SMCKeyData_t*)arguments->structureInput;
            SMCKeyData_t *output = (SMCKeyData_t*)arguments->structureOutput;

            switch (input->data8) {
                case SMC_CMD_READ_INDEX: {
                    FakeSMCKey *key = keyStore->getKey(input->data32);
                    output->key = _strtoul(key->getKey(), 4, 16);
                    result = kIOReturnSuccess;
                    break;
                }

                case SMC_CMD_READ_KEYINFO: {
                    char name[5];

                    _ultostr(name, input->key);

                    FakeSMCKey *key = keyStore->getKey(name);

                    if (key) {

                        output->keyInfo.dataSize = key->getSize();
                        output->keyInfo.dataType = _strtoul(key->getType(), 4, 16);

                        result = kIOReturnSuccess;
                    }
                    else result = kIOReturnNotFound;

                    break;
                }

                case SMC_CMD_READ_BYTES: {
                    char name[5];

                    _ultostr(name, input->key);

                    FakeSMCKey *key = keyStore->getKey(name);

                    if (key) {

                        memcpy(output->bytes, key->getValue(), key->getSize());

                        result = kIOReturnSuccess;
                    }
                    else result = kIOReturnNotFound;

                    break;
                }
                    
                case SMC_CMD_WRITE_BYTES: {
                    char name[5];
                    
                    _ultostr(name, input->key);
                
                    IOLog("FakeSMCKeyStoreUserClient: SMC_CMD_WRITE_BYTES key=%s", name);
                    
                    FakeSMCKey *key = keyStore->getKey(name);
                    
                    if (key) {
                        
                        key->setValueFromBuffer(input->bytes, input->keyInfo.dataSize);
                        
                        result = kIOReturnSuccess;
                    }
                    else {
                        char type[5];
                        
                        if (input->keyInfo.dataType) {
                            _ultostr(type, input->keyInfo.dataType);
                        }
                        else {
                            type[0] = '\0';
                        }
                        
                        keyStore->addKeyWithValue(name, type, input->keyInfo.dataSize, input->bytes);
                        
                        result = kIOReturnSuccess;
                    }
                    
                    break;
                }

                default:
                    result = kIOReturnBadArgument;
                    break;
            }

            break;
        }

        default:
            result = kIOReturnBadArgument;
            break;
    }

    SYNCUNLOCK;

    return result;
}
FakeSMCKey *FakeSMCDevice::addKeyWithValue(const char *name, const char *type, unsigned char size, const void *value)
{
    KEYSLOCK;
    
    FakeSMCKey* key;
	if ((key = getKey(name))) {
        
        if (type && strncmp(type, key->getType(), 4) == 0) {
            key->setType(type);
        }
        
        if (value) {
            key->setSize(size);
            key->setValueFromBuffer(value, size);
        }

#ifdef DEBUG
        if (debug) {
            if (strncmp("NATJ", key->getKey(), 5) == 0) {
                UInt8 val = *(UInt8*)key->getValue();
                
                switch (val) {
                    case 0:
                        HWSensorsInfoLog("Ninja Action Timer Job: do nothing");
                        break;
                        
                    case 1:
                        HWSensorsInfoLog("Ninja Action Timer Job: force shutdown to S5");
                        break;
                        
                    case 2:
                        HWSensorsInfoLog("Ninja Action Timer Job: force restart");
                        break;
                        
                    case 3:
                        HWSensorsInfoLog("Ninja Action Timer Job: force startup");
                        break;
                        
                    default:
                        break;
                }
            }
            else if (strncmp("NATi", key->getKey(), 5) == 0) {
                UInt16 val = *(UInt16*)key->getValue();
                
                HWSensorsInfoLog("Ninja Action Timer is set to %d", val);
            }
            else if (strncmp("MSDW", key->getKey(), 5) == 0) {
                UInt8 val = *(UInt8*)key->getValue();
                
                switch (val) {
                    case 0:
                        HWSensorsInfoLog("display is now asleep");
                        break;
                        
                    case 1:
                        HWSensorsInfoLog("display is now awake");
                        break;
                        
                    default:
                        break;
                }
            }
        }
        
		FakeSMCDebugLog("value updated for key %s, type: %s, size: %d", name, type, size);
#endif
	}
    else {
        
        FakeSMCDebugLog("adding key %s with value, type: %s, size: %d", name, type, size);
        
        OSString *wellKnownType = 0;
        
        if (!type) wellKnownType = OSDynamicCast(OSString, types->getObject(name));
        
        key = FakeSMCKey::withValue(name, type ? type : wellKnownType ? wellKnownType->getCStringNoCopy() : 0, size, value);
        if (key) {
            keys->setObject(key);
            updateKeyCounterKey();
        }
	}
    
    KEYSUNLOCK;
    
    if (!key)
        HWSensorsErrorLog("failed to create key %s", name);
    
	return key;
}
FakeSMCKey *FakeSMCDevice::addKeyWithValue(const char *name, const char *type, unsigned char size, const void *value)
{
    IORecursiveLockLock(device_lock);
    
    FakeSMCKey* key;
	if ((key = getKey(name))) {
        
        if (value) {
            key->setType(type);
            key->setSize(size);
            key->setValueFromBuffer(value, size);
        }
        
        if (debug) {
            if (strncmp("NATJ", key->getKey(), 5) == 0) {
                UInt8 val = *(UInt8*)key->getValue();
                
                switch (val) {
                    case 0:
                        HWSensorsInfoLog("Ninja Action Timer Job: do nothing");
                        break;
                        
                    case 1:
                        HWSensorsInfoLog("Ninja Action Timer Job: force shutdown to S5");
                        break;
                        
                    case 2:
                        HWSensorsInfoLog("Ninja Action Timer Job: force restart");
                        break;
                        
                    case 3:
                        HWSensorsInfoLog("Ninja Action Timer Job: force startup");
                        break;
                        
                    default:
                        break;
                }
            }
            else if (strncmp("NATi", key->getKey(), 5) == 0) {
                UInt16 val = *(UInt16*)key->getValue();
                
                HWSensorsInfoLog("Ninja Action Timer is set to %d", val);
            }
            else if (strncmp("MSDW", key->getKey(), 5) == 0) {
                UInt8 val = *(UInt8*)key->getValue();
                
                switch (val) {
                    case 0:
                        HWSensorsInfoLog("display is now asleep");
                        break;
                        
                    case 1:
                        HWSensorsInfoLog("display is now awake");
                        break;
                        
                    default:
                        break;
                }
            }
        }
        
		FakeSMCDebugLog("updating value for key %s, type: %s, size: %d", name, type, size);
	}
    else {
    
        FakeSMCDebugLog("adding key %s with value, type: %s, size: %d", name, type, size);
        
        if ((key = FakeSMCKey::withValue(name, type, size, value))) {
            keys->setObject(key);
            updateKeyCounterKey();
        }
	}
    IORecursiveLockUnlock(device_lock);

    if (!key)
        HWSensorsErrorLog("failed to create key %s", name);
        
	return key;
}