]> sigrok.org Git - libsigrok.git/blob - hardware/gmc-mh-1x-2x/protocol.h
Use std_serial_dev_close() to replace matching dev_close functions.
[libsigrok.git] / hardware / gmc-mh-1x-2x / protocol.h
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef LIBSIGROK_HARDWARE_GMC_MH_1X_2X_PROTOCOL_H
21 #define LIBSIGROK_HARDWARE_GMC_MH_1X_2X_PROTOCOL_H
22
23 #include <stdint.h>
24 #include <glib.h>
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
27
28 /* Message logging helpers with subsystem-specific prefix string. */
29 #define LOG_PREFIX "gmc-mh-1x-2x: "
30 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
31 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
32 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
33 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
34 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
35 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
36
37 #define GMC_BUFSIZE  266
38
39 /** Message ID bits 4, 5 */
40 #define MSGID_MASK 0x30 /**< Mask to get message ID bits */
41 #define MSGID_INF  0x00 /**< Start of message with device info */
42 #define MSGID_D10  0x10 /**< Start of data message, non-displayed intermediate */
43 #define MSGID_DTA  0x20 /**< Start of data message, displayed, averaged */
44 #define MSGID_DATA 0x30 /**< Data byte in message */
45
46 #define MSGC_MASK 0x0f  /**< Mask to get message byte contents */
47
48 #define MSGSRC_MASK 0xc0 /**< Mask to get bits related to message source */
49
50 #define bc(x) (x & MSGC_MASK) /**< Get contents from a byte */
51
52 #define MASK_6BITS 0x3f /**< Mask lower six bits. */
53
54 /** Internal multimeter model codes. In opposite to the multimeter models from
55  *  protocol (see decode_model()), these codes allow working with ranges.
56  */
57 enum model {
58         SR_METRAHIT_NONE        = 0,    /**< Value for uninitialized variable */
59         SR_METRAHIT_12S         = 12,
60         SR_METRAHIT_13S14A      = 13,
61         SR_METRAHIT_14S         = 14,
62         SR_METRAHIT_15S         = 15,
63         SR_METRAHIT_16S         = 16,
64         SR_METRAHIT_16I         = 17,
65         SR_METRAHIT_16X = SR_METRAHIT_16I,  /**< All Metrahit 16 */
66         /* A Metrahit 17 exists, but seems not to have an IR interface. */
67         SR_METRAHIT_18S         = 18,
68         SR_METRAHIT_2X          = 20,   /**< For model type comparisons */
69         SR_METRAHIT_22SM        = 22,
70         SR_METRAHIT_23S         = 23,
71         SR_METRAHIT_24S         = 24,
72         SR_METRAHIT_25SM        = 25,
73         SR_METRAHIT_26S         = 26,
74         SR_METRAHIT_28S         = 28,
75         SR_METRAHIT_29S         = 29,
76 };
77
78 /** Convert GMC model code in send mode to sigrok-internal one. */
79 SR_PRIV int sr_gmc_decode_model_sm(guchar mcode);
80
81 /** Convert GMC model code in bidirectional mode to sigrok-internal one.
82     \param[in] mcode Model code.
83
84     \return Model code
85 */
86 SR_PRIV int sr_gmc_decode_model_bidi(guchar mcode);
87
88 /** Create 14-bytes (42 byte) message used in bidirectional mode.
89  *  \param[in] addr Target address. 0..15, 0=Broadcast.
90  *  \param[in] func Function code.
91  *  \param[in] params Further parameters, &char[9]. Unused bytes must be 0.
92  *  \param[out] buf &guchar[42] Output buffer (3*14).
93  *
94  */
95 void create_msg_14(guchar addr, guchar func, guchar* params, guchar* buf);
96
97 /** Get model string from sigrok-internal model code.
98  */
99 SR_PRIV const char* sr_gmc_model_str(enum model mcode);
100
101 /** Private, per-device-instance driver context. */
102 struct dev_context {
103         /* Model-specific information */
104         enum model model;       /**< Model code. */
105
106         /* Acquisition settings */
107         uint64_t limit_samples; /**< Target number of samples */
108         uint64_t limit_msec;    /**< Target sampling time */
109
110         /* Opaque pointer passed in by frontend. */
111         void *cb_data;
112
113         /* Operational state */
114         gboolean settings_ok;   /**< Settings msg received yet. */
115         int msg_type;   /**< Message type (MSGID_INF, ...). */
116         int msg_len;    /**< Message lengh (valid when msg, curr. type known).*/
117         int mq;         /**< Measured quantity */
118         int unit;               /**< Measured unit */
119         uint64_t mqflags;       /**< Measured quantity flags */
120         float value;            /**< Measured value */
121         float scale;            /**< Scale for value. */
122         int8_t scale1000;   /**< Additional scale factor 1000^x. */
123         gboolean vmains_29S;    /**< Measured ctmv is V mains (29S only). */
124
125         /* Temporary state across callbacks */
126         uint64_t num_samples;   /**< Current #samples for limit_samples */
127         GTimer* elapsed_msec;   /**< Used for sampling with limit_msec  */
128         unsigned char buf[GMC_BUFSIZE]; /**< Buffer for read callback */
129         int buflen;                     /**< Data len in buf */
130 };
131
132 SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data);
133
134 #endif