Skip to content

Commit 437becb

Browse files
SeppoTakalonashif
authored andcommitted
modem: cmux: Add struct cmux_config into struct cmux
Instead of copying all fields from cmux_config into run-time struct cmux, just have the configuration structure as a member. Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
1 parent 3926655 commit 437becb

File tree

2 files changed

+52
-70
lines changed

2 files changed

+52
-70
lines changed

include/zephyr/modem/cmux.h

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ enum modem_cmux_event {
5353
typedef void (*modem_cmux_callback)(struct modem_cmux *cmux, enum modem_cmux_event event,
5454
void *user_data);
5555

56+
/**
57+
* @brief Contains CMUX instance configuration data
58+
*/
59+
struct modem_cmux_config {
60+
/** Invoked when event occurs */
61+
modem_cmux_callback callback;
62+
/** Free to use pointer passed to event handler when invoked */
63+
void *user_data;
64+
/** Receive buffer */
65+
uint8_t *receive_buf;
66+
/** Size of receive buffer in bytes [127, ...] */
67+
uint16_t receive_buf_size;
68+
/** Transmit buffer */
69+
uint8_t *transmit_buf;
70+
/** Size of transmit buffer in bytes [149, ...] */
71+
uint16_t transmit_buf_size;
72+
/** Enable runtime power management */
73+
bool enable_runtime_power_management;
74+
/** Close pipe on power save */
75+
bool close_pipe_on_power_save;
76+
/** Idle timeout for power save */
77+
k_timeout_t idle_timeout;
78+
};
79+
5680
/**
5781
* @cond INTERNAL_HIDDEN
5882
*/
@@ -148,34 +172,20 @@ struct modem_cmux {
148172
/* Bus pipe */
149173
struct modem_pipe *pipe;
150174

151-
/* Event handler */
152-
modem_cmux_callback callback;
153-
void *user_data;
154-
155175
/* DLCI channel contexts */
156176
sys_slist_t dlcis;
157177

158178
/* State */
159179
enum modem_cmux_state state;
160180
bool flow_control_on : 1;
161181
bool initiator : 1;
162-
/** Enable runtime power management */
163-
bool enable_runtime_power_management;
164-
/** Close pipe on power save */
165-
bool close_pipe_on_power_save;
166-
/** Idle timeout for power save */
167-
k_timeout_t idle_timeout;
168182

169183
/* Work lock */
170184
bool attached : 1;
171185
struct k_spinlock work_lock;
172186

173187
/* Receive state*/
174188
enum modem_cmux_receive_state receive_state;
175-
176-
/* Receive buffer */
177-
uint8_t *receive_buf;
178-
uint16_t receive_buf_size;
179189
uint16_t receive_buf_len;
180190

181191
uint8_t work_buf[MODEM_CMUX_WORK_BUFFER_SIZE];
@@ -206,36 +216,13 @@ struct modem_cmux {
206216
struct modem_stats_buffer receive_buf_stats;
207217
struct modem_stats_buffer transmit_buf_stats;
208218
#endif
219+
struct modem_cmux_config config;
209220
};
210221

211222
/**
212223
* @endcond
213224
*/
214225

215-
/**
216-
* @brief Contains CMUX instance configuration data
217-
*/
218-
struct modem_cmux_config {
219-
/** Invoked when event occurs */
220-
modem_cmux_callback callback;
221-
/** Free to use pointer passed to event handler when invoked */
222-
void *user_data;
223-
/** Receive buffer */
224-
uint8_t *receive_buf;
225-
/** Size of receive buffer in bytes [127, ...] */
226-
uint16_t receive_buf_size;
227-
/** Transmit buffer */
228-
uint8_t *transmit_buf;
229-
/** Size of transmit buffer in bytes [149, ...] */
230-
uint16_t transmit_buf_size;
231-
/** Enable runtime power management */
232-
bool enable_runtime_power_management;
233-
/** Close pipe on power save */
234-
bool close_pipe_on_power_save;
235-
/** Idle timeout for power save */
236-
k_timeout_t idle_timeout;
237-
};
238-
239226
/**
240227
* @brief Initialize CMUX instance
241228
* @param cmux CMUX instance

subsys/modem/modem_cmux.c

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ static uint32_t modem_cmux_get_receive_buf_length(struct modem_cmux *cmux)
400400

401401
static uint32_t modem_cmux_get_receive_buf_size(struct modem_cmux *cmux)
402402
{
403-
return cmux->receive_buf_size;
403+
return cmux->config.receive_buf_size;
404404
}
405405

406406
static uint32_t modem_cmux_get_transmit_buf_length(struct modem_cmux *cmux)
@@ -483,11 +483,11 @@ static void modem_cmux_log_received_command(const struct modem_cmux_command *com
483483

484484
static void modem_cmux_raise_event(struct modem_cmux *cmux, enum modem_cmux_event event)
485485
{
486-
if (cmux->callback == NULL) {
486+
if (cmux->config.callback == NULL) {
487487
return;
488488
}
489489

490-
cmux->callback(cmux, event, cmux->user_data);
490+
cmux->config.callback(cmux, event, cmux->config.user_data);
491491
}
492492

493493
static void modem_cmux_bus_callback(struct modem_pipe *pipe, enum modem_pipe_event event,
@@ -506,7 +506,7 @@ static void modem_cmux_bus_callback(struct modem_pipe *pipe, enum modem_pipe_eve
506506
break;
507507
case MODEM_PIPE_EVENT_TRANSMIT_IDLE:
508508
/* If we keep UART open in power-save, we should avoid waking up on RX idle */
509-
if (!cmux->close_pipe_on_power_save && is_powersaving(cmux)) {
509+
if (!cmux->config.close_pipe_on_power_save && is_powersaving(cmux)) {
510510
break;
511511
}
512512
modem_work_schedule(&cmux->transmit_work, K_NO_WAIT);
@@ -896,7 +896,7 @@ static void modem_cmux_on_psc_response(struct modem_cmux *cmux)
896896
set_state(cmux, MODEM_CMUX_STATE_POWERSAVE);
897897
k_mutex_unlock(&cmux->transmit_rb_lock);
898898

899-
if (cmux->close_pipe_on_power_save) {
899+
if (cmux->config.close_pipe_on_power_save) {
900900
modem_pipe_close_async(cmux->pipe);
901901
}
902902
}
@@ -1249,8 +1249,8 @@ static void modem_cmux_drop_frame(struct modem_cmux *cmux)
12491249
#if defined(CONFIG_MODEM_CMUX_LOG_LEVEL_DBG)
12501250
struct modem_cmux_frame *frame = &cmux->frame;
12511251

1252-
frame->data = cmux->receive_buf;
1253-
modem_cmux_log_frame(frame, "dropped", MIN(frame->data_len, cmux->receive_buf_size));
1252+
frame->data = cmux->config.receive_buf;
1253+
modem_cmux_log_frame(frame, "dropped", MIN(frame->data_len, cmux->config.receive_buf_size));
12541254
#endif
12551255
}
12561256

@@ -1374,9 +1374,9 @@ static void modem_cmux_process_received_byte(struct modem_cmux *cmux, uint8_t by
13741374
break;
13751375
}
13761376

1377-
if (cmux->frame.data_len > cmux->receive_buf_size) {
1377+
if (cmux->frame.data_len > cmux->config.receive_buf_size) {
13781378
LOG_ERR("Indicated frame data length %u exceeds receive buffer size %u",
1379-
cmux->frame.data_len, cmux->receive_buf_size);
1379+
cmux->frame.data_len, cmux->config.receive_buf_size);
13801380

13811381
modem_cmux_drop_frame(cmux);
13821382
break;
@@ -1388,8 +1388,8 @@ static void modem_cmux_process_received_byte(struct modem_cmux *cmux, uint8_t by
13881388

13891389
case MODEM_CMUX_RECEIVE_STATE_DATA:
13901390
/* Copy byte to data */
1391-
if (cmux->receive_buf_len < cmux->receive_buf_size) {
1392-
cmux->receive_buf[cmux->receive_buf_len] = byte;
1391+
if (cmux->receive_buf_len < cmux->config.receive_buf_size) {
1392+
cmux->config.receive_buf[cmux->receive_buf_len] = byte;
13931393
}
13941394
cmux->receive_buf_len++;
13951395

@@ -1402,9 +1402,9 @@ static void modem_cmux_process_received_byte(struct modem_cmux *cmux, uint8_t by
14021402
break;
14031403

14041404
case MODEM_CMUX_RECEIVE_STATE_FCS:
1405-
if (cmux->receive_buf_len > cmux->receive_buf_size) {
1406-
LOG_WRN("Receive buffer overrun (%u > %u)",
1407-
cmux->receive_buf_len, cmux->receive_buf_size);
1405+
if (cmux->receive_buf_len > cmux->config.receive_buf_size) {
1406+
LOG_WRN("Receive buffer overrun (%u > %u)", cmux->receive_buf_len,
1407+
cmux->config.receive_buf_size);
14081408
modem_cmux_drop_frame(cmux);
14091409
break;
14101410
}
@@ -1438,7 +1438,7 @@ static void modem_cmux_process_received_byte(struct modem_cmux *cmux, uint8_t by
14381438
}
14391439

14401440
/* Process frame */
1441-
cmux->frame.data = cmux->receive_buf;
1441+
cmux->frame.data = cmux->config.receive_buf;
14421442
modem_cmux_on_frame(cmux);
14431443

14441444
/* Await start of next frame */
@@ -1488,7 +1488,7 @@ static void modem_cmux_runtime_pm_handler(struct k_work *item)
14881488
struct k_work_delayable *dwork = k_work_delayable_from_work(item);
14891489
struct modem_cmux *cmux = CONTAINER_OF(dwork, struct modem_cmux, runtime_pm_work);
14901490

1491-
if (!cmux->enable_runtime_power_management) {
1491+
if (!cmux->config.enable_runtime_power_management) {
14921492
return;
14931493
}
14941494

@@ -1515,12 +1515,12 @@ static void modem_cmux_runtime_pm_handler(struct k_work *item)
15151515

15161516
static void runtime_pm_keepalive(struct modem_cmux *cmux)
15171517
{
1518-
if (cmux == NULL || !cmux->enable_runtime_power_management) {
1518+
if (cmux == NULL || !cmux->config.enable_runtime_power_management) {
15191519
return;
15201520
}
15211521

1522-
cmux->idle_timepoint = sys_timepoint_calc(cmux->idle_timeout);
1523-
k_work_reschedule(&cmux->runtime_pm_work, cmux->idle_timeout);
1522+
cmux->idle_timepoint = sys_timepoint_calc(cmux->config.idle_timeout);
1523+
k_work_reschedule(&cmux->runtime_pm_work, cmux->config.idle_timeout);
15241524
}
15251525

15261526
/** Transmit bytes bypassing the CMUX buffers.
@@ -1545,7 +1545,7 @@ static bool powersave_wait_wakeup(struct modem_cmux *cmux)
15451545
LOG_DBG("Power saving mode, wake up first");
15461546
set_state(cmux, MODEM_CMUX_STATE_WAKEUP);
15471547

1548-
if (cmux->close_pipe_on_power_save) {
1548+
if (cmux->config.close_pipe_on_power_save) {
15491549
ret = modem_pipe_open(cmux->pipe, K_FOREVER);
15501550
if (ret < 0) {
15511551
LOG_ERR("Failed to open pipe for wake up (%d)", ret);
@@ -1634,7 +1634,7 @@ static void modem_cmux_transmit_handler(struct k_work *item)
16341634
if (cmux->state == MODEM_CMUX_STATE_CONFIRM_POWERSAVE) {
16351635
set_state(cmux, MODEM_CMUX_STATE_POWERSAVE);
16361636
LOG_DBG("Entered power saving mode");
1637-
if (cmux->close_pipe_on_power_save) {
1637+
if (cmux->config.close_pipe_on_power_save) {
16381638
modem_pipe_close_async(cmux->pipe);
16391639
}
16401640
}
@@ -1940,18 +1940,13 @@ void modem_cmux_init(struct modem_cmux *cmux, const struct modem_cmux_config *co
19401940
__ASSERT_NO_MSG(config->transmit_buf != NULL);
19411941
__ASSERT_NO_MSG(config->transmit_buf_size >= MODEM_CMUX_DATA_FRAME_SIZE_MAX);
19421942

1943-
memset(cmux, 0x00, sizeof(*cmux));
1944-
cmux->callback = config->callback;
1945-
cmux->user_data = config->user_data;
1946-
cmux->receive_buf = config->receive_buf;
1947-
cmux->receive_buf_size = config->receive_buf_size;
1948-
cmux->t3_timepoint = sys_timepoint_calc(K_NO_WAIT);
1949-
cmux->enable_runtime_power_management = config->enable_runtime_power_management;
1950-
cmux->close_pipe_on_power_save = config->close_pipe_on_power_save;
1951-
cmux->idle_timeout =
1952-
cmux->enable_runtime_power_management ? config->idle_timeout : K_FOREVER;
1943+
*cmux = (struct modem_cmux){
1944+
.t3_timepoint = sys_timepoint_calc(K_NO_WAIT),
1945+
.config = *config,
1946+
};
19531947
sys_slist_init(&cmux->dlcis);
1954-
ring_buf_init(&cmux->transmit_rb, config->transmit_buf_size, config->transmit_buf);
1948+
ring_buf_init(&cmux->transmit_rb, cmux->config.transmit_buf_size,
1949+
cmux->config.transmit_buf);
19551950
k_mutex_init(&cmux->transmit_rb_lock);
19561951
k_work_init_delayable(&cmux->receive_work, modem_cmux_receive_handler);
19571952
k_work_init_delayable(&cmux->transmit_work, modem_cmux_transmit_handler);

0 commit comments

Comments
 (0)