Example #1
0
/**
 * assuan_transact:
 * @ctx: The Assuan context
 * @command: Command line to be send to the server
 * @data_cb: Callback function for data lines
 * @data_cb_arg: first argument passed to @data_cb
 * @inquire_cb: Callback function for a inquire response
 * @inquire_cb_arg: first argument passed to @inquire_cb
 * @status_cb: Callback function for a status response
 * @status_cb_arg: first argument passed to @status_cb
 * 
 * FIXME: Write documentation
 * 
 * Return value: 0 on success or an error code.  The error code may be
 * the one one returned by the server via error lines or from the
 * callback functions.  Take care:  If a callback returns an error
 * this function returns immediately with this error.
 **/
gpg_error_t
assuan_transact (assuan_context_t ctx,
                 const char *command,
                 gpg_error_t (*data_cb)(void *, const void *, size_t),
                 void *data_cb_arg,
                 gpg_error_t (*inquire_cb)(void*, const char *),
                 void *inquire_cb_arg,
                 gpg_error_t (*status_cb)(void*, const char *),
                 void *status_cb_arg)
{
  gpg_error_t rc;
  assuan_response_t response;
  int off;
  char *line;
  int linelen;

  rc = assuan_write_line (ctx, command);
  if (rc)
    return rc;

  if (*command == '#' || !*command)
    return 0; /* Don't expect a response for a comment line.  */

 again:
  rc = _assuan_read_from_server (ctx, &response, &off,
                                 ctx->flags.convey_comments);
  if (rc)
    return rc; /* error reading from server */

  line = ctx->inbound.line + off;
  linelen = ctx->inbound.linelen - off;

  if (response == ASSUAN_RESPONSE_ERROR)
    rc = atoi (line);
  else if (response == ASSUAN_RESPONSE_DATA)
    {
      if (!data_cb)
        rc = _assuan_error (ctx, GPG_ERR_ASS_NO_DATA_CB);
      else 
        {
          rc = data_cb (data_cb_arg, line, linelen);
          if (!rc)
            goto again;
        }
    }
  else if (response == ASSUAN_RESPONSE_INQUIRE)
    {
      if (!inquire_cb)
        {
          assuan_write_line (ctx, "END"); /* get out of inquire mode */
          _assuan_read_from_server (ctx, &response, &off, 0); /* dummy read */
          rc = _assuan_error (ctx, GPG_ERR_ASS_NO_INQUIRE_CB);
        }
      else
        {
          rc = inquire_cb (inquire_cb_arg, line);
          if (!rc)
            rc = assuan_send_data (ctx, NULL, 0); /* flush and send END */
          if (!rc)
            goto again;
        }
    }
  else if (response == ASSUAN_RESPONSE_STATUS)
    {
      if (status_cb)
        rc = status_cb (status_cb_arg, line);
      if (!rc)
        goto again;
    }
  else if (response == ASSUAN_RESPONSE_COMMENT && ctx->flags.convey_comments)
    {
      line -= off; /* Send line with the comment marker.  */
      if (status_cb)
        rc = status_cb (status_cb_arg, line);
      if (!rc)
        goto again;
    }
  else if (response == ASSUAN_RESPONSE_END)
    {
      if (!data_cb)
        rc = _assuan_error (ctx, GPG_ERR_ASS_NO_DATA_CB);
      else 
        {
          rc = data_cb (data_cb_arg, NULL, 0);
          if (!rc)
            goto again;
        }
    }

  return rc;
}
/**
 * assuan_transact2:
 * @ctx: The Assuan context
 * @command: Coimmand line to be send to server
 * @data_cb: Callback function for data lines
 * @data_cb_arg: first argument passed to @data_cb
 * @inquire_cb: Callback function for a inquire response
 * @inquire_cb_arg: first argument passed to @inquire_cb
 * @status_cb: Callback function for a status response
 * @status_cb_arg: first argument passed to @status_cb
 * @okay_cb: Callback function for the final  OK response
 * @okay_cb_arg: first argument passed to @okay_cb
 * 
 * FIXME: Write documentation
 * 
 * Return value: 0 on success or error code.  The error code may be
 * the one one returned by the server in error lines or from the
 * callback functions.
 **/
assuan_error_t
assuan_transact2 (assuan_context_t ctx,
                  const char *command,
                  assuan_error_t (*data_cb)(void *, const void *, size_t),
                  void *data_cb_arg,
                  assuan_error_t (*inquire_cb)(void*, const char *),
                  void *inquire_cb_arg,
                  assuan_error_t (*status_cb)(void*, const char *),
                  void *status_cb_arg,
                  assuan_error_t (*okay_cb)(void*, const char *),
                  void *okay_cb_arg)
{
  int rc, okay, off;
  unsigned char *line;
  int linelen;

  rc = assuan_write_line (ctx, command);
  if (rc)
    return rc;

  if (*command == '#' || !*command)
    return 0; /* Don't expect a response for a comment line.  */

 again:
  rc = _assuan_read_from_server (ctx, &okay, &off);
  if (rc)
    return rc; /* error reading from server */

  line = ctx->inbound.line + off;
  linelen = ctx->inbound.linelen - off;

  if (!okay)
    {
      rc = atoi (line);
      if (rc < 100)
        rc = ASSUAN_Server_Fault;
    }
  else if (okay == 1) /* Received OK. */
    {
      if (okay_cb)
        {
          rc = okay_cb (okay_cb_arg, line);
          /* We better wipe out the buffer after processing it.  This
             is no real guarantee that it won't get swapped out but at
             least for the standard cases we can make sure that a
             passphrase returned with the OK line is rendered
             unreadable.  In fact the current Assuan interface suffers
             from the problem that it is not possible to do assuan I/O
             through secure memory.  There is no easy solution given
             the current implementation but we need to address it
             sooner or later.  The problem was introduced with
             gpg-agent's GET_PASPHRASE command but it might also make
             sense to have a way to convey sessions keys through
             secured memory.  Note that the old implementation in gpg
             for accessing the passphrase in fact used secure memory
             but had the drawback of using a limited and not fully
             conforming Assuan implementation - given that pinentry
             and gpg-agent neither use secured memory for Assuan I/O,
             it is negligible to drop the old implementation in gpg's
             passphrase.c and use the wipememory workaround here.  */
          memset (line, 0, strlen (line));
        }
    }
  else if (okay == 2)
    {
      if (!data_cb)
        rc = ASSUAN_No_Data_Callback;
      else 
        {
          unsigned char *s, *d;

          for (s=d=line; linelen; linelen--)
            {
              if (*s == '%' && linelen > 2)
                { /* handle escaping */
                  s++;
                  *d++ = xtoi_2 (s);
                  s += 2;
                  linelen -= 2;
                }
              else
                *d++ = *s++;
            }
          *d = 0; /* add a hidden string terminator */
          rc = data_cb (data_cb_arg, line, d - line);
          if (!rc)
            goto again;
        }
    }
  else if (okay == 3)
    {
      if (!inquire_cb)
        {
          assuan_write_line (ctx, "END"); /* get out of inquire mode */
          _assuan_read_from_server (ctx, &okay, &off); /* dummy read */
          rc = ASSUAN_No_Inquire_Callback;
        }
      else
        {
          rc = inquire_cb (inquire_cb_arg, line);
          if (!rc)
            rc = assuan_send_data (ctx, NULL, 0); /* flush and send END */
          if (!rc)
            goto again;
        }
    }
  else if (okay == 4)
    {
      if (status_cb)
        rc = status_cb (status_cb_arg, line);
      if (!rc)
        goto again;
    }
  else if (okay == 5)
    {
      if (!data_cb)
        rc = ASSUAN_No_Data_Callback;
      else 
        {
          rc = data_cb (data_cb_arg, NULL, 0);
          if (!rc)
            goto again;
        }
    }

  return rc;
}
Example #3
0
/**
 * assuan_transact:
 * @ctx: The Assuan context
 * @command: Command line to be send to the server
 * @data_cb: Callback function for data lines
 * @data_cb_arg: first argument passed to @data_cb
 * @inquire_cb: Callback function for a inquire response
 * @inquire_cb_arg: first argument passed to @inquire_cb
 * @status_cb: Callback function for a status response
 * @status_cb_arg: first argument passed to @status_cb
 * 
 * FIXME: Write documentation
 * 
 * Return value: 0 on success or error code.  The error code may be
 * the one one returned by the server in error lines or from the
 * callback functions.  Take care: When a callback returns an error
 * this function returns immediately with an error and thus the caller
 * will altter return an Assuan error (write erro in most cases).
 **/
assuan_error_t
assuan_transact (assuan_context_t ctx,
                 const char *command,
                 int (*data_cb)(void *, const void *, size_t),
                 void *data_cb_arg,
                 int (*inquire_cb)(void*, const char *),
                 void *inquire_cb_arg,
                 int (*status_cb)(void*, const char *),
                 void *status_cb_arg)
{
  assuan_error_t rc;
  int okay, off;
  char *line;
  int linelen;

  rc = assuan_write_line (ctx, command);
  if (rc)
    return rc;

  if (*command == '#' || !*command)
    return 0; /* Don't expect a response for a comment line.  */

 again:
  rc = _assuan_read_from_server (ctx, &okay, &off);
  if (rc)
    return rc; /* error reading from server */

  line = ctx->inbound.line + off;
  linelen = ctx->inbound.linelen - off;

  if (!okay)
    {
      rc = atoi (line);
      if (rc > 0 && rc < 100)
        rc = _assuan_error (ASSUAN_Server_Fault);
      else if (rc > 0 && rc <= 405)
        rc = _assuan_error (rc);
    }
  else if (okay == 2)
    {
      if (!data_cb)
        rc = _assuan_error (ASSUAN_No_Data_Callback);
      else 
        {
          char *s, *d;

          for (s=d=line; linelen; linelen--)
            {
              if (*s == '%' && linelen > 2)
                { /* handle escaping */
                  s++;
                  *d++ = xtoi_2 (s);
                  s += 2;
                  linelen -= 2;
                }
              else
                *d++ = *s++;
            }
          *d = 0; /* add a hidden string terminator */
          rc = data_cb (data_cb_arg, line, d - line);
          if (!rc)
            goto again;
        }
    }
  else if (okay == 3)
    {
      if (!inquire_cb)
        {
          assuan_write_line (ctx, "END"); /* get out of inquire mode */
          _assuan_read_from_server (ctx, &okay, &off); /* dummy read */
          rc = _assuan_error (ASSUAN_No_Inquire_Callback);
        }
      else
        {
          rc = inquire_cb (inquire_cb_arg, line);
          if (!rc)
            rc = assuan_send_data (ctx, NULL, 0); /* flush and send END */
          if (!rc)
            goto again;
        }
    }
  else if (okay == 4)
    {
      if (status_cb)
        rc = status_cb (status_cb_arg, line);
      if (!rc)
        goto again;
    }
  else if (okay == 5)
    {
      if (!data_cb)
        rc = _assuan_error (ASSUAN_No_Data_Callback);
      else 
        {
          rc = data_cb (data_cb_arg, NULL, 0);
          if (!rc)
            goto again;
        }
    }

  return rc;
}
Example #4
0
static void
pt_read_raw(FILE *in, void *context,
            void (*config_cb)(unsigned interval, double rec_int_secs,
                              unsigned wheel_sz_mm, void *context),
            void (*time_cb)(struct tm *time, time_t since_epoch, void *context),
            void (*data_cb)(double secs, double nm, double mph, double watts,
                            double miles, double alt, unsigned cad, unsigned hr,
                            unsigned interval, void *context),
            void (*error_cb)(const char *msg, void *context))
{
    unsigned interval = 0;
    unsigned last_interval = 0;
    unsigned wheel_sz_mm = 0;
    double rec_int_secs = 0.0;
    int i, n, row = 0;
    unsigned char buf[6];
    unsigned sbuf[6];
    double meters = 0.0;
    double secs = 0.0, start_secs = 0.0;
    double miles;
    double mph;
    double nm;
    double watts;
    double alt = 0;
    unsigned cad;
    unsigned hr;
    struct tm time;
    time_t since_epoch;
    char ebuf[256];
    bool bIsVer81 = false;

    while ((n = fscanf(in, "%x %x %x %x %x %x\n",
            sbuf, sbuf+1, sbuf+2, sbuf+3, sbuf+4, sbuf+5)) == 6) {
        ++row;
        for (i = 0; i < 6; ++i) {
            if (sbuf[i] > 0xff) { n = 1; break; }
            buf[i] = sbuf[i];
        }
        if (row == 1)
        {
            /* Serial number? */
            bIsVer81 = PowerTapUtil::is_Ver81(buf);
        }
        else if (PowerTapUtil::is_ignore_record(buf, bIsVer81)) {
            // do nothing
        }
        else if (PowerTapUtil::is_config(buf, bIsVer81)) {
            double new_rec_int_secs = 0.0;
            if (PowerTapUtil::unpack_config(buf, &interval, &last_interval,
                                        &new_rec_int_secs, &wheel_sz_mm, bIsVer81) < 0) {
                sprintf(ebuf, "Couldn't unpack config record.");
                if (error_cb) error_cb(ebuf, context);
                return;
            }
            if ((rec_int_secs != 0.0) && (rec_int_secs != new_rec_int_secs)) {
                sprintf(ebuf, "Ride has multiple recording intervals, which "
                        "is not yet supported.<br>(Recording interval changes "
                        "from %0.2f to %0.2f after %0.2f minutes of ride data.)\n",
                        rec_int_secs, new_rec_int_secs, secs / 60.0);
                if (error_cb) error_cb(ebuf, context);
                return;
            }
            rec_int_secs = new_rec_int_secs;
            if (config_cb) config_cb(interval, rec_int_secs, wheel_sz_mm, context);
        }
        else if (PowerTapUtil::is_time(buf, bIsVer81)) {
            since_epoch = PowerTapUtil::unpack_time(buf, &time, bIsVer81);
            bool ignore = false;
            if (start_secs == 0.0)
                start_secs = since_epoch;
            else if (since_epoch - start_secs > secs)
                secs = since_epoch - start_secs;
            else {
                sprintf(ebuf, "Warning: %0.3f minutes into the ride, "
                        "time jumps backwards by %0.3f minutes; ignoring it.",
                        secs / 60.0, (secs - since_epoch + start_secs) / 60.0);
                if (error_cb) error_cb(ebuf, context);
                ignore = true;
            }
            if (time_cb && !ignore) time_cb(&time, since_epoch, context);
        }
        else if (PowerTapUtil::is_data(buf, bIsVer81)) {
            if (wheel_sz_mm == 0) {
                sprintf(ebuf, "Read data row before wheel size set.");
                if (error_cb) error_cb(ebuf, context);
                return;
            }
            PowerTapUtil::unpack_data(buf, rec_int_secs, wheel_sz_mm, &secs,
                                      &nm, &mph, &watts, &meters, &cad, &hr, bIsVer81);
            miles = meters / 1000.0 * MILES_PER_KM;
            if (data_cb)
                data_cb(secs, nm, mph, watts, miles, alt, cad,
                        hr, interval, context);
        }
        else {
            sprintf(ebuf, "Unknown record type 0x%x on row %d.", buf[0], row);
            if (error_cb) error_cb(ebuf, context);
            return;
        }
    }
    if (n != -1) {
        sprintf(ebuf, "Parse error on row %d.", row);
        if (error_cb) error_cb(ebuf, context);
        return;
    }
}
void native_send_data_callback(int32_t msgType,
                              camera_memory_t * framebuffer,
                              void* user)
{
  ALOGE("Q%s: E", __func__);
  static unsigned int counter = 0;
#if 0
  camera_device * device = (camera_device *)user;
  if(device) {
    camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
    if(camHal) {
      camera_data_callback data_cb = camHal->data_cb;
      void *user_data = camHal->user_data;
      if(data_cb) {
        q_cam_memory_t *qmem = (q_cam_memory_t *)malloc(sizeof(q_cam_memory_t));
        if (qmem) {
          qmem->dataPtr = dataPtr;
          qmem->mem.data = (void *)((int)dataPtr->pointer() + dataPtr->offset());
          qmem->mem.handle = NULL; //(void *)dataPtr->getHeapID();
          qmem->mem.size = dataPtr->size( );
          qmem->mem.release = camera_release_memory;
          qmem->msgType = msgType;
          qmem->index = counter;
#endif
          data_cb(msgType, framebuffer, counter, NULL, user);
          counter++;
#if 0
        } else {
          ALOGE("%s: out of memory", __func__);
        }
#endif
//      }
//    }
//  }
}
#endif

static void cam_data_callback(int32_t msgType,
                              const sp<IMemory>& dataPtr,
                              void* user)
{
  ALOGV("Q%s: E", __func__);
  static unsigned int counter = 0;
  camera_device * device = (camera_device *)user;
  if(device) {
    camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
    if(camHal) {
      camera_data_callback data_cb = camHal->data_cb;
      void *user_data = camHal->user_data;
      if(data_cb) {
        q_cam_memory_t *qmem = (q_cam_memory_t *)malloc(sizeof(q_cam_memory_t));
        if (qmem) {
          qmem->dataPtr = dataPtr;
          qmem->mem.data = (void *)((int)dataPtr->pointer() + dataPtr->offset());
          qmem->mem.handle = NULL; //(void *)dataPtr->getHeapID();
          qmem->mem.size = dataPtr->size( );
          qmem->mem.release = camera_release_memory;
          qmem->msgType = msgType;
          qmem->index = counter;
          counter++;
          data_cb(msgType, (camera_memory_t *)qmem, counter, NULL, user_data);
        } else {
          ALOGE("%s: out of memory", __func__);
        }
      }
    }
  }
}

static void cam_data_callback_timestamp(nsecs_t timestamp,
                                        int32_t msgType,
                                        const sp<IMemory>& dataPtr,
                                        void* user)
{
  ALOGV("Q%s: E", __func__);

  static unsigned int counter = 0;
  camera_device * device = (camera_device *)user;
  if(device) {
    camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
    if(camHal) {
      camera_data_timestamp_callback data_cb_timestamp = camHal->data_cb_timestamp;
      void *user_data = camHal->user_data;
      if(data_cb_timestamp) {
        q_cam_memory_t *qmem = (q_cam_memory_t *)malloc(sizeof(q_cam_memory_t));
        if (qmem) {
          qmem->dataPtr = dataPtr;
          qmem->mem.data = (void *)((int)dataPtr->pointer() + dataPtr->offset());
          qmem->mem.handle = NULL; //(void *)dataPtr->getHeapID();
          qmem->mem.size = dataPtr->size( );
          qmem->mem.release = camera_release_memory;
          qmem->msgType = msgType;
          qmem->index = counter;
          counter++;
          data_cb_timestamp(timestamp, msgType, (camera_memory_t *)qmem, counter, user_data);
        } else {
          ALOGE("%s: out of memory", __func__);
        }
      }
    }
  }
}

QualcommCameraHardware * util_get_Hal_obj( struct camera_device * device)
{
  QualcommCameraHardware* hardware = NULL;
  if(device && device->priv){
      camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
      hardware = camHal->hardware;
  }
  return hardware;
}