GCC Code Coverage Report


Directory: src/gate/
File: src/gate/io/cxx_serialports.cpp
Date: 2026-03-20 22:56:14
Exec Total Coverage
Lines: 8 44 18.2%
Functions: 2 10 20.0%
Branches: 4 60 6.7%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/io/serialports.hpp"
30 #include "gate/exceptions.hpp"
31
32 namespace gate
33 {
34 namespace io
35 {
36
37 1 void SerialPort::enumPorts(gate_serialport_enum_callback_t callback, void* param)
38 {
39 1 result_t result = gate_serialport_enum(callback, param);
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
41 1 }
42
43 static gate_bool_t GATE_CALL enumPortsCallback(char const* portid, char const* description, void* userparam)
44 {
45 SerialPort::EnumPortsDelegate const* ptrCallback = static_cast<SerialPort::EnumPortsDelegate const*>(userparam);
46 ExceptionInfo xcptInfo;
47 GATEXX_TRY_CATCHINFO(xcptInfo, {
48 String portId(portid);
49 String descr(description);
50 ptrCallback->invoke(portId, descr);
51 });
52 return xcptInfo.succeeded();
53 }
54
55 void SerialPort::enumPorts(EnumPortsDelegate const& callback)
56 {
57 void const* param = &callback;
58 SerialPort::enumPorts(&enumPortsCallback, const_cast<void*>(param));
59 }
60
61 static gate_bool_t GATE_CALL enumPortsCallbackArray(char const* portid, char const* description, void* userparam)
62 {
63 ArrayList<String>* ptrList = static_cast<ArrayList<String>*>(userparam);
64 ExceptionInfo xcptInfo;
65 GATEXX_TRY_CATCHINFO(xcptInfo, {
66 String portId(portid);
67 ptrList->add(portId);
68 });
69 return xcptInfo.succeeded();
70 }
71
72 1 Array<String> SerialPort::enumPorts()
73 {
74
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 ArrayList<String> ret;
75
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 SerialPort::enumPorts(&enumPortsCallbackArray, static_cast<void*>(&ret));
76
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return ret.toArray();
77 }
78
79 SerialPort::porthandle_t SerialPort::open(String const& portId,
80 uint32_t baudrate, enumint_t bits, enumint_t parity, enumint_t stopbits, enumint_t flowcontrol,
81 uint32_t timeoutMs, bool_t asynchronous)
82 {
83 porthandle_t ret = NULL;
84 result_t result = gate_serialport_open(portId.c_impl(), baudrate, bits, parity, stopbits, flowcontrol, timeoutMs, asynchronous, &ret);
85 GATEXX_CHECK_EXCEPTION(result);
86 return ret;
87 }
88
89 result_t SerialPort::close(porthandle_t port)
90 {
91 return gate_serialport_close(port);
92 }
93
94 size_t SerialPort::read(porthandle_t port, char* buffer, size_t bufferlen)
95 {
96 gate_size_t retrieved = 0;
97 result_t result = gate_serialport_read(port, buffer, bufferlen, &retrieved);
98 GATEXX_CHECK_EXCEPTION(result);
99 return retrieved;
100 }
101
102 size_t SerialPort::write(porthandle_t port, char const* buffer, size_t bufferlen)
103 {
104 gate_size_t written = 0;
105 result_t result = gate_serialport_write(port, buffer, bufferlen, &written);
106 GATEXX_CHECK_EXCEPTION(result);
107 return written;
108 }
109
110 Stream SerialPort::openStream(String const& portId,
111 uint32_t baudrate, enumint_t bits, enumint_t parity, enumint_t stopbits, enumint_t flowcontrol)
112 {
113 gate_stream_t* ptrStream = NULL;
114 result_t result = gate_serialport_openstream(portId.c_impl(), baudrate, bits, parity, stopbits, flowcontrol, &ptrStream);
115 GATEXX_CHECK_EXCEPTION(result);
116 return Stream(ptrStream);
117 }
118
119 } // end of namespace io
120 } // end of namespace gate
121