]> sigrok.org Git - libsigrok.git/commitdiff
session: Add helper sr_session_send_meta() to send SR_DF_META packages.
authorFrank Stettner <redacted>
Tue, 9 Apr 2019 12:12:31 +0000 (14:12 +0200)
committerUwe Hermann <redacted>
Wed, 17 Apr 2019 15:11:19 +0000 (17:11 +0200)
Replace individual functions in drivers for arachnid-labs-re-load-pro
and lcr/es51919 with sr_session_send_meta() helper function.

src/hardware/arachnid-labs-re-load-pro/protocol.c
src/lcr/es51919.c
src/libsigrok-internal.h
src/session.c

index 86e317a5cff8eca7c0aef5869bc9b8696105da7e..3d7a014606de4c5746c214e1cbc9253ea147fd40 100644 (file)
@@ -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));
                }
index b9894a1b439cc7edbe3af2acc6b93f7cfb2bcb29..474602b41ee72bd888ade1c5e0e4750c578c4bc4 100644 (file)
@@ -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]));
 }
 
index b50cd57b90df8669e2cc1de1f909269987aca41e..f5dde75a96c68d3c24fc9a7211d93e9d90754c2f 100644 (file)
@@ -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);
index b3fdfb54a2451b0ac506b0f82b452fda30ad6807..bfc0a35a9782aeb4a399f49d07234a1e4e73e2fc 100644 (file)
@@ -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.
  *