Skip to content

Commit fe75c71

Browse files
committed
[nrf fromlist] 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. Upstream PR #: 97362 Signed-off-by: Seppo Takalo <[email protected]>
1 parent 30557af commit fe75c71

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
}
@@ -1244,8 +1244,8 @@ static void modem_cmux_drop_frame(struct modem_cmux *cmux)
12441244
#if defined(CONFIG_MODEM_CMUX_LOG_LEVEL_DBG)
12451245
struct modem_cmux_frame *frame = &cmux->frame;
12461246

1247-
frame->data = cmux->receive_buf;
1248-
modem_cmux_log_frame(frame, "dropped", MIN(frame->data_len, cmux->receive_buf_size));
1247+
frame->data = cmux->config.receive_buf;
1248+
modem_cmux_log_frame(frame, "dropped", MIN(frame->data_len, cmux->config.receive_buf_size));
12491249
#endif
12501250
}
12511251

@@ -1369,9 +1369,9 @@ static void modem_cmux_process_received_byte(struct modem_cmux *cmux, uint8_t by
13691369
break;
13701370
}
13711371

1372-
if (cmux->frame.data_len > cmux->receive_buf_size) {
1372+
if (cmux->frame.data_len > cmux->config.receive_buf_size) {
13731373
LOG_ERR("Indicated frame data length %u exceeds receive buffer size %u",
1374-
cmux->frame.data_len, cmux->receive_buf_size);
1374+
cmux->frame.data_len, cmux->config.receive_buf_size);
13751375

13761376
modem_cmux_drop_frame(cmux);
13771377
break;
@@ -1383,8 +1383,8 @@ static void modem_cmux_process_received_byte(struct modem_cmux *cmux, uint8_t by
13831383

13841384
case MODEM_CMUX_RECEIVE_STATE_DATA:
13851385
/* Copy byte to data */
1386-
if (cmux->receive_buf_len < cmux->receive_buf_size) {
1387-
cmux->receive_buf[cmux->receive_buf_len] = byte;
1386+
if (cmux->receive_buf_len < cmux->config.receive_buf_size) {
1387+
cmux->config.receive_buf[cmux->receive_buf_len] = byte;
13881388
}
13891389
cmux->receive_buf_len++;
13901390

@@ -1397,9 +1397,9 @@ static void modem_cmux_process_received_byte(struct modem_cmux *cmux, uint8_t by
13971397
break;
13981398

13991399
case MODEM_CMUX_RECEIVE_STATE_FCS:
1400-
if (cmux->receive_buf_len > cmux->receive_buf_size) {
1401-
LOG_WRN("Receive buffer overrun (%u > %u)",
1402-
cmux->receive_buf_len, cmux->receive_buf_size);
1400+
if (cmux->receive_buf_len > cmux->config.receive_buf_size) {
1401+
LOG_WRN("Receive buffer overrun (%u > %u)", cmux->receive_buf_len,
1402+
cmux->config.receive_buf_size);
14031403
modem_cmux_drop_frame(cmux);
14041404
break;
14051405
}
@@ -1433,7 +1433,7 @@ static void modem_cmux_process_received_byte(struct modem_cmux *cmux, uint8_t by
14331433
}
14341434

14351435
/* Process frame */
1436-
cmux->frame.data = cmux->receive_buf;
1436+
cmux->frame.data = cmux->config.receive_buf;
14371437
modem_cmux_on_frame(cmux);
14381438

14391439
/* Await start of next frame */
@@ -1483,7 +1483,7 @@ static void modem_cmux_runtime_pm_handler(struct k_work *item)
14831483
struct k_work_delayable *dwork = k_work_delayable_from_work(item);
14841484
struct modem_cmux *cmux = CONTAINER_OF(dwork, struct modem_cmux, runtime_pm_work);
14851485

1486-
if (!cmux->enable_runtime_power_management) {
1486+
if (!cmux->config.enable_runtime_power_management) {
14871487
return;
14881488
}
14891489

@@ -1510,12 +1510,12 @@ static void modem_cmux_runtime_pm_handler(struct k_work *item)
15101510

15111511
static void runtime_pm_keepalive(struct modem_cmux *cmux)
15121512
{
1513-
if (cmux == NULL || !cmux->enable_runtime_power_management) {
1513+
if (cmux == NULL || !cmux->config.enable_runtime_power_management) {
15141514
return;
15151515
}
15161516

1517-
cmux->idle_timepoint = sys_timepoint_calc(cmux->idle_timeout);
1518-
k_work_reschedule(&cmux->runtime_pm_work, cmux->idle_timeout);
1517+
cmux->idle_timepoint = sys_timepoint_calc(cmux->config.idle_timeout);
1518+
k_work_reschedule(&cmux->runtime_pm_work, cmux->config.idle_timeout);
15191519
}
15201520

15211521
/** Transmit bytes bypassing the CMUX buffers.
@@ -1540,7 +1540,7 @@ static bool powersave_wait_wakeup(struct modem_cmux *cmux)
15401540
LOG_DBG("Power saving mode, wake up first");
15411541
set_state(cmux, MODEM_CMUX_STATE_WAKEUP);
15421542

1543-
if (cmux->close_pipe_on_power_save) {
1543+
if (cmux->config.close_pipe_on_power_save) {
15441544
ret = modem_pipe_open(cmux->pipe, K_FOREVER);
15451545
if (ret < 0) {
15461546
LOG_ERR("Failed to open pipe for wake up (%d)", ret);
@@ -1629,7 +1629,7 @@ static void modem_cmux_transmit_handler(struct k_work *item)
16291629
if (cmux->state == MODEM_CMUX_STATE_CONFIRM_POWERSAVE) {
16301630
set_state(cmux, MODEM_CMUX_STATE_POWERSAVE);
16311631
LOG_DBG("Entered power saving mode");
1632-
if (cmux->close_pipe_on_power_save) {
1632+
if (cmux->config.close_pipe_on_power_save) {
16331633
modem_pipe_close_async(cmux->pipe);
16341634
}
16351635
}
@@ -1934,18 +1934,13 @@ void modem_cmux_init(struct modem_cmux *cmux, const struct modem_cmux_config *co
19341934
__ASSERT_NO_MSG(config->transmit_buf != NULL);
19351935
__ASSERT_NO_MSG(config->transmit_buf_size >= MODEM_CMUX_DATA_FRAME_SIZE_MAX);
19361936

1937-
memset(cmux, 0x00, sizeof(*cmux));
1938-
cmux->callback = config->callback;
1939-
cmux->user_data = config->user_data;
1940-
cmux->receive_buf = config->receive_buf;
1941-
cmux->receive_buf_size = config->receive_buf_size;
1942-
cmux->t3_timepoint = sys_timepoint_calc(K_NO_WAIT);
1943-
cmux->enable_runtime_power_management = config->enable_runtime_power_management;
1944-
cmux->close_pipe_on_power_save = config->close_pipe_on_power_save;
1945-
cmux->idle_timeout =
1946-
cmux->enable_runtime_power_management ? config->idle_timeout : K_FOREVER;
1937+
*cmux = (struct modem_cmux){
1938+
.t3_timepoint = sys_timepoint_calc(K_NO_WAIT),
1939+
.config = *config,
1940+
};
19471941
sys_slist_init(&cmux->dlcis);
1948-
ring_buf_init(&cmux->transmit_rb, config->transmit_buf_size, config->transmit_buf);
1942+
ring_buf_init(&cmux->transmit_rb, cmux->config.transmit_buf_size,
1943+
cmux->config.transmit_buf);
19491944
k_mutex_init(&cmux->transmit_rb_lock);
19501945
k_work_init_delayable(&cmux->receive_work, modem_cmux_receive_handler);
19511946
k_work_init_delayable(&cmux->transmit_work, modem_cmux_transmit_handler);

0 commit comments

Comments
 (0)