Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
cmux.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Trackunit Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7/*
8 * This library uses CMUX to create multiple data channels, called DLCIs, on a single serial bus.
9 * Each DLCI has an address from 1 to 63. DLCI address 0 is reserved for control commands.
10 *
11 * Design overview:
12 *
13 * DLCI1 <-----------+ +-------> DLCI1
14 * v v
15 * DLCI2 <---> CMUX instance <--> Serial bus <--> Client <--> DLCI2
16 * ^ ^
17 * DLCI3 <-----------+ +-------> DLCI3
18 *
19 * Writing to and from the CMUX instances is done using the modem_pipe API.
20 */
21
22#include <zephyr/kernel.h>
23#include <zephyr/types.h>
25#include <zephyr/sys/atomic.h>
26
27#include <zephyr/modem/pipe.h>
28#include <zephyr/modem/stats.h>
29
30#ifndef ZEPHYR_MODEM_CMUX_
31#define ZEPHYR_MODEM_CMUX_
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
44struct modem_cmux;
45
50
51typedef void (*modem_cmux_callback)(struct modem_cmux *cmux, enum modem_cmux_event event,
52 void *user_data);
53
58enum modem_cmux_state {
59 MODEM_CMUX_STATE_DISCONNECTED = 0,
60 MODEM_CMUX_STATE_CONNECTING,
61 MODEM_CMUX_STATE_CONNECTED,
62 MODEM_CMUX_STATE_DISCONNECTING,
63};
64
65enum modem_cmux_receive_state {
66 MODEM_CMUX_RECEIVE_STATE_SOF = 0,
67 MODEM_CMUX_RECEIVE_STATE_RESYNC,
68 MODEM_CMUX_RECEIVE_STATE_ADDRESS,
69 MODEM_CMUX_RECEIVE_STATE_ADDRESS_CONT,
70 MODEM_CMUX_RECEIVE_STATE_CONTROL,
71 MODEM_CMUX_RECEIVE_STATE_LENGTH,
72 MODEM_CMUX_RECEIVE_STATE_LENGTH_CONT,
73 MODEM_CMUX_RECEIVE_STATE_DATA,
74 MODEM_CMUX_RECEIVE_STATE_FCS,
75 MODEM_CMUX_RECEIVE_STATE_DROP,
76 MODEM_CMUX_RECEIVE_STATE_EOF,
77};
78
79enum modem_cmux_dlci_state {
80 MODEM_CMUX_DLCI_STATE_CLOSED,
81 MODEM_CMUX_DLCI_STATE_OPENING,
82 MODEM_CMUX_DLCI_STATE_OPEN,
83 MODEM_CMUX_DLCI_STATE_CLOSING,
84};
85
86struct modem_cmux_dlci {
87 sys_snode_t node;
88
89 /* Pipe */
90 struct modem_pipe pipe;
91
92 /* Context */
93 uint16_t dlci_address;
94 struct modem_cmux *cmux;
95
96 /* Receive buffer */
97 struct ring_buf receive_rb;
98 struct k_mutex receive_rb_lock;
99
100 /* Work */
101 struct k_work_delayable open_work;
102 struct k_work_delayable close_work;
103
104 /* State */
105 enum modem_cmux_dlci_state state;
106
107 /* Statistics */
108#if CONFIG_MODEM_STATS
109 struct modem_stats_buffer receive_buf_stats;
110#endif
111};
112
113struct modem_cmux_frame {
114 uint8_t dlci_address;
115 bool cr;
116 bool pf;
117 uint8_t type;
118 const uint8_t *data;
119 uint16_t data_len;
120};
121
122struct modem_cmux_work {
123 struct k_work_delayable dwork;
124 struct modem_cmux *cmux;
125};
126
127struct modem_cmux {
128 /* Bus pipe */
129 struct modem_pipe *pipe;
130
131 /* Event handler */
132 modem_cmux_callback callback;
133 void *user_data;
134
135 /* DLCI channel contexts */
136 sys_slist_t dlcis;
137
138 /* State */
139 enum modem_cmux_state state;
140 bool flow_control_on;
141
142 /* Work lock */
143 bool attached;
144 struct k_spinlock work_lock;
145
146 /* Receive state*/
147 enum modem_cmux_receive_state receive_state;
148
149 /* Receive buffer */
150 uint8_t *receive_buf;
151 uint16_t receive_buf_size;
152 uint16_t receive_buf_len;
153
154 uint8_t work_buf[CONFIG_MODEM_CMUX_WORK_BUFFER_SIZE];
155
156 /* Transmit buffer */
157 struct ring_buf transmit_rb;
158 struct k_mutex transmit_rb_lock;
159
160 /* Received frame */
161 struct modem_cmux_frame frame;
162 uint8_t frame_header[5];
163 uint16_t frame_header_len;
164
165 /* Work */
166 struct k_work_delayable receive_work;
167 struct k_work_delayable transmit_work;
168 struct k_work_delayable connect_work;
169 struct k_work_delayable disconnect_work;
170
171 /* Synchronize actions */
172 struct k_event event;
173
174 /* Statistics */
175#if CONFIG_MODEM_STATS
176 struct modem_stats_buffer receive_buf_stats;
177 struct modem_stats_buffer transmit_buf_stats;
178#endif
179};
180
202
208void modem_cmux_init(struct modem_cmux *cmux, const struct modem_cmux_config *config);
209
221
229struct modem_pipe *modem_cmux_dlci_init(struct modem_cmux *cmux, struct modem_cmux_dlci *dlci,
230 const struct modem_cmux_dlci_config *config);
231
238int modem_cmux_attach(struct modem_cmux *cmux, struct modem_pipe *pipe);
239
250int modem_cmux_connect(struct modem_cmux *cmux);
251
262int modem_cmux_connect_async(struct modem_cmux *cmux);
263
274int modem_cmux_disconnect(struct modem_cmux *cmux);
275
286int modem_cmux_disconnect_async(struct modem_cmux *cmux);
287
298void modem_cmux_release(struct modem_cmux *cmux);
299
304#ifdef __cplusplus
305}
306#endif
307
308#endif /* ZEPHYR_MODEM_CMUX_ */
int modem_cmux_connect(struct modem_cmux *cmux)
Connect CMUX instance.
int modem_cmux_disconnect(struct modem_cmux *cmux)
Close down and disconnect CMUX instance.
int modem_cmux_disconnect_async(struct modem_cmux *cmux)
Close down and disconnect CMUX instance asynchronously.
void(* modem_cmux_callback)(struct modem_cmux *cmux, enum modem_cmux_event event, void *user_data)
Definition cmux.h:51
int modem_cmux_connect_async(struct modem_cmux *cmux)
Connect CMUX instance asynchronously.
modem_cmux_event
Definition cmux.h:46
int modem_cmux_attach(struct modem_cmux *cmux, struct modem_pipe *pipe)
Attach CMUX instance to pipe.
struct modem_pipe * modem_cmux_dlci_init(struct modem_cmux *cmux, struct modem_cmux_dlci *dlci, const struct modem_cmux_dlci_config *config)
Initialize DLCI instance and register it with CMUX instance.
void modem_cmux_init(struct modem_cmux *cmux, const struct modem_cmux_config *config)
Initialize CMUX instance.
void modem_cmux_release(struct modem_cmux *cmux)
Release CMUX instance from pipe.
@ MODEM_CMUX_EVENT_DISCONNECTED
Definition cmux.h:48
@ MODEM_CMUX_EVENT_CONNECTED
Definition cmux.h:47
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:49
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
Public kernel APIs.
state
Definition parser_state.h:29
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Event Structure.
Definition kernel.h:2307
Mutex Structure.
Definition kernel.h:3004
Kernel Spin Lock.
Definition spinlock.h:45
A structure used to submit work after a delay.
Definition kernel.h:4013
Contains CMUX instance configuration data.
Definition cmux.h:188
uint8_t * receive_buf
Receive buffer.
Definition cmux.h:194
modem_cmux_callback callback
Invoked when event occurs.
Definition cmux.h:190
uint16_t receive_buf_size
Size of receive buffer in bytes [127, ...].
Definition cmux.h:196
void * user_data
Free to use pointer passed to event handler when invoked.
Definition cmux.h:192
uint8_t * transmit_buf
Transmit buffer.
Definition cmux.h:198
uint16_t transmit_buf_size
Size of transmit buffer in bytes [149, ...].
Definition cmux.h:200
CMUX DLCI configuration.
Definition cmux.h:213
uint8_t dlci_address
DLCI channel address.
Definition cmux.h:215
uint8_t * receive_buf
Receive buffer used by pipe.
Definition cmux.h:217
uint16_t receive_buf_size
Size of receive buffer used by pipe [127, ...].
Definition cmux.h:219
A structure to represent a ring buffer.
Definition ring_buffer.h:41