X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fhameg-hmo%2Fprotocol.c;h=d014eafe9cec9ac2ae1ad6cf3e83232fdf7f3695;hb=dafafb0e9429fcc9185df1c861184ce79defb7ee;hp=cd94f0ca1bb9393fec76f0065bf0805a9c914cd6;hpb=13f2b9d789fdc7130c0f31b88319bd0f3445109a;p=libsigrok.git diff --git a/hardware/hameg-hmo/protocol.c b/hardware/hameg-hmo/protocol.c index cd94f0ca..d014eafe 100644 --- a/hardware/hameg-hmo/protocol.c +++ b/hardware/hameg-hmo/protocol.c @@ -19,10 +19,6 @@ #include "protocol.h" -static const char *manufacturers[] = { - "HAMEG", -}; - static const char *hameg_scpi_dialect[] = { [SCPI_CMD_GET_DIG_DATA] = ":POD%d:DATA?", [SCPI_CMD_GET_TIMEBASE] = ":TIM:SCAL?", @@ -256,24 +252,13 @@ static struct scope_config scope_models[] = { }, }; -static int check_manufacturer(const char *manufacturer) -{ - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(manufacturers); ++i) - if (strcmp(manufacturer, manufacturers[i]) == 0) - return SR_OK; - - return SR_ERR; -} - static void scope_state_dump(struct scope_config *config, struct scope_state *state) { unsigned int i; for (i = 0; i < config->analog_channels; ++i) { - sr_info("State of analog channel %d -> %s : %s %.3eV %.3e offset", i+1, + sr_info("State of analog channel %d -> %s : %s %.3eV %.3e offset", i + 1, state->analog_channels[i].state ? "On" : "Off", (*config->coupling_options)[state->analog_channels[i].coupling], state->analog_channels[i].vdiv, state->analog_channels[i].vertical_offset); @@ -296,16 +281,14 @@ static void scope_state_dump(struct scope_config *config, state->horiz_triggerpos); } -static int scope_state_get_array_option(struct sr_serial_dev_inst *serial, - const char *command, const char *(*array)[], - int *result) +static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi, + const char *command, const char *(*array)[], int *result) { char *tmp; unsigned int i; - if (sr_scpi_get_string(serial, command, &tmp) != SR_OK) { - if (tmp) - g_free(tmp); + if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) { + g_free(tmp); return SR_ERR; } @@ -326,7 +309,7 @@ static int scope_state_get_array_option(struct sr_serial_dev_inst *serial, return SR_OK; } -static int analog_channel_state_get(struct sr_serial_dev_inst *serial, +static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi, struct scope_config *config, struct scope_state *state) { @@ -338,7 +321,7 @@ static int analog_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE], i + 1); - if (sr_scpi_get_bool(serial, command, + if (sr_scpi_get_bool(scpi, command, &state->analog_channels[i].state) != SR_OK) return SR_ERR; @@ -346,7 +329,7 @@ static int analog_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV], i + 1); - if (sr_scpi_get_float(serial, command, + if (sr_scpi_get_float(scpi, command, &state->analog_channels[i].vdiv) != SR_OK) return SR_ERR; @@ -354,7 +337,7 @@ static int analog_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET], i + 1); - if (sr_scpi_get_float(serial, command, + if (sr_scpi_get_float(scpi, command, &state->analog_channels[i].vertical_offset) != SR_OK) return SR_ERR; @@ -362,7 +345,7 @@ static int analog_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING], i + 1); - if (scope_state_get_array_option(serial, command, config->coupling_options, + if (scope_state_get_array_option(scpi, command, config->coupling_options, &state->analog_channels[i].coupling) != SR_OK) return SR_ERR; } @@ -370,7 +353,7 @@ static int analog_channel_state_get(struct sr_serial_dev_inst *serial, return SR_OK; } -static int digital_channel_state_get(struct sr_serial_dev_inst *serial, +static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi, struct scope_config *config, struct scope_state *state) { @@ -382,7 +365,7 @@ static int digital_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE], i); - if (sr_scpi_get_bool(serial, command, + if (sr_scpi_get_bool(scpi, command, &state->digital_channels[i]) != SR_OK) return SR_ERR; } @@ -392,7 +375,7 @@ static int digital_channel_state_get(struct sr_serial_dev_inst *serial, (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE], i + 1); - if (sr_scpi_get_bool(serial, command, + if (sr_scpi_get_bool(scpi, command, &state->digital_pods[i]) != SR_OK) return SR_ERR; } @@ -400,7 +383,7 @@ static int digital_channel_state_get(struct sr_serial_dev_inst *serial, return SR_OK; } -SR_PRIV int scope_state_get(struct sr_dev_inst *sdi) +SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi) { struct dev_context *devc; struct scope_state *state; @@ -416,21 +399,25 @@ SR_PRIV int scope_state_get(struct sr_dev_inst *sdi) if (digital_channel_state_get(sdi->conn, config, state) != SR_OK) return SR_ERR; - /* TODO check if value is sensible */ - if (sr_scpi_get_float(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE], - &state->timebase) != SR_OK) + /* TODO: Check if value is sensible. */ + if (sr_scpi_get_float(sdi->conn, + (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE], + &state->timebase) != SR_OK) return SR_ERR; - if (sr_scpi_get_float(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS], - &state->horiz_triggerpos) != SR_OK) + if (sr_scpi_get_float(sdi->conn, + (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS], + &state->horiz_triggerpos) != SR_OK) return SR_ERR; - if (scope_state_get_array_option(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE], - config->trigger_sources, &state->trigger_source) != SR_OK) + if (scope_state_get_array_option(sdi->conn, + (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE], + config->trigger_sources, &state->trigger_source) != SR_OK) return SR_ERR; - if (scope_state_get_array_option(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE], - config->trigger_slopes, &state->trigger_slope) != SR_OK) + if (scope_state_get_array_option(sdi->conn, + (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE], + config->trigger_slopes, &state->trigger_slope) != SR_OK) return SR_ERR; scope_state_dump(config, state); @@ -446,15 +433,15 @@ SR_PRIV struct scope_state *scope_state_new(struct scope_config *config) return NULL; if (!(state->analog_channels = g_try_malloc0_n(config->analog_channels, - sizeof(struct analog_channel_state)))) + sizeof(struct analog_channel_state)))) goto fail; - if (!(state->digital_channels = g_try_malloc0_n(config->digital_channels, - sizeof(gboolean)))) + if (!(state->digital_channels = g_try_malloc0_n( + config->digital_channels, sizeof(gboolean)))) goto fail; if (!(state->digital_pods = g_try_malloc0_n(config->digital_pods, - sizeof(gboolean)))) + sizeof(gboolean)))) goto fail; return state; @@ -471,7 +458,7 @@ fail: return NULL; } -SR_PRIV void scope_state_free(struct scope_state *state) +SR_PRIV void hmo_scope_state_free(struct scope_state *state) { g_free(state->analog_channels); g_free(state->digital_channels); @@ -483,16 +470,14 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi) { char tmp[25]; int model_index; - unsigned int i; - unsigned int j; - + unsigned int i, j; struct sr_probe *probe; struct dev_context *devc; devc = sdi->priv; model_index = -1; - /* Find the exact model */ + /* Find the exact model. */ for (i = 0; i < ARRAY_SIZE(scope_models); i++) { for (j = 0; scope_models[i].name[j]; j++) { if (!strcmp(sdi->model, scope_models[i].name[j])) { @@ -505,7 +490,7 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi) } if (model_index == -1) { - sr_dbg("Unsupported HMO device"); + sr_dbg("Unsupported HMO device."); return SR_ERR_NA; } @@ -517,37 +502,38 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi) scope_models[model_index].digital_pods))) return SR_ERR_MALLOC; - /* Add analog channels */ + /* Add analog channels. */ for (i = 0; i < scope_models[model_index].analog_channels; i++) { if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, - (*scope_models[model_index].analog_names)[i]))) + (*scope_models[model_index].analog_names)[i]))) return SR_ERR_MALLOC; sdi->probes = g_slist_append(sdi->probes, probe); - devc->analog_groups[i].name = (char *) (*scope_models[model_index].analog_names)[i]; + devc->analog_groups[i].name = + (char *)(*scope_models[model_index].analog_names)[i]; devc->analog_groups[i].probes = g_slist_append(NULL, probe); sdi->probe_groups = g_slist_append(sdi->probe_groups, &devc->analog_groups[i]); } - /* Add digital probe groups */ + /* Add digital probe groups. */ for (i = 0; i < scope_models[model_index].digital_pods; ++i) { g_snprintf(tmp, 25, "POD%d", i); devc->digital_groups[i].name = g_strdup(tmp); sdi->probe_groups = g_slist_append(sdi->probe_groups, - &devc->digital_groups[i < 8 ? 0 : 1]); + &devc->digital_groups[i < 8 ? 0 : 1]); } - /* Add digital channels */ + /* Add digital channels. */ for (i = 0; i < scope_models[model_index].digital_channels; i++) { if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, - (*scope_models[model_index].digital_names)[i]))) + (*scope_models[model_index].digital_names)[i]))) return SR_ERR_MALLOC; sdi->probes = g_slist_append(sdi->probes, probe); - devc->digital_groups[i < 8 ? 0 : 1].probes = g_slist_append(devc->digital_groups[i < 8 ? 0 : 1].probes, - probe); + devc->digital_groups[i < 8 ? 0 : 1].probes = g_slist_append( + devc->digital_groups[i < 8 ? 0 : 1].probes, probe); } devc->model_config = &scope_models[model_index]; @@ -559,74 +545,15 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi) return SR_OK; } -SR_PRIV struct sr_dev_inst *hameg_probe_serial_device(const char *serial_device, - const char *serial_options) -{ - struct sr_dev_inst *sdi; - struct dev_context *devc; - struct sr_scpi_hw_info *hw_info; - struct sr_serial_dev_inst *serial; - - sdi = NULL; - devc = NULL; - serial = NULL; - hw_info = NULL; - - if (!(serial = sr_serial_dev_inst_new(serial_device, serial_options))) - goto fail; - - sr_info("Probing %s.", serial_device); - if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) - goto fail; - - if (sr_scpi_get_hw_id(serial, &hw_info) != SR_OK) { - sr_info("Couldn't get IDN response"); - goto fail; - } - - if (check_manufacturer(hw_info->manufacturer) != SR_OK) - goto fail; - - if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, - hw_info->manufacturer, hw_info->model, - hw_info->firmware_version))) { - goto fail; - } - sr_scpi_hw_info_free(hw_info); - hw_info = NULL; - - if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) - goto fail; - - sdi->driver = di; - sdi->priv = devc; - sdi->inst_type = SR_INST_SERIAL; - sdi->conn = serial; - - if (hmo_init_device(sdi) != SR_OK) - goto fail; - - return sdi; - -fail: - if (hw_info) - sr_scpi_hw_info_free(hw_info); - if (serial) - sr_serial_dev_inst_free(serial); - if (sdi) - sr_dev_inst_free(sdi); - if (devc) - g_free(devc); - - return NULL; -} - -SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data) +SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data) { struct sr_probe *probe; struct sr_dev_inst *sdi; struct dev_context *devc; struct sr_datafeed_packet packet; + GArray *data; + struct sr_datafeed_analog analog; + struct sr_datafeed_logic logic; (void)fd; @@ -641,10 +568,6 @@ SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data) switch (probe->type) { case SR_PROBE_ANALOG: - { - GArray *data; - struct sr_datafeed_analog analog; - if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) { if (data) g_array_free(data, TRUE); @@ -666,14 +589,8 @@ SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data) sr_session_send(cb_data, &packet); g_slist_free(analog.probes); g_array_free(data, TRUE); - } - break; - + break; case SR_PROBE_LOGIC: - { - GArray *data; - struct sr_datafeed_logic logic; - if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) { if (data) g_free(data); @@ -690,11 +607,9 @@ SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data) packet.payload = &logic; sr_session_send(cb_data, &packet); g_array_free(data, TRUE); - } - break; - + break; default: - sr_err("Invalid probe type"); + sr_err("Invalid probe type."); break; }