open5gs/src/init.c

120 lines
2.4 KiB
C
Raw Normal View History

2017-02-02 11:34:37 +00:00
/**
* @file init.c
*/
/* Core library */
#define TRACE_MODULE _init
#include "core_debug.h"
2017-02-13 00:58:55 +00:00
#include "core_thread.h"
2017-02-24 01:50:49 +00:00
#include "s6a_app.h"
2017-02-21 06:35:54 +00:00
2017-02-06 11:54:31 +00:00
#include "context.h"
2017-02-13 00:58:55 +00:00
#include "event.h"
2017-02-13 04:19:53 +00:00
#define EVENT_WAIT_TIMEOUT 10000 /* 10 msec */
2017-02-13 00:58:55 +00:00
2017-02-21 07:16:35 +00:00
static thread_id mme_sm_thread;
static thread_id mme_net_thread;
2017-02-02 11:34:37 +00:00
status_t cellwire_initialize(char *config_path)
{
2017-02-06 11:54:31 +00:00
status_t rv;
2017-02-02 11:34:37 +00:00
srand(time(NULL)*getpid());
2017-02-21 07:03:05 +00:00
rv = context_init();
2017-02-21 06:35:54 +00:00
if (rv != CORE_OK)
return rv;
2017-02-02 11:34:37 +00:00
return CORE_OK;
}
void cellwire_terminate(void)
{
2017-02-06 11:54:31 +00:00
context_final();
2017-02-02 11:34:37 +00:00
}
2017-02-13 04:19:53 +00:00
2017-02-21 07:16:35 +00:00
void *THREAD_FUNC mme_sm_main(void *data)
2017-02-13 04:19:53 +00:00
{
event_t event;
msgq_id queue_id;
mme_sm_t mme_sm;
c_time_t prev_tm, now_tm;
int r;
memset(&event, 0, sizeof(event_t));
queue_id = event_create();
d_assert(queue_id, return NULL, "MME event queue creation failed");
fsm_create(&mme_sm.fsm, mme_state_initial, mme_state_final);
d_assert(&mme_sm.fsm, return NULL, "MME state machine creation failed");
mme_sm.queue_id = queue_id;
tm_service_init(&mme_sm.tm_service);
fsm_init((fsm_t*)&mme_sm, 0);
prev_tm = time_now();
2017-02-15 00:17:44 +00:00
while ((!thread_should_stop()))
2017-02-13 04:19:53 +00:00
{
r = event_timedrecv(queue_id, &event, EVENT_WAIT_TIMEOUT);
d_assert(r != CORE_ERROR, continue,
"While receiving a event message, error occurs");
now_tm = time_now();
/* if the gap is over 10 ms, execute preriodic jobs */
if (now_tm - prev_tm > EVENT_WAIT_TIMEOUT)
{
event_timer_execute(&mme_sm.tm_service);
prev_tm = now_tm;
}
if (r == CORE_TIMEUP)
{
continue;
}
fsm_dispatch((fsm_t*)&mme_sm, (fsm_event_t*)&event);
}
fsm_final((fsm_t*)&mme_sm, 0);
fsm_clear((fsm_t*)&mme_sm);
event_delete(queue_id);
return NULL;
}
2017-02-21 07:16:35 +00:00
void *THREAD_FUNC mme_net_main(void *data)
2017-02-13 04:19:53 +00:00
{
while (!thread_should_stop())
{
net_fds_read_run(50);
}
return NULL;
}
void threads_start()
{
status_t rv;
2017-02-21 07:16:35 +00:00
rv = thread_create(&mme_sm_thread, NULL, mme_sm_main, NULL);
2017-02-13 04:19:53 +00:00
d_assert(rv == CORE_OK, return,
"MME State machine thread creation failed");
2017-02-21 07:16:35 +00:00
rv = thread_create(&mme_net_thread, NULL, mme_net_main, NULL);
2017-02-13 04:19:53 +00:00
d_assert(rv == CORE_OK, return,
2017-02-21 07:16:35 +00:00
"MME Network socket recv thread creation failed");
2017-02-13 04:19:53 +00:00
}
void threads_stop()
{
2017-02-21 07:16:35 +00:00
thread_delete(mme_net_thread);
thread_delete(mme_sm_thread);
2017-02-13 04:19:53 +00:00
}