#define LOG_TAG "Mokoid"
#include "utils/Log.h"
#include
#include
#include
#include
#include
#include "led.h"
// ----------------------------------------------------------------------------
structled_control_device_t*sLedDevice=NULL;
staticjboolean mokoid_setOn(JNIEnv*env,jobject thiz,jint led)
{
LOGI("LedService JNI: mokoid_setOn() is invoked.");
if(sLedDevice==NULL){
LOGI("LedService JNI: sLedDevice was not fetched correctly.");
return-1;
}else{
returnsLedDevice->set_on(sLedDevice,led);
}
}
staticjboolean mokoid_setOff(JNIEnv*env,jobject thiz,jint led)
{
LOGI("LedService JNI: mokoid_setOff() is invoked.");
if(sLedDevice==NULL){
LOGI("LedService JNI: sLedDevice was not fetched correctly.");
return-1;
}else{
returnsLedDevice->set_off(sLedDevice,led);
}
}
/** helper APIs */
staticinlineintled_control_open(conststructhw_module_t*module,
structled_control_device_t**device){
returnmodule->methods->open(module,
LED_HARDWARE_MODULE_ID,(structhw_device_t**)device);
}
staticjboolean mokoid_init(JNIEnv*env,jclass clazz)
{
led_module_t*module;
if(hw_get_module(LED_HARDWARE_MODULE_ID,(consthw_module_t**)&module)==0){
LOGI("LedService JNI: LED Stub found.");
if(led_control_open(&module->common,&sLedDevice)==0){
LOGI("LedService JNI: Got Stub operations.");
return0;
}
}
LOGE("LedService JNI: Get Stub operations failed.");
return-1;
}
// --------------------------------------------------------------------------
/*
* Array of methods.
*
* Each entry has three fields: the name of the method, the method
* signature, and a pointer to the native implementation.
*/
staticconstJNINativeMethod gMethods[]={
{"_init","()Z",(void*)mokoid_init},
{"_set_on","(I)Z",(void*)mokoid_setOn},
{"_set_off","(I)Z",(void*)mokoid_setOff},
};
intregister_mokoid_server_LedService(JNIEnv*env){
staticconstchar*constkClassName=
"com/mokoid/server/LedService";
jclass clazz;
/* look up the class */
clazz=env->FindClass(kClassName);
if(clazz==NULL){
LOGE("Can't find class %s\n",kClassName);
return-1;
}
/* register all the methods */
if(env->RegisterNatives(clazz,gMethods,
sizeof(gMethods)/sizeof(gMethods[0]))!=JNI_OK)
{
LOGE("Failed registering methods for %s\n",kClassName);
return-1;
}
/* fill out the rest of the ID cache */
return0;
}
staticintregisterMethods(JNIEnv*env){
staticconstchar*constkClassName="com/mokoid/server/LedService";
jclass clazz;
/* look up the class */
clazz=env->FindClass(kClassName);
if(clazz==NULL){
LOGE("Can't find class %s/n",kClassName);
return-1;
}
/* register all the methods */
if(env->RegisterNatives(clazz,gMethods,
sizeof(gMethods)/sizeof(gMethods[0]))!=JNI_OK)
{
LOGE("Failed registering methods for %s/n",kClassName);
return-1;
}
/* fill out the rest of the ID cache */
return0;
}
/*
* This is called by the VM when the shared library is first loaded.
*/
jint JNI_OnLoad(JavaVM*vm,void*reserved){
JNIEnv*env=NULL;
jint result=-1;
LOGI("JNI_OnLoad LED");
if(vm->GetEnv((void**)&env,JNI_VERSION_1_4)!=JNI_OK){
LOGE("ERROR: GetEnv failed/n");
gotofail;
}
assert(env!=NULL);
if(registerMethods(env)!=0){
LOGE("ERROR: PlatformLibrary native registration failed/n");
gotofail;
}
/* success -- return valid version number */
result=JNI_VERSION_1_4;
fail:
returnresult;
}