以backlight为例说明hal层调用

int hw_get_module(const char *id, const struct hw_module_t **module)
{
typedef struct hw_module_t {
       struct hw_module_methods_t* methods;
typedef struct hw_module_methods_t {
   /** Open a specific device */
   int (*open)(const struct hw_module_t* module, const char* id,  struct hw_device_t** device);


typedef struct hw_device_t {
   /** tag must be initialized to HARDWARE_DEVICE_TAG */
   uint32_t tag;


   /** reference to the module this device belongs to */
   struct hw_module_t* module;


} hw_device_t;
} hw_module_methods_t;

   /** module's dso */
   void* dso;
   
} hw_module_t;


    return hw_get_module_by_class(id, NULL, module);


int hw_get_module_by_class(const char *class_id, const char *inst,
                          const struct hw_module_t **module)
{
  
   for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
static const int HAL_VARIANT_KEYS_COUNT =
   (sizeof(variant_keys)/sizeof(variant_keys[0]));
   
             property_get(variant_keys[i], prop, NULL);
static const char *variant_keys[] = {
   "ro.hardware",  /* This goes first so that it can pick up a different
                      file on the emulator. */
   "ro.product.board",
   "ro.board.platform",
   "ro.arch"
};




   
    hw_module_exists(path, sizeof(path), name, prop);
   snprintf(path, path_len, "%s/%s.%s.so",HAL_LIBRARY_PATH2, name, subname);
   access(path, R_OK) ;
   
   snprintf(path, path_len, "%s/%s.%s.so",HAL_LIBRARY_PATH1, name, subname);
   access(path, R_OK) ;


   load(class_id, path, module);
  void *handle;
         struct hw_module_t *hmi;
         
    handle = dlopen(path, RTLD_NOW);
 
const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
hmi = (struct hw_module_t *)dlsym(handle, sym);


   /* Check that the id matches */
   if (strcmp(id, hmi->id) != 0) {
   
  typedef struct hw_module_t {
   /** Identifier of module */
   const char *id;    
} hw_module_t;

       ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
       status = -EINVAL;
       goto done;
   }


  hmi->dso = handle;
  typedef struct hw_module_t {
   /** module's dso */
   void* dso;
} hw_module_t;


*pHmi = hmi;
   
   }
}
}





Onload.cpp (z:\home\administrator\ivaan\msm8994\frameworks\base\services\core\jni):int register_android_server_LightsService(JNIEnv* env);
namespace android {
int register_android_server_LightsService(JNIEnv* env);
com_android_server_lights_LightsService.cpp (z:\home\administrator\ivaan\msm8994\frameworks\base\services\core\jni):    err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
namespace android
{
int register_android_server_LightsService(JNIEnv *env)
{
   return jniRegisterNativeMethods(env, "com/android/server/lights/LightsService",method_table, NELEM(method_table));
static JNINativeMethod method_table[] = {
   { "init_native", "()J", (void*)init_native },
static jlong init_native(JNIEnv *env, jobject clazz)
hw_module_t* module;
err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
#define LIGHTS_HARDWARE_MODULE_ID "lights"
struct hw_module_t HAL_MODULE_INFO_SYM = {
   .tag = HARDWARE_MODULE_TAG,
   .version_major = 1,
   .version_minor = 0,
   .id = LIGHTS_HARDWARE_MODULE_ID,
#define LIGHTS_HARDWARE_MODULE_ID "lights"
   .name = "lights Module",
   .author = "Google, Inc.",
   .methods = &lights_module_methods,
static struct hw_module_methods_t lights_module_methods = {
   .open =  open_lights,
static int open_lights(const struct hw_module_t* module, char const* name,
       struct hw_device_t** device)
{
   int (*set_light)(struct light_device_t* dev,
           struct light_state_t const* state);


   if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
    #define LIGHT_ID_BACKLIGHT          "backlight"
       set_light = set_light_backlight;
static int set_light_backlight(struct light_device_t* dev, struct light_state_t const* state)
{
   int err = 0;
   int brightness = rgb_to_brightness(state);
static int rgb_to_brightness(struct light_state_t const* state)
{
   int color = state->color & 0x00ffffff;
   return ((77*((color>>16)&0x00ff)) + (150*((color>>8)&0x00ff)) + (29* (color&0x00ff))) >> 8;
}


   if(!dev) {
       return -1;
   }
   
   pthread_mutex_lock(&g_lock);
   err = write_int(LCD_FILE, brightness);
 char const*const LCD_FILE  = "/sys/class/leds/lcd-backlight/brightness";
static int write_int(char const* path, int value)
{
   int fd;
   static int already_warned = 0;


   fd = open(path, O_RDWR);
   if (fd >= 0) {
       char buffer[20];
       int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
       ssize_t amt = write(fd, buffer, (size_t)bytes);
       close(fd);
       return amt == -1 ? -errno : 0;
   } else {
       if (already_warned == 0) {
           ALOGE("write_int failed to open %s\n", path);
           already_warned = 1;
       }
       return -errno;
   }
}


   pthread_mutex_unlock(&g_lock);
   return err;
}






       
   else if (0 == strcmp(LIGHT_ID_BATTERY, name))
       set_light = set_light_battery;
   else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
       set_light = set_light_notifications;
   else if (0 == strcmp(LIGHT_ID_BUTTONS, name))
       set_light = set_light_buttons;
   else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
       set_light = set_light_attention;
   else
       return -EINVAL;


   pthread_once(&g_init, init_globals);


   struct light_device_t *dev = malloc(sizeof(struct light_device_t));
struct light_device_t {
   struct hw_device_t common;


   /**
    * Set the provided lights to the provided values.
    *
    * Returns: 0 on succes, error code on failure.
    */
   int (*set_light)(struct light_device_t* dev,
           struct light_state_t const* state);
};


   if(!dev)
       return -ENOMEM;


   memset(dev, 0, sizeof(*dev));


   dev->common.tag = HARDWARE_DEVICE_TAG;
   dev->common.version = 0;
   dev->common.module = (struct hw_module_t*)module;
   dev->common.close = (int (*)(struct hw_device_t*))close_lights;
   dev->set_light = set_light;


   *device = (struct hw_device_t*)dev;
   return 0;
}    
};
};









        devices->lights[LIGHT_INDEX_BACKLIGHT] = get_device(module, LIGHT_ID_BACKLIGHT);
enum {
   LIGHT_INDEX_BACKLIGHT = 0,
   LIGHT_INDEX_KEYBOARD = 1,
   LIGHT_INDEX_BUTTONS = 2,
   LIGHT_INDEX_BATTERY = 3,
   LIGHT_INDEX_NOTIFICATIONS = 4,
   LIGHT_INDEX_ATTENTION = 5,
   LIGHT_INDEX_BLUETOOTH = 6,
   LIGHT_INDEX_WIFI = 7,
   LIGHT_COUNT
};


struct Devices {
   light_device_t* lights[LIGHT_COUNT];
};


static light_device_t* get_device(hw_module_t* module, char const* name)
{
hw_device_t* device;
        module->methods->open(module, name, &device);
static int open_lights(const struct hw_module_t* module, char const* name,
       struct hw_device_t** device)
{
   int (*set_light)(struct light_device_t* dev, struct light_state_t const* state);


   set_light = set_light_backlight;
   
   struct light_device_t *dev = malloc(sizeof(struct light_device_t));


   dev->common.tag = HARDWARE_DEVICE_TAG;
   dev->common.version = 0;
   dev->common.module = (struct hw_module_t*)module;
   dev->common.close = (int (*)(struct hw_device_t*))close_lights;
   dev->set_light = set_light;


   *device = (struct hw_device_t*)dev;
   return 0;
}


        return (light_device_t*)device;
        
}
   { "finalize_native", "(J)V", (void*)finalize_native },
   { "setLight_native", "(JIIIIII)V", (void*)setLight_native },
};
}


};
};


using namespace android;


extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv* env = NULL;
    jint result = -1;
    
    register_android_server_LightsService(env);
    
    return JNI_VERSION_1_4;
}












char const*const LCD_FILE = "/sys/class/leds/lcd-backlight/brightness";
bl = backlight_device_register("lcd-backlight", lp->dev, lp, &lp855x_bl_ops, &props);
led_classdev_register(&pdev->dev, &backlight_led);
static int __init leds_init(void)
{
leds_class = class_create(THIS_MODULE, "leds");
led_classdev_register(&pdev->dev, &backlight_led);
led_cdev->dev = device_create(leds_class, parent, 0, led_cdev, "%s", led_cdev->name);
static struct led_classdev backlight_led = {
.name           = "lcd-backlight",
.brightness     = MDSS_MAX_BL_BRIGHTNESS,
.brightness_set = mdss_fb_set_bl_brightness,
.max_brightness = MDSS_MAX_BL_BRIGHTNESS,
};

if (IS_ERR(leds_class))
return PTR_ERR(leds_class);
leds_class->suspend = led_suspend;
leds_class->resume = led_resume;
leds_class->dev_attrs = led_class_attrs;
return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值