Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SerialPortErrorFormatter.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Common/ResultErrorFormatter.h"
5#include "SerialPortError.h"
6
7namespace SC
8{
11
12namespace detail
13{
14inline ResultErrorFormat formatSerialPortErrorWithContext(SerialPortError error, SerialPortErrorDetail detail,
15 uint32_t nativeError, Span<char> output)
16{
17 ResultErrorFormatter formatter(output);
18 switch (error)
19 {
20 case SerialPortError::InvalidBaudRate: formatter.append("Baud rate must be greater than zero"); break;
21 case SerialPortError::InvalidDataBits: formatter.append("Data bits value is invalid"); break;
22 case SerialPortError::InvalidParity: formatter.append("Parity value is invalid"); break;
23 case SerialPortError::InvalidStopBits: formatter.append("Stop bits value is invalid"); break;
24 case SerialPortError::InvalidFlowControl: formatter.append("Flow control value is invalid"); break;
25 case SerialPortError::InvalidHandle: formatter.append("Serial descriptor is invalid"); break;
26 case SerialPortError::UnsupportedPathEncoding: formatter.append("Serial path encoding is not supported"); break;
27 case SerialPortError::InvalidPath: formatter.append("Serial path is invalid"); break;
28 case SerialPortError::PathMustBeAbsolute: formatter.append("Serial path must be absolute"); break;
29 case SerialPortError::OpenFailed: formatter.append("Failed to open serial port"); break;
30 case SerialPortError::ReadSettingsFailed: formatter.append("Failed to read serial settings"); break;
31 case SerialPortError::SetSettingsFailed: formatter.append("Failed to set serial settings"); break;
32 case SerialPortError::UnsupportedHardwareFlowControl:
33 formatter.append("Hardware flow control is not supported");
34 break;
35 case SerialPortError::UnsupportedBaudRate: formatter.append("Baud rate is not supported"); break;
36 case SerialPortError::UnsupportedDataBits: formatter.append("Data bits value is not supported"); break;
37 case SerialPortError::UnsupportedParity: formatter.append("Parity value is not supported"); break;
38 case SerialPortError::UnsupportedStopBits: formatter.append("Stop bits value is not supported"); break;
39 case SerialPortError::PathTooLong: formatter.append("Serial path is too long"); break;
40 default: return ResultErrorFormatter::failure(ResultErrorFormatStatus::UnknownError, output);
41 }
42
43 Span<const char> detailText = {};
44 switch (detail)
45 {
46 case SerialPortErrorDetail::None: break;
47 case SerialPortErrorDetail::PosixOpen: detailText = {"POSIX open", sizeof("POSIX open") - 1}; break;
48 case SerialPortErrorDetail::PosixReadDescriptorFlags:
49 detailText = {"POSIX read descriptor flags", sizeof("POSIX read descriptor flags") - 1};
50 break;
51 case SerialPortErrorDetail::PosixSetDescriptorFlags:
52 detailText = {"POSIX set descriptor flags", sizeof("POSIX set descriptor flags") - 1};
53 break;
54 case SerialPortErrorDetail::PosixReadSettings:
55 detailText = {"POSIX read serial settings", sizeof("POSIX read serial settings") - 1};
56 break;
57 case SerialPortErrorDetail::PosixSetInputBaudRate:
58 detailText = {"POSIX set input baud rate", sizeof("POSIX set input baud rate") - 1};
59 break;
60 case SerialPortErrorDetail::PosixSetOutputBaudRate:
61 detailText = {"POSIX set output baud rate", sizeof("POSIX set output baud rate") - 1};
62 break;
63 case SerialPortErrorDetail::PosixSetSettings:
64 detailText = {"POSIX set serial settings", sizeof("POSIX set serial settings") - 1};
65 break;
66 case SerialPortErrorDetail::WindowsCreateFile:
67 detailText = {"Windows CreateFile", sizeof("Windows CreateFile") - 1};
68 break;
69 case SerialPortErrorDetail::WindowsSetTimeouts:
70 detailText = {"Windows SetCommTimeouts", sizeof("Windows SetCommTimeouts") - 1};
71 break;
72 case SerialPortErrorDetail::WindowsReadSettings:
73 detailText = {"Windows GetCommState", sizeof("Windows GetCommState") - 1};
74 break;
75 case SerialPortErrorDetail::WindowsSetSettings:
76 detailText = {"Windows SetCommState", sizeof("Windows SetCommState") - 1};
77 break;
78 default: return ResultErrorFormatter::failure(ResultErrorFormatStatus::UnknownError, output);
79 }
80
81 if (not detailText.empty() or nativeError != 0)
82 {
83 formatter.append(" (");
84 if (not detailText.empty())
85 {
86 formatter.append(detailText);
87 if (nativeError != 0)
88 formatter.append(", ");
89 }
90 if (nativeError != 0)
91 {
92 formatter.append("native error: ");
93 formatter.append(static_cast<uint64_t>(nativeError));
94 }
95 formatter.append(")");
96 }
97 return formatter.finish();
98}
99} // namespace detail
100
104inline ResultErrorFormat formatSerialPortError(SerialPortError error, Span<char> output)
105{
106 return detail::formatSerialPortErrorWithContext(error, SerialPortErrorDetail::None, 0, output);
107}
108
110inline ResultErrorFormat formatSerialPortError(Result result, Span<char> output)
111{
112 if (result)
113 return ResultErrorFormatter::failure(ResultErrorFormatStatus::NotAnError, output);
114 if (result.category() != SerialPortResultCategory)
115 return ResultErrorFormatter::failure(ResultErrorFormatStatus::ForeignCategory, output);
116 return formatSerialPortError(static_cast<SerialPortError>(result.errorValue()), output);
117}
118
120inline ResultErrorFormat formatSerialPortError(ResultSerialPort result, Span<char> output)
121{
122 if (result)
123 return ResultErrorFormatter::failure(ResultErrorFormatStatus::NotAnError, output);
124 if (result.result.category() != SerialPortResultCategory)
125 return ResultErrorFormatter::failure(ResultErrorFormatStatus::ForeignCategory, output);
126 return detail::formatSerialPortErrorWithContext(static_cast<SerialPortError>(result.result.errorValue()),
127 result.detail, result.nativeError, output);
128}
129
131} // namespace SC
SerialPortErrorDetail
Stable lower-level and backend-specific context for a SerialPort error.
Definition SerialPortError.h:38
SerialPortError
Stable error codes returned by the SerialPort library.
Definition SerialPortError.h:13
ResultErrorFormat formatSerialPortError(SerialPortError error, Span< char > output)
Formats a canonical English SerialPort diagnostic into caller-owned storage.
Definition SerialPortErrorFormatter.h:104
SerialPort result retaining optional lower-level and native error details.
Definition SerialPortError.h:60