forked from acouzens/open5gs
intermediate
This commit is contained in:
parent
d017e729e0
commit
03f50ca6a0
78
src/event.c
78
src/event.c
|
@ -12,50 +12,32 @@
|
|||
|
||||
#define EVT_Q_DEPTH 16
|
||||
|
||||
int g_initialized = 0;
|
||||
|
||||
msgq_id g_mme_evt_q = 0;
|
||||
|
||||
tm_service_t g_tm_serv;
|
||||
|
||||
status_t event_init(void)
|
||||
msgq_id event_create(void)
|
||||
{
|
||||
if (g_initialized)
|
||||
return CORE_OK;
|
||||
|
||||
tm_service_init(&g_tm_serv);
|
||||
msgq_id queue_id = 0;
|
||||
|
||||
/* Start threads */
|
||||
g_mme_evt_q = msgq_create(EVT_Q_DEPTH, EVENT_SIZE, MSGQ_O_BLOCK);
|
||||
d_assert(g_mme_evt_q != 0, return CORE_ERROR,
|
||||
"Message queue creation failed");
|
||||
|
||||
g_initialized = 1;
|
||||
queue_id = msgq_create(EVT_Q_DEPTH, EVENT_SIZE, MSGQ_O_BLOCK);
|
||||
d_assert(queue_id != 0, return CORE_ERROR, "Message queue creation failed");
|
||||
|
||||
return CORE_OK;
|
||||
}
|
||||
|
||||
status_t event_final(void)
|
||||
status_t event_delete(msgq_id queue_id)
|
||||
{
|
||||
if (!g_initialized)
|
||||
return CORE_OK;
|
||||
|
||||
msgq_delete(g_mme_evt_q);
|
||||
|
||||
g_initialized = 0;
|
||||
msgq_delete(queue_id);
|
||||
|
||||
return CORE_OK;
|
||||
}
|
||||
|
||||
int event_send(event_t *e)
|
||||
int event_send(msgq_id queue_id, event_t *e)
|
||||
{
|
||||
int r;
|
||||
|
||||
d_assert(g_initialized, return -1, "event framework isn't initialized");
|
||||
d_assert(g_mme_evt_q, return -1, "event queue isn't initialized");
|
||||
d_assert(e, return -1, "Null param");
|
||||
d_assert(queue_id, return -1, "event queue isn't initialized");
|
||||
|
||||
r = msgq_send(g_mme_evt_q, (const char*)e, EVENT_SIZE);
|
||||
r = msgq_send(queue_id, (const char*)e, EVENT_SIZE);
|
||||
if (r != EVENT_SIZE)
|
||||
{
|
||||
d_error("msgq_send() failed");
|
||||
|
@ -65,15 +47,14 @@ int event_send(event_t *e)
|
|||
return r;
|
||||
}
|
||||
|
||||
int event_timedrecv(event_t *e, c_time_t timeout)
|
||||
int event_timedrecv(msgq_id queue_id, event_t *e, c_time_t timeout)
|
||||
{
|
||||
int r;
|
||||
|
||||
d_assert(g_initialized, return -1, "event framework isn't initialized");
|
||||
d_assert(g_mme_evt_q, return -1, "event queue isn't initialized");
|
||||
d_assert(e, return -1, "Null param");
|
||||
d_assert(queue_id, return -1, "event queue isn't initialized");
|
||||
|
||||
r = msgq_timedrecv(g_mme_evt_q, (char*)e, EVENT_SIZE, timeout);
|
||||
r = msgq_timedrecv(queue_id, (char*)e, EVENT_SIZE, timeout);
|
||||
if (r != CORE_TIMEUP && r != EVENT_SIZE)
|
||||
{
|
||||
d_error("msgq_timedrecv() failed");
|
||||
|
@ -83,15 +64,16 @@ int event_timedrecv(event_t *e, c_time_t timeout)
|
|||
return r;
|
||||
}
|
||||
|
||||
void* event_timer_expire_func(c_uintptr_t arg1, c_uintptr_t arg2, c_uintptr_t arg3)
|
||||
void* event_timer_expire_func(
|
||||
c_uintptr_t queue_id, c_uintptr_t event, c_uintptr_t param)
|
||||
{
|
||||
event_t e;
|
||||
int r;
|
||||
|
||||
event_set(&e, arg1, arg2);
|
||||
event_set_param2(&e, arg3);
|
||||
d_assert(queue_id, return NULL, "Null param");
|
||||
event_set(&e, event, param);
|
||||
|
||||
r = msgq_send(g_mme_evt_q, (const char*)&e, EVENT_SIZE);
|
||||
r = msgq_send(queue_id, (const char*)&e, EVENT_SIZE);
|
||||
if (r <= 0)
|
||||
{
|
||||
d_error("msgq_send() failed");
|
||||
|
@ -100,35 +82,30 @@ void* event_timer_expire_func(c_uintptr_t arg1, c_uintptr_t arg2, c_uintptr_t ar
|
|||
return NULL;
|
||||
}
|
||||
|
||||
tm_block_id event_timer_create(void)
|
||||
tm_block_id event_timer_create(tm_service_t *tm_service)
|
||||
{
|
||||
tm_block_id id;
|
||||
|
||||
d_assert(g_initialized, return 0, "event framework isn't initialized");
|
||||
|
||||
id = tm_create(&g_tm_serv);
|
||||
id = tm_create(tm_service);
|
||||
d_assert(id, return 0, "tm_create() failed");
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
status_t event_timer_set(tm_block_id id, event_e te, tm_type_e type,
|
||||
c_uint32_t duration, c_uintptr_t param1, c_uintptr_t param2)
|
||||
status_t event_timer_set(tm_block_id id, event_e event, tm_type_e type,
|
||||
c_uint32_t duration, c_uintptr_t queue_id, c_uintptr_t param)
|
||||
{
|
||||
d_assert(g_initialized, return 0, "event framework isn't initialized");
|
||||
d_assert(type == TIMER_TYPE_ONE_SHOT || type == TIMER_TYPE_PERIODIC,
|
||||
return 0, "param 'type' is invalid");
|
||||
|
||||
tm_set(id, type, duration, (expire_func_t)event_timer_expire_func,
|
||||
te, param1, param2);
|
||||
queue_id, event, param);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
status_t event_timer_delete(tm_block_id id)
|
||||
{
|
||||
d_assert(g_initialized, return CORE_ERROR,
|
||||
"event framework isn't initialized");
|
||||
d_assert(id, return CORE_ERROR, "param 'id' is zero");
|
||||
|
||||
tm_delete(id);
|
||||
|
@ -136,12 +113,9 @@ status_t event_timer_delete(tm_block_id id)
|
|||
return CORE_OK;
|
||||
}
|
||||
|
||||
status_t event_timer_execute(void)
|
||||
status_t event_timer_execute(tm_service_t *tm_service)
|
||||
{
|
||||
d_assert(g_initialized, return CORE_ERROR,
|
||||
"event framework isn't initialized");
|
||||
|
||||
return tm_execute_tm_service(&g_tm_serv);
|
||||
return tm_execute_tm_service(tm_service);
|
||||
}
|
||||
|
||||
static char FSM_NAME_INIT_SIG[] = "INIT";
|
||||
|
@ -151,8 +125,6 @@ static char FSM_NAME_EXIT_SIG[] = "EXIT";
|
|||
static char EVT_NAME_LO_ENB_S1_ACCEPT[] = "LO_ENB_S1_ACCEPT";
|
||||
static char EVT_NAME_LO_ENB_S1_CONNREFUSED[] = "LO_ENB_S1_CONNREFUSED";
|
||||
|
||||
static char EVT_NAME_TM_MME_S1_WAIT_CONN[] = "TM_MME_S1_WAIT_CONN";
|
||||
|
||||
static char EVT_NAME_S1_ENB_INF[] = "S1_ENB_INF";
|
||||
|
||||
static char EVT_NAME_UNKNOWN[] = "UNKNOWN";
|
||||
|
@ -173,8 +145,6 @@ char* event_get_name(event_t *e)
|
|||
case EVT_LO_ENB_S1_CONNREFUSED:
|
||||
return EVT_NAME_LO_ENB_S1_CONNREFUSED;
|
||||
|
||||
case EVT_TM_MME_S1_WAIT_CONN: return EVT_NAME_TM_MME_S1_WAIT_CONN;
|
||||
|
||||
default: return EVT_NAME_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
|
33
src/event.h
33
src/event.h
|
@ -5,6 +5,7 @@
|
|||
* @file event.h
|
||||
* @brief Event header
|
||||
*/
|
||||
#include "core_msgq.h"
|
||||
#include "core_time.h"
|
||||
#include "core_timer.h"
|
||||
#include "core_fsm.h"
|
||||
|
@ -23,19 +24,13 @@ typedef enum {
|
|||
EVT_LO_TOP,
|
||||
|
||||
EVT_TM_BASE,
|
||||
EVT_TM_MME_S1_WAIT_CONN,
|
||||
EVT_TM_TOP,
|
||||
|
||||
EVT_MSG_BASE,
|
||||
EVT_S1_MME_INF,
|
||||
EVT_S1_ENB_INF,
|
||||
EVT_MSG_TOP,
|
||||
|
||||
EVT_GTP_BASE,
|
||||
EVT_GTP_C_SGW_INF,
|
||||
EVT_GTP_C_ENB_INF,
|
||||
EVT_GTP_U_SGW_INF,
|
||||
EVT_GTP_U_ENB_INF,
|
||||
EVT_GTP_TOP,
|
||||
|
||||
EVT_TOP,
|
||||
|
@ -123,18 +118,19 @@ typedef struct {
|
|||
#define event_get_msg_ip_addr(__ptr_e) ((__ptr_e)->u.m.ip_addr)
|
||||
|
||||
/**
|
||||
* Initialize event framework
|
||||
* Create event message queue
|
||||
*
|
||||
* @return CORE_OK or CORE_ERROR
|
||||
* @return event queue or 0
|
||||
*/
|
||||
CORE_DECLARE(status_t) event_init(void);
|
||||
CORE_DECLARE(msgq_id) event_create(void);
|
||||
|
||||
/**
|
||||
* Finalize event framework
|
||||
* Delete event message queue
|
||||
*
|
||||
* @return CORE_OK or CORE_ERROR
|
||||
*/
|
||||
CORE_DECLARE(status_t) event_final(void);
|
||||
CORE_DECLARE(status_t) event_delete(msgq_id queue_id);
|
||||
|
||||
|
||||
/**
|
||||
* Send a event to event queue
|
||||
|
@ -142,7 +138,7 @@ CORE_DECLARE(status_t) event_final(void);
|
|||
* @return If success, return the size to be sent.
|
||||
* If else, return -1
|
||||
*/
|
||||
CORE_DECLARE(int) event_send(event_t *e);
|
||||
CORE_DECLARE(int) event_send(msgq_id queue_id, event_t *e);
|
||||
|
||||
/**
|
||||
* Receive a event from event queue with timeout
|
||||
|
@ -151,22 +147,19 @@ CORE_DECLARE(int) event_send(event_t *e);
|
|||
* If timout occurs, return CORE_TIMEUP.
|
||||
* If else, return -1.
|
||||
*/
|
||||
CORE_DECLARE(int) event_timedrecv(event_t *e, c_time_t timeout);
|
||||
|
||||
CORE_DECLARE(int) event_sendto(uint32_t dest_id, event_t *e);
|
||||
|
||||
CORE_DECLARE(int) event_recvfrom(uint32_t id, event_t *e);
|
||||
CORE_DECLARE(int) event_timedrecv(
|
||||
msgq_id queue_id, event_t *e, c_time_t timeout);
|
||||
|
||||
/**
|
||||
* Create a timer
|
||||
*/
|
||||
CORE_DECLARE(tm_block_id) event_timer_create(void);
|
||||
CORE_DECLARE(tm_block_id) event_timer_create(tm_service_t *tm_service);
|
||||
|
||||
/**
|
||||
* Set a timer
|
||||
*/
|
||||
status_t event_timer_set(tm_block_id id, event_e te, tm_type_e type,
|
||||
c_uint32_t duration, c_uintptr_t param1, c_uintptr_t param2);
|
||||
c_uint32_t duration, c_uintptr_t queue_id, c_uintptr_t param);
|
||||
|
||||
/**
|
||||
* Delete a timer
|
||||
|
@ -176,7 +169,7 @@ CORE_DECLARE(status_t) event_timer_delete(tm_block_id id);
|
|||
/*
|
||||
* Execute timer engine
|
||||
*/
|
||||
CORE_DECLARE(status_t) event_timer_execute(void);
|
||||
CORE_DECLARE(status_t) event_timer_execute(tm_service_t *tm_service);
|
||||
|
||||
char* event_get_name(event_t *e);
|
||||
|
||||
|
|
18
src/init.c
18
src/init.c
|
@ -10,23 +10,21 @@
|
|||
#include "context.h"
|
||||
#include "event.h"
|
||||
|
||||
static thread_id thr_sm;
|
||||
#define THREAD_SM_STACK_SIZE
|
||||
#define THREAD_SM_PRIORITY
|
||||
extern void *THREAD_FUNC sm_main(void *data);
|
||||
static thread_id mme_thread;
|
||||
extern void *THREAD_FUNC mme_main(void *data);
|
||||
|
||||
void threads_start()
|
||||
{
|
||||
status_t rv;
|
||||
|
||||
rv = thread_create(&thr_sm, NULL, sm_main, NULL);
|
||||
rv = thread_create(&mme_thread, NULL, mme_main, NULL);
|
||||
d_assert(rv == CORE_OK, return,
|
||||
"State machine thread creation failed");
|
||||
"MME State machine thread creation failed");
|
||||
}
|
||||
|
||||
void threads_stop()
|
||||
{
|
||||
thread_delete(thr_sm);
|
||||
thread_delete(mme_thread);
|
||||
}
|
||||
|
||||
status_t cellwire_initialize(char *config_path)
|
||||
|
@ -35,10 +33,6 @@ status_t cellwire_initialize(char *config_path)
|
|||
|
||||
srand(time(NULL)*getpid());
|
||||
|
||||
rv = event_init();
|
||||
if (rv != CORE_OK)
|
||||
return rv;
|
||||
|
||||
rv = context_init();
|
||||
if (rv != CORE_OK)
|
||||
return rv;
|
||||
|
@ -49,6 +43,4 @@ status_t cellwire_initialize(char *config_path)
|
|||
void cellwire_terminate(void)
|
||||
{
|
||||
context_final();
|
||||
|
||||
event_final();
|
||||
}
|
||||
|
|
|
@ -11,30 +11,37 @@
|
|||
#define TRACE_MODULE _smmain
|
||||
#include "core_debug.h"
|
||||
#include "core_thread.h"
|
||||
#include "core_msgq.h"
|
||||
|
||||
#include "context.h"
|
||||
|
||||
static mme_sm_t g_mme_sm;
|
||||
|
||||
#define EVENT_WAIT_TIMEOUT 10000 /* 10 msec */
|
||||
|
||||
void *THREAD_FUNC sm_main(void *data)
|
||||
void *THREAD_FUNC mme_main(void *data)
|
||||
{
|
||||
event_t e;
|
||||
event_t event;
|
||||
msgq_id queue_id;
|
||||
mme_sm_t mme_sm;
|
||||
c_time_t prev_tm, now_tm;
|
||||
int r;
|
||||
|
||||
fsm_create(&g_mme_sm.fsm, mme_state_initial, mme_state_final);
|
||||
d_assert(&g_mme_sm.fsm, return NULL,
|
||||
"Master state machine creation failed");
|
||||
memset(&event, 0, sizeof(event_t));
|
||||
|
||||
fsm_init((fsm_t*)&g_mme_sm, 0);
|
||||
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();
|
||||
|
||||
while (!thread_should_stop())
|
||||
{
|
||||
r = event_timedrecv(&e, EVENT_WAIT_TIMEOUT);
|
||||
r = event_timedrecv(queue_id, &event, EVENT_WAIT_TIMEOUT);
|
||||
|
||||
d_assert(r != CORE_ERROR, continue,
|
||||
"While receiving a event message, error occurs");
|
||||
|
@ -44,7 +51,7 @@ void *THREAD_FUNC sm_main(void *data)
|
|||
/* if the gap is over 10 ms, execute preriodic jobs */
|
||||
if (now_tm - prev_tm > EVENT_WAIT_TIMEOUT)
|
||||
{
|
||||
event_timer_execute();
|
||||
event_timer_execute(&mme_sm.tm_service);
|
||||
|
||||
prev_tm = now_tm;
|
||||
}
|
||||
|
@ -54,11 +61,13 @@ void *THREAD_FUNC sm_main(void *data)
|
|||
continue;
|
||||
}
|
||||
|
||||
fsm_dispatch((fsm_t*)&g_mme_sm, (fsm_event_t*)&e);
|
||||
fsm_dispatch((fsm_t*)&mme_sm, (fsm_event_t*)&event);
|
||||
}
|
||||
|
||||
fsm_final((fsm_t*)&g_mme_sm, 0);
|
||||
fsm_clear((fsm_t*)&g_mme_sm);
|
||||
fsm_final((fsm_t*)&mme_sm, 0);
|
||||
fsm_clear((fsm_t*)&mme_sm);
|
||||
|
||||
event_delete(queue_id);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue