| 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.h" | ||
| 30 | #include "gate/threading.h" | ||
| 31 | #include "gate/results.h" | ||
| 32 | #include "gate/debugging.h" | ||
| 33 | |||
| 34 | #if defined(GATE_SYS_WIN16) | ||
| 35 | # define GATE_IO_SERIALPORTS_WIN16_IMPL 1 | ||
| 36 | #elif defined(GATE_SYS_WIN) | ||
| 37 | # define GATE_IO_SERIALPORTS_WINAPI_IMPL 1 | ||
| 38 | #elif defined(GATE_SYS_POSIX) | ||
| 39 | # define GATE_IO_SERIALPORTS_POSIX_IMPL 1 | ||
| 40 | #elif defined(GATE_SYS_EFI) | ||
| 41 | # define GATE_IO_SERIALPORTS_EFI_IMPL 1 | ||
| 42 | #elif defined(GATE_SYS_DOS) | ||
| 43 | # define GATE_IO_SERIALPORTS_DOS_IMPL 1 | ||
| 44 | #else | ||
| 45 | # define GATE_IO_SERIALPORTS_NO_IMPL 1 | ||
| 46 | #endif | ||
| 47 | |||
| 48 | |||
| 49 | typedef struct gate_serialport_stream | ||
| 50 | { | ||
| 51 | GATE_INTERFACE_VTBL(gate_resourcestream) const* vtbl; | ||
| 52 | |||
| 53 | gate_atomic_int_t ref_counter; | ||
| 54 | gate_serialport_t port_handle; | ||
| 55 | char buffer[4096]; | ||
| 56 | gate_size_t buffer_used; | ||
| 57 | |||
| 58 | } gate_serialport_stream_t; | ||
| 59 | |||
| 60 | ✗ | static void gate_serialport_stream_release(void* self) | |
| 61 | { | ||
| 62 | ✗ | gate_serialport_stream_t* stream = (gate_serialport_stream_t*)self; | |
| 63 | ✗ | if (0 == gate_atomic_int_dec(&stream->ref_counter)) | |
| 64 | { | ||
| 65 | ✗ | gate_serialport_close(stream->port_handle); | |
| 66 | ✗ | gate_mem_dealloc(stream); | |
| 67 | } | ||
| 68 | ✗ | } | |
| 69 | ✗ | static int gate_serialport_stream_retain(void* self) | |
| 70 | { | ||
| 71 | ✗ | gate_serialport_stream_t* stream = (gate_serialport_stream_t*)self; | |
| 72 | ✗ | return gate_atomic_int_inc(&stream->ref_counter); | |
| 73 | } | ||
| 74 | ✗ | static char const* gate_serialport_stream_get_interface_name(void* self) | |
| 75 | { | ||
| 76 | (void)self; | ||
| 77 | ✗ | return GATE_INTERFACE_NAME_RESOURCESTREAM; | |
| 78 | } | ||
| 79 | ✗ | static gate_result_t gate_serialport_stream_peek(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned) | |
| 80 | { | ||
| 81 | ✗ | gate_serialport_stream_t* stream = (gate_serialport_stream_t*)self; | |
| 82 | gate_result_t ret; | ||
| 83 | |||
| 84 | do | ||
| 85 | { | ||
| 86 | ✗ | if (stream->buffer_used == 0) | |
| 87 | { | ||
| 88 | ✗ | ret = gate_serialport_read(stream->port_handle, &stream->buffer[0], sizeof(stream->buffer), &stream->buffer_used); | |
| 89 | ✗ | if (GATE_FAILED(ret)) | |
| 90 | { | ||
| 91 | ✗ | stream->buffer_used = 0; | |
| 92 | ✗ | if (ret != GATE_RESULT_NODATA) | |
| 93 | { | ||
| 94 | ✗ | break; | |
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | ✗ | ret = GATE_RESULT_OK; | |
| 100 | |||
| 101 | ✗ | if (stream->buffer_used != 0) | |
| 102 | { | ||
| 103 | ✗ | if (bufferlength > stream->buffer_used) | |
| 104 | { | ||
| 105 | ✗ | bufferlength = stream->buffer_used; | |
| 106 | } | ||
| 107 | ✗ | gate_mem_copy(buffer, &stream->buffer[0], bufferlength); | |
| 108 | } | ||
| 109 | else | ||
| 110 | { | ||
| 111 | ✗ | bufferlength = 0; | |
| 112 | } | ||
| 113 | |||
| 114 | ✗ | if (returned != NULL) | |
| 115 | { | ||
| 116 | ✗ | *returned = bufferlength; | |
| 117 | } | ||
| 118 | } while (0); | ||
| 119 | |||
| 120 | ✗ | return ret; | |
| 121 | } | ||
| 122 | ✗ | static gate_result_t gate_serialport_stream_read(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned) | |
| 123 | { | ||
| 124 | ✗ | gate_serialport_stream_t* stream = (gate_serialport_stream_t*)self; | |
| 125 | gate_result_t ret; | ||
| 126 | ✗ | gate_size_t bytesreceived = 0; | |
| 127 | ✗ | gate_uint32_t delay = 0; | |
| 128 | static gate_uint32_t const max_delay = 50; | ||
| 129 | ✗ | while (bytesreceived == 0) | |
| 130 | { | ||
| 131 | ✗ | ret = gate_serialport_stream_peek(self, buffer, bufferlength, &bytesreceived); | |
| 132 | ✗ | if (GATE_FAILED(ret)) | |
| 133 | { | ||
| 134 | ✗ | break; | |
| 135 | } | ||
| 136 | else | ||
| 137 | { | ||
| 138 | ✗ | if (bytesreceived == 0) | |
| 139 | { | ||
| 140 | ✗ | gate_thread_sleep(delay); | |
| 141 | ✗ | if (delay < max_delay) | |
| 142 | { | ||
| 143 | ✗ | ++delay; | |
| 144 | } | ||
| 145 | } | ||
| 146 | else | ||
| 147 | { | ||
| 148 | ✗ | if (bytesreceived < stream->buffer_used) | |
| 149 | { | ||
| 150 | ✗ | gate_mem_move(&stream->buffer[0], &stream->buffer[bytesreceived], stream->buffer_used - bytesreceived); | |
| 151 | ✗ | stream->buffer_used -= bytesreceived; | |
| 152 | } | ||
| 153 | else | ||
| 154 | { | ||
| 155 | ✗ | stream->buffer_used = 0; | |
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | ✗ | if (returned != NULL) | |
| 161 | { | ||
| 162 | ✗ | *returned = bytesreceived; | |
| 163 | } | ||
| 164 | ✗ | return ret; | |
| 165 | } | ||
| 166 | |||
| 167 | ✗ | static gate_result_t gate_serialport_stream_write(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written) | |
| 168 | { | ||
| 169 | ✗ | gate_serialport_stream_t* stream = (gate_serialport_stream_t*)self; | |
| 170 | ✗ | return gate_serialport_write(stream->port_handle, buffer, bufferlength, written); | |
| 171 | } | ||
| 172 | ✗ | static gate_result_t gate_serialport_stream_flush(void* self) | |
| 173 | { | ||
| 174 | (void)self; | ||
| 175 | ✗ | return GATE_RESULT_OK; | |
| 176 | } | ||
| 177 | |||
| 178 | ✗ | static gate_result_t gate_serialport_stream_get_resource(void* self, gate_enumint_t resource_type, gate_uintptr_t* resource) | |
| 179 | { | ||
| 180 | ✗ | gate_result_t ret = GATE_RESULT_OK; | |
| 181 | ✗ | gate_serialport_stream_t* stream = (gate_serialport_stream_t*)self; | |
| 182 | |||
| 183 | ✗ | switch (resource_type) | |
| 184 | { | ||
| 185 | ✗ | case GATE_STREAM_RESOURCE_DEFAULT: | |
| 186 | case GATE_STREAM_RESOURCE_INPUT: | ||
| 187 | ✗ | case GATE_STREAM_RESOURCE_OUTPUT: *resource = (gate_uintptr_t)stream->port_handle; | |
| 188 | ✗ | default: | |
| 189 | ✗ | ret = GATE_RESULT_NOTSUPPORTED; | |
| 190 | } | ||
| 191 | ✗ | return ret; | |
| 192 | } | ||
| 193 | |||
| 194 | static GATE_INTERFACE_VTBL(gate_resourcestream) gate_serialport_stream_vtbl; | ||
| 195 | ✗ | static void gate_init_serialport_stream_vtbl() | |
| 196 | { | ||
| 197 | ✗ | if (!gate_serialport_stream_vtbl.get_interface_name) | |
| 198 | { | ||
| 199 | GATE_INTERFACE_VTBL(gate_resourcestream) const local_vtbl = | ||
| 200 | { | ||
| 201 | &gate_serialport_stream_get_interface_name, | ||
| 202 | &gate_serialport_stream_release, | ||
| 203 | &gate_serialport_stream_retain, | ||
| 204 | |||
| 205 | &gate_serialport_stream_read, | ||
| 206 | &gate_serialport_stream_peek, | ||
| 207 | &gate_serialport_stream_write, | ||
| 208 | &gate_serialport_stream_flush, | ||
| 209 | |||
| 210 | &gate_serialport_stream_get_resource | ||
| 211 | }; | ||
| 212 | ✗ | gate_serialport_stream_vtbl = local_vtbl; | |
| 213 | } | ||
| 214 | ✗ | } | |
| 215 | |||
| 216 | ✗ | gate_result_t gate_serialport_openstream( | |
| 217 | gate_string_t const* port_id, gate_uint32_t baudrate, | ||
| 218 | gate_enumint_t bits, gate_enumint_t parity, gate_enumint_t stopbits, gate_enumint_t flowcontrol, | ||
| 219 | gate_stream_t** stream) | ||
| 220 | { | ||
| 221 | gate_result_t ret; | ||
| 222 | gate_serialport_stream_t* ptr; | ||
| 223 | |||
| 224 | ✗ | ptr = (gate_serialport_stream_t*)gate_mem_alloc(sizeof(gate_serialport_stream_t)); | |
| 225 | ✗ | if (ptr == NULL) | |
| 226 | { | ||
| 227 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 228 | } | ||
| 229 | else | ||
| 230 | { | ||
| 231 | ✗ | gate_mem_clear(ptr, sizeof(gate_serialport_stream_t)); | |
| 232 | ✗ | gate_init_serialport_stream_vtbl(); | |
| 233 | ✗ | ptr->vtbl = &gate_serialport_stream_vtbl; | |
| 234 | ✗ | gate_atomic_int_init(&ptr->ref_counter, 1); | |
| 235 | ✗ | ptr->buffer_used = 0; | |
| 236 | ✗ | ret = gate_serialport_open(port_id, baudrate, bits, parity, stopbits, flowcontrol, 0, false, &ptr->port_handle); | |
| 237 | ✗ | if (GATE_FAILED(ret)) | |
| 238 | { | ||
| 239 | ✗ | gate_mem_dealloc(ptr); | |
| 240 | } | ||
| 241 | else | ||
| 242 | { | ||
| 243 | ✗ | *stream = (gate_stream_t*)ptr; | |
| 244 | } | ||
| 245 | } | ||
| 246 | ✗ | return ret; | |
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | #if defined(GATE_SYS_WIN) | ||
| 251 | |||
| 252 | #include "gate/platforms.h" | ||
| 253 | |||
| 254 | static gate_result_t winapi_setup_comm(HANDLE hfile, DCB* dcb, | ||
| 255 | gate_uint32_t baudrate, unsigned bits, unsigned parity, unsigned stopbits, | ||
| 256 | unsigned flowcontrol, gate_uint32_t timeout, gate_bool_t asynchronous) | ||
| 257 | { | ||
| 258 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 259 | |||
| 260 | do | ||
| 261 | { | ||
| 262 | #if !defined(GATE_SYS_WIN16) | ||
| 263 | dcb->DCBlength = sizeof(dcb); | ||
| 264 | #endif | ||
| 265 | if (FALSE == GetCommState(hfile, dcb)) | ||
| 266 | { | ||
| 267 | gate_win32_print_lasterror(&ret, NULL, 0); | ||
| 268 | break; | ||
| 269 | } | ||
| 270 | if (baudrate != 0) | ||
| 271 | { | ||
| 272 | dcb->BaudRate = baudrate; | ||
| 273 | } | ||
| 274 | dcb->ByteSize = (BYTE)bits; | ||
| 275 | dcb->fParity = TRUE; | ||
| 276 | dcb->fNull = FALSE; | ||
| 277 | dcb->fBinary = TRUE; | ||
| 278 | #if !defined(GATE_SYS_WIN16) | ||
| 279 | dcb->fAbortOnError = FALSE; | ||
| 280 | #endif | ||
| 281 | |||
| 282 | switch (parity) | ||
| 283 | { | ||
| 284 | case GATE_SERIALPORT_PARITY_ODD: { dcb->Parity = ODDPARITY; break; } | ||
| 285 | case GATE_SERIALPORT_PARITY_EVEN: { dcb->Parity = EVENPARITY; break; } | ||
| 286 | case GATE_SERIALPORT_PARITY_MARK: { dcb->Parity = MARKPARITY; break; } | ||
| 287 | case GATE_SERIALPORT_PARITY_SPACE: { dcb->Parity = SPACEPARITY; break; } | ||
| 288 | case GATE_SERIALPORT_PARITY_NONE: | ||
| 289 | default: { dcb->Parity = NOPARITY; break; } | ||
| 290 | } | ||
| 291 | |||
| 292 | switch (stopbits) | ||
| 293 | { | ||
| 294 | case GATE_SERIALPORT_STOPBITS_1_5: { dcb->StopBits = ONE5STOPBITS; break; } | ||
| 295 | case GATE_SERIALPORT_STOPBITS_2_0: { dcb->StopBits = TWOSTOPBITS; break; } | ||
| 296 | case GATE_SERIALPORT_STOPBITS_1_0: | ||
| 297 | default: { dcb->StopBits = ONESTOPBIT; break; } | ||
| 298 | } | ||
| 299 | |||
| 300 | switch (flowcontrol) | ||
| 301 | { | ||
| 302 | case GATE_SERIALPORT_FLOWCTRL_HARDWARE: | ||
| 303 | { | ||
| 304 | dcb->fInX = FALSE; | ||
| 305 | dcb->fOutX = FALSE; | ||
| 306 | dcb->fOutxDsrFlow = FALSE; | ||
| 307 | dcb->fOutxCtsFlow = FALSE; | ||
| 308 | #if defined(GATE_SYS_WIN16) | ||
| 309 | dcb->fDtrDisable = 0; | ||
| 310 | dcb->fRtsDisable = 0; | ||
| 311 | #else | ||
| 312 | dcb->fDtrControl = DTR_CONTROL_ENABLE; | ||
| 313 | dcb->fRtsControl = RTS_CONTROL_ENABLE; | ||
| 314 | #endif | ||
| 315 | break; | ||
| 316 | } | ||
| 317 | case GATE_SERIALPORT_FLOWCTRL_HANDSHAKE: | ||
| 318 | { | ||
| 319 | dcb->fInX = FALSE; | ||
| 320 | dcb->fOutX = FALSE; | ||
| 321 | dcb->fOutxDsrFlow = FALSE; | ||
| 322 | dcb->fOutxCtsFlow = FALSE; | ||
| 323 | #if defined(GATE_SYS_WIN16) | ||
| 324 | dcb->fDtrDisable = 0; | ||
| 325 | dcb->fRtsDisable = 0; | ||
| 326 | #else | ||
| 327 | dcb->fDtrControl = DTR_CONTROL_HANDSHAKE; | ||
| 328 | dcb->fRtsControl = RTS_CONTROL_HANDSHAKE; | ||
| 329 | #endif | ||
| 330 | break; | ||
| 331 | } | ||
| 332 | case GATE_SERIALPORT_FLOWCTRL_XON: | ||
| 333 | { | ||
| 334 | dcb->fInX = TRUE; | ||
| 335 | dcb->fOutX = TRUE; | ||
| 336 | dcb->fOutxDsrFlow = FALSE; | ||
| 337 | dcb->fOutxCtsFlow = FALSE; | ||
| 338 | #if defined(GATE_SYS_WIN16) | ||
| 339 | dcb->fDtrDisable = 1; | ||
| 340 | dcb->fRtsDisable = 1; | ||
| 341 | #else | ||
| 342 | dcb->fDtrControl = DTR_CONTROL_DISABLE; | ||
| 343 | dcb->fRtsControl = RTS_CONTROL_DISABLE; | ||
| 344 | #endif | ||
| 345 | break; | ||
| 346 | } | ||
| 347 | case GATE_SERIALPORT_FLOWCTRL_NONE: | ||
| 348 | default: | ||
| 349 | { | ||
| 350 | dcb->fInX = FALSE; | ||
| 351 | dcb->fOutX = FALSE; | ||
| 352 | dcb->fOutxDsrFlow = FALSE; | ||
| 353 | dcb->fOutxCtsFlow = FALSE; | ||
| 354 | #if defined(GATE_SYS_WIN16) | ||
| 355 | dcb->fDtrDisable = 1; | ||
| 356 | dcb->fRtsDisable = 1; | ||
| 357 | #else | ||
| 358 | dcb->fDtrControl = DTR_CONTROL_DISABLE; | ||
| 359 | dcb->fRtsControl = RTS_CONTROL_DISABLE; | ||
| 360 | #endif | ||
| 361 | break; | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | #if defined(GATE_SYS_WIN16) | ||
| 366 | if (0 != SetCommState(dcb)) | ||
| 367 | #else | ||
| 368 | if (FALSE == SetCommState(hfile, dcb)) | ||
| 369 | #endif | ||
| 370 | { | ||
| 371 | gate_win32_print_lasterror(&ret, NULL, 0); | ||
| 372 | break; | ||
| 373 | } | ||
| 374 | ret = GATE_RESULT_OK; | ||
| 375 | } while (0); | ||
| 376 | |||
| 377 | return ret; | ||
| 378 | } | ||
| 379 | |||
| 380 | #endif | ||
| 381 | |||
| 382 | |||
| 383 | #if defined(GATE_IO_SERIALPORTS_WIN16_IMPL) | ||
| 384 | |||
| 385 | #define GATE_IO_SERIALPORTS_DEFAULT_READ_BUFFER 1024 | ||
| 386 | #define GATE_IO_SERIALPORTS_DEFAULT_WRITE_BUFFER 128 | ||
| 387 | |||
| 388 | #include "tchar.h" | ||
| 389 | |||
| 390 | gate_result_t gate_serialport_enum(gate_serialport_enum_callback_t callback, void* userparam) | ||
| 391 | { | ||
| 392 | return GATE_RESULT_NOTSUPPORTED; | ||
| 393 | } | ||
| 394 | |||
| 395 | gate_result_t gate_serialport_open( | ||
| 396 | gate_string_t const* port_id, gate_uint32_t baudrate, | ||
| 397 | gate_enumint_t bits, gate_enumint_t parity, gate_enumint_t stopbits, gate_enumint_t flowcontrol, | ||
| 398 | gate_uint32_t timeout, gate_bool_t asynchronous, gate_serialport_t* port_handle) | ||
| 399 | { | ||
| 400 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 401 | char port_name[256]; | ||
| 402 | int handle = -1; | ||
| 403 | DCB dcb; | ||
| 404 | do | ||
| 405 | { | ||
| 406 | handle = OpenComm(port_name, GATE_IO_SERIALPORTS_DEFAULT_READ_BUFFER, GATE_IO_SERIALPORTS_DEFAULT_WRITE_BUFFER); | ||
| 407 | if (handle < 0) | ||
| 408 | { | ||
| 409 | ret = GATE_RESULT_FAILED; | ||
| 410 | break; | ||
| 411 | } | ||
| 412 | |||
| 413 | ret = winapi_setup_comm(handle, &dcb, baudrate, bits, parity, stopbits, flowcontrol, timeout, asynchronous); | ||
| 414 | GATE_BREAK_IF_FAILED(ret); | ||
| 415 | |||
| 416 | if (port_handle) | ||
| 417 | { | ||
| 418 | *port_handle = (gate_serialport_t)handle; | ||
| 419 | handle = -1; | ||
| 420 | } | ||
| 421 | ret = GATE_RESULT_OK; | ||
| 422 | } while (0); | ||
| 423 | |||
| 424 | if (handle >= 0) | ||
| 425 | { | ||
| 426 | CloseComm(handle); | ||
| 427 | } | ||
| 428 | |||
| 429 | return ret; | ||
| 430 | } | ||
| 431 | |||
| 432 | gate_result_t gate_serialport_close(gate_serialport_t port_handle) | ||
| 433 | { | ||
| 434 | int handle = (int)port_handle; | ||
| 435 | if (handle < 0) | ||
| 436 | { | ||
| 437 | return GATE_RESULT_INVALIDARG; | ||
| 438 | } | ||
| 439 | else | ||
| 440 | { | ||
| 441 | CloseComm(handle); | ||
| 442 | return GATE_RESULT_OK; | ||
| 443 | } | ||
| 444 | |||
| 445 | } | ||
| 446 | gate_result_t gate_serialport_read(gate_serialport_t port_handle, char* buffer, gate_size_t bufferlen, gate_size_t* bytesreceived) | ||
| 447 | { | ||
| 448 | int handle = (int)port_handle; | ||
| 449 | int result; | ||
| 450 | |||
| 451 | if (bufferlen > GATE_IO_SERIALPORTS_DEFAULT_READ_BUFFER) | ||
| 452 | { | ||
| 453 | bufferlen = GATE_IO_SERIALPORTS_DEFAULT_READ_BUFFER; | ||
| 454 | } | ||
| 455 | result = ReadComm(handle, &buffer[0], (int)bufferlen); | ||
| 456 | if (result < 0) | ||
| 457 | { | ||
| 458 | if (bytesreceived) | ||
| 459 | { | ||
| 460 | *bytesreceived = (gate_size_t)(-result); | ||
| 461 | } | ||
| 462 | return GATE_RESULT_FAILED; | ||
| 463 | } | ||
| 464 | else | ||
| 465 | { | ||
| 466 | if (bytesreceived) | ||
| 467 | { | ||
| 468 | *bytesreceived = (gate_size_t)result; | ||
| 469 | |||
| 470 | } | ||
| 471 | return GATE_RESULT_OK; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | gate_result_t gate_serialport_write(gate_serialport_t port_handle, char const* buffer, gate_size_t bufferlen, gate_size_t* byteswritten) | ||
| 475 | { | ||
| 476 | int handle = (int)port_handle; | ||
| 477 | int result; | ||
| 478 | |||
| 479 | if (bufferlen > GATE_IO_SERIALPORTS_DEFAULT_WRITE_BUFFER) | ||
| 480 | { | ||
| 481 | bufferlen = GATE_IO_SERIALPORTS_DEFAULT_WRITE_BUFFER; | ||
| 482 | } | ||
| 483 | result = WriteComm(handle, &buffer[0], (int)bufferlen); | ||
| 484 | if (result < 0) | ||
| 485 | { | ||
| 486 | if (byteswritten) | ||
| 487 | { | ||
| 488 | *byteswritten = (gate_size_t)(-result); | ||
| 489 | } | ||
| 490 | return GATE_RESULT_FAILED; | ||
| 491 | } | ||
| 492 | else | ||
| 493 | { | ||
| 494 | if (byteswritten) | ||
| 495 | { | ||
| 496 | *byteswritten = (gate_size_t)result; | ||
| 497 | } | ||
| 498 | return GATE_RESULT_OK; | ||
| 499 | } | ||
| 500 | } | ||
| 501 | |||
| 502 | |||
| 503 | #endif /* GATE_IO_SERIALPORTS_WIN16_IMPL */ | ||
| 504 | |||
| 505 | |||
| 506 | |||
| 507 | #if defined(GATE_IO_SERIALPORTS_WINAPI_IMPL) | ||
| 508 | |||
| 509 | # include "gate/platforms.h" | ||
| 510 | # include "gate/platform/windows/win32registry.h" | ||
| 511 | |||
| 512 | |||
| 513 | #if defined(GATE_SYS_WINCE) | ||
| 514 | |||
| 515 | typedef struct | ||
| 516 | { | ||
| 517 | gate_serialport_enum_callback_t callback; | ||
| 518 | void* userparam; | ||
| 519 | //gate_win32_registry_enum_keys | ||
| 520 | |||
| 521 | } gate_serialport_enum_registry_param_t; | ||
| 522 | |||
| 523 | static gate_bool_t gate_serialport_enum_registry_callback(LPCTSTR base_path, LPCTSTR sub_path, void* userparam) | ||
| 524 | { | ||
| 525 | gate_bool_t ret = true; | ||
| 526 | gate_serialport_enum_registry_param_t* param = (gate_serialport_enum_registry_param_t*)userparam; | ||
| 527 | gate_result_t result; | ||
| 528 | char com_name[GATE_MAX_FILENAME_LENGTH]; | ||
| 529 | gate_size_t com_name_used; | ||
| 530 | TCHAR com_key[GATE_MAX_FILEPATH_LENGTH]; | ||
| 531 | gate_size_t com_key_used; | ||
| 532 | char com_descr[GATE_MAX_FILENAME_LENGTH]; | ||
| 533 | gate_size_t com_descr_used = sizeof(com_descr); | ||
| 534 | |||
| 535 | do | ||
| 536 | { | ||
| 537 | result = gate_win32_registry_read_str(GATE_WIN32_REGISTRY_LOCALMACHINE, sub_path, _T("Name"), | ||
| 538 | com_name, sizeof(com_name), &com_name_used); | ||
| 539 | if (GATE_FAILED(result)) | ||
| 540 | { | ||
| 541 | break; | ||
| 542 | } | ||
| 543 | |||
| 544 | if (0 != gate_str_pos(com_name, com_name_used, "COM", 3, 0)) | ||
| 545 | { | ||
| 546 | break; | ||
| 547 | } | ||
| 548 | |||
| 549 | com_key[0] = 0; | ||
| 550 | com_descr[0] = 0; | ||
| 551 | |||
| 552 | result = gate_win32_registry_read_winstr(GATE_WIN32_REGISTRY_LOCALMACHINE, sub_path, _T("Key"), | ||
| 553 | com_key, sizeof(com_key) / sizeof(com_key[0]), &com_key_used); | ||
| 554 | if (GATE_SUCCEEDED(result)) | ||
| 555 | { | ||
| 556 | gate_win32_registry_read_str(GATE_WIN32_REGISTRY_LOCALMACHINE, com_key, _T("FriendlyName"), | ||
| 557 | com_descr, sizeof(com_descr), &com_descr_used); | ||
| 558 | } | ||
| 559 | |||
| 560 | if (param->callback) | ||
| 561 | { | ||
| 562 | ret = param->callback(com_name, com_descr, param->userparam); | ||
| 563 | } | ||
| 564 | } while (0); | ||
| 565 | return ret; | ||
| 566 | } | ||
| 567 | |||
| 568 | |||
| 569 | gate_result_t gate_serialport_enum(gate_serialport_enum_callback_t callback, void* userparam) | ||
| 570 | { | ||
| 571 | gate_result_t ret = GATE_RESULT_OK; | ||
| 572 | gate_serialport_enum_registry_param_t param; | ||
| 573 | |||
| 574 | param.callback = callback; | ||
| 575 | param.userparam = userparam; | ||
| 576 | |||
| 577 | ret = gate_win32_registry_enum_keys(GATE_WIN32_REGISTRY_LOCALMACHINE, _T("Drivers\\Active\\"), | ||
| 578 | &gate_serialport_enum_registry_callback, ¶m); | ||
| 579 | |||
| 580 | return ret; | ||
| 581 | } | ||
| 582 | |||
| 583 | #elif defined(GATE_SYS_WINSTORE) /* defined(GATE_SYS_WINCE) */ | ||
| 584 | |||
| 585 | gate_result_t gate_serialport_enum(gate_serialport_enum_callback_t callback, void* userparam) | ||
| 586 | { | ||
| 587 | return GATE_RESULT_NOTSUPPORTED; | ||
| 588 | } | ||
| 589 | |||
| 590 | #else /* defined(GATE_SYS_WINSTORE) */ | ||
| 591 | |||
| 592 | # include <setupapi.h> | ||
| 593 | |||
| 594 | gate_result_t gate_serialport_enum(gate_serialport_enum_callback_t callback, void* userparam) | ||
| 595 | { | ||
| 596 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 597 | DWORD dwClassCount = 0; | ||
| 598 | DWORD ndxClass; | ||
| 599 | DWORD ndxInfo; | ||
| 600 | GUID classGuids[256]; | ||
| 601 | HDEVINFO hdevinfo; | ||
| 602 | SP_DEVINFO_DATA devinfo_data; | ||
| 603 | HKEY hKey; | ||
| 604 | DWORD dwType; | ||
| 605 | TCHAR buffer[GATE_MAX_FILENAME_LENGTH * 2]; | ||
| 606 | DWORD bufferlen; | ||
| 607 | char portid[GATE_MAX_FILENAME_LENGTH * 2]; | ||
| 608 | char descr[GATE_MAX_FILENAME_LENGTH * 2]; | ||
| 609 | gate_bool_t continueEnum = true; | ||
| 610 | gate_win32_setupapi_t const* const setupapi = gate_win32_setupapi(); | ||
| 611 | |||
| 612 | do | ||
| 613 | { | ||
| 614 | if ((setupapi->SetupApiDiDestroyDeviceInfoList == NULL) | ||
| 615 | || (setupapi->SetupApiDiGetClassDevs == NULL) | ||
| 616 | || (setupapi->SetupApiDiEnumDeviceInfo == NULL) | ||
| 617 | || (setupapi->SetupApiDiGetDeviceRegistryProperty == NULL) | ||
| 618 | || (setupapi->SetupApiDiClassGuidsFromName == NULL) | ||
| 619 | || (setupapi->SetupApiDiOpenDevRegKey == NULL) | ||
| 620 | || (gate_platform.AdvRegQueryValueEx == NULL) | ||
| 621 | || (gate_platform.AdvRegCloseKey == NULL) | ||
| 622 | ) | ||
| 623 | { | ||
| 624 | ret = GATE_RESULT_NOTSUPPORTED; | ||
| 625 | break; | ||
| 626 | } | ||
| 627 | |||
| 628 | if (!setupapi->SetupApiDiClassGuidsFromName(_T("Ports"), classGuids, sizeof(classGuids) / sizeof(classGuids[0]), &dwClassCount)) | ||
| 629 | { | ||
| 630 | ret = GATE_RESULT_FAILED; | ||
| 631 | break; | ||
| 632 | } | ||
| 633 | |||
| 634 | devinfo_data.cbSize = sizeof(devinfo_data); | ||
| 635 | |||
| 636 | for (ndxClass = 0; continueEnum && (ndxClass != dwClassCount); ++ndxClass) | ||
| 637 | { | ||
| 638 | hdevinfo = (HDEVINFO)setupapi->SetupApiDiGetClassDevs(&classGuids[ndxClass], NULL, NULL, DIGCF_PRESENT); | ||
| 639 | if (INVALID_HANDLE_VALUE == hdevinfo) | ||
| 640 | { | ||
| 641 | continue; | ||
| 642 | } | ||
| 643 | |||
| 644 | for (ndxInfo = 0; continueEnum && setupapi->SetupApiDiEnumDeviceInfo(hdevinfo, ndxInfo, &devinfo_data); ++ndxInfo) | ||
| 645 | { | ||
| 646 | hKey = setupapi->SetupApiDiOpenDevRegKey(hdevinfo, &devinfo_data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE); | ||
| 647 | if (hKey == INVALID_HANDLE_VALUE) | ||
| 648 | { | ||
| 649 | continue; | ||
| 650 | } | ||
| 651 | |||
| 652 | dwType = 0; | ||
| 653 | bufferlen = sizeof(buffer) - sizeof(buffer[0]); | ||
| 654 | |||
| 655 | if (ERROR_SUCCESS == gate_platform.AdvRegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (LPBYTE)buffer, &bufferlen)) | ||
| 656 | { | ||
| 657 | if (REG_SZ == dwType) | ||
| 658 | { | ||
| 659 | buffer[bufferlen / sizeof(TCHAR)] = 0; | ||
| 660 | } | ||
| 661 | if (0 != gate_win32_winstr_2_utf8(buffer, bufferlen / sizeof(TCHAR), portid, sizeof(portid))) | ||
| 662 | { | ||
| 663 | if (setupapi->SetupApiDiGetDeviceRegistryProperty(hdevinfo, &devinfo_data, SPDRP_FRIENDLYNAME, NULL, (PBYTE)&buffer[0], sizeof(buffer), &bufferlen)) | ||
| 664 | { | ||
| 665 | gate_win32_winstr_2_utf8(buffer, bufferlen / sizeof(TCHAR), descr, sizeof(descr)); | ||
| 666 | } | ||
| 667 | else | ||
| 668 | { | ||
| 669 | descr[0] = 0; | ||
| 670 | } | ||
| 671 | continueEnum &= callback(portid, descr, userparam); | ||
| 672 | } | ||
| 673 | } | ||
| 674 | gate_platform.AdvRegCloseKey(hKey); | ||
| 675 | } | ||
| 676 | setupapi->SetupApiDiDestroyDeviceInfoList(hdevinfo); | ||
| 677 | ret = GATE_RESULT_OK; | ||
| 678 | } | ||
| 679 | } while (0); | ||
| 680 | return ret; | ||
| 681 | } | ||
| 682 | #endif | ||
| 683 | |||
| 684 | |||
| 685 | gate_result_t gate_serialport_open( | ||
| 686 | gate_string_t const* port_id, gate_uint32_t baudrate, | ||
| 687 | gate_enumint_t bits, gate_enumint_t parity, gate_enumint_t stopbits, gate_enumint_t flowcontrol, | ||
| 688 | gate_uint32_t timeout, gate_bool_t asynchronous, gate_serialport_t* port_handle) | ||
| 689 | { | ||
| 690 | gate_result_t ret; | ||
| 691 | TCHAR path[GATE_MAX_FILENAME_LENGTH * 2] = _T("\\\\.\\"); | ||
| 692 | HANDLE hfile = INVALID_HANDLE_VALUE; | ||
| 693 | DCB dcb; | ||
| 694 | #if !defined(GATE_SYS_WIN16) | ||
| 695 | COMMTIMEOUTS cto; | ||
| 696 | #endif | ||
| 697 | |||
| 698 | do | ||
| 699 | { | ||
| 700 | gate_win32_utf8_2_winstr(port_id->str, port_id->length, &path[4], sizeof(path) / sizeof(path[0]) - 5); | ||
| 701 | hfile = gate_win32_createfile(path, GENERIC_READ | GENERIC_WRITE, 0, OPEN_EXISTING, 0, | ||
| 702 | (asynchronous ? FILE_FLAG_OVERLAPPED : 0), NULL); | ||
| 703 | if (hfile == INVALID_HANDLE_VALUE) | ||
| 704 | { | ||
| 705 | gate_win32_print_lasterror(&ret, NULL, 0); | ||
| 706 | break; | ||
| 707 | } | ||
| 708 | |||
| 709 | ret = winapi_setup_comm(hfile, &dcb, baudrate, bits, parity, stopbits, flowcontrol, timeout, asynchronous); | ||
| 710 | GATE_BREAK_IF_FAILED(ret); | ||
| 711 | |||
| 712 | if (timeout != 0) | ||
| 713 | { | ||
| 714 | #if !defined(GATE_SYS_WIN16) | ||
| 715 | if (GetCommTimeouts(hfile, &cto)) | ||
| 716 | { | ||
| 717 | cto.ReadIntervalTimeout = 0; | ||
| 718 | cto.ReadTotalTimeoutMultiplier = 0; | ||
| 719 | cto.ReadTotalTimeoutConstant = 10; | ||
| 720 | |||
| 721 | /* | ||
| 722 | cto.ReadIntervalTimeout = MAXDWORD; | ||
| 723 | cto.ReadTotalTimeoutConstant = timeout; | ||
| 724 | cto.ReadIntervalTimeout = 0; | ||
| 725 | cto.ReadTotalTimeoutMultiplier = 0; | ||
| 726 | cto.ReadTotalTimeoutConstant = 0; | ||
| 727 | */ | ||
| 728 | |||
| 729 | cto.WriteTotalTimeoutMultiplier = 1; | ||
| 730 | cto.WriteTotalTimeoutConstant = 1; | ||
| 731 | /* | ||
| 732 | cto.WriteTotalTimeoutConstant = readTimeout ? *readTimeout : 100; | ||
| 733 | */ | ||
| 734 | |||
| 735 | SetCommTimeouts(hfile, &cto); | ||
| 736 | } | ||
| 737 | #endif | ||
| 738 | } | ||
| 739 | /* | ||
| 740 | SetCommMask(hfile, EV_RXCHAR | EV_TXEMPTY); | ||
| 741 | */ | ||
| 742 | |||
| 743 | *port_handle = (gate_serialport_t)hfile; | ||
| 744 | hfile = INVALID_HANDLE_VALUE; | ||
| 745 | ret = GATE_RESULT_OK; | ||
| 746 | } while (0); | ||
| 747 | |||
| 748 | if ((hfile != INVALID_HANDLE_VALUE) && (hfile != (HANDLE)NULL)) | ||
| 749 | { | ||
| 750 | CloseHandle(hfile); | ||
| 751 | } | ||
| 752 | |||
| 753 | return ret; | ||
| 754 | } | ||
| 755 | gate_result_t gate_serialport_close(gate_serialport_t port_handle) | ||
| 756 | { | ||
| 757 | HANDLE hfile = (HANDLE)port_handle; | ||
| 758 | /* | ||
| 759 | SetCommMask(hfile, 0); | ||
| 760 | */ | ||
| 761 | CloseHandle(hfile); | ||
| 762 | return GATE_RESULT_OK; | ||
| 763 | } | ||
| 764 | gate_result_t gate_serialport_read(gate_serialport_t port_handle, char* buffer, gate_size_t bufferlen, gate_size_t* bytesreceived) | ||
| 765 | { | ||
| 766 | gate_result_t ret; | ||
| 767 | HANDLE hfile = (HANDLE)port_handle; | ||
| 768 | DWORD bytesread; | ||
| 769 | DWORD dwError = 0; | ||
| 770 | COMSTAT status = GATE_INIT_EMPTY; | ||
| 771 | |||
| 772 | do | ||
| 773 | { | ||
| 774 | if (ClearCommError(hfile, &dwError, &status)) | ||
| 775 | { | ||
| 776 | if (status.cbInQue == 0) | ||
| 777 | { | ||
| 778 | ret = GATE_RESULT_NODATA; | ||
| 779 | break; | ||
| 780 | } | ||
| 781 | if (status.cbInQue < bufferlen) | ||
| 782 | { | ||
| 783 | bufferlen = (gate_size_t)status.cbInQue; | ||
| 784 | } | ||
| 785 | } | ||
| 786 | if (FALSE == gate_win32_readfile(hfile, buffer, (DWORD)bufferlen, &bytesread, NULL)) | ||
| 787 | { | ||
| 788 | gate_win32_print_lasterror(&ret, NULL, 0); | ||
| 789 | break; | ||
| 790 | } | ||
| 791 | if (bytesreceived != NULL) | ||
| 792 | { | ||
| 793 | *bytesreceived = (gate_size_t)bytesread; | ||
| 794 | } | ||
| 795 | ret = GATE_RESULT_OK; | ||
| 796 | |||
| 797 | } while (0); | ||
| 798 | |||
| 799 | return ret; | ||
| 800 | } | ||
| 801 | |||
| 802 | gate_result_t gate_serialport_write(gate_serialport_t port_handle, char const* buffer, gate_size_t bufferlen, gate_size_t* byteswritten) | ||
| 803 | { | ||
| 804 | gate_result_t ret; | ||
| 805 | HANDLE hfile = (HANDLE)port_handle; | ||
| 806 | DWORD written; | ||
| 807 | |||
| 808 | do | ||
| 809 | { | ||
| 810 | if (FALSE == gate_win32_writefile(hfile, buffer, (DWORD)bufferlen, &written, NULL)) | ||
| 811 | { | ||
| 812 | gate_win32_print_lasterror(&ret, NULL, 0); | ||
| 813 | break; | ||
| 814 | } | ||
| 815 | if (byteswritten != NULL) | ||
| 816 | { | ||
| 817 | *byteswritten = (gate_size_t)written; | ||
| 818 | } | ||
| 819 | ret = GATE_RESULT_OK; | ||
| 820 | } while (0); | ||
| 821 | |||
| 822 | return ret; | ||
| 823 | } | ||
| 824 | |||
| 825 | #endif /* GATE_IO_SERIALPORTS_WINAPI_IMPL */ | ||
| 826 | |||
| 827 | |||
| 828 | |||
| 829 | |||
| 830 | #if defined(GATE_IO_SERIALPORTS_POSIX_IMPL) | ||
| 831 | |||
| 832 | #include "gate/platforms.h" | ||
| 833 | #include "gate/files.h" | ||
| 834 | |||
| 835 | #include <termios.h> | ||
| 836 | |||
| 837 | struct gate_serialport_enum_dir_param | ||
| 838 | { | ||
| 839 | gate_serialport_enum_callback_t callback; | ||
| 840 | void* callback_param; | ||
| 841 | }; | ||
| 842 | |||
| 843 | ✗ | static gate_bool_t gate_serialport_enum_dir_cb(gate_file_entry_t const* entry, void* userparam) | |
| 844 | { | ||
| 845 | ✗ | struct gate_serialport_enum_dir_param* param = (struct gate_serialport_enum_dir_param*)userparam; | |
| 846 | gate_result_t result; | ||
| 847 | gate_string_t sub_dir_path; | ||
| 848 | ✗ | gate_string_create_static(&sub_dir_path, entry->path); | |
| 849 | |||
| 850 | ✗ | result = gate_file_exists(&sub_dir_path); | |
| 851 | ✗ | if (GATE_SUCCEEDED(result)) | |
| 852 | { | ||
| 853 | char path[4096]; | ||
| 854 | ✗ | gate_file_build_path(path, sizeof(path), sub_dir_path.str, sub_dir_path.length, "device", 6); | |
| 855 | //TODO: implementation | ||
| 856 | } | ||
| 857 | ✗ | return true; | |
| 858 | } | ||
| 859 | |||
| 860 | ✗ | gate_result_t gate_serialport_enum(gate_serialport_enum_callback_t callback, void* userparam) | |
| 861 | { | ||
| 862 | ✗ | gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED; | |
| 863 | static gate_string_t const sys_dir = { "/sys/class/tty", 14, NULL }; | ||
| 864 | struct gate_serialport_enum_dir_param param; | ||
| 865 | |||
| 866 | ✗ | param.callback = callback; | |
| 867 | ✗ | param.callback_param = userparam; | |
| 868 | |||
| 869 | ✗ | ret = gate_file_list(&sys_dir, &gate_serialport_enum_dir_cb, ¶m); | |
| 870 | ✗ | return ret; | |
| 871 | } | ||
| 872 | |||
| 873 | ✗ | gate_result_t gate_serialport_open( | |
| 874 | gate_string_t const* port_id, gate_uint32_t baudrate, | ||
| 875 | gate_enumint_t bits, gate_enumint_t parity, gate_enumint_t stopbits, gate_enumint_t flowcontrol, | ||
| 876 | gate_uint32_t timeout, gate_bool_t asynchronous, gate_serialport_t* port_handle) | ||
| 877 | { | ||
| 878 | ✗ | gate_result_t ret = GATE_RESULT_FAILED; | |
| 879 | gate_platform_stream_t stream; | ||
| 880 | |||
| 881 | ✗ | gate_platform_stream_set_invalid(&stream); | |
| 882 | do | ||
| 883 | { | ||
| 884 | ✗ | char path[GATE_MAX_FILEPATH_LENGTH] = GATE_INIT_EMPTY; | |
| 885 | int fd; | ||
| 886 | struct termios term_info; | ||
| 887 | |||
| 888 | ✗ | gate_string_to_buffer(port_id, path, sizeof(path)); | |
| 889 | ✗ | ret = gate_platform_stream_open(path, GATE_PLATFORM_STREAM_OPEN_READWRITE, 0, &stream); | |
| 890 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 891 | |||
| 892 | ✗ | fd = (int)(gate_intptr_t)stream; | |
| 893 | |||
| 894 | ✗ | if (0 != tcgetattr(fd, &term_info)) | |
| 895 | { | ||
| 896 | |||
| 897 | } | ||
| 898 | |||
| 899 | ✗ | if (port_handle) | |
| 900 | { | ||
| 901 | ✗ | *port_handle = stream; | |
| 902 | ✗ | gate_platform_stream_set_invalid(&stream); | |
| 903 | } | ||
| 904 | ✗ | ret = GATE_RESULT_OK; | |
| 905 | } while (0); | ||
| 906 | |||
| 907 | ✗ | if (gate_platform_stream_valid(stream)) | |
| 908 | { | ||
| 909 | ✗ | gate_platform_stream_close(stream); | |
| 910 | } | ||
| 911 | |||
| 912 | ✗ | return ret; | |
| 913 | } | ||
| 914 | ✗ | gate_result_t gate_serialport_close(gate_serialport_t port_handle) | |
| 915 | { | ||
| 916 | ✗ | gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED; | |
| 917 | |||
| 918 | ✗ | return ret; | |
| 919 | } | ||
| 920 | ✗ | gate_result_t gate_serialport_read(gate_serialport_t port_handle, char* buffer, gate_size_t bufferlen, gate_size_t* bytesreceived) | |
| 921 | { | ||
| 922 | ✗ | gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED; | |
| 923 | |||
| 924 | ✗ | return ret; | |
| 925 | } | ||
| 926 | ✗ | gate_result_t gate_serialport_write(gate_serialport_t port_handle, char const* buffer, gate_size_t bufferlen, gate_size_t* byteswritten) | |
| 927 | { | ||
| 928 | ✗ | gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED; | |
| 929 | |||
| 930 | ✗ | return ret; | |
| 931 | } | ||
| 932 | |||
| 933 | |||
| 934 | #endif /* GATE_IO_SERIALPORTS_POSIX_IMPL */ | ||
| 935 | |||
| 936 | |||
| 937 | |||
| 938 | |||
| 939 | #if defined(GATE_IO_SERIALPORTS_EFI_IMPL) | ||
| 940 | |||
| 941 | #include "gate/platform/efi/efi_gate.h" | ||
| 942 | |||
| 943 | static char const* const gate_serial_port_efi_id = "COM"; | ||
| 944 | |||
| 945 | gate_result_t gate_serialport_enum(gate_serialport_enum_callback_t callback, void* userparam) | ||
| 946 | { | ||
| 947 | gate_result_t result; | ||
| 948 | void* ser_port = NULL; | ||
| 949 | |||
| 950 | result = gate_platform_efi_serport_get(&ser_port); | ||
| 951 | if (GATE_SUCCEEDED(result)) | ||
| 952 | { | ||
| 953 | if (callback) | ||
| 954 | { | ||
| 955 | callback(gate_serial_port_efi_id, "EFI Serial Port", userparam); | ||
| 956 | } | ||
| 957 | } | ||
| 958 | return result; | ||
| 959 | } | ||
| 960 | |||
| 961 | #define GATE_SERIALPORT_PARITY_NONE 0 | ||
| 962 | #define GATE_SERIALPORT_PARITY_ODD 1 | ||
| 963 | #define GATE_SERIALPORT_PARITY_EVEN 2 | ||
| 964 | #define GATE_SERIALPORT_PARITY_MARK 3 | ||
| 965 | #define GATE_SERIALPORT_PARITY_SPACE 4 | ||
| 966 | |||
| 967 | #define GATE_SERIALPORT_STOPBITS_1_0 10 | ||
| 968 | #define GATE_SERIALPORT_STOPBITS_1_5 15 | ||
| 969 | #define GATE_SERIALPORT_STOPBITS_2_0 20 | ||
| 970 | |||
| 971 | gate_result_t gate_serialport_open( | ||
| 972 | gate_string_t const* port_id, gate_uint32_t baudrate, | ||
| 973 | gate_enumint_t bits, gate_enumint_t parity, gate_enumint_t stopbits, gate_enumint_t flowcontrol, | ||
| 974 | gate_uint32_t timeout_ms, gate_bool_t asynchronous, gate_serialport_t* port_handle) | ||
| 975 | { | ||
| 976 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 977 | void* efi_ser_port = NULL; | ||
| 978 | gate_uint8_t efi_parity = GATE_PLATFORM_EFI_SERIAL_PARITY_DEFAULT; | ||
| 979 | gate_uint8_t efi_stopbits = GATE_PLATFORM_EFI_SERIAL_STOPBITS_DEFAULT; | ||
| 980 | |||
| 981 | do | ||
| 982 | { | ||
| 983 | if (!gate_string_equals_str(port_id, gate_serial_port_efi_id)) | ||
| 984 | { | ||
| 985 | ret = GATE_RESULT_NOMATCH; | ||
| 986 | break; | ||
| 987 | } | ||
| 988 | |||
| 989 | ret = gate_platform_efi_serport_get(&efi_ser_port); | ||
| 990 | GATE_BREAK_IF_FAILED(ret); | ||
| 991 | |||
| 992 | switch (parity) | ||
| 993 | { | ||
| 994 | case GATE_SERIALPORT_PARITY_NONE: efi_parity = GATE_PLATFORM_EFI_SERIAL_PARITY_NO; break; | ||
| 995 | case GATE_SERIALPORT_PARITY_ODD: efi_parity = GATE_PLATFORM_EFI_SERIAL_PARITY_ODD; break; | ||
| 996 | case GATE_SERIALPORT_PARITY_EVEN: efi_parity = GATE_PLATFORM_EFI_SERIAL_PARITY_EVEN; break; | ||
| 997 | case GATE_SERIALPORT_PARITY_MARK: efi_parity = GATE_PLATFORM_EFI_SERIAL_PARITY_MARK; break; | ||
| 998 | case GATE_SERIALPORT_PARITY_SPACE: efi_parity = GATE_PLATFORM_EFI_SERIAL_PARITY_SPACE; break; | ||
| 999 | } | ||
| 1000 | |||
| 1001 | switch (stopbits) | ||
| 1002 | { | ||
| 1003 | case GATE_SERIALPORT_STOPBITS_1_0: efi_stopbits = GATE_PLATFORM_EFI_SERIAL_STOPBITS_1; break; | ||
| 1004 | case GATE_SERIALPORT_STOPBITS_1_5: efi_stopbits = GATE_PLATFORM_EFI_SERIAL_STOPBITS_15; break; | ||
| 1005 | case GATE_SERIALPORT_STOPBITS_2_0: efi_stopbits = GATE_PLATFORM_EFI_SERIAL_STOPBITS_2; break; | ||
| 1006 | } | ||
| 1007 | |||
| 1008 | ret = gate_platform_efi_serport_setup(efi_ser_port, baudrate, timeout_ms * 1000, efi_parity, bits, efi_stopbits); | ||
| 1009 | GATE_BREAK_IF_FAILED(ret); | ||
| 1010 | |||
| 1011 | if (port_handle) | ||
| 1012 | { | ||
| 1013 | *port_handle = efi_ser_port; | ||
| 1014 | } | ||
| 1015 | ret = GATE_RESULT_OK; | ||
| 1016 | |||
| 1017 | } while (0); | ||
| 1018 | |||
| 1019 | return ret; | ||
| 1020 | } | ||
| 1021 | |||
| 1022 | gate_result_t gate_serialport_close(gate_serialport_t port_handle) | ||
| 1023 | { | ||
| 1024 | (void)port_handle; | ||
| 1025 | return GATE_RESULT_OK; | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | gate_result_t gate_serialport_read(gate_serialport_t port_handle, char* buffer, gate_size_t bufferlen, gate_size_t* bytesreceived) | ||
| 1029 | { | ||
| 1030 | gate_size_t received = 0; | ||
| 1031 | gate_result_t result = gate_platform_efi_serport_read(port_handle, (void*)buffer, bufferlen, &received); | ||
| 1032 | if (GATE_SUCCEEDED(result)) | ||
| 1033 | { | ||
| 1034 | if (bytesreceived) | ||
| 1035 | { | ||
| 1036 | *bytesreceived = received; | ||
| 1037 | } | ||
| 1038 | if (received == 0) | ||
| 1039 | { | ||
| 1040 | result = GATE_RESULT_NODATA; | ||
| 1041 | } | ||
| 1042 | } | ||
| 1043 | else if (result == GATE_RESULT_TIMEOUT) | ||
| 1044 | { | ||
| 1045 | if (bytesreceived) | ||
| 1046 | { | ||
| 1047 | *bytesreceived = 0; | ||
| 1048 | } | ||
| 1049 | result = GATE_RESULT_NODATA; | ||
| 1050 | } | ||
| 1051 | return result; | ||
| 1052 | } | ||
| 1053 | |||
| 1054 | gate_result_t gate_serialport_write(gate_serialport_t port_handle, char const* buffer, gate_size_t bufferlen, gate_size_t* byteswritten) | ||
| 1055 | { | ||
| 1056 | return gate_platform_efi_serport_write(port_handle, (void const*)buffer, bufferlen, byteswritten); | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | #endif /* GATE_IO_SERIALPORTS_EFI_IMPL */ | ||
| 1060 | |||
| 1061 | |||
| 1062 | #if defined(GATE_IO_SERIALPORTS_DOS_IMPL) | ||
| 1063 | |||
| 1064 | #include <bios.h> | ||
| 1065 | #include "gate/times.h" | ||
| 1066 | |||
| 1067 | static gate_string_t const port_id_com1 = GATE_STRING_INIT_STATIC("COM1"); | ||
| 1068 | static gate_string_t const port_id_com2 = GATE_STRING_INIT_STATIC("COM2"); | ||
| 1069 | static gate_string_t const port_id_com3 = GATE_STRING_INIT_STATIC("COM3"); | ||
| 1070 | static gate_string_t const port_id_com4 = GATE_STRING_INIT_STATIC("COM4"); | ||
| 1071 | static gate_uint32_t port_timeout[4] = { 0 }; | ||
| 1072 | |||
| 1073 | gate_result_t gate_serialport_enum(gate_serialport_enum_callback_t callback, void* userparam) | ||
| 1074 | { | ||
| 1075 | if (callback) | ||
| 1076 | { | ||
| 1077 | callback(port_id_com1.str, port_id_com1.str, userparam); | ||
| 1078 | callback(port_id_com2.str, port_id_com2.str, userparam); | ||
| 1079 | callback(port_id_com3.str, port_id_com3.str, userparam); | ||
| 1080 | callback(port_id_com4.str, port_id_com4.str, userparam); | ||
| 1081 | } | ||
| 1082 | return GATE_RESULT_OK; | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | gate_result_t gate_serialport_open( | ||
| 1086 | gate_string_t const* port_id, gate_uint32_t baudrate, | ||
| 1087 | gate_enumint_t bits, gate_enumint_t parity, gate_enumint_t stopbits, gate_enumint_t flowcontrol, | ||
| 1088 | gate_uint32_t timeout_ms, gate_bool_t asynchronous, gate_serialport_t* port_handle) | ||
| 1089 | { | ||
| 1090 | unsigned config = 0; | ||
| 1091 | unsigned port = 0; | ||
| 1092 | |||
| 1093 | if (gate_string_equals(port_id, &port_id_com1)) | ||
| 1094 | { | ||
| 1095 | port = 0; | ||
| 1096 | } | ||
| 1097 | else if (gate_string_equals(port_id, &port_id_com2)) | ||
| 1098 | { | ||
| 1099 | port = 1; | ||
| 1100 | } | ||
| 1101 | else if (gate_string_equals(port_id, &port_id_com3)) | ||
| 1102 | { | ||
| 1103 | port = 2; | ||
| 1104 | } | ||
| 1105 | else if (gate_string_equals(port_id, &port_id_com4)) | ||
| 1106 | { | ||
| 1107 | port = 3; | ||
| 1108 | } | ||
| 1109 | else | ||
| 1110 | { | ||
| 1111 | return GATE_RESULT_INVALIDARG; | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | if (asynchronous) | ||
| 1115 | { | ||
| 1116 | return GATE_RESULT_INVALIDARG; | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | switch (flowcontrol) | ||
| 1120 | { | ||
| 1121 | case GATE_SERIALPORT_FLOWCTRL_NONE: | ||
| 1122 | break; | ||
| 1123 | default: | ||
| 1124 | return GATE_RESULT_INVALIDARG; | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | switch (bits) | ||
| 1128 | { | ||
| 1129 | case 7: | ||
| 1130 | config |= _COM_CHR7; | ||
| 1131 | break; | ||
| 1132 | case 8: | ||
| 1133 | config |= _COM_CHR8; | ||
| 1134 | break; | ||
| 1135 | default: | ||
| 1136 | return GATE_RESULT_INVALIDARG; | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | switch (parity) | ||
| 1140 | { | ||
| 1141 | case GATE_SERIALPORT_PARITY_NONE: | ||
| 1142 | config |= _COM_NOPARITY; | ||
| 1143 | break; | ||
| 1144 | case GATE_SERIALPORT_PARITY_ODD: | ||
| 1145 | config |= _COM_ODDPARITY; | ||
| 1146 | break; | ||
| 1147 | case GATE_SERIALPORT_PARITY_EVEN: | ||
| 1148 | config |= _COM_EVENPARITY; | ||
| 1149 | break; | ||
| 1150 | case GATE_SERIALPORT_PARITY_SPACE: | ||
| 1151 | config |= _COM_SPACEPARITY; | ||
| 1152 | break; | ||
| 1153 | default: | ||
| 1154 | return GATE_RESULT_INVALIDARG; | ||
| 1155 | } | ||
| 1156 | |||
| 1157 | switch (stopbits) | ||
| 1158 | { | ||
| 1159 | case GATE_SERIALPORT_STOPBITS_1_0: | ||
| 1160 | config |= _COM_STOP1; | ||
| 1161 | break; | ||
| 1162 | case GATE_SERIALPORT_STOPBITS_2_0: | ||
| 1163 | config |= _COM_STOP2; | ||
| 1164 | break; | ||
| 1165 | default: | ||
| 1166 | return GATE_RESULT_INVALIDARG; | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | switch (baudrate) | ||
| 1170 | { | ||
| 1171 | case GATE_SERIALPORT_BAUDRATE_110: | ||
| 1172 | config |= _COM_110; | ||
| 1173 | break; | ||
| 1174 | case GATE_SERIALPORT_BAUDRATE_300: | ||
| 1175 | config |= _COM_300; | ||
| 1176 | break; | ||
| 1177 | case GATE_SERIALPORT_BAUDRATE_600: | ||
| 1178 | config |= _COM_600; | ||
| 1179 | break; | ||
| 1180 | case GATE_SERIALPORT_BAUDRATE_1200: | ||
| 1181 | config |= _COM_1200; | ||
| 1182 | break; | ||
| 1183 | case GATE_SERIALPORT_BAUDRATE_2400: | ||
| 1184 | config |= _COM_2400; | ||
| 1185 | break; | ||
| 1186 | case GATE_SERIALPORT_BAUDRATE_4800: | ||
| 1187 | config |= _COM_4800; | ||
| 1188 | break; | ||
| 1189 | case GATE_SERIALPORT_BAUDRATE_9600: | ||
| 1190 | config |= _COM_9600; | ||
| 1191 | break; | ||
| 1192 | default: | ||
| 1193 | return GATE_RESULT_INVALIDARG; | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | _bios_serialcom(_COM_INIT, port, config); | ||
| 1197 | port_timeout[port] = timeout_ms; | ||
| 1198 | if (port_handle) | ||
| 1199 | { | ||
| 1200 | *port_handle = (gate_serialport_t)port; | ||
| 1201 | } | ||
| 1202 | return GATE_RESULT_OK; | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | #define GATE_SERIALPORT_BIOS_STATUS_DATA_RECEIVED 0x0100 | ||
| 1206 | #define GATE_SERIALPORT_BIOS_STATUS_OVERFLOW 0x0200 | ||
| 1207 | #define GATE_SERIALPORT_BIOS_STATUS_PARITY_ERROR 0x0400 | ||
| 1208 | #define GATE_SERIALPORT_BIOS_STATUS_PROTOCOL_ERROR 0x0800 | ||
| 1209 | #define GATE_SERIALPORT_BIOS_STATUS_INTERRUPTION 0x1000 | ||
| 1210 | #define GATE_SERIALPORT_BIOS_STATUS_HOLDREG 0x2000 | ||
| 1211 | #define GATE_SERIALPORT_BIOS_STATUS_SHIFTREG 0x4000 | ||
| 1212 | #define GATE_SERIALPORT_BIOS_STATUS_TIMEOUT 0x8000 | ||
| 1213 | |||
| 1214 | |||
| 1215 | gate_result_t gate_serialport_close(gate_serialport_t port_handle) | ||
| 1216 | { | ||
| 1217 | (void)port_handle; | ||
| 1218 | return GATE_RESULT_OK; | ||
| 1219 | } | ||
| 1220 | gate_result_t gate_serialport_read(gate_serialport_t port_handle, char* buffer, gate_size_t bufferlen, gate_size_t* bytesreceived) | ||
| 1221 | { | ||
| 1222 | static unsigned const avail_mask = GATE_SERIALPORT_BIOS_STATUS_HOLDREG | GATE_SERIALPORT_BIOS_STATUS_SHIFTREG | GATE_SERIALPORT_BIOS_STATUS_DATA_RECEIVED; | ||
| 1223 | static unsigned const error_mask = GATE_SERIALPORT_BIOS_STATUS_TIMEOUT | GATE_SERIALPORT_BIOS_STATUS_OVERFLOW | GATE_SERIALPORT_BIOS_STATUS_PARITY_ERROR | GATE_SERIALPORT_BIOS_STATUS_PROTOCOL_ERROR; | ||
| 1224 | //static unsigned avail_mask = GATE_SERIALPORT_BIOS_STATUS_DATA_RECEIVED; | ||
| 1225 | unsigned port = (unsigned)port_handle; | ||
| 1226 | unsigned old_status = 0; | ||
| 1227 | unsigned timeout_ms = port_timeout[port]; | ||
| 1228 | gate_timecounter_t now; | ||
| 1229 | gate_timecounter_t timeout_point; | ||
| 1230 | |||
| 1231 | gate_size_t received = 0; | ||
| 1232 | gate_timecounter_now(&now); | ||
| 1233 | timeout_point = gate_timecounter_add(now, (gate_int64_t)timeout_ms * 1000); | ||
| 1234 | |||
| 1235 | while ((received < bufferlen) && (now <= timeout_point)) | ||
| 1236 | { | ||
| 1237 | unsigned status = _bios_serialcom(_COM_STATUS, port, 0); | ||
| 1238 | if (status != old_status) | ||
| 1239 | { | ||
| 1240 | old_status = status; | ||
| 1241 | } | ||
| 1242 | if ((status & avail_mask) == 0) | ||
| 1243 | { | ||
| 1244 | /* nothing to read */ | ||
| 1245 | if (received > 0) | ||
| 1246 | { | ||
| 1247 | break; | ||
| 1248 | } | ||
| 1249 | } | ||
| 1250 | else | ||
| 1251 | { | ||
| 1252 | /* there is data to read */ | ||
| 1253 | unsigned data = _bios_serialcom(_COM_RECEIVE, port, 0); | ||
| 1254 | if ((data & error_mask) == 0) | ||
| 1255 | { | ||
| 1256 | buffer[received] = (char)(data & 0xff); | ||
| 1257 | ++received; | ||
| 1258 | } | ||
| 1259 | break; | ||
| 1260 | } | ||
| 1261 | gate_timecounter_now(&now); | ||
| 1262 | } | ||
| 1263 | |||
| 1264 | if (received > 0) | ||
| 1265 | { | ||
| 1266 | if (bytesreceived) | ||
| 1267 | { | ||
| 1268 | *bytesreceived = received; | ||
| 1269 | } | ||
| 1270 | return GATE_RESULT_OK; | ||
| 1271 | } | ||
| 1272 | else | ||
| 1273 | { | ||
| 1274 | return GATE_RESULT_NODATA; | ||
| 1275 | } | ||
| 1276 | } | ||
| 1277 | gate_result_t gate_serialport_write(gate_serialport_t port_handle, char const* buffer, gate_size_t bufferlen, gate_size_t* byteswritten) | ||
| 1278 | { | ||
| 1279 | unsigned port = (unsigned)port_handle; | ||
| 1280 | unsigned written = 0; | ||
| 1281 | |||
| 1282 | while (bufferlen > 0) | ||
| 1283 | { | ||
| 1284 | unsigned chr = (unsigned char)*buffer; | ||
| 1285 | unsigned status = _bios_serialcom(_COM_SEND, port, chr); | ||
| 1286 | (void)status; /* TODO */ | ||
| 1287 | --bufferlen; | ||
| 1288 | ++buffer; | ||
| 1289 | ++written; | ||
| 1290 | } | ||
| 1291 | if (byteswritten) | ||
| 1292 | { | ||
| 1293 | *byteswritten = written; | ||
| 1294 | } | ||
| 1295 | return GATE_RESULT_OK; | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | #endif /* GATE_IO_SERIALPORTS_DOS_IMPL */ | ||
| 1299 | |||
| 1300 | |||
| 1301 | |||
| 1302 | |||
| 1303 | #if defined(GATE_IO_SERIALPORTS_NO_IMPL) | ||
| 1304 | |||
| 1305 | gate_result_t gate_serialport_enum(gate_serialport_enum_callback_t callback, void* userparam) | ||
| 1306 | { | ||
| 1307 | GATE_UNUSED_ARG(callback); | ||
| 1308 | GATE_UNUSED_ARG(userparam); | ||
| 1309 | return GATE_RESULT_NOTIMPLEMENTED; | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | gate_result_t gate_serialport_open( | ||
| 1313 | gate_string_t const* port_id, gate_uint32_t baudrate, | ||
| 1314 | gate_enumint_t bits, gate_enumint_t parity, gate_enumint_t stopbits, gate_enumint_t flowcontrol, | ||
| 1315 | gate_uint32_t timeout, gate_bool_t asynchronous, gate_serialport_t* port_handle) | ||
| 1316 | { | ||
| 1317 | GATE_UNUSED_ARG(port_id); | ||
| 1318 | GATE_UNUSED_ARG(baudrate); | ||
| 1319 | GATE_UNUSED_ARG(bits); | ||
| 1320 | GATE_UNUSED_ARG(parity); | ||
| 1321 | GATE_UNUSED_ARG(stopbits); | ||
| 1322 | GATE_UNUSED_ARG(flowcontrol); | ||
| 1323 | GATE_UNUSED_ARG(timeout); | ||
| 1324 | GATE_UNUSED_ARG(asynchronous); | ||
| 1325 | GATE_UNUSED_ARG(port_handle); | ||
| 1326 | return GATE_RESULT_NOTIMPLEMENTED; | ||
| 1327 | } | ||
| 1328 | |||
| 1329 | gate_result_t gate_serialport_close(gate_serialport_t port_handle) | ||
| 1330 | { | ||
| 1331 | GATE_UNUSED_ARG(port_handle); | ||
| 1332 | return GATE_RESULT_NOTIMPLEMENTED; | ||
| 1333 | } | ||
| 1334 | gate_result_t gate_serialport_read(gate_serialport_t port_handle, char* buffer, gate_size_t bufferlen, gate_size_t* bytesreceived) | ||
| 1335 | { | ||
| 1336 | GATE_UNUSED_ARG(port_handle); | ||
| 1337 | GATE_UNUSED_ARG(buffer); | ||
| 1338 | GATE_UNUSED_ARG(bufferlen); | ||
| 1339 | GATE_UNUSED_ARG(bytesreceived); | ||
| 1340 | return GATE_RESULT_NOTIMPLEMENTED; | ||
| 1341 | } | ||
| 1342 | gate_result_t gate_serialport_write(gate_serialport_t port_handle, char const* buffer, gate_size_t bufferlen, gate_size_t* byteswritten) | ||
| 1343 | { | ||
| 1344 | GATE_UNUSED_ARG(port_handle); | ||
| 1345 | GATE_UNUSED_ARG(buffer); | ||
| 1346 | GATE_UNUSED_ARG(bufferlen); | ||
| 1347 | GATE_UNUSED_ARG(byteswritten); | ||
| 1348 | return GATE_RESULT_NOTIMPLEMENTED; | ||
| 1349 | } | ||
| 1350 | |||
| 1351 | #endif /* GATE_IO_SERIALPORTS_NO_IMPL */ | ||
| 1352 |