android 2.3 gps移植杂记(一)

    近期任务是把产品的gps适配到android2.3源码中,一般的移植做法是:从串口中读取gps nema数据,在gps硬件适配层(HAL)对数据进行解析并上报到Framework层。我的任务需求不同,对串口的读写操作统一由一个串口代理负责,gps只需与串口代理通信,获取nema数据。

    概括来说,gps移植有以下几个工作:

(1)在串口代理中实现socket进程通信服务端,负责把串口代理从串口读到的数据发送到socket客户端,并把socket客户端发来的数据交由串口代理写入串口。

(2)在gps适配层实现socket进程通信客户端,负责与串口代理中的socket服务器通信。

(3)gps适配层把由socket客户端读取的nema数据解析并上报。


socket通信在本篇中就不详谈了,主要说gps适配。

(1)首先在android 2.3源码新建目录hardware/libhardware/modules/gps,把sdk/emulator/gps/gps_qemu.c复制到新建的目录

(2)新建Android.mk文件,内容为:

LOCAL_PATH := $(call my-dir)

ifneq ($(TARGET_PRODUCT),sim)
# HAL module implemenation, not prelinked and stored in
# hw/<GPS_HARDWARE_MODULE_ID>.<ro.hardware>.so
include $(CLEAR_VARS)
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_CFLAGS += -DQEMU_HARDWARE
LOCAL_SHARED_LIBRARIES := liblog libcutils libhardware
LOCAL_SRC_FILES := gps_qemu.c socket_msg.c
LOCAL_MODULE := gps.default
LOCAL_MODULE_TAGS := debug
include $(BUILD_SHARED_LIBRARY)

切记:LOCAL_MODULE最好为:gps.default,否则很有可能导致上层调用不到so动态库。

(3)修改gps_qemu.c,主要包括以下几个方面:

1.修改gps_state_init函数,删除与qemu通信的初始化代码,改为与我的socket服务端进行通信。

static void
gps_state_init( GpsState*  state, GpsCallbacks* callbacks )
{
    state->init       = 1;
    state->control[0] = -1;
    state->control[1] = -1;
    state->fd         = -1;

    /*state->fd = qemud_channel_open(QEMU_CHANNEL_NAME);
	
    if (state->fd < 0) {
        D("no gps emulation detected");
        return;
    }

    D("gps emulation will read from '%s' qemud channel", QEMU_CHANNEL_NAME );*/

	//initialize cache,and register callback function to handle message
	socket_cache_init(&state->cache,gps_msg_handle);
    //Connect to socket server
    if(gps_socket_connect(state) == 0){
		goto Fail;
    }
	
    if ( socketpair( AF_LOCAL, SOCK_STREAM, 0, state->control ) < 0 ) {
        LOGE("could not create thread control socket pair: %s", strerror(errno));
        goto Fail;
    }

    state->thread = callbacks->create_thread_cb( "gps_state_thread", gps_state_thread, state );

    if ( !state->thread ) {
        LOGE("could not create gps thread: %s", strerror(errno));
        goto Fail;
    }

    state->callbacks = *callbacks;

    D("gps initialized success!");
	//start gps
	gps_state_start(state);
    return;

Fail:
    gps_state_done( state );
}

2.修改gps_state_thread函数,当socket服务端有数据来的时候,调用我的处理函数进行处理(主要包括对socket消息的解析)。

else if (fd == gps_fd)
                {
                    //char  buff[32];
                    unsigned char buff[32 + SOCKET_MSG_FORMAT_SIZE];
                    D("gps fd event");
                    for (;;) {
                        int  nn, ret;

                        ret = read( fd, buff, sizeof(buff) );
                        if (ret < 0) {
                            if (errno == EINTR)
                                continue;
                            if (errno != EWOULDBLOCK)
                                LOGE("error while reading from gps daemon socket: %s:", strerror(errno));
                            break;
                        }
                        //D("received %d bytes: %.*s", ret, ret, buff);
						gps_msg_recv(state,buff,ret,(void *)reader);
						/*
                        for (nn = 0; nn < ret; nn++)
                            nmea_reader_addc( reader, buff[nn] );*/
                    }
                    D("gps fd event end");
                }

(3)然后对模块进行编译,并打包sysem.img镜像文件

 mmm hardware/libhardware/modules/gps
 make snod
提示:生成的gps.default.so最终会在out/target/product/generic/system/lib/hw目录下,如果该目录下同时还有gps.goldfish.so文件,必须先把gps.goldfish.so文件删除,然后再打包镜像文件。

(4)剩下的就是测试了...



你可能感兴趣的:(thread,android,cache,socket,Module,callback)