From: Frank Stettner Date: Tue, 9 Apr 2019 12:12:31 +0000 (+0200) Subject: session: Add helper sr_session_send_meta() to send SR_DF_META packages. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=7d1a4a52392a048c24ec25459f462a0126c9ad2c;p=libsigrok.git session: Add helper sr_session_send_meta() to send SR_DF_META packages. Replace individual functions in drivers for arachnid-labs-re-load-pro and lcr/es51919 with sr_session_send_meta() helper function. --- diff --git a/src/hardware/arachnid-labs-re-load-pro/protocol.c b/src/hardware/arachnid-labs-re-load-pro/protocol.c index 86e317a5..3d7a0146 100644 --- a/src/hardware/arachnid-labs-re-load-pro/protocol.c +++ b/src/hardware/arachnid-labs-re-load-pro/protocol.c @@ -265,32 +265,6 @@ SR_PRIV int reloadpro_get_voltage_current(const struct sr_dev_inst *sdi, return SR_OK; } -static int send_config_update_key(const struct sr_dev_inst *sdi, - uint32_t key, GVariant *var) -{ - struct sr_config *cfg; - struct sr_datafeed_packet packet; - struct sr_datafeed_meta meta; - int ret; - - cfg = sr_config_new(key, var); - if (!cfg) - return SR_ERR; - - memset(&meta, 0, sizeof(meta)); - - packet.type = SR_DF_META; - packet.payload = &meta; - - meta.config = g_slist_append(NULL, cfg); - - ret = sr_session_send(sdi, &packet); - g_slist_free(meta.config); - sr_config_free(cfg); - - return ret; -} - static void handle_packet(const struct sr_dev_inst *sdi) { struct sr_datafeed_packet packet; @@ -307,7 +281,7 @@ static void handle_packet(const struct sr_dev_inst *sdi) if (g_str_has_prefix((const char *)devc->buf, "overtemp")) { sr_warn("Overtemperature condition!"); devc->otp_active = TRUE; - send_config_update_key(sdi, SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE, + sr_session_send_meta(sdi, SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE, g_variant_new_boolean(TRUE)); return; } @@ -315,7 +289,7 @@ static void handle_packet(const struct sr_dev_inst *sdi) if (g_str_has_prefix((const char *)devc->buf, "undervolt")) { sr_warn("Undervoltage condition!"); devc->uvc_active = TRUE; - send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE, + sr_session_send_meta(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE, g_variant_new_boolean(TRUE)); return; } @@ -330,7 +304,7 @@ static void handle_packet(const struct sr_dev_inst *sdi) devc->current_limit = g_ascii_strtod(tokens[1], NULL) / 1000; g_strfreev(tokens); g_cond_signal(&devc->current_limit_cond); - send_config_update_key(sdi, SR_CONF_CURRENT_LIMIT, + sr_session_send_meta(sdi, SR_CONF_CURRENT_LIMIT, g_variant_new_double(devc->current_limit)); return; } @@ -341,12 +315,12 @@ static void handle_packet(const struct sr_dev_inst *sdi) g_strfreev(tokens); g_cond_signal(&devc->uvc_threshold_cond); if (devc->uvc_threshold == .0) { - send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION, + sr_session_send_meta(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION, g_variant_new_boolean(FALSE)); } else { - send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION, + sr_session_send_meta(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION, g_variant_new_boolean(TRUE)); - send_config_update_key(sdi, + sr_session_send_meta(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD, g_variant_new_double(devc->uvc_threshold)); } diff --git a/src/lcr/es51919.c b/src/lcr/es51919.c index b9894a1b..474602b4 100644 --- a/src/lcr/es51919.c +++ b/src/lcr/es51919.c @@ -251,42 +251,6 @@ static int serial_stream_check(struct sr_serial_dev_inst *serial, is_valid, timeout_ms, baudrate); } -static int send_config_update(struct sr_dev_inst *sdi, struct sr_config *cfg) -{ - struct sr_datafeed_packet packet; - struct sr_datafeed_meta meta; - int ret; - - memset(&meta, 0, sizeof(meta)); - - packet.type = SR_DF_META; - packet.payload = &meta; - - meta.config = g_slist_append(NULL, cfg); - - ret = sr_session_send(sdi, &packet); - - g_slist_free(meta.config); - - return ret; -} - -static int send_config_update_key(struct sr_dev_inst *sdi, uint32_t key, - GVariant *var) -{ - struct sr_config *cfg; - int ret; - - cfg = sr_config_new(key, var); - if (!cfg) - return SR_ERR; - - ret = send_config_update(sdi, cfg); - sr_config_free(cfg); - - return ret; -} - /* * Cyrustek ES51919 LCR chipset host protocol. * @@ -569,21 +533,15 @@ static gboolean packet_valid(const uint8_t *pkt) return FALSE; } -static int do_config_update(struct sr_dev_inst *sdi, uint32_t key, - GVariant *var) -{ - return send_config_update_key(sdi, key, var); -} - static int send_freq_update(struct sr_dev_inst *sdi, unsigned int freq) { - return do_config_update(sdi, SR_CONF_OUTPUT_FREQUENCY, + return sr_session_send_meta(sdi, SR_CONF_OUTPUT_FREQUENCY, g_variant_new_double(frequencies[freq])); } static int send_model_update(struct sr_dev_inst *sdi, unsigned int model) { - return do_config_update(sdi, SR_CONF_EQUIV_CIRCUIT_MODEL, + return sr_session_send_meta(sdi, SR_CONF_EQUIV_CIRCUIT_MODEL, g_variant_new_string(models[model])); } diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index b50cd57b..f5dde75a 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -908,6 +908,8 @@ SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session, SR_PRIV int sr_session_source_remove_channel(struct sr_session *session, GIOChannel *channel); +SR_PRIV int sr_session_send_meta(const struct sr_dev_inst *sdi, + uint32_t key, GVariant *var); SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *packet); SR_PRIV int sr_sessionfile_check(const char *filename); diff --git a/src/session.c b/src/session.c index b3fdfb54..bfc0a35a 100644 --- a/src/session.c +++ b/src/session.c @@ -1071,6 +1071,44 @@ static void datafeed_dump(const struct sr_datafeed_packet *packet) } } +/** + * Helper to send a meta datafeed package (SR_DF_META) to the session bus. + * + * @param sdi The device instance to send the package from. Must not be NULL. + * @param key The config key to send to the session bus. + * @param var The value to send to the session bus. + * + * @retval SR_OK Success. + * @retval SR_ERR_ARG Invalid argument. + * + * @private + */ +SR_PRIV int sr_session_send_meta(const struct sr_dev_inst *sdi, + uint32_t key, GVariant *var) +{ + struct sr_config *cfg; + struct sr_datafeed_packet packet; + struct sr_datafeed_meta meta; + int ret; + + cfg = sr_config_new(key, var); + if (!cfg) + return SR_ERR; + + memset(&meta, 0, sizeof(meta)); + + packet.type = SR_DF_META; + packet.payload = &meta; + + meta.config = g_slist_append(NULL, cfg); + + ret = sr_session_send(sdi, &packet); + g_slist_free(meta.config); + sr_config_free(cfg); + + return ret; +} + /** * Send a packet to whatever is listening on the datafeed bus. *