1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013, 2014 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/libsigrok.h>
26 #include "libsigrok-internal.h"
27 
28 #define LOG_PREFIX "gmc-mh-1x-2x"
29 
30 #define GMC_BUFSIZE 266
31 #define GMC_REPLY_SIZE 14
32 
33 /** Message ID bits 4, 5 */
34 #define MSGID_MASK  0x30 /**< Mask to get message ID bits */
35 #define MSGID_INF   0x00 /**< Start of message with device info */
36 #define MSGID_D10   0x10 /**< Start of data message, non-displayed intermediate */
37 #define MSGID_DTA   0x20 /**< Start of data message, displayed, averaged */
38 #define MSGID_DATA  0x30 /**< Data byte in message */
39 
40 #define MSGC_MASK   0x0f /**< Mask to get message byte contents in send mode */
41 
42 #define MSGSRC_MASK 0xc0 /**< Mask to get bits related to message source */
43 
44 #define bc(x) (x & MSGC_MASK) /**< Get contents from a byte */
45 
46 #define MASK_6BITS  0x3f /**< Mask lower six bits. */
47 
48 /**
49  * Internal multimeter model codes. In opposite to the multimeter models from
50  * protocol (see decode_model()), these codes allow working with ranges.
51  */
52 enum model {
53 	METRAHIT_NONE		= 0,  /**< Value for uninitialized variable */
54 	METRAHIT_12S		= 12,
55 	METRAHIT_13S14A		= 13,
56 	METRAHIT_14S		= 14,
57 	METRAHIT_15S		= 15,
58 	METRAHIT_16S		= 16,
59 	METRAHIT_16I		= 17, /**< Metrahit 16I, L */
60 	METRAHIT_16T		= 18, /**< Metrahit 16T, U, KMM2002 */
61 	METRAHIT_16X = METRAHIT_16T,  /**< All Metrahit 16 */
62 	/* A Metrahit 17 exists, but seems not to have an IR interface. */
63 	METRAHIT_18S		= 19,
64 	METRAHIT_2X		= 20, /**< For model type comparisons */
65 	METRAHIT_22SM		= METRAHIT_2X + 1,	/**< Send mode */
66 	METRAHIT_22S		= METRAHIT_22SM + 1,	/**< Bidi mode */
67 	METRAHIT_22M		= METRAHIT_22S + 1,	/**< Bidi mode */
68 	METRAHIT_23S		= METRAHIT_22M + 1,
69 	METRAHIT_24S		= METRAHIT_23S + 1,
70 	METRAHIT_25S		= METRAHIT_24S + 1,
71 	METRAHIT_26SM		= METRAHIT_25S + 1,	/**< Send mode */
72 	METRAHIT_26S		= METRAHIT_26SM + 1,	/**< Bidi mode */
73 	METRAHIT_26M		= METRAHIT_26S + 1,	/**< Bidi mode */
74 	/* The Metrahit 27x and 28Cx have a totally different protocol */
75 	METRAHIT_28S		= METRAHIT_26M + 1,
76 	METRAHIT_29S		= METRAHIT_28S + 1,
77 };
78 
79 struct dev_context {
80 	enum model model;	/**< Model code. */
81 
82 	struct sr_sw_limits limits;
83 
84 	gboolean settings_ok;	/**< Settings msg received yet. */
85 	int msg_type;       /**< Message type (MSGID_INF, ...). */
86 	int msg_len;        /**< Message length (valid when msg, curr. type known).*/
87 	enum sr_mq mq;      /**< Measured quantity */
88 	enum sr_unit unit;  /**< Measured unit */
89 	enum sr_mqflag mqflags;	/**< Measured quantity flags */
90 	float value;		/**< Measured value */
91 	int8_t scale;       /**< Scale for value. */
92 	int8_t scale1000;   /**< Additional scale factor 1000x. */
93 	int addr;           /**< Device address (1..15). */
94 	int cmd_idx;        /**< Parameter "Idx" (Index) of current command, if required. */
95 	int cmd_seq;        /**< Command sequence. Used to query status every n messages. */
96 	gboolean autorng;   /**< Auto range enabled. */
97 	float ubatt;        /**< Battery voltage. */
98 	uint8_t fw_ver_maj; /**< Firmware version major. */
99 	uint8_t fw_ver_min; /**< Firmware version minor. */
100 	int64_t req_sent_at;    /**< Request sent. */
101 	gboolean response_pending; /**< Request sent, response is pending. */
102 
103 	uint8_t buf[GMC_BUFSIZE];	/**< Buffer for read callback */
104 	int buflen;			/**< Data len in buf */
105 };
106 
107 SR_PRIV int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
108 		const struct sr_channel_group *cg);
109 SR_PRIV int gmc_decode_model_bd(uint8_t mcode);
110 SR_PRIV int gmc_decode_model_sm(uint8_t mcode);
111 SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data);
112 SR_PRIV int gmc_mh_2x_receive_data(int fd, int revents, void *cb_data);
113 SR_PRIV const char *gmc_model_str(enum model mcode);
114 SR_PRIV int process_msg14(struct sr_dev_inst *sdi);
115 SR_PRIV int req_meas14(const struct sr_dev_inst *sdi);
116 SR_PRIV int req_stat14(const struct sr_dev_inst *sdi, gboolean power_on);
117 
118 #endif
119