-
-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathos_serial_ioctl.cpp
More file actions
159 lines (138 loc) · 4.32 KB
/
Copy pathos_serial_ioctl.cpp
File metadata and controls
159 lines (138 loc) · 4.32 KB
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
#include <stdlib.h>
#include <string.h>
#include "os_serial.h"
// ioctl headers conflict with standard posix headers for serial port, ioctl needed for custom
// baudrate, move this function to another compilation unit from serial.cpp
#if defined(_WIN32)
#include <windows.h>
// linux, also need to check that system headers installed
#elif defined(__linux__) && !defined(__ANDROID__)
#if defined __has_include
#if __has_include(<sys/ioctl.h>) && __has_include(</usr/include/asm/ioctls.h>) && __has_include(</usr/include/asm/termbits.h>)
#include <sys/ioctl.h>
#include </usr/include/asm/ioctls.h>
#include </usr/include/asm/termbits.h>
#else
#define NO_IOCTL_HEADERS
#endif
#else
#define NO_IOCTL_HEADERS
#endif
#elif defined(__APPLE__)
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#if defined __has_include
#if __has_include(<sys/ioctl.h>) && __has_include(<IOKit/serial/ioss.h>)
#include <sys/ioctl.h>
#include <IOKit/serial/ioss.h>
#else
#define NO_IOCTL_HEADERS
#endif
#else
#define NO_IOCTL_HEADERS
#endif
#endif
// to dont mess even more with if defined above handle one part separately
#if defined(__linux__) && !defined(__ANDROID__) && defined(NO_IOCTL_HEADERS)
#if defined __has_include
#if __has_include(<sys/ioctl.h>) && __has_include(</usr/include/asm-generic/ioctls.h>) && __has_include(</usr/include/asm-generic/termbits.h>)
#include <sys/ioctl.h>
#include </usr/include/asm-generic/ioctls.h>
#include </usr/include/asm-generic/termbits.h>
#undef NO_IOCTL_HEADERS
#else
#define NO_IOCTL_HEADERS
#endif
#else
#define NO_IOCTL_HEADERS
#endif
#endif
#if defined(_WIN32)
int OSSerial::set_custom_baudrate (int baudrate)
{
DCB dcb_serial_params = {0};
dcb_serial_params.DCBlength = sizeof (dcb_serial_params);
if (GetCommState (this->port_descriptor, &dcb_serial_params) == 0)
{
CloseHandle (this->port_descriptor);
return SerialExitCodes::GET_PORT_STATE_ERROR;
}
dcb_serial_params.BaudRate = baudrate;
if (SetCommState (this->port_descriptor, &dcb_serial_params) == 0)
{
CloseHandle (this->port_descriptor);
return SerialExitCodes::SET_PORT_STATE_ERROR;
}
return SerialExitCodes::OK;
}
int OSSerial::set_custom_latency (int latency)
{
// there is a method to set latency in registry but it restarts the device, keep it separate and
// dont add here
return SerialExitCodes::SET_PORT_STATE_ERROR;
}
#elif defined(__linux__) && !defined(__ANDROID__)
int OSSerial::set_custom_latency (int latency)
{
return SerialExitCodes::SET_PORT_STATE_ERROR;
}
#ifdef NO_IOCTL_HEADERS
int OSSerial::set_custom_baudrate (int baudrate)
{
return SerialExitCodes::NO_SYSTEM_HEADERS_FOUND_ERROR;
}
#else
int OSSerial::set_custom_baudrate (int baudrate)
{
struct termios2 port_settings;
memset (&port_settings, 0, sizeof (port_settings));
ioctl (this->port_descriptor, TCGETS2, &port_settings);
port_settings.c_cflag &= ~CBAUD; // Remove current BAUD rate
port_settings.c_cflag |= BOTHER; // Allow custom BAUD rate using int input
port_settings.c_ispeed = baudrate; // Set the input BAUD rate
port_settings.c_ospeed = baudrate; // Set the output BAUD rate
int r = ioctl (this->port_descriptor, TCSETS2, &port_settings);
if (r == 0)
{
return SerialExitCodes::OK;
}
else
{
return SerialExitCodes::SET_PORT_STATE_ERROR;
}
}
#endif
#elif defined(__APPLE__)
int OSSerial::set_custom_latency (int latency)
{
#ifndef NO_IOCTL_HEADERS
unsigned long mics = latency;
ioctl (this->port_descriptor, IOSSDATALAT, &mics);
return SerialExitCodes::OK;
#else
return SerialExitCodes::NO_SYSTEM_HEADERS_FOUND_ERROR;
#endif
}
int OSSerial::set_custom_baudrate (int baudrate)
{
struct termios port_settings;
memset (&port_settings, 0, sizeof (port_settings));
tcgetattr (this->port_descriptor, &port_settings);
cfsetispeed (&port_settings, baudrate);
cfsetospeed (&port_settings, baudrate);
if (tcsetattr (this->port_descriptor, TCSANOW, &port_settings) != 0)
return SerialExitCodes::SET_PORT_STATE_ERROR;
tcflush (this->port_descriptor, TCIOFLUSH);
return SerialExitCodes::OK;
}
#else
int OSSerial::set_custom_baudrate (int baudrate)
{
return SerialExitCodes::SET_PORT_STATE_ERROR;
}
int OSSerial::set_custom_latency (int latency)
{
return SerialExitCodes::SET_PORT_STATE_ERROR;
}
#endif