Line |
Branch |
Exec |
Source |
1 |
|
|
/* GATE PROJECT LICENSE: |
2 |
|
|
+----------------------------------------------------------------------------+ |
3 |
|
|
| Copyright(c) 2018-2025, 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 |
|
✗ |
void SerialPort::enumPorts(gate_serialport_enum_callback_t callback, void* param) |
38 |
|
|
{ |
39 |
|
✗ |
result_t result = gate_serialport_enum(callback, param); |
40 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
41 |
|
✗ |
} |
42 |
|
|
|
43 |
|
✗ |
static gate_bool_t enumPortsCallback(char const* portid, char const* description, void* userparam) |
44 |
|
|
{ |
45 |
|
✗ |
SerialPort::EnumPortsDelegate const* ptrCallback = static_cast<SerialPort::EnumPortsDelegate const*>(userparam); |
46 |
|
|
try |
47 |
|
|
{ |
48 |
|
✗ |
String portId(portid); |
49 |
|
✗ |
String descr(description); |
50 |
|
✗ |
ptrCallback->invoke(portId, descr); |
51 |
|
✗ |
return true; |
52 |
|
|
} |
53 |
|
✗ |
catch (...) |
54 |
|
|
{ |
55 |
|
✗ |
return false; |
56 |
|
|
} |
57 |
|
|
} |
58 |
|
|
|
59 |
|
✗ |
void SerialPort::enumPorts(EnumPortsDelegate const& callback) |
60 |
|
|
{ |
61 |
|
✗ |
void const* param = &callback; |
62 |
|
✗ |
SerialPort::enumPorts(&enumPortsCallback, const_cast<void*>(param)); |
63 |
|
✗ |
} |
64 |
|
|
|
65 |
|
✗ |
static gate_bool_t enumPortsCallbackArray(char const* portid, char const* description, void* userparam) |
66 |
|
|
{ |
67 |
|
✗ |
ArrayList<String>* ptrList = static_cast<ArrayList<String>*>(userparam); |
68 |
|
|
try |
69 |
|
|
{ |
70 |
|
✗ |
String portId(portid); |
71 |
|
✗ |
ptrList->add(portId); |
72 |
|
✗ |
return true; |
73 |
|
|
} |
74 |
|
✗ |
catch (...) |
75 |
|
|
{ |
76 |
|
✗ |
return false; |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
|
80 |
|
✗ |
Array<String> SerialPort::enumPorts() |
81 |
|
|
{ |
82 |
|
✗ |
ArrayList<String> ret; |
83 |
|
✗ |
SerialPort::enumPorts(&enumPortsCallbackArray, static_cast<void*>(&ret)); |
84 |
|
✗ |
return ret.toArray(); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
✗ |
SerialPort::porthandle_t SerialPort::open(String const& portId, |
88 |
|
|
uint32_t baudrate, enumint_t bits, enumint_t parity, enumint_t stopbits, enumint_t flowcontrol, |
89 |
|
|
uint32_t timeoutMs, bool_t asynchronous) |
90 |
|
|
{ |
91 |
|
✗ |
porthandle_t ret = NULL; |
92 |
|
✗ |
result_t result = gate_serialport_open(portId.c_impl(), baudrate, bits, parity, stopbits, flowcontrol, timeoutMs, asynchronous, &ret); |
93 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
94 |
|
✗ |
return ret; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
✗ |
result_t SerialPort::close(porthandle_t port) |
98 |
|
|
{ |
99 |
|
✗ |
return gate_serialport_close(port); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
✗ |
size_t SerialPort::read(porthandle_t port, char* buffer, size_t bufferlen) |
103 |
|
|
{ |
104 |
|
✗ |
gate_size_t retrieved = 0; |
105 |
|
✗ |
result_t result = gate_serialport_read(port, buffer, bufferlen, &retrieved); |
106 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
107 |
|
✗ |
return retrieved; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
✗ |
size_t SerialPort::write(porthandle_t port, char const* buffer, size_t bufferlen) |
111 |
|
|
{ |
112 |
|
✗ |
gate_size_t written = 0; |
113 |
|
✗ |
result_t result = gate_serialport_write(port, buffer, bufferlen, &written); |
114 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
115 |
|
✗ |
return written; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
✗ |
Stream SerialPort::openStream(String const& portId, |
119 |
|
|
uint32_t baudrate, enumint_t bits, enumint_t parity, enumint_t stopbits, enumint_t flowcontrol) |
120 |
|
|
{ |
121 |
|
✗ |
gate_stream_t* ptrStream = NULL; |
122 |
|
✗ |
result_t result = gate_serialport_openstream(portId.c_impl(), baudrate, bits, parity, stopbits, flowcontrol, &ptrStream); |
123 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
124 |
|
✗ |
return Stream(ptrStream); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
} // end of namespace io |
128 |
|
|
} // end of namespace gate |
129 |
|
|
|