]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/cem-dt-885x/protocol.c
Prefer postfix-increment for consistency across the code-base.
[libsigrok.git] / src / hardware / cem-dt-885x / protocol.c
index 19e083062c69331230589be10ab7e6ec34bab4e2..68d003a0d0243d50c05cb58169b5ba9fe180722d 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <string.h>
 #include "protocol.h"
 
 /* Length of expected payload for each token. */
-static int token_payloads[][2] = {
+static const int token_payloads[][2] = {
        { TOKEN_WEIGHT_TIME_FAST, 0 },
        { TOKEN_WEIGHT_TIME_SLOW, 0 },
        { TOKEN_HOLD_MAX, 0 },
@@ -65,7 +66,7 @@ static void process_mset(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
-       struct sr_datafeed_analog analog;
+       struct sr_datafeed_analog_old analog;
        GString *dbg;
        float fvalue;
        int i;
@@ -84,7 +85,7 @@ static void process_mset(const struct sr_dev_inst *sdi)
                g_string_free(dbg, TRUE);
        }
 
-       switch(devc->token) {
+       switch (devc->token) {
        case TOKEN_WEIGHT_TIME_FAST:
                devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F;
                devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_S;
@@ -130,14 +131,14 @@ static void process_mset(const struct sr_dev_inst *sdi)
                                break;
                        }
                }
-               memset(&analog, 0, sizeof(struct sr_datafeed_analog));
+               memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
                analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
                analog.mqflags = devc->cur_mqflags;
                analog.unit = SR_UNIT_DECIBEL_SPL;
                analog.channels = sdi->channels;
                analog.num_samples = 1;
                analog.data = &devc->last_spl;
-               packet.type = SR_DF_ANALOG;
+               packet.type = SR_DF_ANALOG_OLD;
                packet.payload = &analog;
                sr_session_send(devc->cb_data, &packet);
 
@@ -166,7 +167,7 @@ static void process_mset(const struct sr_dev_inst *sdi)
        case TOKEN_MEAS_RANGE_OK:
        case TOKEN_MEAS_RANGE_OVER:
        case TOKEN_MEAS_RANGE_UNDER:
-               /* Not useful, or not expressable in sigrok. */
+               /* Not useful, or not expressible in sigrok. */
                break;
        }
 
@@ -177,26 +178,26 @@ static void send_data(const struct sr_dev_inst *sdi, unsigned char *data,
 {
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
-       struct sr_datafeed_analog analog;
+       struct sr_datafeed_analog_old analog;
        float fbuf[SAMPLES_PER_PACKET];
        unsigned int i;
 
        devc = sdi->priv;
 
-       for (i = 0; i < num_samples; i ++) {
+       for (i = 0; i < num_samples; i++) {
                fbuf[i] = ((data[i * 2] & 0xf0) >> 4) * 100;
                fbuf[i] += (data[i * 2] & 0x0f) * 10;
                fbuf[i] += ((data[i * 2 + 1] & 0xf0) >> 4);
                fbuf[i] += (data[i * 2 + 1] & 0x0f) / 10.0;
        }
-       memset(&analog, 0, sizeof(struct sr_datafeed_analog));
+       memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
        analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
        analog.mqflags = devc->cur_mqflags;
        analog.unit = SR_UNIT_DECIBEL_SPL;
        analog.channels = sdi->channels;
        analog.num_samples = num_samples;
        analog.data = fbuf;
-       packet.type = SR_DF_ANALOG;
+       packet.type = SR_DF_ANALOG_OLD;
        packet.payload = &analog;
        sr_session_send(devc->cb_data, &packet);
 
@@ -385,7 +386,7 @@ SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data)
        devc = sdi->priv;
        serial = sdi->conn;
        if (revents == G_IO_IN) {
-               if (serial_read(serial, &c, 1) != 1)
+               if (serial_read_nonblocking(serial, &c, 1) != 1)
                        return TRUE;
                process_byte(sdi, c, TRUE);
 
@@ -396,7 +397,7 @@ SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data)
                        } else {
                                /* Tell device to start transferring from memory. */
                                cmd = CMD_TRANSFER_MEMORY;
-                               serial_write(serial, &cmd, 1);
+                               serial_write_nonblocking(serial, &cmd, 1);
                        }
                }
        }
@@ -404,7 +405,6 @@ SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data)
        return TRUE;
 }
 
-
 static int wait_for_token(const struct sr_dev_inst *sdi, int8_t *tokens, int timeout)
 {
        struct dev_context *devc;
@@ -418,7 +418,7 @@ static int wait_for_token(const struct sr_dev_inst *sdi, int8_t *tokens, int tim
        devc->state = ST_INIT;
        start_time = g_get_monotonic_time() / 1000;
        while (TRUE) {
-               if (serial_read(serial, &c, 1) != 1)
+               if (serial_read_nonblocking(serial, &c, 1) != 1)
                        /* Device might have gone away. */
                        return SR_ERR;
                process_byte(sdi, c, FALSE);
@@ -453,7 +453,7 @@ static int cem_dt_885x_toggle(const struct sr_dev_inst *sdi, uint8_t cmd,
         * only thing to do is wait for the token that will confirm
         * whether the command worked or not, and resend if needed. */
        while (TRUE) {
-               if (serial_write(serial, (const void *)&cmd, 1) != 1)
+               if (serial_write_nonblocking(serial, (const void *)&cmd, 1) != 1)
                        return SR_ERR;
                if (wait_for_token(sdi, tokens, timeout) == SR_ERR)
                        return SR_ERR;
@@ -811,20 +811,14 @@ SR_PRIV int cem_dt_885x_power_off(const struct sr_dev_inst *sdi)
 
        serial = sdi->conn;
 
-       /* Reopen the port in non-blocking mode, so we can properly
-        * detect when the device stops communicating. */
-       serial_close(serial);
-       if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
-               return SR_ERR;
-
        cmd = CMD_TOGGLE_POWER_OFF;
        while (TRUE) {
                serial_flush(serial);
-               if (serial_write(serial, (const void *)&cmd, 1) != 1)
+               if (serial_write_nonblocking(serial, (const void *)&cmd, 1) != 1)
                        return SR_ERR;
                /* It never takes more than 23ms for the next token to arrive. */
                g_usleep(25 * 1000);
-               if (serial_read(serial, &c, 1) != 1)
+               if (serial_read_nonblocking(serial, &c, 1) != 1)
                        /* Device is no longer responding. Good! */
                        break;
        }