From 1481e4d727ee16f3459682fa938a702ad083fc33 Mon Sep 17 00:00:00 2001 From: martin-s Date: Tue, 9 Oct 2007 18:14:27 +0000 Subject: [PATCH] Fixed some errors in demo vehicle, added logging system, renamed this to this_ in vehicle.c git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@454 ffa7fe5e-494d-0410-b361-a75ebd5db220 --- navit/src/attr_def.h | 7 +- navit/src/coord.c | 7 +- navit/src/graphics.c | 14 +- navit/src/item_def.h | 2 + navit/src/log.c | 267 +++++++++++++++++++-------- navit/src/log.h | 14 +- navit/src/navit.c | 1 - navit/src/transform.c | 1 + navit/src/vehicle.c | 490 +++++++++++++++++++++++++++----------------------- navit/src/vehicle.h | 6 +- navit/src/xmlconfig.c | 18 +- 11 files changed, 509 insertions(+), 318 deletions(-) diff --git a/navit/src/attr_def.h b/navit/src/attr_def.h index 32be1a3..cd81c81 100644 --- a/navit/src/attr_def.h +++ b/navit/src/attr_def.h @@ -20,10 +20,14 @@ ATTR(limit) ATTR(w) ATTR(x) ATTR(y) +ATTR(flush_size) +ATTR(flush_time) +/* boolean */ +ATTR(overwrite) ATTR2(type_int_end,0x0002ffff) ATTR2(type_string_begin,0x00030000) -ATTR(skin) +ATTR(type) ATTR(label) ATTR(data) ATTR(charset) @@ -43,6 +47,7 @@ ATTR(address) ATTR(phone) ATTR(entry_fee) ATTR(open_hours) +ATTR(skin) /* poi */ ATTR(icon) ATTR(info_html) diff --git a/navit/src/coord.c b/navit/src/coord.c index b2682fd..7c1c645 100644 --- a/navit/src/coord.c +++ b/navit/src/coord.c @@ -183,8 +183,11 @@ coord_parse(const char *c_str, enum projection pro, struct coord *c_ret) args=sscanf(str, "%lf %lf%n", &lng, &lat, &ret); if (args < 2) return 0; - printf("str='%s' x=%f y=%f c=%d\n", str, lng, lat, ret); - printf("rest='%s'\n", str+ret); + dbg(1,"str='%s' x=%f y=%f c=%d\n", str, lng, lat, ret); + dbg(1,"rest='%s'\n", str+ret); + g.lng=lng; + g.lat=lat; + transform_from_geo(pro, &g, c_ret); } if (debug) printf("rest='%s'\n", str+ret); diff --git a/navit/src/graphics.c b/navit/src/graphics.c index 1bc9b3d..1b57ab3 100644 --- a/navit/src/graphics.c +++ b/navit/src/graphics.c @@ -360,11 +360,13 @@ xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct it if (e->u.circle.width > 1) gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width); gra->meth.draw_circle(gra->priv, gc->priv, &di->pnt[0], e->u.circle.radius); - p.x=di->pnt[0].x+3; - p.y=di->pnt[0].y+10; - if (! gra->font[e->label_size]) - gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20); - gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->label_size]->priv, di->label, &p, 0x10000, 0); + if (e->label_size) { + p.x=di->pnt[0].x+3; + p.y=di->pnt[0].y+10; + if (! gra->font[e->label_size]) + gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20); + gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->label_size]->priv, di->label, &p, 0x10000, 0); + } break; case element_label: if (di->label) { @@ -504,7 +506,7 @@ do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapset } g_assert(count < max); if (!transform_contains(t, pro, &r)) { - dbg(1,"not visible\n"); + dbg(1,"poly not visible\n"); continue; } if (route && route_contains(route, item)) { diff --git a/navit/src/item_def.h b/navit/src/item_def.h index 0dbc179..5643610 100644 --- a/navit/src/item_def.h +++ b/navit/src/item_def.h @@ -63,6 +63,7 @@ ITEM(poi_lake) ITEM(poi_island) ITEM(poi) ITEM(waypoint) +ITEM(trackpoint) ITEM(bookmark) ITEM(former_destination) ITEM(poi_land_feature) @@ -120,6 +121,7 @@ ITEM(street_unkn) ITEM(street_route) ITEM(height_line_1) ITEM(height_line_2) +ITEM(track) /* Area */ ITEM2(0xc0000000,area) ITEM2(0xc0000001,area_unspecified) diff --git a/navit/src/log.c b/navit/src/log.c index 59c1fc3..ab70dfe 100644 --- a/navit/src/log.c +++ b/navit/src/log.c @@ -3,87 +3,208 @@ #include #include #include +#include +#include +#include #include "file.h" -#include "map_data.h" +#include "item.h" +#include "debug.h" #include "log.h" +struct log_data { + int len; + int max_len; + char *data; +}; + +struct log { + FILE *f; + int overwrite; + int empty; + int flush_size; + int flush_time; + guint timer; + struct timeval last_flush; + char *filename; + char *filename_ex1; + char *filename_ex2; + struct log_data header; + struct log_data data; + struct log_data trailer; +}; + +static void +strftime_localtime(char *buffer, int size, char *fmt) +{ + time_t t; + struct tm *tm; + + t=time(NULL); + tm=localtime(&t); + strftime(buffer, 4096, fmt, tm); +} + +static void +expand_filenames(struct log *this_) +{ + char buffer[4096]; + int i; + + strftime_localtime(buffer, 4096, this_->filename); + this_->filename_ex1=g_strdup(buffer); + if (strstr(this_->filename_ex1,"%i")) { + i=0; + do { + g_free(this_->filename_ex2); + this_->filename_ex2=g_strdup_printf(this_->filename_ex1,i++); + } while (file_exists(this_->filename_ex2)); + } else + this_->filename_ex2=g_strdup(this_->filename_ex1); +} + +static void +log_open(struct log *this_) +{ + char *mode; + if (this_->overwrite) + mode="w"; + else + mode="a"; + this_->f=fopen(this_->filename_ex2, mode); + this_->empty = !ftell(this_->f); +} + +static void +log_close(struct log *this_) +{ + if (this_->trailer.len) + fwrite(this_->header.data, 1, this_->trailer.len, this_->f); + fflush(this_->f); + fclose(this_->f); + this_->f=NULL; +} + +static void +log_flush(struct log *this_) +{ + if (this_->empty) { + if (this_->header.len) + fwrite(this_->header.data, 1, this_->header.len, this_->f); + if (this_->header.len || this_->data.len) + this_->empty=0; + } + fwrite(this_->data.data, 1, this_->data.len, this_->f); + fflush(this_->f); + g_free(this_->data.data); + this_->data.data=NULL; + this_->data.max_len=this_->data.len=0; + gettimeofday(&this_->last_flush, NULL); +} + +static int +log_flush_required(struct log *this_) +{ + return this_->data.len > this_->flush_size; +} + + +static void +log_change(struct log *this_) +{ + log_flush(this_); + log_close(this_); + log_open(this_); +} + +static int +log_change_required(struct log *this_) +{ + char buffer[4096]; + + strftime_localtime(buffer, 4096, this_->filename); + return (strcmp(this_->filename_ex1, buffer) != 0); +} + +static gboolean +log_timer(gpointer data) +{ + struct log *this_=data; + struct timeval tv; + int delta; + gettimeofday(&tv, NULL); + delta=(tv.tv_sec-this_->last_flush.tv_sec)*1000+(tv.tv_usec-this_->last_flush.tv_usec)/1000; + if (this_->flush_time && delta > this_->flush_time*1000) + log_flush(this_); + + return TRUE; +} + +struct log * +log_new(struct attr **attrs) +{ + struct log *ret=g_new0(struct log, 1); + struct attr *data,*overwrite,*flush_size,*flush_time; + + dbg(0,"enter\n"); + data=attr_search(attrs, NULL, attr_data); + if (! data) + return NULL; + ret->filename=g_strdup(data->u.str); + overwrite=attr_search(attrs, NULL, attr_overwrite); + if (overwrite) + ret->overwrite=overwrite->u.num; + flush_size=attr_search(attrs, NULL, attr_flush_size); + if (flush_size) + ret->flush_size=flush_size->u.num; + flush_time=attr_search(attrs, NULL, attr_flush_time); + if (flush_time) + ret->flush_time=flush_time->u.num; + if (ret->flush_time) + ret->timer=g_timeout_add(ret->flush_time*1000, log_timer, ret); + expand_filenames(ret); + log_open(ret); + return ret; +} + +void +log_set_header(struct log *this_, char *data, int len) +{ + this_->header.data=g_malloc(len); + this_->header.max_len=this_->header.len=len; + memcpy(this_->header.data, data, len); +} + +void +log_set_trailer(struct log *this_, char *data, int len) +{ + this_->trailer.data=g_malloc(len); + this_->trailer.max_len=this_->trailer.len=len; + memcpy(this_->trailer.data, data, len); +} + void -log_write(char *message, struct file *file, void *data, int size) +log_write(struct log *this_, char *data, int len) { - char log[4096]; - char *l; - unsigned char *p=data; - - int fd=open("log.txt",O_RDWR|O_CREAT|O_APPEND|O_SYNC, 0644); - sprintf(log,"# %s\n",message); - l=log+strlen(log); - sprintf(l, "%s 0x%x ", file->name, p-file->begin); - l=log+strlen(log); - while (size) { - sprintf(l,"%02x ", *p++); - l+=3; - size--; + dbg(1,"enter\n"); + if (log_change_required(this_)) + log_change(this_); + if (this_->data.len + len > this_->data.max_len) { + dbg(2,"overflow\n"); + this_->data.max_len+=16384; + this_->data.data=g_realloc(this_->data.data,this_->data.max_len); } - *l++='\n'; - *l='\0'; - write(fd, log, strlen(log)); - close(fd); + memcpy(this_->data.data+this_->data.len, data, len); + this_->data.len+=len; + if (log_flush_required(this_)) + log_flush(this_); } void -log_apply(struct map_data *map, int files) +log_destroy(struct log *this_) { - char buffer[4096],*p; - char *filename, *addr; - struct file *file; - unsigned char *data; - unsigned long dataval; - struct map_data *curr; - int j; - - FILE *f=fopen("log.txt","r"); - if (!f) - return; - while (fgets(buffer, 4096, f)) { - if (buffer[0] != '#') { - buffer[strlen(buffer)-1]='\0'; - filename=buffer; - p=buffer; - while (*p && *p != ' ') - p++; - if (! *p) - continue; - *p++=0; - file=NULL; - curr=map; - while (curr) { - for(j = 0 ; j < files ; j++) { - if (curr->file[j] && !strcmp(curr->file[j]->name, filename)) { - file=curr->file[j]; - break; - } - } - if (file) - break; - curr=curr->next; - } - if (file) { - addr=p; - while (*p && *p != ' ') - p++; - if (! *p) - continue; - *p++=0; - data=file->begin+strtoul(addr, NULL, 16); - while (*p) { - dataval=strtoul(p, NULL, 16); - *data++=dataval; - p+=2; - while (*p == ' ') - p++; - } - } - } - } - fclose(f); + if (this_->timer) + g_source_remove(this_->timer); + log_flush(this_); + log_close(this_); + g_free(this_); } diff --git a/navit/src/log.h b/navit/src/log.h index 3f2658a..dd11677 100644 --- a/navit/src/log.h +++ b/navit/src/log.h @@ -1,8 +1,12 @@ #ifndef NAVIT_LOG_H #define NAVIT_LOG_H - -void log_write(char *message, struct file *file, void *data, int size); -void log_apply(struct map_data *map, int files); - +/* prototypes */ +struct attr; +struct log; +struct log *log_new(struct attr **attrs); +void log_set_header(struct log *this_, char *data, int len); +void log_set_trailer(struct log *this_, char *data, int len); +void log_write(struct log *this_, char *data, int len); +void log_destroy(struct log *this_); +/* end of prototypes */ #endif - diff --git a/navit/src/navit.c b/navit/src/navit.c index c19a937..415c860 100644 --- a/navit/src/navit.c +++ b/navit/src/navit.c @@ -55,7 +55,6 @@ struct navit { struct action *action; struct transformation *trans; struct compass *compass; - struct map_data *map_data; struct menu *menu; struct menu *toolbar; struct statusbar *statusbar; diff --git a/navit/src/transform.c b/navit/src/transform.c index abf9c35..74d2a35 100644 --- a/navit/src/transform.c +++ b/navit/src/transform.c @@ -89,6 +89,7 @@ transform(struct transformation *t, enum projection pro, struct coord *c, struct } xc=c->x; yc=c->y; + dbg(2,"0x%x, 0x%x - 0x%x,0x%x contains 0x%x,0x%x\n", t->r.lu.x, t->r.lu.y, t->r.rl.x, t->r.rl.y, c->x, c->y); ret=coord_rect_contains(&t->r, c); xc-=t->center.x; yc-=t->center.y; diff --git a/navit/src/vehicle.c b/navit/src/vehicle.c index e50e2cc..87ec119 100644 --- a/navit/src/vehicle.c +++ b/navit/src/vehicle.c @@ -22,15 +22,14 @@ #include "projection.h" #include "statusbar.h" #include "navit.h" +#include "item.h" +#include "log.h" #include "vehicle.h" #include "item.h" #include "route.h" -int vfd; - - -static void disable_watch(struct vehicle *this); -static void enable_watch(struct vehicle *this); +static void disable_watch(struct vehicle *this_); +static void enable_watch(struct vehicle *this_); /* #define INTERPOLATION_TIME 50 */ @@ -75,26 +74,27 @@ struct vehicle { int is_udp; int interval; struct sockaddr_in rem; + struct log *nmea_log, *gpx_log, *textfile_log; struct navit *navit; }; -// FIXME : this is an ugly hack (dixit cp15 ;) ) +// FIXME : this_ is an ugly hack (dixit cp15 ;) ) struct vehicle *vehicle_last; #if INTERPOLATION_TIME static int vehicle_timer(gpointer t) { - struct vehicle *this=t; -/* if (this->timer_count++ < 1000/INTERPOLATION_TIME) { */ - if (this->delta.x || this->delta.y) { - this->curr.x+=this->delta.x; - this->curr.y+=this->delta.y; - this->current_pos.x=this->curr.x; - this->current_pos.y=this->curr.y; - if (this->callback_func) - (*this->callback_func)(this, this->callback_data); + struct vehicle *this_=t; +/* if (this_->timer_count++ < 1000/INTERPOLATION_TIME) { */ + if (this_->delta.x || this_->delta.y) { + this_->curr.x+=this_->delta.x; + this_->curr.y+=this_->delta.y; + this_->current_pos.x=this_->curr.x; + this_->current_pos.y=this_->curr.y; + if (this_->callback_func) + (*this_->callback_func)(this_, this_->callback_data); } /* } */ return TRUE; @@ -102,79 +102,79 @@ vehicle_timer(gpointer t) #endif enum projection -vehicle_projection(struct vehicle *this) +vehicle_projection(struct vehicle *this_) { return projection_mg; } struct coord * -vehicle_pos_get(struct vehicle *this) +vehicle_pos_get(struct vehicle *this_) { - return &this->current_pos; + return &this_->current_pos; } double * -vehicle_speed_get(struct vehicle *this) +vehicle_speed_get(struct vehicle *this_) { - return &this->speed; + return &this_->speed; } double * -vehicle_height_get(struct vehicle *this) +vehicle_height_get(struct vehicle *this_) { - return &this->height; + return &this_->height; } double * -vehicle_dir_get(struct vehicle *this) +vehicle_dir_get(struct vehicle *this_) { - return &this->dir; + return &this_->dir; } int * -vehicle_status_get(struct vehicle *this) +vehicle_status_get(struct vehicle *this_) { - return &this->status; + return &this_->status; } int * -vehicle_sats_get(struct vehicle *this) +vehicle_sats_get(struct vehicle *this_) { - return &this->sats; + return &this_->sats; } int * -vehicle_sats_used_get(struct vehicle *this) +vehicle_sats_used_get(struct vehicle *this_) { - return &this->sats_used; + return &this_->sats_used; } double * -vehicle_pdop_get(struct vehicle *this) +vehicle_pdop_get(struct vehicle *this_) { - return &this->pdop; + return &this_->pdop; } void -vehicle_set_position(struct vehicle *this, struct coord *pos) +vehicle_set_position(struct vehicle *this_, struct coord *pos) { - this->current_pos=*pos; - this->curr.x=this->current_pos.x; - this->curr.y=this->current_pos.y; - this->delta.x=0; - this->delta.y=0; - callback_list_call_1(this->cbl, this); + this_->current_pos=*pos; + this_->curr.x=this_->current_pos.x; + this_->curr.y=this_->current_pos.y; + this_->delta.x=0; + this_->delta.y=0; + callback_list_call_1(this_->cbl, this_); } static int enable_watch_timer(gpointer t) { - struct vehicle *this=t; - enable_watch(this); + struct vehicle *this_=t; + enable_watch(this_); return FALSE; } -// FIXME Should this function be static ? +// FIXME Should this_ function be static ? void vehicle_set_navit(struct vehicle *this_,struct navit *nav) { dbg(0,"vehicle_set_navit called\n"); @@ -182,7 +182,7 @@ vehicle_set_navit(struct vehicle *this_,struct navit *nav) { } static void -vehicle_parse_gps(struct vehicle *this, char *buffer) +vehicle_parse_gps(struct vehicle *this_, char *buffer) { char *p,*item[16]; double lat,lng,scale,speed; @@ -191,8 +191,10 @@ vehicle_parse_gps(struct vehicle *this, char *buffer) unsigned char csum=0; dbg(1, "buffer='%s' ", buffer); - write(vfd, buffer, len); - write(vfd, "\n", 1); + if (this_->nmea_log) { + log_write(this_->nmea_log, buffer, len); + log_write(this_->nmea_log, "\n", 1); + } for (;;) { if (len < 4) { dbg(0, "too short\n"); @@ -238,28 +240,39 @@ vehicle_parse_gps(struct vehicle *this, char *buffer) } sscanf(item[2],"%lf",&lat); - this->geo.lat=floor(lat/100); - lat-=this->geo.lat*100; - this->geo.lat+=lat/60; + this_->geo.lat=floor(lat/100); + lat-=this_->geo.lat*100; + this_->geo.lat+=lat/60; sscanf(item[4],"%lf",&lng); - this->geo.lng=floor(lng/100); - lng-=this->geo.lng*100; - this->geo.lng+=lng/60; + this_->geo.lng=floor(lng/100); + lng-=this_->geo.lng*100; + this_->geo.lng+=lng/60; - sscanf(item[6],"%d",&this->status); - sscanf(item[7],"%d",&this->sats); - sscanf(item[9],"%lf",&this->height); - - transform_from_geo(projection_mg, &this->geo, &this->current_pos); + sscanf(item[6],"%d",&this_->status); + sscanf(item[7],"%d",&this_->sats); + sscanf(item[9],"%lf",&this_->height); + + if (this_->gpx_log) { + char buffer[256]; + sprintf(buffer,"\n",this_->geo.lat,this_->geo.lng); + log_write(this_->gpx_log, buffer, strlen(buffer)); + + } + if (this_->textfile_log) { + char buffer[256]; + sprintf(buffer,"%f %f type=trackpoint\n",this_->geo.lng,this_->geo.lat); + log_write(this_->textfile_log, buffer, strlen(buffer)); + } + transform_from_geo(projection_mg, &this_->geo, &this_->current_pos); - this->curr.x=this->current_pos.x; - this->curr.y=this->current_pos.y; - this->timer_count=0; - callback_list_call_1(this->cbl, this); - if (this->is_file) { - disable_watch(this); - g_timeout_add(1000, enable_watch_timer, this); + this_->curr.x=this_->current_pos.x; + this_->curr.y=this_->current_pos.y; + this_->timer_count=0; + callback_list_call_1(this_->cbl, this_); + if (this_->is_file) { + disable_watch(this_); + g_timeout_add(1000, enable_watch_timer, this_); } } if (!strncmp(buffer,"$GPVTG",6)) { @@ -276,16 +289,16 @@ vehicle_parse_gps(struct vehicle *this, char *buffer) if (! *p) break; *p++='\0'; } - sscanf(item[1],"%lf",&this->dir); - sscanf(item[7],"%lf",&this->speed); + sscanf(item[1],"%lf",&this_->dir); + sscanf(item[7],"%lf",&this_->speed); - scale=transform_scale(this->current_pos.y); - speed=this->speed+(this->speed-this->speed_last)/2; + scale=transform_scale(this_->current_pos.y); + speed=this_->speed+(this_->speed-this_->speed_last)/2; #ifdef INTERPOLATION_TIME - this->delta.x=sin(M_PI*this->dir/180)*speed*scale/3600*INTERPOLATION_TIME; - this->delta.y=cos(M_PI*this->dir/180)*speed*scale/3600*INTERPOLATION_TIME; + this_->delta.x=sin(M_PI*this_->dir/180)*speed*scale/3600*INTERPOLATION_TIME; + this_->delta.y=cos(M_PI*this_->dir/180)*speed*scale/3600*INTERPOLATION_TIME; #endif - this->speed_last=this->speed; + this_->speed_last=this_->speed; } if (!strncmp(buffer,"$GPRMC",6)) { /* $GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A */ @@ -299,16 +312,16 @@ vehicle_parse_gps(struct vehicle *this, char *buffer) if (! *p) break; *p++='\0'; } - sscanf(item[8],"%lf",&this->dir); - sscanf(item[7],"%lf",&this->speed); - this->speed *= 1.852; - scale=transform_scale(this->current_pos.y); - speed=this->speed+(this->speed-this->speed_last)/2; + sscanf(item[8],"%lf",&this_->dir); + sscanf(item[7],"%lf",&this_->speed); + this_->speed *= 1.852; + scale=transform_scale(this_->current_pos.y); + speed=this_->speed+(this_->speed-this_->speed_last)/2; #ifdef INTERPOLATION_TIME - this->delta.x=sin(M_PI*this->dir/180)*speed*scale/3600*INTERPOLATION_TIME; - this->delta.y=cos(M_PI*this->dir/180)*speed*scale/3600*INTERPOLATION_TIME; + this_->delta.x=sin(M_PI*this_->dir/180)*speed*scale/3600*INTERPOLATION_TIME; + this_->delta.y=cos(M_PI*this_->dir/180)*speed*scale/3600*INTERPOLATION_TIME; #endif - this->speed_last=this->speed; + this_->speed_last=this_->speed; } } @@ -321,54 +334,54 @@ vehicle_gps_callback(struct gps_data_t *data, char *buf, size_t len, int level) return; } - struct vehicle *this=vehicle_last; + struct vehicle *this_=vehicle_last; double scale,speed; #if INTERPOLATION_TIME if (! (data->set & TIME_SET)) { return; } data->set &= ~TIME_SET; - if (this->time == data->fix.time) + if (this_->time == data->fix.time) return; - this->time=data->fix.time; + this_->time=data->fix.time; #endif if (data->set & SPEED_SET) { - this->speed_last=this->speed; - this->speed=data->fix.speed*3.6; + this_->speed_last=this_->speed; + this_->speed=data->fix.speed*3.6; data->set &= ~SPEED_SET; } if (data->set & TRACK_SET) { - speed=this->speed+(this->speed-this->speed_last)/2; - this->dir=data->fix.track; - scale=transform_scale(this->current_pos.y); + speed=this_->speed+(this_->speed-this_->speed_last)/2; + this_->dir=data->fix.track; + scale=transform_scale(this_->current_pos.y); #ifdef INTERPOLATION_TIME - this->delta.x=sin(M_PI*this->dir/180)*speed*scale/3600*INTERPOLATION_TIME; - this->delta.y=cos(M_PI*this->dir/180)*speed*scale/3600*INTERPOLATION_TIME; + this_->delta.x=sin(M_PI*this_->dir/180)*speed*scale/3600*INTERPOLATION_TIME; + this_->delta.y=cos(M_PI*this_->dir/180)*speed*scale/3600*INTERPOLATION_TIME; #endif data->set &= ~TRACK_SET; } if (data->set & LATLON_SET) { - this->geo.lat=data->fix.latitude; - this->geo.lng=data->fix.longitude; - transform_from_geo(projection_mg, &this->geo, &this->current_pos); - this->curr.x=this->current_pos.x; - this->curr.y=this->current_pos.y; - this->timer_count=0; - callback_list_call_1(this->cbl, this); + this_->geo.lat=data->fix.latitude; + this_->geo.lng=data->fix.longitude; + transform_from_geo(projection_mg, &this_->geo, &this_->current_pos); + this_->curr.x=this_->current_pos.x; + this_->curr.y=this_->current_pos.y; + this_->timer_count=0; + callback_list_call_1(this_->cbl, this_); data->set &= ~LATLON_SET; } if (data->set & ALTITUDE_SET) { - this->height=data->fix.altitude; + this_->height=data->fix.altitude; data->set &= ~ALTITUDE_SET; } if (data->set & SATELLITE_SET) { - this->sats=data->satellites; + this_->sats=data->satellites; data->set &= ~SATELLITE_SET; /* FIXME : the USED_SET check does not work yet. */ - this->sats_used=data->satellites_used; + this_->sats_used=data->satellites_used; } if (data->set & STATUS_SET) { - this->status=data->status; + this_->status=data->status; data->set &= ~STATUS_SET; } if(data->set & PDOP_SET){ @@ -379,20 +392,20 @@ vehicle_gps_callback(struct gps_data_t *data, char *buf, size_t len, int level) static void -vehicle_close(struct vehicle *this) +vehicle_close(struct vehicle *this_) { GError *error=NULL; - g_io_channel_shutdown(this->iochan,0,&error); + g_io_channel_shutdown(this_->iochan,0,&error); #ifdef HAVE_LIBGPS - if (this->gps) - gps_close(this->gps); + if (this_->gps) + gps_close(this_->gps); #endif - if (this->file) - pclose(this->file); - if (this->fd != -1) - close(this->fd); + if (this_->file) + pclose(this_->file); + if (this_->fd != -1) + close(this_->fd); } struct packet { @@ -409,25 +422,25 @@ struct packet { } __attribute__ ((packed)) ; static void -vehicle_udp_recv(struct vehicle *this) +vehicle_udp_recv(struct vehicle *this_) { struct packet pkt; int size; - dbg(2,"enter this=%p\n",this); - size=recv(this->fd, &pkt, 15, 0); - if (pkt.magic == this->magic) { + dbg(2,"enter this_=%p\n",this_); + size=recv(this_->fd, &pkt, 15, 0); + if (pkt.magic == this_->magic) { dbg(3,"magic 0x%x size=%d\n", pkt.magic, size); - this->current_pos.x=pkt.u.pos.x; - this->current_pos.y=pkt.u.pos.y; - this->speed=pkt.u.pos.speed; - this->dir=pkt.u.pos.dir*2; - callback_list_call_1(this->cbl, this); + this_->current_pos.x=pkt.u.pos.x; + this_->current_pos.y=pkt.u.pos.y; + this_->speed=pkt.u.pos.speed; + this_->dir=pkt.u.pos.dir*2; + callback_list_call_1(this_->cbl, this_); } } static void -vehicle_udp_update(struct vehicle *this, struct vehicle *child) +vehicle_udp_update(struct vehicle *this_, struct vehicle *child) { struct coord *pos=&child->current_pos; struct packet pkt; @@ -435,69 +448,69 @@ vehicle_udp_update(struct vehicle *this, struct vehicle *child) int dir=child->dir/2; if (speed > 255) speed=255; - pkt.magic=this->magic; + pkt.magic=this_->magic; pkt.type=1; pkt.u.pos.x=pos->x; pkt.u.pos.y=pos->y; pkt.u.pos.speed=speed; pkt.u.pos.dir=dir; - sendto(this->fd, &pkt, 15, 0, (struct sockaddr *)&this->rem, sizeof(this->rem)); - this->current_pos=child->current_pos; - this->speed=child->speed; - this->dir=child->dir; - callback_list_call_1(this->cbl, this); + sendto(this_->fd, &pkt, 15, 0, (struct sockaddr *)&this_->rem, sizeof(this_->rem)); + this_->current_pos=child->current_pos; + this_->speed=child->speed; + this_->dir=child->dir; + callback_list_call_1(this_->cbl, this_); } static int vehicle_udp_query(void *data) { - struct vehicle *this=(struct vehicle *)data; + struct vehicle *this_=(struct vehicle *)data; struct packet pkt; - dbg(2,"enter this=%p\n", this); - pkt.magic=this->magic; + dbg(2,"enter this_=%p\n", this_); + pkt.magic=this_->magic; pkt.type=2; - sendto(this->fd, &pkt, 5, 0, (struct sockaddr *)&this->rem, sizeof(this->rem)); + sendto(this_->fd, &pkt, 5, 0, (struct sockaddr *)&this_->rem, sizeof(this_->rem)); dbg(2,"ret=TRUE\n"); return TRUE; } static int -vehicle_udp_open(struct vehicle *this) +vehicle_udp_open(struct vehicle *this_) { char *host,*child,*url,*colon; int port; - url=g_strdup(this->url); + url=g_strdup(this_->url); colon=index(url+6,':'); struct sockaddr_in lcl; - if (! colon || sscanf(colon+1,"%d/%i/%d", &port, &this->magic, &this->interval) != 3) { - g_warning("Wrong syntax in %s\n", this->url); + if (! colon || sscanf(colon+1,"%d/%i/%d", &port, &this_->magic, &this_->interval) != 3) { + g_warning("Wrong syntax in %s\n", this_->url); return 0; } host=url+6; *colon='\0'; - this->fd=socket(PF_INET, SOCK_DGRAM, 0); - this->is_udp=1; + this_->fd=socket(PF_INET, SOCK_DGRAM, 0); + this_->is_udp=1; memset(&lcl, 0, sizeof(lcl)); lcl.sin_family = AF_INET; - this->rem.sin_family = AF_INET; - inet_aton(host, &this->rem.sin_addr); - this->rem.sin_port=htons(port); + this_->rem.sin_family = AF_INET; + inet_aton(host, &this_->rem.sin_addr); + this_->rem.sin_port=htons(port); - bind(this->fd, (struct sockaddr *)&lcl, sizeof(lcl)); + bind(this_->fd, (struct sockaddr *)&lcl, sizeof(lcl)); child=index(colon+1,' '); if (child) { child++; - if (!this->child) { + if (!this_->child) { dbg(3,"child=%s\n", child); - this->child=vehicle_new(child); - this->child_cb=callback_new_1(callback_cast(vehicle_udp_update), this); - vehicle_callback_add(this->child, this->child_cb); + this_->child=vehicle_new(child); + this_->child_cb=callback_new_1(callback_cast(vehicle_udp_update), this_); + vehicle_callback_add(this_->child, this_->child_cb); } } else { - vehicle_udp_query(this); - g_timeout_add(this->interval*1000, vehicle_udp_query, this); + vehicle_udp_query(this_); + g_timeout_add(this_->interval*1000, vehicle_udp_query, this_); } g_free(url); return 0; @@ -600,15 +613,15 @@ vehicle_demo_timer (struct vehicle *this) if(street->count){ dbg(1,"vehicle is at %lx,%lx (2)\n",current_pos->x,current_pos->y); - dbg(1,"street->c[0] = %lx, street->c[street->count-1]= %lx (2)\n",street->c[0],street->c[street->count-1]); - if((current_pos->x==street->c[i].x)&&(current_pos->y==street->c[i].y)){ + dbg(1,"street->c[0] = %lx,%lx, street->c[street->count-1]= %lx,%lx count=%d\n",street->c[0].x,street->c[0].y, street->c[street->count-1].x, street->c[street->count-1].y, street->count); + if((current_pos->x==street->c[0].x)&&(current_pos->y==street->c[0].y)){ target->x=street->c[1].x; target->y=street->c[1].y; - dbg(1,"Moving to : street->c[0] (%lx,%lx)\n",street->c[0].x,street->c[0].y); + dbg(1,"Moving to : street->c[1] (%lx,%lx)\n",street->c[1].x,street->c[1].y); } else { - target->x=street->c[street->count-1].x; - target->y=street->c[street->count-1].y; - dbg(1,"Moving to : street->c[street->count-1] (%lx,%lx)\n",street->c[street->count-1].x,street->c[street->count-1].y); + target->x=street->c[street->count-2].x; + target->y=street->c[street->count-2].y; + dbg(1,"Moving to : street->c[street->count-2] (%lx,%lx)\n",street->c[street->count-2].x,street->c[street->count-2].y); } vehicle_set_position(this,target); } else { @@ -623,7 +636,7 @@ vehicle_demo_timer (struct vehicle *this) } static int -vehicle_open(struct vehicle *this) +vehicle_open(struct vehicle *this_) { struct termios tio; struct stat st; @@ -633,15 +646,15 @@ vehicle_open(struct vehicle *this) struct gps_data_t *gps=NULL; char *url_,*colon; #endif - if (! strncmp(this->url,"file:",5)) { - fd=open(this->url+5,O_RDONLY|O_NDELAY); + if (! strncmp(this_->url,"file:",5)) { + fd=open(this_->url+5,O_RDONLY|O_NDELAY); if (fd < 0) { - g_warning("Failed to open %s", this->url); + g_warning("Failed to open %s", this_->url); return 0; } - stat(this->url+5, &st); + stat(this_->url+5, &st); if (S_ISREG (st.st_mode)) { - this->is_file=1; + this_->is_file=1; } else { tcgetattr(fd, &tio); cfmakeraw(&tio); @@ -651,46 +664,46 @@ vehicle_open(struct vehicle *this) tio.c_cc[VTIME]=1; tcsetattr(fd, TCSANOW, &tio); } - this->fd=fd; - } else if (! strncmp(this->url,"pipe:",5)) { - this->file=popen(this->url+5, "r"); - this->is_pipe=1; - if (! this->file) { - g_warning("Failed to open %s", this->url); + this_->fd=fd; + } else if (! strncmp(this_->url,"pipe:",5)) { + this_->file=popen(this_->url+5, "r"); + this_->is_pipe=1; + if (! this_->file) { + g_warning("Failed to open %s", this_->url); return 0; } - fd=fileno(this->file); - } else if (! strncmp(this->url,"gpsd://",7)) { + fd=fileno(this_->file); + } else if (! strncmp(this_->url,"gpsd://",7)) { #ifdef HAVE_LIBGPS - url_=g_strdup(this->url); + url_=g_strdup(this_->url); colon=index(url_+7,':'); if (colon) { *colon=0; gps=gps_open(url_+7,colon+1); } else - gps=gps_open(this->url+7,NULL); + gps=gps_open(this_->url+7,NULL); g_free(url_); if (! gps) { - g_warning("Failed to connect to %s", this->url); + g_warning("Failed to connect to %s", this_->url); return 0; } gps_query(gps, "w+x\n"); gps_set_raw_hook(gps, vehicle_gps_callback); fd=gps->gps_fd; - this->gps=gps; + this_->gps=gps; #else g_warning("No support for gpsd compiled in\n"); return 0; #endif - } else if (! strncmp(this->url,"udp://",6)) { - vehicle_udp_open(this); - fd=this->fd; - } else if (! strncmp(this->url,"demo://",7)) { + } else if (! strncmp(this_->url,"udp://",6)) { + vehicle_udp_open(this_); + fd=this_->fd; + } else if (! strncmp(this_->url,"demo://",7)) { dbg(0,"Creating a demo vehicle\n"); - g_timeout_add(1000, vehicle_demo_timer, this); + g_timeout_add(1000, vehicle_demo_timer, this_); } - this->iochan=g_io_channel_unix_new(fd); - enable_watch(this); + this_->iochan=g_io_channel_unix_new(fd); + enable_watch(this_); return 1; } @@ -698,48 +711,48 @@ vehicle_open(struct vehicle *this) static gboolean vehicle_track(GIOChannel *iochan, GIOCondition condition, gpointer t) { - struct vehicle *this=t; + struct vehicle *this_=t; char *str,*tok; gsize size; - dbg(0,"enter condition=%d\n", condition); + dbg(1,"enter condition=%d\n", condition); if (condition == G_IO_IN) { #ifdef HAVE_LIBGPS - if (this->gps) { - vehicle_last=this; - gps_poll(this->gps); + if (this_->gps) { + vehicle_last=this_; + gps_poll(this_->gps); } else { #else { #endif - if (this->is_udp) { - vehicle_udp_recv(this); + if (this_->is_udp) { + vehicle_udp_recv(this_); return TRUE; } - size=read(g_io_channel_unix_get_fd(iochan), this->buffer+this->buffer_pos, BUFFER_SIZE-this->buffer_pos-1); + size=read(g_io_channel_unix_get_fd(iochan), this_->buffer+this_->buffer_pos, BUFFER_SIZE-this_->buffer_pos-1); if (size <= 0) { - vehicle_close(this); - vehicle_open(this); + vehicle_close(this_); + vehicle_open(this_); return TRUE; } - this->buffer_pos+=size; - this->buffer[this->buffer_pos]='\0'; - dbg(1,"size=%d pos=%d buffer='%s'\n", size, this->buffer_pos, this->buffer); - str=this->buffer; + this_->buffer_pos+=size; + this_->buffer[this_->buffer_pos]='\0'; + dbg(1,"size=%d pos=%d buffer='%s'\n", size, this_->buffer_pos, this_->buffer); + str=this_->buffer; while ((tok=index(str, '\n'))) { *tok++='\0'; dbg(1,"line='%s'\n", str); - vehicle_parse_gps(this, str); + vehicle_parse_gps(this_, str); str=tok; } - if (str != this->buffer) { - size=this->buffer+this->buffer_pos-str; - memmove(this->buffer, str, size+1); - this->buffer_pos=size; - dbg(1,"now pos=%d buffer='%s'\n", this->buffer_pos, this->buffer); - } else if (this->buffer_pos == BUFFER_SIZE-1) { + if (str != this_->buffer) { + size=this_->buffer+this_->buffer_pos-str; + memmove(this_->buffer, str, size+1); + this_->buffer_pos=size; + dbg(1,"now pos=%d buffer='%s'\n", this_->buffer_pos, this_->buffer); + } else if (this_->buffer_pos == BUFFER_SIZE-1) { dbg(0,"overflow\n"); - this->buffer_pos=0; + this_->buffer_pos=0; } } @@ -750,62 +763,83 @@ vehicle_track(GIOChannel *iochan, GIOCondition condition, gpointer t) } static void -enable_watch(struct vehicle *this) +enable_watch(struct vehicle *this_) { - this->watch=g_io_add_watch(this->iochan, G_IO_IN|G_IO_ERR|G_IO_HUP, vehicle_track, this); + this_->watch=g_io_add_watch(this_->iochan, G_IO_IN|G_IO_ERR|G_IO_HUP, vehicle_track, this_); } static void -disable_watch(struct vehicle *this) +disable_watch(struct vehicle *this_) { - g_source_remove(this->watch); + g_source_remove(this_->watch); } struct vehicle * vehicle_new(const char *url) { - struct vehicle *this; - this=g_new0(struct vehicle,1); - - this->cbl=callback_list_new(); - this->url=g_strdup(url); - this->fd=-1; - - if (! vfd) { - vfd=open("vlog.txt", O_RDWR|O_APPEND|O_CREAT, 0644); - } - vehicle_open(this); - this->current_pos.x=0x130000; - this->current_pos.y=0x600000; - this->curr.x=this->current_pos.x; - this->curr.y=this->current_pos.y; - this->delta.x=0; - this->delta.y=0; + struct vehicle *this_; + this_=g_new0(struct vehicle,1); + + this_->cbl=callback_list_new(); + this_->url=g_strdup(url); + this_->fd=-1; + + vehicle_open(this_); + this_->current_pos.x=0x130000; + this_->current_pos.y=0x600000; + this_->curr.x=this_->current_pos.x; + this_->curr.y=this_->current_pos.y; + this_->delta.x=0; + this_->delta.y=0; #if INTERPOLATION_TIME - g_timeout_add(INTERPOLATION_TIME, vehicle_timer, this); + g_timeout_add(INTERPOLATION_TIME, vehicle_timer, this_); #endif - return this; + return this_; } void -vehicle_callback_add(struct vehicle *this, struct callback *cb) +vehicle_callback_add(struct vehicle *this_, struct callback *cb) { - callback_list_add(this->cbl, cb); + callback_list_add(this_->cbl, cb); } void -vehicle_callback_remove(struct vehicle *this, struct callback *cb) +vehicle_callback_remove(struct vehicle *this_, struct callback *cb) { - callback_list_remove(this->cbl, cb); + callback_list_remove(this_->cbl, cb); } +int +vehicle_add_log(struct vehicle *this_, struct log *log, struct attr **attrs) +{ + struct attr *type; + type=attr_search(attrs, NULL, attr_type); + if (! type) + return 1; + if (!strcmp(type->u.str,"nmea")) { + this_->nmea_log=log; + } else if (!strcmp(type->u.str,"gpx")) { + char *header="\n\n\n\n"; + char *trailer="\n\n\n"; + this_->gpx_log=log; + log_set_header(log,header,strlen(header)); + log_set_trailer(log,trailer,strlen(trailer)); + } else if (!strcmp(type->u.str,"textfile")) { + char *header="type=track\n"; + this_->textfile_log=log; + log_set_header(log,header,strlen(header)); + } else + return 1; + return 0; +} + void -vehicle_destroy(struct vehicle *this) +vehicle_destroy(struct vehicle *this_) { - vehicle_close(this); - callback_list_destroy(this->cbl); - g_free(this->url); - g_free(this); + vehicle_close(this_); + callback_list_destroy(this_->cbl); + g_free(this_->url); + g_free(this_); } diff --git a/navit/src/vehicle.h b/navit/src/vehicle.h index b958379..557b927 100644 --- a/navit/src/vehicle.h +++ b/navit/src/vehicle.h @@ -6,8 +6,11 @@ extern "C" { #endif /* prototypes */ enum projection; +struct attr; struct callback; struct coord; +struct log; +struct navit; struct vehicle; enum projection vehicle_projection(struct vehicle *this_); struct coord *vehicle_pos_get(struct vehicle *this_); @@ -19,10 +22,11 @@ int *vehicle_sats_get(struct vehicle *this_); int *vehicle_sats_used_get(struct vehicle *this_); double *vehicle_pdop_get(struct vehicle *this_); void vehicle_set_position(struct vehicle *this_, struct coord *pos); -void vehicle_set_navit(struct vehicle *this_,struct navit *nav); +void vehicle_set_navit(struct vehicle *this_, struct navit *nav); struct vehicle *vehicle_new(const char *url); void vehicle_callback_add(struct vehicle *this_, struct callback *cb); void vehicle_callback_remove(struct vehicle *this_, struct callback *cb); +int vehicle_add_log(struct vehicle *this_, struct log *log, struct attr **attrs); void vehicle_destroy(struct vehicle *this_); /* end of prototypes */ #ifdef __cplusplus diff --git a/navit/src/xmlconfig.c b/navit/src/xmlconfig.c index deba3dc..bf5ceb4 100644 --- a/navit/src/xmlconfig.c +++ b/navit/src/xmlconfig.c @@ -17,8 +17,9 @@ #include "point.h" #include "graphics.h" #include "gui.h" -#include "xmlconfig.h" #include "osd.h" +#include "log.h" +#include "xmlconfig.h" struct xmlstate { @@ -270,6 +271,20 @@ xmlconfig_vehicle(struct xmlstate *state) } static int +xmlconfig_log(struct xmlstate *state) +{ + struct attr **attrs; + attrs=convert_to_attrs(state); + state->element_object = log_new(attrs); + if (! state->element_object) + return 0; + if (vehicle_add_log(state->parent->element_object, state->element_object, attrs)) + return 0; + return 1; +} + + +static int xmlconfig_window_items(struct xmlstate *state) { int distance=-1; @@ -611,6 +626,7 @@ struct element_func { { "route", "navit", xmlconfig_route}, { "speed", "route", xmlconfig_speed}, { "vehicle", "navit", xmlconfig_vehicle}, + { "log", "vehicle", xmlconfig_log}, { "window_items", "navit", xmlconfig_window_items}, { "plugins", NULL, xmlconfig_plugins}, { "plugin", "plugins", xmlconfig_plugin}, -- 2.7.4