Skip to content

Commit

Permalink
Generalize state to string
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRosenow committed Dec 9, 2024
1 parent 407bc7e commit 9dc02c5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
2 changes: 2 additions & 0 deletions include/reactor-uc/network_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ typedef enum {
NETWORK_CHANNEL_TYPE_COAP_UDP_IP,
} NetworkChannelType;

char *NetworkChannel_state_to_string(NetworkChannelState state);

typedef struct FederatedConnectionBundle FederatedConnectionBundle;
typedef struct NetworkChannel NetworkChannel;

Expand Down
23 changes: 23 additions & 0 deletions src/network_channel.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "reactor-uc/network_channel.h"

#if defined(PLATFORM_POSIX)
#ifdef NETWORK_CHANNEL_TCP_POSIX
#include "platform/posix/tcp_ip_channel.c"
Expand Down Expand Up @@ -29,3 +31,24 @@
#else
#error "Platform not supported"
#endif

char *NetworkChannel_state_to_string(NetworkChannelState state) {
switch (state) {
case NETWORK_CHANNEL_STATE_UNINITIALIZED:
return "UNINITIALIZED";
case NETWORK_CHANNEL_STATE_OPEN:
return "OPEN";
case NETWORK_CHANNEL_STATE_CONNECTION_IN_PROGRESS:
return "CONNECTION_IN_PROGRESS";
case NETWORK_CHANNEL_STATE_CONNECTION_FAILED:
return "CONNECTION_FAILED";
case NETWORK_CHANNEL_STATE_CONNECTED:
return "CONNECTED";
case NETWORK_CHANNEL_STATE_LOST_CONNECTION:
return "LOST_CONNECTION";
case NETWORK_CHANNEL_STATE_CLOSED:
return "CLOSED";
}

return "UNKNOWN";
}
25 changes: 2 additions & 23 deletions src/platform/posix/tcp_ip_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,9 @@ static Environment *_env;
// Forward declarations
static void *_TcpIpChannel_worker_thread(void *untyped_self);

static char *_TcpIpChannel_state_to_string(NetworkChannelState state) {
switch (state) {
case NETWORK_CHANNEL_STATE_UNINITIALIZED:
return "UNINITIALIZED";
case NETWORK_CHANNEL_STATE_OPEN:
return "OPEN";
case NETWORK_CHANNEL_STATE_CONNECTION_IN_PROGRESS:
return "CONNECTION_IN_PROGRESS";
case NETWORK_CHANNEL_STATE_CONNECTION_FAILED:
return "CONNECTION_FAILED";
case NETWORK_CHANNEL_STATE_CONNECTED:
return "CONNECTED";
case NETWORK_CHANNEL_STATE_LOST_CONNECTION:
return "LOST_CONNECTION";
case NETWORK_CHANNEL_STATE_CLOSED:
return "CLOSED";
}

return "UNKNOWN";
}

static void _TcpIpChannel_update_state(TcpIpChannel *self, NetworkChannelState new_state) {
LF_DEBUG(NET, "TcpIpChannel: Update state: %s => %s\n", _TcpIpChannel_state_to_string(self->state),
_TcpIpChannel_state_to_string(new_state));
LF_DEBUG(NET, "TcpIpChannel: Update state: %s => %s\n", NetworkChannel_state_to_string(self->state),
NetworkChannel_state_to_string(new_state));

// Update the state of the channel itself
self->state = new_state;
Expand Down
3 changes: 2 additions & 1 deletion src/platform/riot/coap_udp_ip_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ static Environment *_env;
static lf_ret_t _CoapUdpIpChannel_client_send_connect_message(CoapUdpIpChannel *self);

static void _CoapUdpIpChannel_update_state(CoapUdpIpChannel *self, NetworkChannelState new_state) {
LF_DEBUG(NET, "CoapUdpIpChannel: Update state: %d => %d\n", self->state, new_state);
LF_DEBUG(NET, "CoapUdpIpChannel: Update state: %s => %s\n", NetworkChannel_state_to_string(self->state),
NetworkChannel_state_to_string(new_state));

// Update the state of the channel itself
mutex_lock(&self->state_mutex);
Expand Down

0 comments on commit 9dc02c5

Please sign in to comment.