title: libgps for extract data from the gpsd daemon
body: <p>I wanted to use libgps to interface with gpsd daemon. That's why I've implemented a little testing application in order to extract a value from a specific satellite.</p>
  
  <p>The documentation on its HOWTO page tells us that </p>
  
  <blockquote>
    <p>The tricky part is interpreting what you get from the blocking read.
    The reason it’s tricky is that you’re not guaranteed that every read
    will pick up exactly one complete JSON object from the daemon. It may
    grab one response object, or more than one, or part of one, or one or
    more followed by a fragment.</p>
  </blockquote>
  
  <p>As recommended the documentation, the <code>PACKET_SET</code> mask bit is checked before doing anything else.</p>
  
  <pre><code>#define _GNU_SOURCE
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;gps.h&gt;
  #include &lt;pthread.h&gt;
  
  pthread_t t_thread;
  
  struct t_args {
     unsigned int ID;
  };
  
  unsigned int status = 0;
  int elevation;
  
  int p_nmea(void *targs);
  
  void start_test(void)
  {
      struct t_args *args = malloc(sizeof *args);
      status = 1;
      args-&gt;ID = 10;
  
      pthread_attr_t attr;
  
      pthread_attr_init(&amp;attr);
      pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE);
      if (pthread_create(&amp;t_thread, &amp;attr, (void *)&amp;p_nmea, args) != 0)
      {
          perror("create: \n");
      }
  }
  
  int test_result(int * Svalue)
  {
      int res;
  
      if(status == 1)
      {
          void * t_res;
          if(pthread_tryjoin_np(t_thread, &amp;t_res) != 0)
          {
              status = 1;
          }
          else
          {       
              if((int)t_res == 1)
              {
                  res = 3;
                  *Svalue = elevation;
                  elevation = 0;
              }
              else
              {
                  res = 4;            
              }
          }
      }
      return res;
  }
  
  int p_nmea(void *targs)
  {
      struct t_args *thread_args = targs;     
      struct gps_data_t gpsdata;
      int ret = -1;
      int count = 10;
      int i,j;
  
     if(gps_open((char *)"localhost", (char *)DEFAULT_GPSD_PORT, &amp;gpsdata) != 0)
     {
          (void)fprintf(stderr, "cgps: no gpsd running or network error: %d, %s\n", errno, gps_errstr(errno));
          return (-1);
     }
     else
     {
          (void)gps_stream(&amp;gpsdata, WATCH_ENABLE, NULL);
          do 
          {
              if(!gps_waiting(&amp;gpsdata, 1000000))
              {       
                  (void)gps_close(&amp;gpsdata);
              }
              else
              {
                  if(gps_read(&amp;gpsdata) == -1)
                  {
                      return (-1);
                  }
                  else
                  {
                      if(gpsdata.set &amp; PACKET_SET)
                      {
                         for (i = 0; i &lt; MAXCHANNELS; i++)
                         {
                              for (j = 0; j &lt; gpsdata-&gt;satellites_visible; j++)
                              {
                                  if(gpsdata-&gt;PRN[i] == thread_args.ID) 
                                  {
                                      elevation = (int)gpsdata-&gt;elevation[i];
                                      ret = 1;
                                      break;
                                  }       
                              }
                              if(gpsdata-&gt;PRN[i] == thread_args.ID)
                              {
                                  break;
                              }
                         }
                      }
                  }
              }
              --count;
          }while(count != 0);
      }
      (void)gps_stream(&amp;gpsdata, WATCH_DISABLE, NULL);
      (void)gps_close(&amp;gpsdata);
      (void)free(thread_args);
      (void)pthread_exit((void*) ret);
  }
  </code></pre>
  
  <p>As recommended in the documentation too, I had a look at cgps and gpxlogger for example codes, but the subtleties of libgps escape me. A while loop has been added before <code>gps_waiting()</code> in order to get, at least, one entire response object. Before introducing pthread, I noted that call the function <code>test_result()</code> just after <code>start_test()</code> take few seconds before returning an answer. By using a thread I thought that <code>3</code> would be imediately returned, then <code>3</code> or <code>4</code> .. but it's not ! I am still losing few seconds. In addition, I voluntarily use <code>pthread_tryjoin_np()</code> because its man page says </p>
  
  <blockquote>
    <p>The pthread_tryjoin_np() function performs a nonblocking join with the thread</p>
  </blockquote>
  
  <p>Can anybody give me his help, I guess that I understand something wrongly but I am not able to say about which part yet? Basically, why I come into the do while loop at least four times before returning the first value ?</p>
  
  <p>EDIT 1 : </p>
  
  <p>After reading the documentation HOWTO again I highlight the lines :</p>
  
  <blockquote>
    <p>The fact that the data-waiting check and the read both block means that, if your application has to deal with other input sources than the GPS, you will probably have to isolate the read loop in a thread with a mutex lock on the gps_data structure.</p>
  </blockquote>
  
  <p>I am a little bit confusing. What does it really mean ?</p>