Integrate the context object in the IrDA code.

This commit is contained in:
Jef Driesen 2012-07-11 16:32:02 +02:00
parent 53e9d72a40
commit ffad80316c
3 changed files with 29 additions and 34 deletions

View File

@ -37,27 +37,14 @@
#include <sys/ioctl.h> // ioctl
#endif
#include <libdivecomputer/utils.h>
#include "irda.h"
#include "context-private.h"
#include "array.h"
#ifdef _WIN32
#define TRACE(expr) \
{ \
DWORD error = WSAGetLastError (); \
message ("TRACE (%s:%d, %s): %s (%d)\n", __FILE__, __LINE__, \
expr, irda_errmsg (), error); \
WSASetLastError (error); \
}
#define ERRNO WSAGetLastError ()
#else
#define TRACE(expr) \
{ \
int error = errno; \
message ("TRACE (%s:%d, %s): %s (%d)\n", __FILE__, __LINE__, \
expr, strerror (errno), errno); \
errno = error; \
}
#define ERRNO errno
#endif
#ifdef _MSC_VER
@ -65,6 +52,7 @@
#endif
struct irda_t {
dc_context_t *context;
#ifdef _WIN32
SOCKET fd;
#else
@ -114,7 +102,7 @@ const char* irda_errmsg (void)
int
irda_socket_open (irda_t **out)
irda_socket_open (irda_t **out, dc_context_t *context)
{
if (out == NULL)
return -1; // EINVAL (Invalid argument)
@ -122,10 +110,17 @@ irda_socket_open (irda_t **out)
// Allocate memory.
irda_t *device = (irda_t *) malloc (sizeof (irda_t));
if (device == NULL) {
TRACE ("malloc");
#ifdef _WIN32
SYSERROR (context, ERROR_OUTOFMEMORY);
#else
SYSERROR (context, errno);
#endif
return -1; // ENOMEM (Not enough space)
}
// Library context.
device->context = context;
// Default to blocking reads.
device->timeout = -1;
@ -134,7 +129,7 @@ irda_socket_open (irda_t **out)
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD (2, 2);
if (WSAStartup (wVersionRequested, &wsaData) != 0) {
TRACE ("WSAStartup");
SYSERROR (context, ERRNO);
free (device);
return -1;
}
@ -144,7 +139,7 @@ irda_socket_open (irda_t **out)
// 2.2, it will still return 2.2 since that is the version we requested.
if (LOBYTE (wsaData.wVersion) != 2 ||
HIBYTE (wsaData.wVersion) != 2) {
TRACE ("wsaData.wVersion");
ERROR (context, "Incorrect winsock version.");
WSACleanup ();
free (device);
return -1;
@ -158,7 +153,7 @@ irda_socket_open (irda_t **out)
#else
if (device->fd == -1) {
#endif
TRACE ("socket");
SYSERROR (context, ERRNO);
#ifdef _WIN32
WSACleanup ();
#endif
@ -184,11 +179,10 @@ irda_socket_close (irda_t *device)
// Close the socket.
#ifdef _WIN32
if (closesocket (device->fd) != 0) {
TRACE ("closesocket");
#else
if (close (device->fd) != 0) {
TRACE ("close");
#endif
SYSERROR (device->context, ERRNO);
#ifdef _WIN32
WSACleanup ();
#endif
@ -199,7 +193,7 @@ irda_socket_close (irda_t *device)
#ifdef _WIN32
// Terminate the winsock dll.
if (WSACleanup () != 0) {
TRACE ("WSACleanup");
SYSERROR (device->context, ERRNO);
free (device);
return -1;
}
@ -269,7 +263,7 @@ irda_socket_discover (irda_t *device, irda_callback_t callback, void *userdata)
#else
if (errno != EAGAIN) {
#endif
TRACE ("getsockopt");
SYSERROR (device->context, ERRNO);
return -1; // Error during getsockopt call.
}
}
@ -345,7 +339,7 @@ irda_socket_connect_name (irda_t *device, unsigned int address, const char *name
#endif
if (connect (device->fd, (struct sockaddr *) &peer, sizeof (peer)) != 0) {
TRACE ("connect");
SYSERROR (device->context, ERRNO);
return -1;
}
@ -375,7 +369,7 @@ irda_socket_connect_lsap (irda_t *device, unsigned int address, unsigned int lsa
#endif
if (connect (device->fd, (struct sockaddr *) &peer, sizeof (peer)) != 0) {
TRACE ("connect");
SYSERROR (device->context, ERRNO);
return -1;
}
@ -392,12 +386,11 @@ irda_socket_available (irda_t *device)
#ifdef _WIN32
unsigned long bytes = 0;
if (ioctlsocket (device->fd, FIONREAD, &bytes) != 0) {
TRACE ("ioctlsocket");
#else
int bytes = 0;
if (ioctl (device->fd, FIONREAD, &bytes) != 0) {
TRACE ("ioctl");
#endif
SYSERROR (device->context, ERRNO);
return -1;
}
@ -425,7 +418,7 @@ irda_socket_read (irda_t *device, void *data, unsigned int size)
while (nbytes < size) {
int rc = select (device->fd + 1, &fds, NULL, NULL, (device->timeout >= 0 ? &tv : NULL));
if (rc < 0) {
TRACE ("select");
SYSERROR (device->context, ERRNO);
return -1; // Error during select call.
} else if (rc == 0) {
break; // Timeout.
@ -433,7 +426,7 @@ irda_socket_read (irda_t *device, void *data, unsigned int size)
int n = recv (device->fd, (char*) data + nbytes, size - nbytes, 0);
if (n < 0) {
TRACE ("recv");
SYSERROR (device->context, ERRNO);
return -1; // Error during recv call.
} else if (n == 0) {
break; // EOF reached.
@ -456,7 +449,7 @@ irda_socket_write (irda_t *device, const void *data, unsigned int size)
while (nbytes < size) {
int n = send (device->fd, (char*) data + nbytes, size - nbytes, 0);
if (n < 0) {
TRACE ("send");
SYSERROR (device->context, ERRNO);
return -1; // Error during send call.
}

View File

@ -22,6 +22,8 @@
#ifndef IRDA_H
#define IRDA_H
#include <libdivecomputer/context.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@ -34,7 +36,7 @@ int irda_errcode (void);
const char* irda_errmsg (void);
int irda_socket_open (irda_t **device);
int irda_socket_open (irda_t **device, dc_context_t *context);
int irda_socket_close (irda_t *device);

View File

@ -175,7 +175,7 @@ uwatec_smart_device_open (dc_device_t **out, dc_context_t *context)
device->devtime = 0;
// Open the irda socket.
int rc = irda_socket_open (&device->socket);
int rc = irda_socket_open (&device->socket, context);
if (rc == -1) {
ERROR (context, "Failed to open the irda socket.");
free (device);