From 44f423f11717367639a12e6b533e293cccf6f6ba Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Mon, 8 May 2017 18:00:28 +0200 Subject: timer: add osmo_timer_setup() Add a new function timer function to set up the timer, similar to what we have in the Linux kernel. This patch also converts existing opencoded timer setup in the libosmocore tree as initial client of this new function. This patch implicitly removes function callback passed by reference that defeat compile time type validation. Compile-tested only, but I ran make check that reports success when testing timer infrastructure. Change-Id: I2fa49972ecaab3748b25168b26d92034e9145666 --- include/osmocom/core/timer.h | 2 ++ src/fsm.c | 3 +-- src/gb/gprs_bssgp.c | 3 +-- src/gb/gprs_ns.c | 3 +-- src/gsm/gsm0411_smc.c | 3 +-- src/gsm/gsm0411_smr.c | 3 +-- src/gsm/lapd_core.c | 6 ++---- src/rate_ctr.c | 2 +- src/stats.c | 2 +- src/timer.c | 12 ++++++++++++ tests/timer/timer_test.c | 9 +++------ 11 files changed, 26 insertions(+), 22 deletions(-) diff --git a/include/osmocom/core/timer.h b/include/osmocom/core/timer.h index dbda13f2..48e7d8da 100644 --- a/include/osmocom/core/timer.h +++ b/include/osmocom/core/timer.h @@ -65,6 +65,8 @@ struct osmo_timer_list { * timer management */ +void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), void *data); + void osmo_timer_add(struct osmo_timer_list *timer); void osmo_timer_schedule(struct osmo_timer_list *timer, int seconds, int microseconds); diff --git a/src/fsm.c b/src/fsm.c index 9e6ef156..5e744827 100644 --- a/src/fsm.c +++ b/src/fsm.c @@ -206,8 +206,7 @@ struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void fi->fsm = fsm; fi->priv = priv; fi->log_level = log_level; - fi->timer.data = fi; - fi->timer.cb = fsm_tmr_cb; + osmo_timer_setup(&fi->timer, fsm_tmr_cb, fi); if (id) fi->id = talloc_strdup(fi, id); diff --git a/src/gb/gprs_bssgp.c b/src/gb/gprs_bssgp.c index dba4d5ca..fdbf7884 100644 --- a/src/gb/gprs_bssgp.c +++ b/src/gb/gprs_bssgp.c @@ -640,8 +640,7 @@ static int fc_queue_timer_cfg(struct bssgp_flow_control *fc) msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate; /* FIXME: add that time to fc->time_last_pdu and subtract it from * current time */ - fc->timer.data = fc; - fc->timer.cb = &fc_timer_cb; + osmo_timer_setup(&fc->timer, fc_timer_cb, fc); osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000); } else { /* If the PCU is telling us to not send any more data at all, diff --git a/src/gb/gprs_ns.c b/src/gb/gprs_ns.c index 7b497c2e..9a2a114f 100644 --- a/src/gb/gprs_ns.c +++ b/src/gb/gprs_ns.c @@ -238,8 +238,7 @@ struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci) /* before RESET procedure: BLOCKED and DEAD */ nsvc->state = NSE_S_BLOCKED; nsvc->nsi = nsi; - nsvc->timer.cb = gprs_ns_timer_cb; - nsvc->timer.data = nsvc; + osmo_timer_setup(&nsvc->timer, gprs_ns_timer_cb, nsvc); nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci); nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, nsvci); diff --git a/src/gsm/gsm0411_smc.c b/src/gsm/gsm0411_smc.c index c44423d4..4c083659 100644 --- a/src/gsm/gsm0411_smc.c +++ b/src/gsm/gsm0411_smc.c @@ -184,8 +184,7 @@ static int gsm411_mmsms_send_msg(struct gsm411_smc_inst *inst) /* 5.2.3.1.2: enter MO-wait for CP-ACK */ /* 5.2.3.2.3: enter MT-wait for CP-ACK */ new_cp_state(inst, GSM411_CPS_WAIT_CP_ACK); - inst->cp_timer.data = inst; - inst->cp_timer.cb = cp_timer_expired; + osmo_timer_setup(&inst->cp_timer, cp_timer_expired, inst); /* 5.3.2.1: Set Timer TC1A */ osmo_timer_schedule(&inst->cp_timer, inst->cp_tc1, 0); /* clone cp_msg */ diff --git a/src/gsm/gsm0411_smr.c b/src/gsm/gsm0411_smr.c index a1ee9804..6d7fb8b5 100644 --- a/src/gsm/gsm0411_smr.c +++ b/src/gsm/gsm0411_smr.c @@ -77,8 +77,7 @@ void gsm411_smr_init(struct gsm411_smr_inst *inst, uint64_t id, int network, inst->rp_state = GSM411_RPS_IDLE; inst->rl_recv = rl_recv; inst->mn_send = mn_send; - inst->rp_timer.data = inst; - inst->rp_timer.cb = rp_timer_expired; + osmo_timer_setup(&inst->rp_timer, rp_timer_expired, inst); LOGP(DLSMS, LOGL_INFO, SMR_LOG_STR "instance created for %s.\n", diff --git a/src/gsm/lapd_core.c b/src/gsm/lapd_core.c index e0bbcabf..c81b2a05 100644 --- a/src/gsm/lapd_core.c +++ b/src/gsm/lapd_core.c @@ -267,12 +267,10 @@ void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, dl->n200 = 3; dl->t200_sec = 1; dl->t200_usec = 0; - dl->t200.data = dl; - dl->t200.cb = &lapd_t200_cb; + osmo_timer_setup(&dl->t200, lapd_t200_cb, dl); dl->t203_sec = 10; dl->t203_usec = 0; - dl->t203.data = dl; - dl->t203.cb = &lapd_t203_cb; + osmo_timer_setup(&dl->t203, lapd_t203_cb, dl); dl->maxf = maxf; if (k > v_range - 1) k = v_range - 1; diff --git a/src/rate_ctr.c b/src/rate_ctr.c index f995f3fd..3ccd065f 100644 --- a/src/rate_ctr.c +++ b/src/rate_ctr.c @@ -147,7 +147,7 @@ static void rate_ctr_timer_cb(void *data) int rate_ctr_init(void *tall_ctx) { tall_rate_ctr_ctx = tall_ctx; - rate_ctr_timer.cb = rate_ctr_timer_cb; + osmo_timer_setup(&rate_ctr_timer, rate_ctr_timer_cb, NULL); osmo_timer_schedule(&rate_ctr_timer, 1, 0); return 0; diff --git a/src/stats.c b/src/stats.c index 1efc8cd8..dee5d81a 100644 --- a/src/stats.c +++ b/src/stats.c @@ -107,7 +107,7 @@ static int start_timer() if (!is_initialised) return -ESRCH; - osmo_stats_timer.cb = osmo_stats_timer_cb; + osmo_timer_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL); osmo_timer_schedule(&osmo_stats_timer, 0, 1); return 0; diff --git a/src/timer.c b/src/timer.c index cc6d5ccd..47d1786b 100644 --- a/src/timer.c +++ b/src/timer.c @@ -65,6 +65,18 @@ static void __add_timer(struct osmo_timer_list *timer) rb_insert_color(&timer->node, &timer_root); } +/*! \brief set up timer callback and data + * \param[in] timer the timer that should be added + * \param[in] callback function to be called when timer expires + * \param[in] pointer to data that passed to the callback function + */ +void osmo_timer_setup(struct osmo_timer_list *timer, void (*cb)(void *data), + void *data) +{ + timer->cb = cb; + timer->data = data; +} + /*! \brief add a new timer to the timer management * \param[in] timer the timer that should be added */ diff --git a/tests/timer/timer_test.c b/tests/timer/timer_test.c index 066dc72d..d2b0204d 100644 --- a/tests/timer/timer_test.c +++ b/tests/timer/timer_test.c @@ -39,10 +39,7 @@ static void main_timer_fired(void *data); static void secondary_timer_fired(void *data); static unsigned int main_timer_step = 0; -static struct osmo_timer_list main_timer = { - .cb = main_timer_fired, - .data = &main_timer_step, -}; +static struct osmo_timer_list main_timer; static LLIST_HEAD(timer_test_list); @@ -92,8 +89,7 @@ static void main_timer_fired(void *data) return; } osmo_gettimeofday(&v->start, NULL); - v->timer.cb = secondary_timer_fired; - v->timer.data = v; + osmo_timer_setup(&v->timer, secondary_timer_fired, v); unsigned int seconds = (i & 0x7) + 1; v->stop.tv_sec = v->start.tv_sec + seconds; v->stop.tv_usec = v->start.tv_usec; @@ -195,6 +191,7 @@ int main(int argc, char *argv[]) " %d steps of %d msecs each\n", timer_nsteps, steps, TIME_BETWEEN_TIMER_CHECKS / 1000); + osmo_timer_setup(&main_timer, main_timer_fired, &main_timer_step); osmo_timer_schedule(&main_timer, 1, 0); #ifdef HAVE_SYS_SELECT_H -- cgit v1.2.3