Download presentation
Presentation is loading. Please wait.
Published byBarbra Craig Modified over 6 years ago
1
chapter 7-Android HAL 中国科学技术大学软件学院
2
Android Architecture 2
3
Android device Driver 3
4
Android device Driver 4
5
Android device Driver #include "s3c6410_leds_hal.h"
#include "leds_hal_define.h" static unsigned char mem[5]; static int major = S3C6410_LEDS_MAJOR; static int minor = S3C6410_LEDS_MINOR; static dev_t dev_number; static struct class *leds_class = NULL; static int bytes_to_int(unsigned char buf[], int start) { int n = 0; n = ((int) buf[start]) << 24 | ((int) buf[start + 1]) << 16 | ((int) buf[start + 2]) << 8 | ((int) buf[start + 3]); return n; } 5
6
Android device Driver static void int_to_bytes(int n, unsigned char buf[], int start) { buf[start] = n >> 24; buf[start + 1] = n >> 16; buf[start + 2] = n >> 8; buf[start + 3] = n; } 6
7
Android device Driver static ssize_t s3c6410_leds_hal_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos) { if (copy_from_user(mem, buf, 5)) return -EFAULT; } else int gpm_type = mem[0]; switch (gpm_type) case S3C6410_LEDS_HAI_WRITE_GPMCON: iowrite32(bytes_to_int(mem, 1), S3C64XX_GPMCON); break; case S3C6410_LEDS_HAI_WRITE_GPMPUD: iowrite32(bytes_to_int(mem, 1), S3C64XX_GPMPUD); case S3C6410_LEDS_HAI_WRITE_GPMDAT: iowrite32(bytes_to_int(mem, 1), S3C64XX_GPMDAT); return 5; 7
8
Android device Driver static ssize_t s3c6410_leds_hal_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { int gpm_type = mem[0]; int gpm_value = 0; switch (gpm_type) case S3C6410_LEDS_HAI_READ_GPMCON: gpm_value = ioread32(S3C64XX_GPMCON); break; case S3C6410_LEDS_HAI_READ_GPMPUD: gpm_value = ioread32(S3C64XX_GPMPUD); case S3C6410_LEDS_HAI_READ_GPMDAT: gpm_value = ioread32(S3C64XX_GPMDAT); } int_to_bytes(gpm_value, mem, 1); if (copy_to_user(buf, (void*) mem, 5)) return -EFAULT; return 5; 8
9
Android device Driver static struct file_operations dev_fops =
{ .owner = THIS_MODULE, .read = s3c6410_leds_hal_read, .write = s3c6410_leds_hal_write }; static struct cdev leds_cdev; 9
10
Android device Driver (/dev/s3c6410_leds_hal)
static int leds_create_device(void) { int ret = 0; int err = 0; cdev_init(&leds_cdev, &dev_fops); leds_cdev.owner = THIS_MODULE; if (major > 0) dev_number = MKDEV(major, minor); err = register_chrdev_region(dev_number, DEVICE_COUNT, DEVICE_NAME); if (err < 0) printk(KERN_WARNING "register_chrdev_region() failed\n"); return err; } 10
11
Android device Driver else {
err = alloc_chrdev_region(&leds_cdev.dev, 10, DEVICE_COUNT, DEVICE_NAME); if (err < 0) printk(KERN_WARNING "alloc_chrdev_region() failed\n"); return err; } major = MAJOR(leds_cdev.dev); minor = MINOR(leds_cdev.dev); //dev_number = MKDEV(major, minor); dev_number = leds_cdev.dev; ret = cdev_add(&leds_cdev, dev_number, DEVICE_COUNT); leds_class = class_create(THIS_MODULE, DEVICE_NAME); device_create(leds_class, NULL, dev_number, NULL, DEVICE_NAME); return ret; 11
12
Android device Driver static int leds_init(void) { int ret;
ret = leds_create_device(); printk(DEVICE_NAME"\tinitialized\n"); printk(KERN_EMERG"tes1fdddfs1t\n"); return ret; } static void leds_destroy_device(void) { device_destroy(leds_class, dev_number); if (leds_class) class_destroy(leds_class); unregister_chrdev_region(dev_number, DEVICE_COUNT); return; static void leds_exit(void) leds_destroy_device(); printk(DEVICE_NAME"\texit!\n"); module_init(leds_init); module_exit(leds_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("ustc"); 12
13
Android device Driver-example
#include <stdlib.h> #include <string.h> #include <unistd.h> #include <assert.h> #include <jni.h> #include <leds_hal.h> //led_control_device_t struct led_control_device_t *led_hal_device = NULL; static jboolean led_setOn(JNIEnv* env, jobject thiz, jint led) { LOGI("Led HAL JNI: led_setOn() is invoked."); if (led_hal_device == NULL) LOGI("Led HAL JNI: led_hal_device was not fetched correctly."); return -1; } else { //led_control_device_t.set_on return led_hal_device->set_on(led_hal_device, led); LedHalService.app 13
14
Android device Driver-example
static jboolean led_setOff(JNIEnv* env, jobject thiz, jint led) { LOGI("Led HAL JNI: led_setOff() is invoked."); if (led_hal_device == NULL) LOGI("Led HAL JNI: led_hal_device was not fetched correctly."); return -1; } else return led_hal_device->set_off(led_hal_device, led); 14
15
Android device Driver-example
static inline int led_control_open(const struct hw_module_t* module, struct led_control_device_t** device) { return module->methods->open(module, LED_HARDWARE_MODULE_ID, (struct hw_device_t**) device); }static jboolean led_init(JNIEnv *env, jclass clazz) led_module_t* module; LOGE("**********start find hal *********"); LOGE(LED_HARDWARE_MODULE_ID); if (hw_get_module(LED_HARDWARE_MODULE_ID, (const hw_module_t**) &module) == 0) LOGI("LedService JNI: LED Stub found."); if (led_control_open(&module->hw_module, &led_hal_device) == 0) LOGI("LedService JNI: Got Stub operations."); return 0; } LOGE("LedService JNI: Get Stub operations failed."); return -1; 15
16
Android device Driver-example
static const JNINativeMethod methods[] = { { "_init", "()Z", (void *) led_init }, { "_set_on", "(I)Z", (void *) led_setOn }, { "_set_off", "(I)Z", (void *) led_setOff }, }; int register_led_hal_jni(JNIEnv* env) static const char* const kClassName = "mobile/android/leds/hal/service/LedHalService"; jclass clazz; clazz = env->FindClass(kClassName); if (clazz == NULL) LOGE("Can't find class %s\n", kClassName); return -1; } if (env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])) != JNI_OK) LOGE("Failed registering methods for %s\n", kClassName); return 0; 16
17
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("GetEnv failed!"); return result; } register_led_hal_jni(env); return JNI_VERSION_1_4; 17
18
Android device Driver-example
package mobile.android.s3c6410.leds.hal.java; import mobile.android.leds.hal.service.LedHalService; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; public class S3C6410LedHalMain extends Activity { private CheckBox[] cbStrLeds = new CheckBox[4]; LedHalService ledHalService = LedHalService.getInstance(); @Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); cbStrLeds[0] = (CheckBox) findViewById(R.id.checkbox_str_led1); cbStrLeds[1] = (CheckBox) findViewById(R.id.checkbox_str_led2); cbStrLeds[2] = (CheckBox) findViewById(R.id.checkbox_str_led3); cbStrLeds[3] = (CheckBox) findViewById(R.id.checkbox_str_led4); } public void onClick_ControlStr(View view) String str = ""; for (int i = 0; i < 4; i++) if (cbStrLeds[i].isChecked()) ledHalService.setOn(i); else ledHalService.setOff(i); 18
19
19
20
20
21
thank you !
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.