aboutsummaryrefslogtreecommitdiffstats
path: root/api/mraa/uart_ow.hpp
blob: e64a14ac1ecb7cc70e5a4294a26a4581419c6430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2016 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * SPDX-License-Identifier: MIT
 */

#pragma once

#include "types.hpp"
#include "uart_ow.h"
#include <cstring>
#include <stdexcept>

namespace mraa
{

/**
 * @brief API for UART One Wire
 *
 * This file defines the UartOW (UART to Dallas 1-wire) interface for libmraa
 *
 * @snippet uart_ow.cpp Interesting
 */
class UartOW
{
  public:
    /**
     * UartOW Constructor, takes a pin number which will map directly to the
     * linux uart number, this 'enables' the uart, nothing more
     *
     * @param uart the index of the uart to use
     * @throws std::invalid_argument in case of error
     */
    UartOW(int uart)
    {
        m_uart = mraa_uart_ow_init(uart);

        if (m_uart == NULL) {
            throw std::invalid_argument("Error initialising UART_OW");
        }
    }

    /**
     * UartOW Constructor, takes a string to the path of the serial
     * interface that is needed.
     *
     * @param path the file path for the UART to use
     * @throws std::invalid_argument in case of error
     */
    UartOW(std::string path)
    {
        m_uart = mraa_uart_ow_init_raw(path.c_str());

        if (m_uart == NULL) {
            throw std::invalid_argument("Error initialising UART");
        }
    }

    /**
     * UartOW Constructor, takes a pointer to the UartOW context and initialises
     * the UartOW class
     *
     * @param uart_ow_context void * to a UartOW context
     */
    UartOW(void* uart_ow_context)
    {
        m_uart = (mraa_uart_ow_context) uart_ow_context;

        if (m_uart == NULL) {
            throw std::invalid_argument("Invalid UART_OW context");
        }
    }

    /**
     * Uart destructor
     */
    ~UartOW()
    {
        if (m_uart != NULL) {
            mraa_uart_ow_stop(m_uart);
        }
    }

    /*
     * Closes UartOW explicitly, prior to implicit closing on object destruction
     */
    void
    close()
    {
        mraa_uart_ow_stop(m_uart);
        m_uart = NULL;
    }

    /**
     * Get string with tty device path within Linux
     * For example. Could point to "/dev/ttyS0"
     *
     * @return char pointer of device path
     */
    std::string
    getDevicePath()
    {
        std::string ret_val(mraa_uart_ow_get_dev_path(m_uart));
        return ret_val;
    }

    /**
     * Read a byte from the 1-wire bus
     *
     * @throws std::invalid_argument in case of error
     * @return the byte read
     */
    uint8_t
    readByte()
    {
        int res = mraa_uart_ow_read_byte(m_uart);
        if (res == -1) {
            throw std::invalid_argument("Unknown UART_OW error");
        }
        return (uint8_t) res;
    }

    /**
     * Write a byte to a 1-wire bus
     *
     * @param byte the byte to write to the bus
     *
     * @throws std::invalid_argument in case of error
     * @return the byte read back during the time slot
     */
    uint8_t
    writeByte(uint8_t byte)
    {
        int res = mraa_uart_ow_write_byte(m_uart, byte);
        if (res == -1) {
            throw std::invalid_argument("Unknown UART_OW error");
        }
        return (uint8_t) res;
    }

    /**
     * Write a bit to a 1-wire bus and read a bit corresponding to the
     * time slot back.  This is possible due to the way we wired the TX
     * and RX together with a diode, forming a loopback.
     *
     * @param bit the bit to write to the bus
     * @throws std::invalid_argument in case of error
     * @return the bit read back during the time slot
     */
    bool
    writeBit(bool bit)
    {
        int res = mraa_uart_ow_bit(m_uart, (bit) ? 1 : 0);
        if (res == -1) {
            throw std::invalid_argument("Unknown UART_OW error");
        }
        return ((res) ? true : false);
    }

    /**
     * Send a reset pulse to the 1-wire bus and test for device presence
     *
     * @return one of the mraa::Result values
     */
    mraa::Result
    reset()
    {
        return (mraa::Result) mraa_uart_ow_reset(m_uart);
    }

    /**
     * Begin a rom code search of the 1-wire bus.  This function
     * implements the 1-wire search algorithm.  See the uart_ow.c example
     * for an idea on how to use this function to identify all devices
     * present on the bus.
     *
     * @param start true to start a search from scratch, false to
     * continue a previously started search
     * @param id the 8-byte rom code id of the current matched device when a
     * device is found
     * @return one of the mraa::Result values
     */
    mraa::Result
    search(bool start, uint8_t* id)
    {
        return (mraa::Result) mraa_uart_ow_rom_search(m_uart, (start) ? 1 : 0, id);
    }

    /**
     * Begin a rom code search of the 1-wire bus.  This function
     * implements the 1-wire search algorithm.  See the UartOW.cpp
     * example for an idea on how to use this function to identify all
     * devices present on the bus.
     *
     * @param start true to start a search from scratch, false to
     * continue a previously started search
     * @return an empty string if no [more] devices are found, or a
     * string containing the 8-byte romcode of a detected device.
     */
    std::string
    search(bool start)
    {
        uint8_t id[MRAA_UART_OW_ROMCODE_SIZE];
        mraa_result_t rv;

        rv = mraa_uart_ow_rom_search(m_uart, (start) ? 1 : 0, id);

        if (rv == MRAA_SUCCESS) {
            // we found one
            std::string idStr((char*) id, MRAA_UART_OW_ROMCODE_SIZE);
            return idStr;
        } else {
            // failure, or end of search
            return "";
        }
    }

    /**
     * Send a command byte to a device on the 1-wire bus
     *
     * @param command the command byte to send
     * @param id the rom code id of the device to receive the command,
     * NULL for all devices on the bus
     * @return one of the mraa::Result values
     */
    mraa::Result
    command(uint8_t command, uint8_t* id)
    {
        return (mraa::Result) mraa_uart_ow_command(m_uart, command, id);
    }

    /**
     * Send a command byte to a device on the 1-wire bus, supplying
     * the id as a std::string
     *
     * @param command the command byte to send
     * @param id std::string representing the code id of the device to
     * receive the command, or an empty string for all devices on the
     * bus.  This string should be 8 bytes in size.
     * @return one of the mraa::Result values
     */
    mraa::Result
    command(uint8_t command, std::string id)
    {
        if (id.empty())
            return (mraa::Result) mraa_uart_ow_command(m_uart, command, NULL);
        else {
            if (id.size() != 8) {
                // Only 8 byte romcodes are legal.
                throw std::invalid_argument(std::string(__FUNCTION__) +
                                            ": id must be 8 bytes only");
            }
            return (mraa::Result) mraa_uart_ow_command(m_uart, command, (uint8_t*) id.data());
        }
    }

    /**
     * Perform a Dallas 1-wire compliant CRC8 computation on a buffer
     *
     * @param buffer the buffer containing the data
     * @param length the length of the buffer
     * @return the computed CRC
     */
    uint8_t
    crc8(uint8_t* buffer, uint16_t length)
    {
        return mraa_uart_ow_crc8(buffer, length);
    }

    /**
     * Perform a Dallas 1-wire compliant CRC8 computation on a
     * std::string based buffer
     *
     * @param buffer std::string buffer containing the data
     * @return the computed CRC
     */
    uint8_t
    crc8(std::string buffer)
    {
        return mraa_uart_ow_crc8((uint8_t*) buffer.data(), buffer.size());
    }

  private:
    mraa_uart_ow_context m_uart;
};
}