Skip to content

Commit

Permalink
Use global _lf_environment
Browse files Browse the repository at this point in the history
  • Loading branch information
erlingrj committed Jan 21, 2025
1 parent 1098b34 commit 7c59706
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
17 changes: 3 additions & 14 deletions src/platform/posix/tcp_ip_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
#define TCP_IP_CHANNEL_DEBUG(fmt, ...) \
LF_DEBUG(NET, "TcpIpChannel: [%s] " fmt, self->is_server ? "server" : "client", ##__VA_ARGS__)

static bool _is_globals_initialized = false;
static Environment *_env;

// Forward declarations
static void *_TcpIpChannel_worker_thread(void *untyped_self);

Expand All @@ -56,7 +53,7 @@ static void _TcpIpChannel_update_state(TcpIpChannel *self, NetworkChannelState n
// Inform runtime about new state if it changed from or to NETWORK_CHANNEL_STATE_CONNECTED
if ((old_state == NETWORK_CHANNEL_STATE_CONNECTED && new_state != NETWORK_CHANNEL_STATE_CONNECTED) ||
(old_state != NETWORK_CHANNEL_STATE_CONNECTED && new_state == NETWORK_CHANNEL_STATE_CONNECTED)) {
_env->platform->new_async_event(_env->platform);
_lf_environment->platform->new_async_event(_lf_environment->platform);
}
TCP_IP_CHANNEL_DEBUG("Update state: %s => %s done", NetworkChannel_state_to_string(self->state),
NetworkChannel_state_to_string(new_state));
Expand Down Expand Up @@ -420,12 +417,12 @@ static void *_TcpIpChannel_worker_thread(void *untyped_self) {
} break;

case NETWORK_CHANNEL_STATE_CONNECTION_IN_PROGRESS: {
_env->platform->wait_for(_env->platform, self->super.expected_connect_duration);
_lf_environment->platform->wait_for(_lf_environment->platform, self->super.expected_connect_duration);
} break;

case NETWORK_CHANNEL_STATE_LOST_CONNECTION:
case NETWORK_CHANNEL_STATE_CONNECTION_FAILED: {
_env->platform->wait_for(_env->platform, self->super.expected_connect_duration);
_lf_environment->platform->wait_for(_lf_environment->platform, self->super.expected_connect_duration);
_TcpIpChannel_reset_socket(self);
_TcpIpChannel_update_state(self, NETWORK_CHANNEL_STATE_OPEN);
} break;
Expand Down Expand Up @@ -539,14 +536,6 @@ void TcpIpChannel_ctor(TcpIpChannel *self, Environment *env, const char *host, u
assert(env != NULL);
assert(host != NULL);

// Initialize global coap server if not already done
if (!_is_globals_initialized) {
_is_globals_initialized = true;

// Set environment
_env = env;
}

if (pthread_mutex_init(&self->mutex, NULL) != 0) {
throw("Failed to initialize mutex");
}
Expand Down
20 changes: 8 additions & 12 deletions src/platform/riot/coap_udp_ip_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

char _connection_thread_stack[THREAD_STACKSIZE_MAIN];
int _connection_thread_pid = 0;
static bool _is_globals_initialized = false;
static Environment *_env;
static bool _coap_is_globals_initialized = false;

static void _CoapUdpIpChannel_update_state(CoapUdpIpChannel *self, NetworkChannelState new_state) {
COAP_UDP_IP_CHANNEL_DEBUG("Update state: %s => %s", NetworkChannel_state_to_string(self->state),
Expand All @@ -37,7 +36,7 @@ static void _CoapUdpIpChannel_update_state(CoapUdpIpChannel *self, NetworkChanne
// Inform runtime about new state if it changed from or to NETWORK_CHANNEL_STATE_CONNECTED
if ((old_state == NETWORK_CHANNEL_STATE_CONNECTED && new_state != NETWORK_CHANNEL_STATE_CONNECTED) ||
(old_state != NETWORK_CHANNEL_STATE_CONNECTED && new_state == NETWORK_CHANNEL_STATE_CONNECTED)) {
_env->platform->new_async_event(_env->platform);
_lf_environment->platform->new_async_event(_lf_environment->platform);
}

// Let connection thread evaluate new state of this channel
Expand All @@ -60,7 +59,7 @@ static void _CoapUdpIpChannel_update_state_if_not(CoapUdpIpChannel *self, Networ
mutex_unlock(&self->state_mutex);

// Inform runtime about new state
_env->platform->new_async_event(_env->platform);
_lf_environment->platform->new_async_event(_lf_environment->platform);
}

static NetworkChannelState _CoapUdpIpChannel_get_state(CoapUdpIpChannel *self) {
Expand All @@ -75,9 +74,9 @@ static NetworkChannelState _CoapUdpIpChannel_get_state(CoapUdpIpChannel *self) {

static CoapUdpIpChannel *_CoapUdpIpChannel_get_coap_channel_by_remote(const sock_udp_ep_t *remote) {
CoapUdpIpChannel *channel;
for (size_t i = 0; i < _env->net_bundles_size; i++) {
if (_env->net_bundles[i]->net_channel->type == NETWORK_CHANNEL_TYPE_COAP_UDP_IP) {
channel = (CoapUdpIpChannel *)_env->net_bundles[i]->net_channel;
for (size_t i = 0; i < _lf_environment->net_bundles_size; i++) {
if (_lf_environment->net_bundles[i]->net_channel->type == NETWORK_CHANNEL_TYPE_COAP_UDP_IP) {
channel = (CoapUdpIpChannel *)_lf_environment->net_bundles[i]->net_channel;

if (sock_udp_ep_equal(&channel->remote, remote)) {
return channel;
Expand Down Expand Up @@ -411,11 +410,8 @@ void CoapUdpIpChannel_ctor(CoapUdpIpChannel *self, Environment *env, const char
assert(remote_address != NULL);

// Initialize global coap server if not already done
if (!_is_globals_initialized) {
_is_globals_initialized = true;

// Set environment
_env = env;
if (!_coap_is_globals_initialized) {
_coap_is_globals_initialized = true;

// Initialize coap server
gcoap_register_listener(&_listener);
Expand Down

0 comments on commit 7c59706

Please sign in to comment.