void    Btowngtw::receiveCMD(QString value, QString key) {
    qDebug() << m_gtwID << " Receive command from Gateway: " << value;
    PlantMessage myMsg;
    SysError theSysError;
    if(OpenMsg::parse(value, myMsg)) {

        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(stopTimer()));
        timer->setSingleShot(true);
        int msgID = qrand() % ((MAX_TIMER_ID + 1) - 0) + 0;
        m_timeoutMap.insert(msgID, key);
        timer->setProperty(TIMER_ID.toStdString().c_str(), msgID);
        timer->setProperty(TYPE.toStdString().c_str(), sender()->property(TYPE.toStdString().c_str()).toString());
        timer->setProperty(FRAME_TYPE.toStdString().c_str(), OpenMsg::getFrameType(value));
        qDebug() << " timer getProperty: " << sender()->property(TYPE.toStdString().c_str()).toString() << " " << timer->property(TYPE.toStdString().c_str()).toString();
        timer->start(TIMEOUT);

        qDebug() << m_gtwID << " Parsing ok, emit sendCommand Signal";
        myMsg.setSourceAddr(QPair<QString, QString>(PlantMessage::EXTERNAL, key), true);
        myMsg.setDestAddr(QPair<QString, QString>(PlantMessage::INTERNAL, PlantMessage::DEVICE), true);
        myMsg.setType(sender()->property(TYPE.toStdString().c_str()).toString());
        myMsg.setId(msgID);
        emit ReleaseMessage(myMsg, theSysError);
    }
    else {
        qDebug() << m_gtwID << " Receive command from Gateway is WRONG FORMAT";
    }
}
Пример #2
0
void wmdf::ReplayFile::ForwardOne()
{
  if ((NULL != curr_message_) && (client_callback_ != NULL))
  {
    client_callback_->OnRecvMsg(curr_message_);
    ReleaseMessage(curr_message_);
    curr_message_ = NULL;
  }
  is_first_ = false;
}
Пример #3
0
static void NScale_tick(Instance *pi)
{
  Handler_message *hm;

  hm = GetData(pi, 1);
  if (hm) {
    hm->handler(pi, hm->data);
    ReleaseMessage(&hm,pi);
  }

  pi->counter++;
}
Пример #4
0
static void RGB3Source_tick(Instance *pi)
{
  RGB3Source_private *priv = (RGB3Source_private *)pi;
  Handler_message *hm;
  int wait_flag = (priv->enable ? 0 : 1);

  hm = GetData(pi, wait_flag);
  if (hm) {
    hm->handler(pi, hm->data);
    ReleaseMessage(&hm,pi);
  }

  if (!priv->enable) {
    return;
  }

  generate_one_frame(pi);

  int sec = priv->period_ms/1000;
  long nsec = (priv->period_ms % 1000) * 1000000;
  nanosleep(&(struct timespec) { .tv_sec = sec, .tv_nsec = nsec}, NULL);
Пример #5
0
static void ALSACapture_tick(Instance *pi)
{
  ALSAio_private *priv = (ALSAio_private *)pi;
  int wait_flag;

  if (!priv->c.enable || !priv->c.handle) {
    wait_flag = 1;
  }
  else {
    wait_flag = 0;
  }

  Handler_message *hm;

  hm = GetData(pi, wait_flag);
  if (hm) {
    hm->handler(pi, hm->data);
    ReleaseMessage(&hm,pi);
  }

  if (!priv->c.enable || !priv->c.handle) {
    /* Not enabled or no handle, don't try to capture anything. */
    return;
  }

  /* Read a block of data. */
  int rc;
  snd_pcm_sframes_t n;
  int state;
  snd_pcm_uframes_t frames = priv->c.frames_per_io;
  int dir = 0;

  state = snd_pcm_state(priv->c.handle);

  dpf("%s: state(1)=%s\n", __func__, ALSAio_state_to_string(state));

  if (state == SND_PCM_STATE_OPEN || state == SND_PCM_STATE_SETUP) {
    /* One time capture setup. */
    fprintf(stderr, "%s: state=%s\n", __func__, ALSAio_state_to_string(state));

    rc = snd_pcm_hw_params_set_period_size_near(priv->c.handle, priv->c.hwparams, &frames, &dir);
    fprintf(stderr, "%s: set_period_size_near returns %d (frames %d:%d)\n",
            __func__, rc, (int)priv->c.frames_per_io, (int)frames);

    rc = snd_pcm_hw_params(priv->c.handle, priv->c.hwparams);
    if (rc < 0) {
      fprintf(stderr, "*** snd_pcm_hw_params %s: %s\n", s(priv->c.device), snd_strerror(rc));
    }

    state = snd_pcm_state(priv->c.handle);
    fprintf(stderr, "%s: state=%s\n", __func__, ALSAio_state_to_string(state));

    rc = snd_pcm_prepare(priv->c.handle);
    state = snd_pcm_state(priv->c.handle);
    fprintf(stderr, "%s: state=%s\n", __func__, ALSAio_state_to_string(state));

    /* Initialize running_timestamp. */
    cti_getdoubletime(&priv->c.running_timestamp);
  }

  snd_pcm_hw_params_get_period_size(priv->c.hwparams, &frames, &dir);
  int size = frames * priv->c.format_bytes * priv->c.channels;

  if (!size) {
    // This can happen if channels or format_bytes was not set...
    fprintf(stderr, "%s: size error - %ld * %d * %d\n", __func__, frames , priv->c.format_bytes , priv->c.channels);
    while (1) {
      sleep(1);
    }
  }

  if (!priv->c.rate) {
    fprintf(stderr, "%s: error - rate is 0!\n", __func__);
    while (1) {
      sleep(1);
    }
  }

  /* Allocate buffer for PCM samples. */
  uint8_t *buffer = Mem_malloc(size+1);
  buffer[size] = 0x55;

  //int val = hwparams->rate;
  //snd_pcm_hw_params_get_period_time(params, &val, &dir);

  /* Read the data. */
  n = snd_pcm_readi(priv->c.handle, buffer, frames);

  if (buffer[size] != 0x55)  {
    fprintf(stderr, "*** overwrote audio buffer!\n");
  }

  if (n != frames) {
    fprintf(stderr, "*** snd_pcm_readi %s: %s\n", s(priv->c.device), snd_strerror((int)n));
    fprintf(stderr, "*** attempting snd_pcm_prepare() to correct...\n");
    snd_pcm_prepare(priv->c.handle);
    goto out;
  }

  // fprintf(stderr, "*** read %d frames\n", (int) n);

  double calculated_period = frames*1.0/(priv->c.rate);

  priv->c.running_timestamp += calculated_period;

  double tnow;
  cti_getdoubletime(&tnow);

  /* Do coarse adjustment if necessary, this can happen after a
     system date change via ntp or htpdate. */
  if (fabs(priv->c.running_timestamp - tnow) > 5.0) {
    fprintf(stderr, "coarse timestamp adjustment, %.3f -> %.3f\n",
            priv->c.running_timestamp, tnow);
    priv->c.running_timestamp = tnow;
  }

  /* Adjust running timestamp if it slips too far either way.  Smoothing, I guess. */
  if (priv->c.running_timestamp - tnow > calculated_period) {
    dpf("priv->c.rate=%d,  %.3f - %.3f (%.5f) > %.5f : - running timestamp\n",
        priv->c.rate,
        priv->c.running_timestamp, tnow,
        (tnow - priv->c.running_timestamp),
        calculated_period);
    priv->c.running_timestamp -= (calculated_period/2.0);
  }
  else if (tnow - priv->c.running_timestamp > calculated_period) {
    dpf("priv->c.rate=%d, %.3f - %.3f (%.5f) > %.5f : + running timestamp\n",
        priv->c.rate,
        tnow, priv->c.running_timestamp,
        (tnow - priv->c.running_timestamp),
        calculated_period);
    priv->c.running_timestamp += (calculated_period/2.0);
  }

  int buffer_bytes = n * priv->c.format_bytes * priv->c.channels;

  if (pi->outputs[OUTPUT_AUDIO].destination) {
    Audio_buffer * audio = Audio_buffer_new(priv->c.rate,
                                            priv->c.channels, priv->c.atype);
    Audio_buffer_add_samples(audio, buffer, buffer_bytes);
    audio->timestamp = priv->c.running_timestamp;
    //fprintf(stderr, "posting audio buffer %d bytes\n", buffer_bytes);
    PostData(audio, pi->outputs[OUTPUT_AUDIO].destination);
    /* TESTING: disable after posting once */
    //pi->outputs[OUTPUT_AUDIO].destination = NULL;
  }

  if (pi->outputs[OUTPUT_WAV].destination) {
    Wav_buffer *wav = Wav_buffer_new(priv->c.rate, priv->c.channels, priv->c.format_bytes);
    wav->timestamp = priv->c.running_timestamp;

    dpf("%s allocated wav @ %p\n", __func__, wav);
    wav->data = buffer;  buffer = 0L;   /* Assign, do not free below. */
    wav->data_length = buffer_bytes;
    Wav_buffer_finalize(wav);

    if (priv->c.analyze) {
      analyze_rate(priv, wav);
    }

    PostData(wav, pi->outputs[OUTPUT_WAV].destination);
    static int x = 0;
    wav->seq = x++;
    wav = 0L;
  }

 out:
  if (buffer) {
    Mem_free(buffer);
  }
}
Пример #6
0
static void ALSAPlayback_tick(Instance *pi)
{
  ALSAio_private *priv = (ALSAio_private *)pi;
  // int wait_flag = (priv->c.enable ? 0 : 1);
  int wait_flag;

  if (!priv->c.enable
      || !priv->c.rate) {
    /* Not enabled, or rate not set, wait for an enable message or a
       Wav buffer.  This avoid spinning below. */
    wait_flag = 1;
  }
  else {
    /* Enabled and rate set, will generate filler if needed. */
    wait_flag = 0;
  }

  Handler_message *hm;

  hm = GetData(pi, wait_flag);
  // fprintf(stderr, "%s hm=%p\n", __func__, hm);
  if (hm) {
    hm->handler(pi, hm->data);
    ReleaseMessage(&hm,pi);
    while (pi->pending_messages > 8) {
      /* This happens when capture clock is faster than playback
         clock.  In cx88play.cmd, it drops a block every ~2s. */
      hm = GetData(pi, wait_flag);
      if (hm->handler == Wav_handler) {
        Wav_buffer *wav_in = hm->data;
        double s = (1.0*wav_in->data_length)/
          (priv->c.rate * priv->c.channels * priv->c.format_bytes);
        priv->c.total_dropped += s;
        fprintf(stderr, "dropping %.4fs of audio (%d %d %d) total %.4f\n",
               s, priv->c.rate, priv->c.channels, priv->c.format_bytes, priv->c.total_dropped);
        Wav_buffer_release(&wav_in);
      }
      else {
        /* Should always handle config messages... */
        hm->handler(pi, hm->data);
      }
      ReleaseMessage(&hm,pi);
    }
  }
  else if (priv->c.enable && priv->c.rate) {
    /* Filler... */
    Wav_buffer *wav_in;
    int i;
    //fprintf(stderr, "filler\n");
    wav_in = Wav_buffer_new(priv->c.rate, priv->c.channels, priv->c.format_bytes);
    wav_in->data_length = 16*priv->c.channels*priv->c.format_bytes;
    wav_in->data = Mem_calloc(1, wav_in->data_length);
    if (access("/dev/shm/a1", R_OK) == 0) {
      /* Blip. */
      for (i=0; i < wav_in->data_length; i+=2) {
        //wav_in->data[i] = 0;
        //wav_in->data[i+1] = (2048 - i);
      }
      unlink("/dev/shm/a1");
    }
    Wav_buffer_finalize(wav_in);
    /* Avoid sending feedback since this was generated locally. */
    wav_in->no_feedback = 1;
    Wav_handler(pi, wav_in);
  }
  else {
    /* Should never get here. */
    fprintf(stderr, "WTF?\n");
    sleep(1);
  }


}