X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fserial.c;h=de97496a8748d698feed02f9eafe8ce47693a099;hb=c73d2ea421c2b425c3f0ae33bce2bfd0c448ca5f;hp=f6efe3e26465e30d42411adcd08e7d922479701f;hpb=e46b8fb154ba90ffec9c1f805399dfa819e736f9;p=libsigrok.git diff --git a/hardware/common/serial.c b/hardware/common/serial.c index f6efe3e2..de97496a 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -1,7 +1,7 @@ /* * This file is part of the sigrok project. * - * Copyright (C) 2010 Bert Vermeulen + * Copyright (C) 2010-2012 Bert Vermeulen * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,22 +23,22 @@ #include #include #ifdef _WIN32 -#include +#include #else #include #include #endif #include #include -#include -#include +#include "sigrok.h" +#include "sigrok-internal.h" // FIXME: Must be moved, or rather passed as function argument. #ifdef _WIN32 -HANDLE hdl; +static HANDLE hdl; #endif -const char *serial_port_glob[] = { +static const char *serial_port_glob[] = { /* Linux */ "/dev/ttyS*", "/dev/ttyUSB*", @@ -50,14 +50,14 @@ const char *serial_port_glob[] = { NULL, }; -GSList *list_serial_ports(void) +SR_PRIV GSList *list_serial_ports(void) { GSList *ports; #ifdef _WIN32 /* TODO */ ports = NULL; - ports = g_slist_append(ports, strdup("COM1")); + ports = g_slist_append(ports, g_strdup("COM1")); #else glob_t g; unsigned int i, j; @@ -67,7 +67,7 @@ GSList *list_serial_ports(void) if (glob(serial_port_glob[i], 0, NULL, &g)) continue; for (j = 0; j < g.gl_pathc; j++) - ports = g_slist_append(ports, strdup(g.gl_pathv[j])); + ports = g_slist_append(ports, g_strdup(g.gl_pathv[j])); globfree(&g); } #endif @@ -75,7 +75,7 @@ GSList *list_serial_ports(void) return ports; } -int serial_open(const char *pathname, int flags) +SR_PRIV int serial_open(const char *pathname, int flags) { #ifdef _WIN32 /* FIXME: Don't hardcode COM1. */ @@ -94,7 +94,7 @@ int serial_open(const char *pathname, int flags) * Close the serial port. * Returns 0 upon success, -1 upon failure. */ -int serial_close(int fd) +SR_PRIV int serial_close(int fd) { #ifdef _WIN32 /* Returns non-zero upon success, 0 upon failure. */ @@ -109,7 +109,7 @@ int serial_close(int fd) * Flush serial port buffers (if any). * Returns 0 upon success, -1 upon failure. */ -int serial_flush(int fd) +SR_PRIV int serial_flush(int fd) { #ifdef _WIN32 /* Returns non-zero upon success, 0 upon failure. */ @@ -124,7 +124,7 @@ int serial_flush(int fd) * Write a number of bytes to the specified serial port. * Returns the number of bytes written, or -1 upon failure. */ -int serial_write(int fd, const void *buf, size_t count) +SR_PRIV int serial_write(int fd, const void *buf, size_t count) { #ifdef _WIN32 DWORD tmp = 0; @@ -142,7 +142,7 @@ int serial_write(int fd, const void *buf, size_t count) * Read a number of bytes from the specified serial port. * Returns the number of bytes read, or -1 upon failure. */ -int serial_read(int fd, void *buf, size_t count) +SR_PRIV int serial_read(int fd, void *buf, size_t count) { #ifdef _WIN32 DWORD tmp = 0; @@ -156,21 +156,26 @@ int serial_read(int fd, void *buf, size_t count) #endif } -void *serial_backup_params(int fd) +SR_PRIV void *serial_backup_params(int fd) { #ifdef _WIN32 /* TODO */ #else struct termios *term; - term = malloc(sizeof(struct termios)); + /* TODO: 'term' is never g_free()'d? */ + if (!(term = g_try_malloc(sizeof(struct termios)))) { + sr_err("serial: %s: term malloc failed", __func__); + return NULL; + } + tcgetattr(fd, term); return term; #endif } -void serial_restore_params(int fd, void *backup) +SR_PRIV void serial_restore_params(int fd, void *backup) { #ifdef _WIN32 /* TODO */ @@ -185,8 +190,8 @@ void serial_restore_params(int fd, void *backup) * flowcontrol: 1 = rts/cts, 2 = xon/xoff * parity: 0 = none, 1 = even, 2 = odd */ -int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, - int flowcontrol) +SR_PRIV int serial_set_params(int fd, int speed, int bits, int parity, + int stopbits, int flowcontrol) { #ifdef _WIN32 DCB dcb; @@ -243,9 +248,11 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, case 115200: baud = B115200; break; +#ifndef __APPLE__ case 460800: baud = B460800; break; +#endif default: return SR_ERR; }