⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gps_qemu.c

📁 Android平台上Midware层源代码合集
💻 C
📖 第 1 页 / 共 2 页
字号:
        p += snprintf( p, end-p, "sending fix" );        if (r->fix.flags & GPS_LOCATION_HAS_LAT_LONG) {            p += snprintf(p, end-p, " lat=%g lon=%g", r->fix.latitude, r->fix.longitude);        }        if (r->fix.flags & GPS_LOCATION_HAS_ALTITUDE) {            p += snprintf(p, end-p, " altitude=%g", r->fix.altitude);        }        if (r->fix.flags & GPS_LOCATION_HAS_SPEED) {            p += snprintf(p, end-p, " speed=%g", r->fix.speed);        }        if (r->fix.flags & GPS_LOCATION_HAS_BEARING) {            p += snprintf(p, end-p, " bearing=%g", r->fix.bearing);        }        if (r->fix.flags & GPS_LOCATION_HAS_ACCURACY) {            p += snprintf(p,end-p, " accuracy=%g", r->fix.accuracy);        }        gmtime_r( (time_t*) &r->fix.timestamp, &utc );        p += snprintf(p, end-p, " time=%s", asctime( &utc ) );        D(temp);#endif        if (r->callback) {            r->callback( &r->fix );            r->fix.flags = 0;        }        else {            D("no callback, keeping data until needed !");        }    }}static voidnmea_reader_addc( NmeaReader*  r, int  c ){    if (r->overflow) {        r->overflow = (c != '\n');        return;    }    if (r->pos >= (int) sizeof(r->in)-1 ) {        r->overflow = 1;        r->pos      = 0;        return;    }    r->in[r->pos] = (char)c;    r->pos       += 1;    if (c == '\n') {        nmea_reader_parse( r );        r->pos = 0;    }}/*****************************************************************//*****************************************************************//*****                                                       *****//*****       C O N N E C T I O N   S T A T E                 *****//*****                                                       *****//*****************************************************************//*****************************************************************//* commands sent to the gps thread */enum {    CMD_QUIT  = 0,    CMD_START = 1,    CMD_STOP  = 2};/* this is the state of our connection to the qemu_gpsd daemon */typedef struct {    int                     init;    int                     fd;    GpsCallbacks            callbacks;    pthread_t               thread;    int                     control[2];} GpsState;static GpsState  _gps_state[1];static voidgps_state_done( GpsState*  s ){    // tell the thread to quit, and wait for it    char   cmd = CMD_QUIT;    void*  dummy;    write( s->control[0], &cmd, 1 );    pthread_join(s->thread, &dummy);    // close the control socket pair    close( s->control[0] ); s->control[0] = -1;    close( s->control[1] ); s->control[1] = -1;    // close connection to the QEMU GPS daemon    close( s->fd ); s->fd = -1;    s->init = 0;}static voidgps_state_start( GpsState*  s ){    char  cmd = CMD_START;    int   ret;    do { ret=write( s->control[0], &cmd, 1 ); }    while (ret < 0 && errno == EINTR);    if (ret != 1)        D("%s: could not send CMD_START command: ret=%d: %s",          __FUNCTION__, ret, strerror(errno));}static voidgps_state_stop( GpsState*  s ){    char  cmd = CMD_STOP;    int   ret;    do { ret=write( s->control[0], &cmd, 1 ); }    while (ret < 0 && errno == EINTR);    if (ret != 1)        D("%s: could not send CMD_STOP command: ret=%d: %s",          __FUNCTION__, ret, strerror(errno));}static intepoll_register( int  epoll_fd, int  fd ){    struct epoll_event  ev;    int                 ret, flags;    /* important: make the fd non-blocking */    flags = fcntl(fd, F_GETFL);    fcntl(fd, F_SETFL, flags | O_NONBLOCK);    ev.events  = EPOLLIN;    ev.data.fd = fd;    do {        ret = epoll_ctl( epoll_fd, EPOLL_CTL_ADD, fd, &ev );    } while (ret < 0 && errno == EINTR);    return ret;}static intepoll_deregister( int  epoll_fd, int  fd ){    int  ret;    do {        ret = epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd, NULL );    } while (ret < 0 && errno == EINTR);    return ret;}/* this is the main thread, it waits for commands from gps_state_start/stop and, * when started, messages from the QEMU GPS daemon. these are simple NMEA sentences * that must be parsed to be converted into GPS fixes sent to the framework */static void*gps_state_thread( void*  arg ){    GpsState*   state = (GpsState*) arg;    NmeaReader  reader[1];    int         epoll_fd   = epoll_create(2);    int         started    = 0;    int         gps_fd     = state->fd;    int         control_fd = state->control[1];    nmea_reader_init( reader );    // register control file descriptors for polling    epoll_register( epoll_fd, control_fd );    epoll_register( epoll_fd, gps_fd );    D("gps thread running");    // now loop    for (;;) {        struct epoll_event   events[2];        int                  ne, nevents;        nevents = epoll_wait( epoll_fd, events, 2, -1 );        if (nevents < 0) {            if (errno != EINTR)                LOGE("epoll_wait() unexpected error: %s", strerror(errno));            continue;        }        D("gps thread received %d events", nevents);        for (ne = 0; ne < nevents; ne++) {            if ((events[ne].events & (EPOLLERR|EPOLLHUP)) != 0) {                LOGE("EPOLLERR or EPOLLHUP after epoll_wait() !?");                goto Exit;            }            if ((events[ne].events & EPOLLIN) != 0) {                int  fd = events[ne].data.fd;                if (fd == control_fd)                {                    char  cmd = 255;                    int   ret;                    D("gps control fd event");                    do {                        ret = read( fd, &cmd, 1 );                    } while (ret < 0 && errno == EINTR);                    if (cmd == CMD_QUIT) {                        D("gps thread quitting on demand");                        goto Exit;                    }                    else if (cmd == CMD_START) {                        if (!started) {                            D("gps thread starting  location_cb=%p", state->callbacks.location_cb);                            started = 1;                            nmea_reader_set_callback( reader, state->callbacks.location_cb );                        }                    }                    else if (cmd == CMD_STOP) {                        if (started) {                            D("gps thread stopping");                            started = 0;                            nmea_reader_set_callback( reader, NULL );                        }                    }                }                else if (fd == gps_fd)                {                    char  buff[32];                    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);                        for (nn = 0; nn < ret; nn++)                            nmea_reader_addc( reader, buff[nn] );                    }                    D("gps fd event end");                }                else                {                    LOGE("epoll_wait() returned unkown fd %d ?", fd);                }            }        }    }Exit:    return NULL;}static voidgps_state_init( GpsState*  state ){    char   prop[PROPERTY_VALUE_MAX];    char   device[256];    int    ret;    int    done = 0;    state->init       = 1;    state->control[0] = -1;    state->control[1] = -1;    state->fd         = -1;    // try to connect to the qemud socket    do {        state->fd = socket_local_client( QEMUD_SOCKET_NAME,                                         ANDROID_SOCKET_NAMESPACE_RESERVED,                                         SOCK_STREAM );        if (state->fd < 0) {            D("no '%s' control socket available: %s", QEMUD_SOCKET_NAME, strerror(errno));            break;        }        snprintf( device, sizeof(device), "/dev/socket/%s", QEMUD_SOCKET_NAME );        done = 1;    } while (0);    // otherwise, look for a kernel-provided device name    if (!done) do {        if (property_get("ro.kernel.android.gps",prop,"") == 0) {            D("no kernel-provided gps device name");            break;        }        if ( snprintf(device, sizeof(device), "/dev/%s", prop) >= (int)sizeof(device) ) {            LOGE("gps serial device name too long: '%s'", prop);            break;        }        do {            state->fd = open( device, O_RDWR );        } while (state->fd < 0 && errno == EINTR);        if (state->fd < 0) {            LOGE("could not open gps serial device %s: %s", device, strerror(errno) );            break;        }        done = 1;    } while (0);    if (!done) {        D("no gps emulation detected");        return;    }    D("gps emulation will read from %s", device);    // disable echo on serial lines    if ( !memcmp( device, "/dev/ttyS", 9 ) ) {        struct termios  ios;        tcgetattr( state->fd, &ios );        ios.c_lflag = 0;  /* disable ECHO, ICANON, etc... */        tcsetattr( state->fd, TCSANOW, &ios );    }    if ( socketpair( AF_LOCAL, SOCK_STREAM, 0, state->control ) < 0 ) {        LOGE("could not create thread control socket pair: %s", strerror(errno));        goto Fail;    }    if ( pthread_create( &state->thread, NULL, gps_state_thread, state ) != 0 ) {        LOGE("could not create gps thread: %s", strerror(errno));        goto Fail;    }    D("gps state initialized");    return;Fail:    gps_state_done( state );}/*****************************************************************//*****************************************************************//*****                                                       *****//*****       I N T E R F A C E                               *****//*****                                                       *****//*****************************************************************//*****************************************************************/static intqemu_gps_init(GpsCallbacks* callbacks){    GpsState*  s = _gps_state;    if (!s->init)        gps_state_init(s);    if (s->fd < 0)        return -1;    s->callbacks = *callbacks;    return 0;}static voidqemu_gps_cleanup(void){    GpsState*  s = _gps_state;    if (s->init)        gps_state_done(s);}static intqemu_gps_start(){    GpsState*  s = _gps_state;    if (!s->init) {        D("%s: called with uninitialized state !!", __FUNCTION__);        return -1;    }    D("%s: called", __FUNCTION__);    gps_state_start(s);    return 0;}static intqemu_gps_stop(){    GpsState*  s = _gps_state;    if (!s->init) {        D("%s: called with uninitialized state !!", __FUNCTION__);        return -1;    }    D("%s: called", __FUNCTION__);    gps_state_stop(s);    return 0;}static voidqemu_gps_set_fix_frequency(){    GpsState*  s = _gps_state;    if (!s->init) {        D("%s: called with uninitialized state !!", __FUNCTION__);        return -1;    }    D("%s: called", __FUNCTION__);    // FIXME - support fix_frequency}static intqemu_gps_inject_time(GpsUtcTime time, int64_t timeReference, int uncertainty){    return 0;}static voidqemu_gps_delete_aiding_data(GpsAidingData flags){}static int qemu_gps_set_position_mode(GpsPositionMode mode, int fix_frequency){    // FIXME - support fix_frequency    // only standalone supported for now.    if (mode != GPS_POSITION_MODE_STANDALONE)        return -1;    return 0;}static const void*qemu_gps_get_extension(const char* name){    return NULL;}static const GpsInterface  qemuGpsInterface = {    qemu_gps_init,    qemu_gps_start,    qemu_gps_stop,    qemu_gps_set_fix_frequency,    qemu_gps_cleanup,    qemu_gps_inject_time,    qemu_gps_delete_aiding_data,    qemu_gps_set_position_mode,    qemu_gps_get_extension,};const GpsInterface* gps_get_qemu_interface(){    return &qemuGpsInterface;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -