Android HAL 研究开发 FOR LED

Led.h    hardware\modules\include\Mokoid

#include <hardware/hardware.h>
……….
struct led_module_t {
   struct hw_module_t common;
};

struct led_control_device_t {
   struct hw_device_t common;
   /* supporting control APIs go here */
   int (*set_on)(struct led_control_device_t *dev, int32_t led);
   int (*set_off)(struct led_control_device_t *dev, int32_t led);
};

/*****************************************************************************/

struct led_control_context_t {
    struct led_control_device_t device;
};
#define LED_HARDWARE_MODULE_ID "led"
Led.c    hardware\modules\Led

static int led_set_on(struct led_control_device_t *dev)
{    
    //FIXME: do system call to control gpio led//可以调用真正的驱动程序,如write,read等

    LOGI("led_set_on");
    return 0;
}
static int led_set_off(struct led_control_device_t *dev)
{
    //FIXME: do system call to control gpio led// //可以调用真正的驱动程序,如write,read等

    LOGI("led_set_off");
    return 0;
}
static int led_device_open(const struct hw_module_t* module, const char* name,
        struct hw_device_t** device) 
{
    struct led_control_device_t *dev;
    dev = (struct led_control_device_t *)malloc(sizeof(*dev));
    memset(dev, 0, sizeof(*dev));
    dev->common.tag = HARDWARE_DEVICE_TAG;
    dev->common.version = 0;
    dev->common.module = module;
    dev->common.close = led_device_close;
    dev->set_on = led_on;
    dev->set_off = led_off;
    *device = &dev->common;
success:
    return 0;
}

static struct hw_module_methods_t led_module_methods = {
    open: led_device_open
};

const struct led_module_t HAL_MODULE_INFO_SYM = {
    common: {
        tag: HARDWARE_MODULE_TAG,
        version_major: 1,
        version_minor: 0,
        id: LED_HARDWARE_MODULE_ID,
        name: "Sample LED Stub",
        author: "The Mokoid Open Source Project",
        methods: &led_module_methods,
    }
    /* supporting APIs go here */
};
LedService.java    frameworks\base\service\java\com\mokoid\Server

public final class LedService extends…….. { 
    static {
        System.load("/system/lib/libmokoid_runtime.so");
    }
    public LedService() {
        Log.i("LedService", "Go to get LED Stub...");
    _init();
    }
    /*
     * Mokoid LED native methods.
     */
    public boolean setOn(int led) {
        Log.i("MokoidPlatform", "LED On");
    return _set_on(led);
    }
    public boolean setOff(int led) {
        Log.i("MokoidPlatform", "LED Off");
    return _set_off(led);
    }
    private static native boolean _init();
    private static native boolean _set_on(int led);
    private static native boolean _set_off(int led);
}
com_mokoid_server_LedService.cpp      frameworks\base\service\Jni

static jboolean mokoid_setOn(JNIEnv* env, jobject thiz, jint led) {………}
static jboolean mokoid_setOff(JNIEnv* env, jobject thiz, jint led) {………}
static inline int led_control_open(const struct hw_module_t* module, struct led_control_device_t** device) {………..}
static jbooleanmokoid_init(JNIEnv *env, jclass clazz){…………..}
static const JNINativeMethod gMethods[] = {
    {"_init",         "()Z",     (void*)mokoid_init},
    { "_set_on", "(I)Z",     (void*)mokoid_setOn },
    { "_set_off", "(I)Z",     (void*)mokoid_setOff },
};
static int registerMethods(JNIEnv* env) {
    static const char* const kClassName = "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 */
    return 0;
}

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    JNIEnv* env = NULL;
    jint result = -1;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        LOGE("ERROR: GetEnv failed\n");
        goto bail;
    }
    assert(env != NULL);

    if (registerMethods(env) != 0) {
        LOGE("ERROR: PlatformLibrary native registration failed\n");
        goto bail;
    }

    /* success -- return valid version number */
    result = JNI_VERSION_1_4;

bail:
    return result;
}
LedManager.java    frameworks\base\core\java\mokoid\Hardware

public class LedManager
{
    private static final String TAG = "LedManager";
    private ILedService mLedService;

    public LedManager() {
    
        mLedService = ILedService.Stub.asInterface(
                             ServiceManager.getService("led"));

    if (mLedService != null) {
            Log.i(TAG, "The LedManager object is ready.");
    }
    }

    public boolean LedOn(int n) {……………..}

    public boolean LedOff(int n) {………………}
}
LedSystemServer.java    apps\ledtest\src\com\mokoid\LedTest

public class LedSystemServer extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onStart(Intent intent, int startId) {
        Log.i("LedSystemServer", "Start LedService...");

    /* Please also see SystemServer.java for your interests. */
    LedService ls = new LedService();

        try {
            ServiceManager.addService("led", ls);
        } catch (RuntimeException e) {
            Log.e("LedSystemServer", "Start LedService failed.");
        }
    }
LedTest.java   apps\ledtest\src\com\mokoid\LedTest

public class LedTest extends Activity implements View.OnClickListener {

    private LedManager mLedManager = null;
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Start LedService in a seperated process.

        startService(new Intent("com.mokoid.ledsystemserver"));

        Button btn = new Button(this);
        btn.setText("Click to turn LED 1 On");
    btn.setOnClickListener(this);

        setContentView(btn);
    }

LedSystemServer.java    apps\ledtest\src\com\mokoid\LedTest
LedTest.java    apps\ledtest\src\com\mokoid\LedTest
LedManager.java     frameworks\base\core\java\mokoid\Hardware
LedService.java     frameworks\base\service\java\com\mokoid\Server
com_mokoid_server_LedService.cpp    frameworks\base\service\Jni
Led.h    hardware\modules\include\Mokoid
Led.c    hardware\modules\Led

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值