Diff of /trunk/src/gps.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 155 by harbaum, Thu Oct 29 13:59:16 2009 UTC revision 156 by harbaum, Tue Nov 3 13:15:35 2009 UTC
# Line 32  Line 32 
32  #include <errno.h>  #include <errno.h>
33  #endif  #endif
34    
35    static void gps_unregister_all(appdata_t *appdata);
36    
37  #ifndef ENABLE_LIBLOCATION  #ifndef ENABLE_LIBLOCATION
38    
39  /* maybe user configurable later on ... */  /* maybe user configurable later on ... */
# Line 120  static int gps_connect(gps_state_t *gps_ Line 122  static int gps_connect(gps_state_t *gps_
122    
123    if(gpsbt_start(NULL, 0, 0, 0, errstr, sizeof(errstr),    if(gpsbt_start(NULL, 0, 0, 0, errstr, sizeof(errstr),
124                   0, &gps_state->context) < 0) {                   0, &gps_state->context) < 0) {
125      printf("Error connecting to GPS receiver: (%d) %s (%s)\n",      printf("gps: Error connecting to GPS receiver: (%d) %s (%s)\n",
126             errno, strerror(errno), errstr);             errno, strerror(errno), errstr);
127    }    }
128  #endif  #endif
# Line 134  static int gps_connect(gps_state_t *gps_ Line 136  static int gps_connect(gps_state_t *gps_
136    while(retries &&    while(retries &&
137          (GNOME_VFS_OK != (vfs_result = gnome_vfs_inet_connection_create(          (GNOME_VFS_OK != (vfs_result = gnome_vfs_inet_connection_create(
138                  &gps_state->iconn, GPSD_HOST, GPSD_PORT, NULL)))) {                  &gps_state->iconn, GPSD_HOST, GPSD_PORT, NULL)))) {
139      printf("Error creating connection to GPSD, retrying ...\n");      printf("gps: Error creating connection to GPSD, retrying ...\n");
140    
141      retries--;      retries--;
142      sleep(1);      sleep(1);
143    }    }
144    
145    if(!retries) {    if(!retries) {
146      printf("Finally failed ...\n");      printf("gps: Finally failed ...\n");
147      return -1;      return -1;
148    }    }
149    
150    retries = 5;    retries = 5;
151    while(retries && ((gps_state->socket =    while(retries && ((gps_state->socket =
152       gnome_vfs_inet_connection_to_socket(gps_state->iconn)) == NULL)) {       gnome_vfs_inet_connection_to_socket(gps_state->iconn)) == NULL)) {
153      printf("Error creating connecting GPSD socket, retrying ...\n");      printf("gps: Error creating connecting GPSD socket, retrying ...\n");
154    
155      retries--;      retries--;
156      sleep(1);      sleep(1);
157    }    }
158    
159    if(!retries) {    if(!retries) {
160      printf("Finally failed ...\n");      printf("gps: Finally failed ...\n");
161      return -1;      return -1;
162    }    }
163    
164    GTimeVal timeout = { 10, 0 };    GTimeVal timeout = { 10, 0 };
165    if(GNOME_VFS_OK != (vfs_result = gnome_vfs_socket_set_timeout(    if(GNOME_VFS_OK != (vfs_result = gnome_vfs_socket_set_timeout(
166          gps_state->socket, &timeout, NULL))) {          gps_state->socket, &timeout, NULL))) {
167      printf("Error setting GPSD timeout\n");      printf("gps: Error setting GPSD timeout\n");
168      return -1;      return -1;
169    }    }
170    
171    printf("GPSD connected ...\n");    printf("gps: GPSD connected ...\n");
172    
173    return 0;    return 0;
174  }  }
# Line 371  static void gps_unpack(char *buf, struct Line 373  static void gps_unpack(char *buf, struct
373              for (j = 0; j < gpsdata->satellites; j++) {              for (j = 0; j < gpsdata->satellites; j++) {
374                PRN[j]=elevation[j]=azimuth[j]=ss[j]=used[j]=0;                PRN[j]=elevation[j]=azimuth[j]=ss[j]=used[j]=0;
375              }              }
376              //      printf("sats = %d\n", gpsdata->satellites);              //      printf("gps: sats = %d\n", gpsdata->satellites);
377              for (j = 0, gpsdata->satellites_used = 0; j < gpsdata->satellites; j++) {              for (j = 0, gpsdata->satellites_used = 0; j < gpsdata->satellites; j++) {
378                if ((sp != NULL) && ((sp = strchr(sp, ':')) != NULL)) {                if ((sp != NULL) && ((sp = strchr(sp, ':')) != NULL)) {
379                  sp++;                  sp++;
# Line 399  static void gps_unpack(char *buf, struct Line 401  static void gps_unpack(char *buf, struct
401    }    }
402  }  }
403    
404    /* call one of the application provided gps callbacks */
405    static void do_app_cb(gpointer data, gpointer user_data) {
406      appdata_t *appdata = (appdata_t*)user_data;
407      gps_cb_t *cb = (gps_cb_t*)data;
408      cb->cb(appdata->gps_state, cb->data);
409    }
410    
411    /* walk though list of all application provided callbacks */
412    static gboolean gps_idle_cb(gpointer data) {
413      appdata_t *appdata = (appdata_t*)data;
414      printf("gps: idle callback, calling app callbacks\n");
415    
416      g_slist_foreach(appdata->gps_state->cb, do_app_cb, appdata);
417    
418      return FALSE;
419    }
420    
421  gpointer gps_thread(gpointer data) {  gpointer gps_thread(gpointer data) {
422    GnomeVFSFileSize bytes_read;    GnomeVFSFileSize bytes_read;
423    GnomeVFSResult vfs_result;    GnomeVFSResult vfs_result;
# Line 416  gpointer gps_thread(gpointer data) { Line 435  gpointer gps_thread(gpointer data) {
435    while(1) {    while(1) {
436      if(appdata->use_gps) {      if(appdata->use_gps) {
437        if(!connected) {        if(!connected) {
438          printf("trying to connect\n");          printf("gps: trying to connect\n");
439    
440          if(gps_connect(appdata->gps_state) < 0)          if(gps_connect(appdata->gps_state) < 0)
441            sleep(10);            sleep(10);
# Line 442  gpointer gps_thread(gpointer data) { Line 461  gpointer gps_thread(gpointer data) {
461              if(vfs_result == GNOME_VFS_OK) {              if(vfs_result == GNOME_VFS_OK) {
462                str[bytes_read] = 0;                str[bytes_read] = 0;
463    
464                //          printf("msg: %s (%d)\n", str, strlen(str));                //          printf("gps: msg: %s (%d)\n", str, strlen(str));
465    
466                g_mutex_lock(appdata->gps_state->mutex);                g_mutex_lock(appdata->gps_state->mutex);
467    
# Line 452  gpointer gps_thread(gpointer data) { Line 471  gpointer gps_thread(gpointer data) {
471    
472                gps_unpack(str, &appdata->gps_state->gpsdata);                gps_unpack(str, &appdata->gps_state->gpsdata);
473                g_mutex_unlock(appdata->gps_state->mutex);                g_mutex_unlock(appdata->gps_state->mutex);
474                  g_idle_add(gps_idle_cb, appdata);
475              }              }
476            }            }
477          }          }
# Line 459  gpointer gps_thread(gpointer data) { Line 479  gpointer gps_thread(gpointer data) {
479        }        }
480      } else {      } else {
481        if(connected) {        if(connected) {
482          printf("stopping GPS connection due to user request\n");          printf("gps: stopping GPS connection due to user request\n");
483          gnome_vfs_inet_connection_destroy(appdata->gps_state->iconn, NULL);          gnome_vfs_inet_connection_destroy(appdata->gps_state->iconn, NULL);
484    
485  #ifdef USE_MAEMO  #ifdef USE_MAEMO
# Line 471  gpointer gps_thread(gpointer data) { Line 491  gpointer gps_thread(gpointer data) {
491      }      }
492    }    }
493    
494    printf("GPS thread ended???\n");    printf("gps: thread ended???\n");
495    return NULL;    return NULL;
496  }  }
497    
# Line 485  void gps_init(appdata_t *appdata) { Line 505  void gps_init(appdata_t *appdata) {
505  }  }
506    
507  void gps_release(appdata_t *appdata) {  void gps_release(appdata_t *appdata) {
508      gps_unregister_all(appdata);
509  #ifdef USE_MAEMO  #ifdef USE_MAEMO
510    gpsbt_stop(&appdata->gps_state->context);    gpsbt_stop(&appdata->gps_state->context);
511  #endif  #endif
# Line 540  location_changed(LocationGPSDevice *devi Line 561  location_changed(LocationGPSDevice *devi
561  void gps_init(appdata_t *appdata) {  void gps_init(appdata_t *appdata) {
562    gps_state_t *gps_state = appdata->gps_state = g_new0(gps_state_t, 1);    gps_state_t *gps_state = appdata->gps_state = g_new0(gps_state_t, 1);
563    
564    printf("GPS init: Using liblocation\n");    printf("gps: init: Using liblocation\n");
565    
566    gps_state->device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);    gps_state->device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
567    if(!gps_state->device) {    if(!gps_state->device) {
568      printf("Unable to connect to liblocation\n");      printf("gps: Unable to connect to liblocation\n");
569      return;      return;
570    }    }
571    
# Line 560  void gps_init(appdata_t *appdata) { Line 581  void gps_init(appdata_t *appdata) {
581  #endif  #endif
582       ) {       ) {
583    
584      printf("Having control over GPSD and GPS is to be enabled, starting it\n");      printf("gps: Having control over GPSD and GPS is to be enabled, starting it\n");
585      location_gpsd_control_start(gps_state->control);      location_gpsd_control_start(gps_state->control);
586    }    }
587  }  }
588    
589  void gps_release(appdata_t *appdata) {  void gps_release(appdata_t *appdata) {
590    gps_state_t *gps_state = appdata->gps_state;    gps_state_t *gps_state = appdata->gps_state;
591      gps_unregister_all(appdata);
592    
593    if(!gps_state->device) return;    if(!gps_state->device) return;
594    
# Line 575  void gps_release(appdata_t *appdata) { Line 597  void gps_release(appdata_t *appdata) {
597       && gps_state->control->can_control       && gps_state->control->can_control
598  #endif  #endif
599       ) {       ) {
600      printf("Having control over GPSD, stopping it\n");      printf("gps: Having control over GPSD, stopping it\n");
601      location_gpsd_control_stop(gps_state->control);      location_gpsd_control_stop(gps_state->control);
602    }    }
603    
# Line 644  gps_sat_t *gps_get_sats(appdata_t *appda Line 666  gps_sat_t *gps_get_sats(appdata_t *appda
666  #endif  #endif
667    
668  void *gps_register_callback(appdata_t *appdata, gps_cb cb, gpointer data) {  void *gps_register_callback(appdata_t *appdata, gps_cb cb, gpointer data) {
669    printf("register gps callback\n");    printf("gps: register gps callback\n");
670    return NULL;  
671      if(!appdata->gps_state)
672        return NULL;
673    
674      /* allocate callback info strcuture */
675      gps_cb_t *cb_info = g_new0(gps_cb_t, 1);
676      cb_info->cb = cb;
677      cb_info->data = data;
678    
679      /* and insert it into list of callbacks */
680      appdata->gps_state->cb = g_slist_append(appdata->gps_state->cb, cb_info);
681    
682      return cb_info;
683  }  }
684    
685    
686  void gps_unregister_callback(appdata_t *appdata, void *cb) {  void gps_unregister_callback(appdata_t *appdata, void *cb) {
687    printf("unregister gps callback\n");    printf("gps: unregister gps callback\n");
688    
689      if(!appdata->gps_state)
690        return;
691    
692      /* the item must be in the list */
693      g_assert(g_slist_find(appdata->gps_state->cb, cb));
694    
695      g_free(cb);
696      appdata->gps_state->cb = g_slist_remove(appdata->gps_state->cb, cb);
697    }
698    
699    static void gps_unregister_all(appdata_t *appdata) {
700      printf("gps: unregister all callbacks: ");
701    
702      while(appdata->gps_state->cb) {
703        printf(".");
704        g_free(appdata->gps_state->cb->data);
705        appdata->gps_state->cb = g_slist_remove(appdata->gps_state->cb,
706                                appdata->gps_state->cb->data);
707      }
708      printf("\n");
709  }  }

Legend:
Removed from v.155  
changed lines
  Added in v.156