qemu/tests/qtest/emc141x-test.c
<<
>>
Prefs
   1/*
   2 * QTest testcase for the EMC141X temperature sensor
   3 *
   4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
   5 * See the COPYING file in the top-level directory.
   6 */
   7
   8#include "qemu/osdep.h"
   9
  10#include "libqtest-single.h"
  11#include "libqos/qgraph.h"
  12#include "libqos/i2c.h"
  13#include "qapi/qmp/qdict.h"
  14#include "hw/misc/emc141x_regs.h"
  15
  16#define EMC1414_TEST_ID   "emc1414-test"
  17
  18static int qmp_emc1414_get_temperature(const char *id)
  19{
  20    QDict *response;
  21    int ret;
  22
  23    response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
  24                   "'property': 'temperature0' } }", id);
  25    g_assert(qdict_haskey(response, "return"));
  26    ret = qdict_get_int(response, "return");
  27    qobject_unref(response);
  28    return ret;
  29}
  30
  31static void qmp_emc1414_set_temperature(const char *id, int value)
  32{
  33    QDict *response;
  34
  35    response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
  36                   "'property': 'temperature0', 'value': %d } }", id, value);
  37    g_assert(qdict_haskey(response, "return"));
  38    qobject_unref(response);
  39}
  40
  41static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
  42{
  43    uint16_t value;
  44    QI2CDevice *i2cdev = (QI2CDevice *)obj;
  45
  46    value = qmp_emc1414_get_temperature(EMC1414_TEST_ID);
  47    g_assert_cmpuint(value, ==, 0);
  48
  49    value = i2c_get8(i2cdev, EMC141X_TEMP_HIGH0);
  50    g_assert_cmphex(value, ==, 0);
  51
  52    /* The default max value is 85C, 0x55=85 */
  53    value = i2c_get8(i2cdev, EMC141X_TEMP_MAX_HIGH0);
  54    g_assert_cmphex(value, ==, 0x55);
  55
  56    value = i2c_get8(i2cdev, EMC141X_TEMP_MIN_HIGH0);
  57    g_assert_cmphex(value, ==, 0);
  58
  59    /* 3000mc = 30C */
  60    qmp_emc1414_set_temperature(EMC1414_TEST_ID, 30000);
  61    value = qmp_emc1414_get_temperature(EMC1414_TEST_ID);
  62    g_assert_cmpuint(value, ==, 30000);
  63
  64    value = i2c_get8(i2cdev, EMC141X_TEMP_HIGH0);
  65    g_assert_cmphex(value, ==, 30);
  66
  67}
  68
  69static void emc1414_register_nodes(void)
  70{
  71    QOSGraphEdgeOptions opts = {
  72        .extra_device_opts = "id=" EMC1414_TEST_ID ",address=0x70"
  73    };
  74    add_qi2c_address(&opts, &(QI2CAddress) { 0x70 });
  75
  76    qos_node_create_driver("emc1414", i2c_device_create);
  77    qos_node_consumes("emc1414", "i2c-bus", &opts);
  78
  79    qos_add_test("tx-rx", "emc1414", send_and_receive, NULL);
  80}
  81libqos_init(emc1414_register_nodes);
  82