0,
};
+SR_PRIV struct sr_dev_driver mic_98581_driver_info;
SR_PRIV struct sr_dev_driver mic_98583_driver_info;
SR_PRIV const struct mic_dev_info mic_devs[] = {
{
- "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE,
+ "MIC", "98581", "38400/8n2", 32000, TRUE, FALSE, 6,
+ packet_valid_temp,
+ &mic_98581_driver_info, receive_data_MIC_98581,
+ },
+ {
+ "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE, 10,
+ packet_valid_temp_hum,
&mic_98583_driver_info, receive_data_MIC_98583,
},
};
goto scan_cleanup;
sdi->probes = g_slist_append(sdi->probes, probe);
- if (!(probe = sr_probe_new(1, SR_PROBE_ANALOG, TRUE, "Humidity")))
- goto scan_cleanup;
- sdi->probes = g_slist_append(sdi->probes, probe);
+ if (mic_devs[idx].has_humidity) {
+ if (!(probe = sr_probe_new(1, SR_PROBE_ANALOG, TRUE, "Humidity")))
+ goto scan_cleanup;
+ sdi->probes = g_slist_append(sdi->probes, probe);
+ }
drvc->instances = g_slist_append(drvc->instances, sdi);
devices = g_slist_append(devices, sdi);
.priv = NULL, \
};
+DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581")
DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583")
#include "protocol.h"
-#define PACKET_SIZE 10
-
static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
{
int ret;
return mic_send(serial, "S 1 M 2 32 3\r");
}
-static gboolean packet_valid(const uint8_t *buf)
+SR_PRIV gboolean packet_valid_temp(const uint8_t *buf)
+{
+ if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != '\r')
+ return FALSE;
+
+ if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
+ return FALSE;
+
+ return TRUE;
+}
+
+SR_PRIV gboolean packet_valid_temp_hum(const uint8_t *buf)
{
if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != ' ' || buf[9] != '\r')
return FALSE;
return TRUE;
}
-static int packet_parse(const char *buf, float *temp, float *humidity)
+static int packet_parse(const char *buf, int idx, float *temp, float *humidity)
{
char tmp[4];
- /* Packet format: "v ttt hhh\r". */
+ /* Packet format MIC98581: "v ttt\r". */
+ /* Packet format MIC98583: "v ttt hhh\r". */
/* TODO: Sanity check on buf. For now we assume well-formed ASCII. */
strncpy((char *)&tmp, &buf[2], 3);
*temp = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
- strncpy((char *)&tmp, &buf[6], 3);
- *humidity = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
+ if (mic_devs[idx].has_humidity) {
+ strncpy((char *)&tmp, &buf[6], 3);
+ *humidity = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
+ }
return SR_OK;
}
devc = sdi->priv;
- ret = packet_parse((const char *)buf, &temperature, &humidity);
+ ret = packet_parse((const char *)buf, idx, &temperature, &humidity);
if (ret < 0) {
sr_err("Failed to parse packet.");
return SR_ERR;
g_slist_free(l);
/* Humidity. */
- l = g_slist_copy(sdi->probes);
- l = g_slist_remove_link(l, g_slist_nth(l, 0));
- analog.probes = l;
- analog.mq = SR_MQ_RELATIVE_HUMIDITY;
- analog.unit = SR_UNIT_PERCENTAGE;
- analog.data = &humidity;
- sr_session_send(devc->cb_data, &packet);
- g_slist_free(l);
+ if (mic_devs[idx].has_humidity) {
+ l = g_slist_copy(sdi->probes);
+ l = g_slist_remove_link(l, g_slist_nth(l, 0));
+ analog.probes = l;
+ analog.mq = SR_MQ_RELATIVE_HUMIDITY;
+ analog.unit = SR_UNIT_PERCENTAGE;
+ analog.data = &humidity;
+ sr_session_send(devc->cb_data, &packet);
+ g_slist_free(l);
+ }
devc->num_samples++;
devc->buflen += len;
/* Now look for packets in that data. */
- while ((devc->buflen - offset) >= PACKET_SIZE) {
- if (packet_valid(devc->buf + offset)) {
+ while ((devc->buflen - offset) >= mic_devs[idx].packet_size) {
+ if (mic_devs[idx].packet_valid(devc->buf + offset)) {
handle_packet(devc->buf + offset, sdi, idx);
- offset += PACKET_SIZE;
+ offset += mic_devs[idx].packet_size;
} else {
offset++;
}
return receive_data(fd, revents, ID_UPPER, cb_data); }
/* Driver-specific receive_data() wrappers */
+RECEIVE_DATA(MIC_98581)
RECEIVE_DATA(MIC_98583)
/* Note: When adding entries here, don't forget to update MIC_DEV_COUNT. */
enum {
+ MIC_98581,
MIC_98583,
};
-#define MIC_DEV_COUNT 1
+#define MIC_DEV_COUNT 2
struct mic_dev_info {
char *vendor;
uint32_t max_sample_points;
gboolean has_temperature;
gboolean has_humidity;
+ uint8_t packet_size;
+ gboolean (*packet_valid)(const uint8_t *);
struct sr_dev_driver *di;
int (*receive_data)(int, int, void *);
};
int buflen;
};
+SR_PRIV gboolean packet_valid_temp(const uint8_t *buf);
+SR_PRIV gboolean packet_valid_temp_hum(const uint8_t *buf);
+
+SR_PRIV int receive_data_MIC_98581(int fd, int revents, void *cb_data);
SR_PRIV int receive_data_MIC_98583(int fd, int revents, void *cb_data);
SR_PRIV int mic_cmd_get_device_info(struct sr_serial_dev_inst *serial);