Skip to content

Commit

Permalink
improved error names
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Aug 19, 2017
1 parent c41a4a6 commit 4f039e4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
18 changes: 9 additions & 9 deletions include/lwmqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
*/
typedef enum {
LWMQTT_SUCCESS = 0,
LWMQTT_UNANSWERED_PING = -1,
LWMQTT_BUFFER_TOO_SHORT = -2,
LWMQTT_REMAINING_LENGTH_OVERFLOW = -3,
LWMQTT_REMAINING_LENGTH_MISMATCH = -4,
LWMQTT_NOT_ENOUGH_DATA = -5,
LWMQTT_NETWORK_CONNECT_ERROR = -6,
LWMQTT_NETWORK_READ_ERROR = -7,
LWMQTT_NETWORK_WRITE_ERROR = -8,
LWMQTT_BUFFER_TOO_SHORT = -1,
LWMQTT_VARNUM_OVERFLOW = -2,
LWMQTT_NETWORK_FAILED_CONNECT = -3,
LWMQTT_NETWORK_TIMEOUT = -4,
LWMQTT_NETWORK_FAILED_READ = -5,
LWMQTT_NETWORK_FAILED_WRITE = -6,
LWMQTT_REMAINING_LENGTH_OVERFLOW = -7,
LWMQTT_REMAINING_LENGTH_MISMATCH = -8,
LWMQTT_MISSING_OR_WRONG_PACKET = -9,
LWMQTT_CONNECTION_DENIED = -10,
LWMQTT_FAILED_SUBSCRIPTION = -11,
LWMQTT_SUBACK_ARRAY_OVERFLOW = -12,
LWMQTT_VARNUM_OVERFLOW = -13
LWMQTT_UNANSWERED_PING = -13,
} lwmqtt_err_t;

/**
Expand Down
7 changes: 4 additions & 3 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static lwmqtt_err_t lwmqtt_read_from_network(lwmqtt_client_t *client, size_t off

// check timeout
if (remaining_time <= 0) {
return LWMQTT_NOT_ENOUGH_DATA;
return LWMQTT_NETWORK_TIMEOUT;
}

// read
Expand Down Expand Up @@ -102,7 +102,7 @@ static lwmqtt_err_t lwmqtt_write_to_network(lwmqtt_client_t *client, size_t offs

// check timeout
if (remaining_time <= 0) {
return LWMQTT_NETWORK_WRITE_ERROR;
return LWMQTT_NETWORK_TIMEOUT;
}

// read
Expand All @@ -127,7 +127,8 @@ static lwmqtt_err_t lwmqtt_read_packet_in_buffer(lwmqtt_client_t *client, size_t

// read or wait for header byte
lwmqtt_err_t err = lwmqtt_read_from_network(client, 0, 1);
if (err == LWMQTT_NOT_ENOUGH_DATA) {
if (err == LWMQTT_NETWORK_TIMEOUT) {
// this is ok as no data has been read at all
return LWMQTT_SUCCESS;
} else if (err != LWMQTT_SUCCESS) {
return err;
Expand Down
18 changes: 9 additions & 9 deletions src/os/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ lwmqtt_err_t lwmqtt_unix_network_connect(lwmqtt_unix_network_t *network, char *h
struct addrinfo *result = NULL;
int rc = getaddrinfo(host, NULL, &hints, &result);
if (rc < 0) {
return LWMQTT_NETWORK_CONNECT_ERROR;
return LWMQTT_NETWORK_FAILED_CONNECT;
}

// prepare selected result
Expand All @@ -76,7 +76,7 @@ lwmqtt_err_t lwmqtt_unix_network_connect(lwmqtt_unix_network_t *network, char *h

// return error if none found
if (selected == NULL) {
return LWMQTT_NETWORK_CONNECT_ERROR;
return LWMQTT_NETWORK_FAILED_CONNECT;
}

// populate address struct
Expand All @@ -91,13 +91,13 @@ lwmqtt_err_t lwmqtt_unix_network_connect(lwmqtt_unix_network_t *network, char *h
// create new socket
network->socket = socket(AF_INET, SOCK_STREAM, 0);
if (network->socket < 0) {
return LWMQTT_NETWORK_CONNECT_ERROR;
return LWMQTT_NETWORK_FAILED_CONNECT;
}

// connect socket
rc = connect(network->socket, (struct sockaddr *)&address, sizeof(address));
if (rc < 0) {
return LWMQTT_NETWORK_CONNECT_ERROR;
return LWMQTT_NETWORK_FAILED_CONNECT;
}

return LWMQTT_SUCCESS;
Expand All @@ -115,7 +115,7 @@ lwmqtt_err_t lwmqtt_unix_network_peek(lwmqtt_unix_network_t *network, size_t *av
// get the available bytes on the socket
int rc = ioctl(network->socket, FIONREAD, available);
if (rc < 0) {
return LWMQTT_NETWORK_READ_ERROR;
return LWMQTT_NETWORK_FAILED_READ;
}

return LWMQTT_SUCCESS;
Expand All @@ -130,13 +130,13 @@ lwmqtt_err_t lwmqtt_unix_network_read(lwmqtt_client_t *client, void *ref, uint8_
struct timeval t = {.tv_sec = timeout / 1000, .tv_usec = (timeout % 1000) * 1000};
int rc = setsockopt(n->socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&t, sizeof(t));
if (rc < 0) {
return LWMQTT_NETWORK_READ_ERROR;
return LWMQTT_NETWORK_FAILED_READ;
}

// read from socket
int bytes = (int)recv(n->socket, buffer, len, 0);
if (bytes < 0 && errno != EAGAIN) {
return LWMQTT_NETWORK_READ_ERROR;
return LWMQTT_NETWORK_FAILED_READ;
}

// increment counter
Expand All @@ -154,13 +154,13 @@ lwmqtt_err_t lwmqtt_unix_network_write(lwmqtt_client_t *client, void *ref, uint8
struct timeval t = {.tv_sec = timeout / 1000, .tv_usec = (timeout % 1000) * 1000};
int rc = setsockopt(n->socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&t, sizeof(t));
if (rc < 0) {
return LWMQTT_NETWORK_WRITE_ERROR;
return LWMQTT_NETWORK_FAILED_WRITE;
}

// write to socket
int bytes = (int)send(n->socket, buffer, len, 0);
if (bytes < 0 && errno != EAGAIN) {
return LWMQTT_NETWORK_WRITE_ERROR;
return LWMQTT_NETWORK_FAILED_WRITE;
}

// increment counter
Expand Down

0 comments on commit 4f039e4

Please sign in to comment.