]> sigrok.org Git - libsigrok.git/blobdiff - hardware/gmc-mh-1x-2x/protocol.h
build: Portability fixes.
[libsigrok.git] / hardware / gmc-mh-1x-2x / protocol.h
index 711dd1c043d179dc01835cbc6b7e6520a9c952fc..aa19be38a8a8d161ae7723f88d41b81293140242 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the libsigrok project.
  *
- * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
+ * Copyright (C) 2013, 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+/** @file
+ *  Gossen Metrawatt Metrahit 1x/2x drivers
+ *  @internal
+ */
+
 #ifndef LIBSIGROK_HARDWARE_GMC_MH_1X_2X_PROTOCOL_H
 #define LIBSIGROK_HARDWARE_GMC_MH_1X_2X_PROTOCOL_H
 
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 
-/* Message logging helpers with subsystem-specific prefix string. */
-#define LOG_PREFIX "gmc-mh-1x-2x: "
-#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
-#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
-#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
-#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
-#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
-#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
+#define LOG_PREFIX "gmc-mh-1x-2x"
 
 #define GMC_BUFSIZE  266
 
 /** Message ID bits 4, 5 */
-#define MSGID_MASK 0x30        /**< Mask to get message ID bits */
-#define MSGID_INF  0x00 /**< Start of message with device info */
-#define MSGID_D10  0x10        /**< Start of data message, non-displayed intermediate */
-#define MSGID_DTA  0x20 /**< Start of data message, displayed, averaged */
-#define MSGID_DATA 0x30        /**< Data byte in message */
+#define MSGID_MASK  0x30 /**< Mask to get message ID bits */
+#define MSGID_INF   0x00 /**< Start of message with device info */
+#define MSGID_D10   0x10 /**< Start of data message, non-displayed intermediate */
+#define MSGID_DTA   0x20 /**< Start of data message, displayed, averaged */
+#define MSGID_DATA  0x30 /**< Data byte in message */
 
-#define MSGC_MASK 0x0f /**< Mask to get message byte contents */
+#define MSGC_MASK   0x0f  /**< Mask to get message byte contents in send mode */
 
 #define MSGSRC_MASK 0xc0 /**< Mask to get bits related to message source */
 
 #define bc(x) (x & MSGC_MASK) /**< Get contents from a byte */
 
-#define MASK_6BITS 0x3f /**< Mask lower six bits. */
+#define MASK_6BITS  0x3f /**< Mask lower six bits. */
 
-/** Internal multimeter model codes. In opposite to the multimeter models from
- *  protocol (see decode_model()), these codes allow working with ranges.
+/**
+ * Internal multimeter model codes. In opposite to the multimeter models from
+ * protocol (see decode_model()), these codes allow working with ranges.
  */
 enum model {
-       SR_METRAHIT_NONE        = 0,    /**< Value for uninitialized variable */
-       SR_METRAHIT_12S         = 12,
-       SR_METRAHIT_13S14A      = 13,
-       SR_METRAHIT_14S         = 14,
-       SR_METRAHIT_15S         = 15,
-       SR_METRAHIT_16S         = 16,
-       SR_METRAHIT_16I         = 17,
-       SR_METRAHIT_16X = SR_METRAHIT_16I,  /**< All Metrahit 16 */
+       METRAHIT_NONE           = 0,  /**< Value for uninitialized variable */
+       METRAHIT_12S            = 12,
+       METRAHIT_13S14A         = 13,
+       METRAHIT_14S            = 14,
+       METRAHIT_15S            = 15,
+       METRAHIT_16S            = 16,
+       METRAHIT_16I            = 17, /**< Metrahit 16I, L */
+       METRAHIT_16T            = 18, /**< Metrahit 16T, U, KMM2002 */
+       METRAHIT_16X = METRAHIT_16T,  /**< All Metrahit 16 */
        /* A Metrahit 17 exists, but seems not to have an IR interface. */
-       SR_METRAHIT_18S         = 18,
-       SR_METRAHIT_2X          = 20,   /**< For model type comparisons */
-       SR_METRAHIT_22SM        = 22,
-       SR_METRAHIT_23S         = 23,
-       SR_METRAHIT_24S         = 24,
-       SR_METRAHIT_25SM        = 25,
-       SR_METRAHIT_26S         = 26,
-       SR_METRAHIT_28S         = 28,
-       SR_METRAHIT_29S         = 29,
+       METRAHIT_18S            = 19,
+       METRAHIT_2X             = 20, /**< For model type comparisons */
+       METRAHIT_22SM           = METRAHIT_2X + 1,      /**< Send mode */
+       METRAHIT_22S            = METRAHIT_22SM + 1,    /**< Bidi mode */
+       METRAHIT_22M            = METRAHIT_22S + 1,     /**< Bidi mode */
+       METRAHIT_23S            = METRAHIT_22M + 1,
+       METRAHIT_24S            = METRAHIT_23S + 1,
+       METRAHIT_25S            = METRAHIT_24S + 1,
+       METRAHIT_26SM           = METRAHIT_25S + 1,     /**< Send mode */
+       METRAHIT_26S            = METRAHIT_26SM + 1,    /**< Bidi mode */
+       METRAHIT_26M            = METRAHIT_26S + 1,     /**< Bidi mode */
+       METRAHIT_28S            = METRAHIT_26M + 1,
+       METRAHIT_29S            = METRAHIT_28S + 1,
 };
 
-/** Convert GMC model code in send mode to sigrok-internal one. */
-SR_PRIV int sr_gmc_decode_model_sm(guchar mcode);
-
-/** Convert GMC model code in bidirectional mode to sigrok-internal one.
-    \param[in] mcode Model code.
-
-    \return Model code
-*/
-SR_PRIV int sr_gmc_decode_model_bidi(guchar mcode);
-
-/** Create 14-bytes (42 byte) message used in bidirectional mode.
- *  \param[in] addr Target address. 0..15, 0=Broadcast.
- *  \param[in] func Function code.
- *  \param[in] params Further parameters, &char[9]. Unused bytes must be 0.
- *  \param[out] buf &guchar[42] Output buffer (3*14).
- *
- */
-void create_msg_14(guchar addr, guchar func, guchar* params, guchar* buf);
-
-/** Get model string from sigrok-internal model code.
- */
-SR_PRIV const char* sr_gmc_model_str(enum model mcode);
-
 /** Private, per-device-instance driver context. */
 struct dev_context {
        /* Model-specific information */
@@ -112,23 +93,44 @@ struct dev_context {
 
        /* Operational state */
        gboolean settings_ok;   /**< Settings msg received yet. */
-       int msg_type;   /**< Message type (MSGID_INF, ...). */
-       int msg_len;    /**< Message lengh (valid when msg, curr. type known).*/
-       int mq;         /**< Measured quantity */
-       int unit;               /**< Measured unit */
+       int msg_type;       /**< Message type (MSGID_INF, ...). */
+       int msg_len;        /**< Message lengh (valid when msg, curr. type known).*/
+       int mq;             /**< Measured quantity */
+       int unit;           /**< Measured unit */
        uint64_t mqflags;       /**< Measured quantity flags */
        float value;            /**< Measured value */
        float scale;            /**< Scale for value. */
-       int8_t scale1000;   /**< Additional scale factor 1000^x. */
+       int8_t scale1000;   /**< Additional scale factor 1000x. */
        gboolean vmains_29S;    /**< Measured ctmv is V mains (29S only). */
+       int addr;           /**< Device address (1..15). */
+       int cmd_idx;        /**< Parameter "Idx" (Index) of current command, if required. */
+       int cmd_seq;        /**< Command sequence. Used to query status every n messages. */
+       gboolean autorng;   /**< Auto range enabled. */
+       float ubatt;        /**< Battery voltage. */
+       uint8_t fw_ver_maj; /**< Firmware version major. */
+       uint8_t fw_ver_min; /**< Firmware version minor. */
+       int64_t req_sent_at;    /**< Request sent. */
+       gboolean response_pending; /**< Request sent, response is pending. */
 
        /* Temporary state across callbacks */
        uint64_t num_samples;   /**< Current #samples for limit_samples */
-       GTimerelapsed_msec;   /**< Used for sampling with limit_msec  */
-       unsigned char buf[GMC_BUFSIZE]; /**< Buffer for read callback */
+       GTimer *elapsed_msec;   /**< Used for sampling with limit_msec  */
+       uint8_t buf[GMC_BUFSIZE];       /**< Buffer for read callback */
        int buflen;                     /**< Data len in buf */
 };
 
+/* Forward declarations */
+SR_PRIV int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg);
+SR_PRIV void create_cmd_14(guchar addr, guchar func, guchar* params, guchar* buf);
+SR_PRIV void dump_msg14(guchar* buf, gboolean raw);
+SR_PRIV int gmc_decode_model_bd(uint8_t mcode);
+SR_PRIV int gmc_decode_model_sm(uint8_t mcode);
 SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data);
+SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data);
+SR_PRIV const char *gmc_model_str(enum model mcode);
+SR_PRIV int process_msg14(struct sr_dev_inst *sdi);
+SR_PRIV int req_meas14(const struct sr_dev_inst *sdi);
+SR_PRIV int req_stat14(const struct sr_dev_inst *sdi, gboolean power_on);
 
 #endif