钟祥碧桂园开盘价格:Android2.3 实现GPS HAL的一个简单实例

来源:百度文库 编辑:偶看新闻 时间:2024/04/28 02:02:43
  有关Android HAL及相关Android GPS的介绍,网上很多,我这里就不在复制粘贴了,呵呵。
    笔者是在hardware/libhardware/modules/添加了gps目录。

1. 修改hardware/libhardware/Android.mk
                 modules/gralloc \
                 modules/gralloc modules/gps \
                    tests \

2. gps/gps_xxxx.c

    (1) 必须包含下面两个头文件
    #include
    #include

    (2) 一些重要的结构体
    struct gps_context_t {
        struct gps_device_t device;
        // our private state goes below here
    };

    struct gps_module_t {
        struct hw_module_t common;
    };

     static struct hw_module_methods_t gps_module_methods = {
         open: gps_device_open
     };

    struct gps_module_t HAL_MODULE_INFO_SYM = {
        common: {
            tag: HARDWARE_MODULE_TAG,
            version_major: 1,
            version_minor: 0,
            id: GPS_HARDWARE_MODULE_ID,
            name: "GPS hardware module",
            author: "Shrek"
            methods: &gps_module_methods,
        }
     };

     (3) 一些重要的函数
     static const GpsInterface *gps_get_interface();
     static int gps_device_open(const struct hw_module_t *module, const char *name
          struct hw_device_t **device);

        static const GpsInterface xxxxGpsInterface = {
        .size = sizeof(GpsInterface),
        .init = xxxx_gps_init,
        .start = xxxx_gps_start,
        .stop = xxxx_gps_stop,
        .cleanup = xxxx_gps_cleanup,
        .inject_time = xxxx_gps_inject_time,
        .inject_location = xxxx_gps_inject_location,
        .delete_aiding_data = xxxx_gps_delete_aiding_data,
        .set_position_mode = xxxx_gps_set_position_mode,
        .get_extention = xxxx_gps_get_extension,
    };

    需要我们实现上述结构体中的相关函数。在xxxx_state_init中创建gps线程时,需要调用callback函数:
    state->thread = state->callbacks.create_thread_cb("xxxx_gps", gps_state_thread, state);

    const GpsInterface *gps_get_hardware_interface()
    {
        return &xxxxGpsInterface;
    }

    static const GpsInterface *sGpsInterface = NULL;
    static void gps_find_hardware(void)
    {
     #ifdef HAVE_GPS_HARDWARE
         sGpsInterface = gps_get_hardware_interface();
     #endif
         if (!sGpsInterface)
             LOGD("No GPS hardware on this device");
    }

    static const GpsInterface *gps_get_interface()
    {
         if (sGpsInterface == NULL)
             gps_find_hardware();
         return sGpsInterface;
    }

    static int gps_contrl_close(struct hw_device_t *dev)
    {
        struct gps_context_t *ctx = (struct gps_context_t *)dev;
        if(ctx){
             // free all resouces associated with this device here
             free(ctx);
        }
        return 0;
    }
   
    static int gps_device_open(const struct hw_module_t *module, const char *name
          struct hw_device_t **device)
    {
        int status = -EINVAL;
        if (!strcmp(name, GPS_HARDWARE_MODULE_ID)) {
            struct gps_context_t *dev;
            dev = (struct gps_context_t *)malloc(sizeof(*dev));
            if (dev == NULL) {
                return -ENOMEM;
            }

            // initialize our state here
            memset(dev, 0, sizeof(*dev));

            // initialize the procs
            dev->device.common.tag = HARDWARE_DEVICE_TAG;
            dev->device.common.version = 0;
            dev->device.common.module = (struct hw_module_t *) module;
            dev->device.commom.close = gps_control_close;

            dev->device.get_gps_interface = gps_get_interface;
         
            *device = &dev->device.commom;
            status = 0
        }
        return status;
    }