protocol.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /** @file
  20. * Gossen Metrawatt Metrahit 1x/2x drivers
  21. * @internal
  22. */
  23. #ifndef LIBSIGROK_HARDWARE_GMC_MH_1X_2X_PROTOCOL_H
  24. #define LIBSIGROK_HARDWARE_GMC_MH_1X_2X_PROTOCOL_H
  25. #include <stdint.h>
  26. #include <glib.h>
  27. #include "libsigrok.h"
  28. #include "libsigrok-internal.h"
  29. #define LOG_PREFIX "gmc-mh-1x-2x"
  30. #define GMC_BUFSIZE 266
  31. /** Message ID bits 4, 5 */
  32. #define MSGID_MASK 0x30 /**< Mask to get message ID bits */
  33. #define MSGID_INF 0x00 /**< Start of message with device info */
  34. #define MSGID_D10 0x10 /**< Start of data message, non-displayed intermediate */
  35. #define MSGID_DTA 0x20 /**< Start of data message, displayed, averaged */
  36. #define MSGID_DATA 0x30 /**< Data byte in message */
  37. #define MSGC_MASK 0x0f /**< Mask to get message byte contents in send mode */
  38. #define MSGSRC_MASK 0xc0 /**< Mask to get bits related to message source */
  39. #define bc(x) (x & MSGC_MASK) /**< Get contents from a byte */
  40. #define MASK_6BITS 0x3f /**< Mask lower six bits. */
  41. /**
  42. * Internal multimeter model codes. In opposite to the multimeter models from
  43. * protocol (see decode_model()), these codes allow working with ranges.
  44. */
  45. enum model {
  46. METRAHIT_NONE = 0, /**< Value for uninitialized variable */
  47. METRAHIT_12S = 12,
  48. METRAHIT_13S14A = 13,
  49. METRAHIT_14S = 14,
  50. METRAHIT_15S = 15,
  51. METRAHIT_16S = 16,
  52. METRAHIT_16I = 17,
  53. METRAHIT_16X = METRAHIT_16I, /**< All Metrahit 16 */
  54. /* A Metrahit 17 exists, but seems not to have an IR interface. */
  55. METRAHIT_18S = 18,
  56. METRAHIT_2X = 20, /**< For model type comparisons */
  57. METRAHIT_22SM = METRAHIT_2X + 1, /**< Send mode */
  58. METRAHIT_22S = METRAHIT_22SM + 1, /**< Bidi mode */
  59. METRAHIT_22M = METRAHIT_22S + 1, /**< Bidi mode */
  60. METRAHIT_23S = METRAHIT_22M + 1,
  61. METRAHIT_24S = METRAHIT_23S + 1,
  62. METRAHIT_25S = METRAHIT_24S + 1,
  63. METRAHIT_26SM = METRAHIT_25S + 1, /**< Send mode */
  64. METRAHIT_26S = METRAHIT_26SM + 1, /**< Bidi mode */
  65. METRAHIT_26M = METRAHIT_26S + 1, /**< Bidi mode */
  66. METRAHIT_28S = METRAHIT_26M + 1,
  67. METRAHIT_29S = METRAHIT_28S + 1,
  68. };
  69. /** Private, per-device-instance driver context. */
  70. struct dev_context {
  71. /* Model-specific information */
  72. enum model model; /**< Model code. */
  73. /* Acquisition settings */
  74. uint64_t limit_samples; /**< Target number of samples */
  75. uint64_t limit_msec; /**< Target sampling time */
  76. /* Opaque pointer passed in by frontend. */
  77. void *cb_data;
  78. /* Operational state */
  79. gboolean settings_ok; /**< Settings msg received yet. */
  80. int msg_type; /**< Message type (MSGID_INF, ...). */
  81. int msg_len; /**< Message lengh (valid when msg, curr. type known).*/
  82. int mq; /**< Measured quantity */
  83. int unit; /**< Measured unit */
  84. uint64_t mqflags; /**< Measured quantity flags */
  85. float value; /**< Measured value */
  86. float scale; /**< Scale for value. */
  87. int8_t scale1000; /**< Additional scale factor 1000x. */
  88. gboolean vmains_29S; /**< Measured ctmv is V mains (29S only). */
  89. int addr; /**< Device address (1..15). */
  90. int cmd_idx; /**< Parameter "Idx" (Index) of current command, if required. */
  91. int cmd_seq; /**< Command sequence. Used to query status every n messages. */
  92. gboolean autorng; /**< Auto range enabled. */
  93. float ubatt; /**< Battery voltage. */
  94. uint8_t fw_ver_maj; /**< Firmware version major. */
  95. uint8_t fw_ver_min; /**< Firmware version minor. */
  96. int64_t req_sent_at; /**< Request sent. */
  97. gboolean response_pending; /**< Request sent, response is pending. */
  98. /* Temporary state across callbacks */
  99. uint64_t num_samples; /**< Current #samples for limit_samples */
  100. GTimer *elapsed_msec; /**< Used for sampling with limit_msec */
  101. uint8_t buf[GMC_BUFSIZE]; /**< Buffer for read callback */
  102. int buflen; /**< Data len in buf */
  103. };
  104. /* Forward declarations */
  105. SR_PRIV int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
  106. const struct sr_channel_group *cg);
  107. SR_PRIV void create_cmd_14(guchar addr, guchar func, guchar* params, guchar* buf);
  108. SR_PRIV void dump_msg14(guchar* buf, gboolean raw);
  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. #endif