3 * Copyright (c) 2015 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include "mraa/iio.hpp"
31 #define EXPECT_FAILURE 0
32 #define EXPECT_SUCCESS 1
34 #define IIO_TRY(func) \
36 bool success = true; \
39 } catch (std::exception& e) { \
42 log_result(#func, "", true, success); \
45 // Macro to run IIO method on attribute and log output
46 #define IIO_RUN(func, attr, value, expect) \
48 std::string attr_name = attr; \
49 bool success = true; \
51 iio_device->func(attr_name, value); \
52 } catch (std::exception& e) { \
55 log_result(#func, attr_name, expect, success); \
58 // Macro to run IIO method on attribute and check for expected result and log output
59 #define IIO_TEST(func, attr, value, expect) \
61 std::string attr_name = attr; \
62 bool success = false; \
64 success = fabs(iio_device->func(attr_name) - value) < FLT_EPSILON; \
65 } catch (std::exception& e) { \
68 log_result(#func, attr_name, expect, success); \
71 mraa::Iio* iio_device;
74 // Log result of test. Note a "fail" (i.e. success is false) will be displayed as a pass if a fail was expected
75 void log_result(std::string test_name, std::string attr_name, bool expect_success, bool success)
79 result = success ? "PASS" : "FAIL";
81 result = success ? "FAIL" : "PASS";
82 if (attr_name.empty())
83 fprintf(stdout, "%s: %s\n", test_name.c_str(), result.c_str());
85 fprintf(stdout, "%s(%s): %s\n", test_name.c_str(), attr_name.c_str(), result.c_str());
88 // Generate iio_dummy driver event by writing a string to a specific sysfs node
91 FILE *fp = fopen("/sys/bus/iio/devices/iio_evgen/poke_ev0", "w");
100 // IIO event handler that checks for event from dummy_iio_evgen driver
101 class IioTestHandler : public mraa::IioHandler
104 void onIioEvent(const mraa::IioEventData& eventData) {
105 if (eventData.channelType == IIO_VOLTAGE && eventData.direction == IIO_EV_DIR_RISING && eventData.type == IIO_EV_TYPE_THRESH)
113 IioTestHandler testHandler;
114 std::string deviceName;
116 mraa::Iio* iio_device0 = new mraa::Iio(0);
117 std::cout << "IIO device 0 found by id." << std::endl;
118 deviceName = iio_device0->getDeviceName();
120 } catch (std::exception& e) {
121 std::cerr << "IIO device 0 not found." << std::endl;
126 mraa::Iio* iio_device1 = new mraa::Iio(1);
128 } catch (std::exception& e) {
129 std::cerr << "IIO device 1 not found. This is expected behavior." << std::endl;
133 iio_device = new mraa::Iio(deviceName);
134 std::cout << "IIO device 0 found by name." << std::endl;
135 } catch (std::exception& e) {
136 std::cerr << "IIO device 0 not found." << std::endl;
141 std::cout << "Using IIO device0. Name is " << iio_device->getDeviceName() << std::endl;
142 IIO_RUN(writeFloat, "in_accel_x_raw", 100, EXPECT_FAILURE);
143 IIO_RUN(writeFloat, "in_voltage0_scale", 100, EXPECT_FAILURE);
144 IIO_RUN(writeInt, "out_voltage0_raw", 100, EXPECT_SUCCESS);
145 IIO_TEST(readInt, "in_accel_x_raw", 34, EXPECT_SUCCESS);
146 IIO_TEST(readFloat, "in_voltage0_scale", 0.001333, EXPECT_SUCCESS);
147 IIO_RUN(writeInt, "events/in_voltage0_thresh_rising_en", 1, EXPECT_SUCCESS);
148 IIO_TRY(registerEventHandler(&testHandler));
152 log_result("eventReceived", "", (eventCount == 1), true);