extern void (*sp_debug_handler)(const char *format, ...);
/* Debug output macros. */
-#define DEBUG(fmt, ...) do { if (sp_debug_handler) sp_debug_handler(fmt ".\n", ##__VA_ARGS__); } while (0)
-#define DEBUG_ERROR(err, fmt, ...) DEBUG("%s returning " #err ": " fmt, __func__, ##__VA_ARGS__)
-#define DEBUG_FAIL(fmt, ...) do { \
+#define DEBUG_FMT(fmt, ...) do { \
+ if (sp_debug_handler) \
+ sp_debug_handler(fmt ".\n", __VA_ARGS__); \
+} while (0)
+#define DEBUG(msg) DEBUG_FMT(msg, NULL)
+#define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__)
+#define DEBUG_FAIL(msg) do { \
char *errmsg = sp_last_error_message(); \
- DEBUG("%s returning SP_ERR_FAIL: "fmt": %s", __func__,##__VA_ARGS__,errmsg); \
+ DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
sp_free_error_message(errmsg); \
} while (0);
-#define RETURN() do { DEBUG("%s returning", __func__); return; } while(0)
-#define RETURN_CODE(x) do { DEBUG("%s returning " #x, __func__); return x; } while (0)
+#define RETURN() do { \
+ DEBUG_FMT("%s returning", __func__); \
+ return; \
+} while(0)
+#define RETURN_CODE(x) do { \
+ DEBUG_FMT("%s returning " #x, __func__); \
+ return x; \
+} while (0)
#define RETURN_CODEVAL(x) do { \
switch (x) { \
case SP_OK: RETURN_CODE(SP_OK); \
} \
} while (0)
#define RETURN_OK() RETURN_CODE(SP_OK);
-#define RETURN_ERROR(err, ...) do { DEBUG_ERROR(err, __VA_ARGS__); return err; } while (0)
-#define RETURN_FAIL(...) do { DEBUG_FAIL(__VA_ARGS__); return SP_ERR_FAIL; } while (0)
+#define RETURN_ERROR(err, msg) do { \
+ DEBUG_ERROR(err, msg); \
+ return err; \
+} while (0)
+#define RETURN_FAIL(msg) do { \
+ DEBUG_FAIL(msg); \
+ return SP_ERR_FAIL; \
+} while (0)
#define RETURN_INT(x) do { \
int _x = x; \
- DEBUG("%s returning %d", __func__, _x); \
+ DEBUG_FMT("%s returning %d", __func__, _x); \
return _x; \
} while (0)
#define RETURN_STRING(x) do { \
char *_x = x; \
- DEBUG("%s returning %s", __func__, _x); \
+ DEBUG_FMT("%s returning %s", __func__, _x); \
return _x; \
} while (0)
#define RETURN_POINTER(x) do { \
void *_x = x; \
- DEBUG("%s returning %p", __func__, _x); \
+ DEBUG_FMT("%s returning %p", __func__, _x); \
return _x; \
} while (0)
#define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
#define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
-#define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__)
+#define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__)
+#define TRACE_VOID() DEBUG_FMT("%s() called", __func__)
#define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0)
int i, count;
if (strncmp(port->name, "/dev/", 5))
- RETURN_ERROR(SP_ERR_ARG, "Device name not recognized (%s)", port->name);
+ RETURN_ERROR(SP_ERR_ARG, "Device name not recognized.");
snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s", dev);
count = readlink(file_name, file_name, sizeof(file_name));
if (count <= 0 || count >= (int) sizeof(file_name)-1)
- RETURN_ERROR(SP_ERR_ARG, "Device not found (%s)", port->name);
+ RETURN_ERROR(SP_ERR_ARG, "Device not found.");
file_name[count] = 0;
if (strstr(file_name, "bluetooth"))
port->transport = SP_TRANSPORT_BLUETOOTH;
if (strstr(target, "virtual"))
continue;
snprintf(name, sizeof(name), "/dev/%s", entry.d_name);
- DEBUG("Found device %s", name);
+ DEBUG_FMT("Found device %s", name);
if (strstr(target, "serial8250")) {
/* The serial8250 driver has a hardcoded number of ports.
* The only way to tell which actually exist on a given system
}
#endif
}
- DEBUG("Found port %s", name);
+ DEBUG_FMT("Found port %s", name);
*list = list_append(*list, name);
if (!list) {
SET_ERROR(ret, SP_ERR_MEM, "list append failed");
if (!portname)
RETURN_ERROR(SP_ERR_ARG, "Null port name");
- DEBUG("Building structure for port %s", portname);
+ DEBUG_FMT("Building structure for port %s", portname);
if (!(port = malloc(sizeof(struct sp_port))))
RETURN_ERROR(SP_ERR_MEM, "Port structure malloc failed");
if (flags > (SP_MODE_READ | SP_MODE_WRITE))
RETURN_ERROR(SP_ERR_ARG, "Invalid flags");
- DEBUG("Opening port %s", port->name);
+ DEBUG_FMT("Opening port %s", port->name);
#ifdef _WIN32
DWORD desired_access = 0, flags_and_attributes = 0, errors;
CHECK_OPEN_PORT();
- DEBUG("Closing port %s", port->name);
+ DEBUG_FMT("Closing port %s", port->name);
#ifdef _WIN32
/* Returns non-zero upon success, 0 upon failure. */
const char *buffer_names[] = {"no", "input", "output", "both"};
- DEBUG("Flushing %s buffers on port %s", buffer_names[buffers], port->name);
+ DEBUG_FMT("Flushing %s buffers on port %s",
+ buffer_names[buffers], port->name);
#ifdef _WIN32
DWORD flags = 0;
CHECK_OPEN_PORT();
- DEBUG("Draining port %s", port->name);
+ DEBUG_FMT("Draining port %s", port->name);
#ifdef _WIN32
/* Returns non-zero upon success, 0 upon failure. */
RETURN_ERROR(SP_ERR_ARG, "Null buffer");
if (timeout)
- DEBUG("Writing %d bytes to port %s, timeout %d ms", count, port->name, timeout);
+ DEBUG_FMT("Writing %d bytes to port %s, timeout %d ms",
+ count, port->name, timeout);
else
- DEBUG("Writing %d bytes to port %s, no timeout", count, port->name);
+ DEBUG_FMT("Writing %d bytes to port %s, no timeout",
+ count, port->name);
if (count == 0)
RETURN_INT(0);
if (GetLastError() == ERROR_IO_PENDING) {
DEBUG("Waiting for write to complete");
GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
- DEBUG("Write completed, %d/%d bytes written", bytes_written, count);
+ DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
RETURN_INT(bytes_written);
} else {
RETURN_FAIL("WriteFile() failed");
if (!buf)
RETURN_ERROR(SP_ERR_ARG, "Null buffer");
- DEBUG("Writing up to %d bytes to port %s", count, port->name);
+ DEBUG_FMT("Writing up to %d bytes to port %s", count, port->name);
if (count == 0)
RETURN_INT(0);
RETURN_ERROR(SP_ERR_ARG, "Null buffer");
if (timeout)
- DEBUG("Reading %d bytes from port %s, timeout %d ms", count, port->name, timeout);
+ DEBUG_FMT("Reading %d bytes from port %s, timeout %d ms",
+ count, port->name, timeout);
else
- DEBUG("Reading %d bytes from port %s, no timeout", count, port->name);
+ DEBUG_FMT("Reading %d bytes from port %s, no timeout",
+ count, port->name);
if (count == 0)
RETURN_INT(0);
if (GetLastError() == ERROR_IO_PENDING) {
DEBUG("Waiting for read to complete");
GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
- DEBUG("Read completed, %d/%d bytes read", bytes_read, count);
+ DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count);
} else {
RETURN_FAIL("ReadFile() failed");
}
if (!buf)
RETURN_ERROR(SP_ERR_ARG, "Null buffer");
- DEBUG("Reading up to %d bytes from port %s", count, port->name);
+ DEBUG_FMT("Reading up to %d bytes from port %s", count, port->name);
#ifdef _WIN32
DWORD bytes_read;
CHECK_OPEN_PORT();
- DEBUG("Checking input bytes waiting on port %s", port->name);
+ DEBUG_FMT("Checking input bytes waiting on port %s", port->name);
#ifdef _WIN32
DWORD errors;
CHECK_OPEN_PORT();
- DEBUG("Checking output bytes waiting on port %s", port->name);
+ DEBUG_FMT("Checking output bytes waiting on port %s", port->name);
#ifdef _WIN32
DWORD errors;
TRACE("%p, %p, %p", port, data, config);
- DEBUG("Getting configuration for port %s", port->name);
+ DEBUG_FMT("Getting configuration for port %s", port->name);
#ifdef _WIN32
if (!GetCommState(port->hdl, &data->dcb))
TRACE("%p, %p, %p", port, data, config);
- DEBUG("Setting configuration for port %s", port->name);
+ DEBUG_FMT("Setting configuration for port %s", port->name);
#ifdef _WIN32
if (config->baudrate >= 0) {
if (!signals)
RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
- DEBUG("Getting control signals for port %s", port->name);
+ DEBUG_FMT("Getting control signals for port %s", port->name);
*signals = 0;
#ifdef _WIN32
SP_API int sp_last_error_code(void)
{
- TRACE("");
+ TRACE_VOID();
#ifdef _WIN32
RETURN_INT(GetLastError());
#else
SP_API char *sp_last_error_message(void)
{
- TRACE("");
+ TRACE_VOID();
#ifdef _WIN32
LPVOID message;