]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/rdtech-tc/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / rdtech-tc / protocol.c
index 649489feeb0da6e0fa106e9035f23ab9f6150661..291604a0c524e6887381d77d9a837263751793c2 100644 (file)
@@ -65,7 +65,7 @@
 
 #define OFF_SERIAL 12
 
-static const uint8_t AES_KEY[] = {
+static const uint8_t aes_key[] = {
        0x58, 0x21, 0xfa, 0x56, 0x01, 0xb2, 0xf0, 0x26,
        0x87, 0xff, 0x12, 0x04, 0x62, 0x2a, 0x4f, 0xb0,
        0x86, 0xf4, 0x02, 0x60, 0x81, 0x6f, 0x9a, 0x0b,
@@ -73,24 +73,24 @@ static const uint8_t AES_KEY[] = {
 };
 
 static const struct rdtech_tc_channel_desc rdtech_tc_channels[] = {
-       { "V",  {   0 + 48, BVT_LE_UINT32, 1, }, { 100, 1e6, }, 4, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
-       { "I",  {   0 + 52, BVT_LE_UINT32, 1, }, {  10, 1e6, }, 5, SR_MQ_CURRENT, SR_UNIT_AMPERE },
-       { "D+", {  64 + 32, BVT_LE_UINT32, 1, }, {  10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
-       { "D-", {  64 + 36, BVT_LE_UINT32, 1, }, {  10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
-       { "E0", {  64 + 12, BVT_LE_UINT32, 1, }, {   1, 1e3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
-       { "E1", {  64 + 20, BVT_LE_UINT32, 1, }, {   1, 1e3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
+       { "V",  {   0 + 48, BVT_LE_UINT32, }, { 100, 1e6, }, 4, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
+       { "I",  {   0 + 52, BVT_LE_UINT32, }, {  10, 1e6, }, 5, SR_MQ_CURRENT, SR_UNIT_AMPERE },
+       { "D+", {  64 + 32, BVT_LE_UINT32, }, {  10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
+       { "D-", {  64 + 36, BVT_LE_UINT32, }, {  10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
+       { "E0", {  64 + 12, BVT_LE_UINT32, }, {   1, 1e3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
+       { "E1", {  64 + 20, BVT_LE_UINT32, }, {   1, 1e3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
 };
 
 static gboolean check_pac_crc(uint8_t *data)
 {
-       uint16_t crc;
-       uint32_t crc_field;
+       uint16_t crc_calc;
+       uint32_t crc_recv;
 
-       crc = sr_crc16(SR_CRC16_DEFAULT_INIT, data, PAC_CRC_POS);
-       crc_field = read_u32le(&data[PAC_CRC_POS]);
-       if (crc != crc_field) {
+       crc_calc = sr_crc16(SR_CRC16_DEFAULT_INIT, data, PAC_CRC_POS);
+       crc_recv = read_u32le(&data[PAC_CRC_POS]);
+       if (crc_calc != crc_recv) {
                sr_spew("CRC error. Calculated: %0x" PRIx16 ", expected: %0x" PRIx32,
-                       crc, crc_field);
+                       crc_calc, crc_recv);
                return FALSE;
        }
 
@@ -102,7 +102,7 @@ static int process_poll_pkt(struct dev_context *devc, uint8_t *dst)
        struct aes256_ctx ctx;
        gboolean ok;
 
-       aes256_set_decrypt_key(&ctx, AES_KEY);
+       aes256_set_decrypt_key(&ctx, aes_key);
        aes256_decrypt(&ctx, TC_POLL_LEN, dst, devc->buf);
 
        ok = TRUE;
@@ -202,10 +202,17 @@ SR_PRIV int rdtech_tc_poll(const struct sr_dev_inst *sdi, gboolean force)
 
        /*
         * Don't send the request while receive data is being accumulated.
+        * Defer request transmission when a previous request has not yet
+        * seen any response data at all (more probable to happen shortly
+        * after connecting to the peripheral).
         */
        devc = sdi->priv;
-       if (!force && devc->rdlen)
-               return SR_OK;
+       if (!force) {
+               if (devc->rdlen)
+                       return SR_OK;
+               if (!devc->rx_after_tx)
+                       return SR_OK;
+       }
 
        /*
         * Send the request when the transmit interval was reached. Or
@@ -228,6 +235,7 @@ SR_PRIV int rdtech_tc_poll(const struct sr_dev_inst *sdi, gboolean force)
                return SR_ERR;
        }
        devc->cmd_sent_at = now;
+       devc->rx_after_tx = 0;
 
        return SR_OK;
 }
@@ -257,10 +265,10 @@ static int handle_poll_data(struct sr_dev_inst *sdi)
        std_session_send_df_frame_begin(sdi);
        for (ch_idx = 0; ch_idx < devc->channel_count; ch_idx++) {
                pch = &devc->channels[ch_idx];
-               ret = bv_get_value(&v, &pch->spec, poll_pkt, TC_POLL_LEN);
+               ret = bv_get_value_len(&v, &pch->spec, poll_pkt, TC_POLL_LEN);
                if (ret != SR_OK)
                        break;
-               ret = feed_queue_analog_submit(devc->feeds[ch_idx], v, 1);
+               ret = feed_queue_analog_submit_one(devc->feeds[ch_idx], v, 1);
                if (ret != SR_OK)
                        break;
        }
@@ -291,6 +299,7 @@ static int recv_poll_data(struct sr_dev_inst *sdi, struct sr_serial_dev_inst *se
                if (len == 0)
                        return SR_OK;
                devc->rdlen += len;
+               devc->rx_after_tx += len;
        }
 
        /*