#include "libserialport.h"
+struct sp_port {
+ char *name;
+#ifdef _WIN32
+ HANDLE hdl;
+#else
+ int fd;
+#endif
+};
+
struct port_data {
#ifdef _WIN32
DCB dcb;
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
-#define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
-
/* Debug output macros. */
#define DEBUG(fmt, ...) do { if (sp_debug_handler) sp_debug_handler(fmt ".\n", ##__VA_ARGS__); } while (0)
#define DEBUG_ERROR(err, msg) DEBUG("%s returning " #err ": " msg, __func__)
#define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = err; } while (0)
#define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__)
+#define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0)
+
/* Helper functions. */
static struct sp_port **list_append(struct sp_port **list, const char *portname);
static enum sp_return get_config(struct sp_port *port, struct port_data *data,
RETURN_OK();
}
+char *sp_get_port_name(const struct sp_port *port)
+{
+ TRACE("%p", port);
+
+ if (!port)
+ return NULL;
+
+ RETURN_VALUE("%s", port->name);
+}
+
enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
{
TRACE("%p, %p", port, copy_ptr);
enum sp_return sp_list_ports(struct sp_port ***list_ptr)
{
struct sp_port **list;
- int ret = SP_OK;
+ int ret = SP_ERR_SUPP;
TRACE("%p", list_ptr);
char *name;
int name_len;
+ ret = SP_OK;
+
DEBUG("Opening registry key");
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
CFTypeRef cf_path;
Boolean result;
+ ret = SP_OK;
+
DEBUG("Getting IOKit master port");
if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
SET_FAIL(ret, "IOMasterPort() failed");
int fd, ioctl_result;
struct serial_struct serial_info;
+ ret = SP_OK;
+
DEBUG("Enumerating tty devices");
ud = udev_new();
ud_enumerate = udev_enumerate_new(ud);
udev_unref(ud);
#endif
- if (ret == SP_OK) {
+ switch (ret) {
+ case SP_OK:
*list_ptr = list;
RETURN_OK();
- } else {
+ case SP_ERR_SUPP:
+ DEBUG_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform.");
+ default:
if (list)
sp_free_port_list(list);
*list_ptr = NULL;
RETURN_OK();
}
+enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config)
+{
+ struct port_data data;
+
+ TRACE("%p, %p", port, config);
+
+ CHECK_OPEN_PORT();
+
+ if (!config)
+ RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
+
+ TRY(get_config(port, &data, config));
+
+ RETURN_OK();
+}
+
enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
{
struct port_data data;
*signals |= SP_SIG_CTS;
if (bits & MS_DSR_ON)
*signals |= SP_SIG_DSR;
- if (bits & MS_RING_ON)
- *signals |= SP_SIG_DCD;
if (bits & MS_RLSD_ON)
+ *signals |= SP_SIG_DCD;
+ if (bits & MS_RING_ON)
*signals |= SP_SIG_RI;
#else
int bits;