Commit Graph

84 Commits

Author SHA1 Message Date
Henrik Skreslet e254a7f3c8 modem: cmux: added validation of cmux frame length
Validates cmux frame length and drops it if its larger
than the receive buffer

Signed-off-by: Henrik Skreslet <henrik.skreslet@gmail.com>
2024-11-07 08:31:48 -06:00
Pisit Sawangvonganan ea2d785a28 modem: cmux: replace `crc8` with `crc8_rohc` for FCS calculation
To improve performance in FCS calculation, this commit replaces
the usage of the generic `crc8` function with the specific `crc8_rohc`
function in `modem_cmux.c`.
The `crc8_rohc` function utilizes a small table approach, enhancing
the efficiency of CRC-8/ROHC variant calculations while optimizing
memory usage.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-10-09 09:48:00 +02:00
Anders T. Akre 26cd98e483 modem: ubx: fix incoming byte processing
`ret` holds the amount of bytes received from the modem. However during
processing of the bytes its value is overwritten by the return value of
`modem_ubx_process_received_byte`, in practice discarding all but the
first byte read.

To prevent this, store the length in a separate variable.

Signed-off-by: Anders T. Akre <anders@akre.io>
2024-08-02 03:29:18 -04:00
Bjarki Arge Andreasen 372c7183ef modem: pipe: Add explicit timeout to sync APIs
The modem pipe APIs include synchronous calls to open/close,
which internally use a fixed timeout of 10 seconds. The timeout
should be configurable through the APIs, anywhere from K_NO_WAIT
to K_FOREVER.

This commit adds timeout parameters to the open/close APIs, and
updates in-tree usage of the open/close APIs to explicitly
provide the previously implicit timeout of 10 seconds.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-07-29 14:14:42 +02:00
Pisit Sawangvonganan c43b20e85c modem: cmux: apply `static const` to reduce RAM usage
Added `static const` to various `struct modem_pipe_api` and
`frame` in `modem_cmux_connect_handler` to make these structures
immutable and reduce RAM usage.

Corrected a typo, changing "consequtive" to "consecutive".

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-07-27 20:51:42 +03:00
Pisit Sawangvonganan ba30e7b350 modem: pipe: make `modem_pipe_api` a pointer to constant
Changed the `api` field in the `modem_pipe` structure to be
a pointer to constant.

This ensures that the `api` field remains immutable after
initialization.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-07-27 20:51:42 +03:00
Bjarki Arge Andreasen 50f1ab6e20 modem: cmux: disable all work when released
The cmux module does not properly cancel all work when it is
released from its modem pipe. Releasing CMUX must cancel all
work as the pipe is no longer available, as it is set to NULL.

If work remains which tries to transmit something to the now
released pipe, a NULL dereference occurs.

This exact issue occurs if a DLCI channel is trying to open
after the modem pipe has been released, as the CMUX component
will send an open request at a fixed interval.

The expected behavior is for the open request to be cancelled,
and further open requests to be prevented until cmux is again
attached to a pipe.

Lastly, added a check to ensure that the pipe is indeed released
when calling attach, to ensure all work is indeed stopped before
modifying the pipe and buffers.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-06-26 09:27:20 -04:00
Bjarki Arge Andreasen 016b4f1d33 modem: ppp: coverity 330618: explicitly ignore retval
Explicitly ignore the return value of net_pkt_read_u8() as the
net_pkt is validated before being queued for transmit within
modem_ppp_ppp_api_send()

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-06-24 14:25:46 +02:00
Bjarki Arge Andreasen 5c24c79a90 modem: pipe: simplify synchronization
The design of the pipe is overly complicated compared to the
in-tree and planned future use of the pipe module.

The pipe is currently designed to protect against multiple
threads calling any API simultaineously. This is not neccesary
as only one thread ever calls open/close/transmit/receive at
once, while the notification APIs are potentially called by a
different thread.

This commit removes the synchronization of calls to the open/
close/receive/transmit APIs. It also uses a k_event for thread
safe event and state handling instead of a k_mutex and k_condvar.

The callback is proteced by a k_sem as it modified using the
attach/release APIs, which can be called simultaneously to a
thread invoking the callback.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-06-13 16:43:49 -04:00
Bjarki Arge Andreasen 494fab8ea4 modem: chat: patch unintended behavior in modem_chat_run_script()
Trying to start a chat script using either modem_chat_run_script()
or modem_chat_run_script_async() should result in returning -EBUSY
without affecting the currently running script and thread waiting
on the current script to stop.

The current behavior causes the thread waiting for the current
script to stop to return with error -EAGAIN, and the thread trying
to start the new script to return with error -EBUSY.

This commit moves the reset of the sem the current thread is
waiting on, to after the check of whether a script is currently
running, leaving the current thread unaffected as is intended.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-06-13 17:49:23 +02:00
Bjarki Arge Andreasen 1f7d0b6cb0 modem: add modem_pipelink module
Add modem pipelink module which exposes modem pipes globally.
The pipelink module implements a callback to inform when a
pipe becomes available to use by whichever modem is attached
to it. This could be a shell, or a network interface.

The module aims to allow modem drivers to be split into modules,
and allowing applications to implement their own custom logic
without altering the modem drivers.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-06-10 15:12:34 -05:00
Bjarki Arge Andreasen 000784b91d modem: chat: Add runtime APIs for chat scripts
Add runtime APIs for chat scripts.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-05-28 10:02:06 +02:00
Bjarki Arge Andreasen 2a575a6bf5 modem: chat: Add runtime API to modem_chat_script_chat
Add APIs allowing for modifying the modem_chat_script_chat
safely at runtime.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-05-28 10:02:06 +02:00
Bjarki Arge Andreasen 8b4822861b modem: chat: Add runtime API to modem chat matches
Add APIs for changing the contents of modem chat matches
safely at runtime.

This allows for reusing a single modem_chat_match at the cost
of placing the match and its buffers in RAM.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-05-28 10:02:06 +02:00
Bjarki Arge Andreasen 5e193edbc3 modem: chat: implement buffer stats
implement buffer statistics for modem_chat module.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-05-20 14:40:06 +03:00
Bjarki Arge Andreasen 88bb45082a modem: ppp: implement modem_statistics
Implement modem buffer statistics for ppp module.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-05-20 14:40:06 +03:00
Bjarki Arge Andreasen b636327601 modem: cmux: Implement modem backend statistics
Implement modem backend statistics into modem CMUX DLCI
channel receive buffer and CMUX transmit and receive buffers.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-05-20 14:40:06 +03:00
Bjarki Arge Andreasen 9f45102299 modem: backend: uart: async: Implement modem backend stat
Implement modem backend statistics in async uart modem backend.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-05-20 14:40:06 +03:00
Bjarki Arge Andreasen beeb769bb1 modem: backend: uart: isr: Implement modem backend stat
Implement modem backend statistics into isr uart modem backend.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-05-20 14:40:06 +03:00
Bjarki Arge Andreasen aba5580af3 modem: backend: Add statistics module
Add statistics module which tracks the usage of modem backend
transmit/receive buffers if implemented by a backend.

A modem backend may register itself to the modem backend
stat module, and advertise its transmit and receive buffer
sizes and momentary usage (length).

The modem backend module tracks the maximum usage, which can be
output to the shell using the "modem_stats buffer" shell command.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-05-20 14:40:06 +03:00
Bjarki Arge Andreasen 2e9279f369 modem_chat: Allow timeout script chat commands
Modem chat scripts should be able to add a script command which
is simply a delay. The validation of chat scripts currently
disallows chat script commands which only have a timeout set.

Update modem_chat to allow modem chat script commands which
only have a timeout. Additionally do a bit of cleanup to
remove duplicate code.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-05-07 15:09:21 -05:00
Tomi Fontanilles a986905df5 modem: chat: implement helper defines/structs
Those helpers allow to define some typical kinds of chat matches and
scripts with more ease/less boilerplate.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-04-24 15:00:10 -04:00
Tomi Fontanilles 18170ebce1 modem: cmux: make work buffer size configurable
A Kconfig item is added and the buffer
is embedded into the modem_cmux struct.
The default value is increased from 16 to 64 bytes in
an effort to reduce the number of modem_pipe_receive() calls.

CONFIG_MODEM_CHAT_LOG_BUFFER is renamed
to CONFIG_MODEM_CHAT_LOG_BUFFER_SIZE for consistency.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-03-26 09:29:55 +00:00
Tomi Fontanilles 419a398c01 modem: backends: uart_isr: improve the reception of bytes
Add a configurable delay between when a byte is received and
MODEM_PIPE_EVENT_RECEIVE_READY is sent.

This fixes data reception at baud rates above 460800, and
most likely also reduces the workload at any baud rate
when receiving bytes by not going through the work item
and callbacks for every single byte.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-03-26 09:29:55 +00:00
Tomi Fontanilles 9cea822cc4 modem: backends: uart_async: small improvements
- Set all the state bits before calling uart_rx_enable() because
  interrupts may happen already before it returns.
- Fix the byte count in the "Receive buffer overrun" log.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-03-26 09:29:55 +00:00
Tomi Fontanilles e7805f8d72 modem: cmux: improve frame debug logging
- Add the frame data length to the frame header log.
- Log the dropped frames as well.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-03-26 09:29:55 +00:00
Tomi Fontanilles 15cc3ff0d6 modem: backends: uart: register their own log modules
Instead of both using the same modem_backend_uart, for better visibility.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-03-26 09:29:55 +00:00
Tomi Fontanilles cf7d166fc1 modem: backends: uart_async: increase default TX timeout
The previous default value made UART TXs of size > 1145
time out at a baud rate of 115200.

It should not be so small that it can cause undesirable timeouts.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-03-26 09:29:55 +00:00
Mayank Mahajan 3d81167eaf ADD: GNSS Driver for U-BLOX M10 & Support for UBX Messages
MODEM_UBX: Adds Support for UBX Messages in Modem Subsystem.

GNSS API Supported: get_supported_systems, set_fix_rate, get_fix_rate,
	set_enabled_systems, get_enabled_systems, set_navigation_mode,
	get_navigation_mode.

Boards Tested: MIMXRT1062_FMURT6, VMU_RT1170.

Note: Partial support for U-BLOX Messages is provided as of now.

Signed-off-by: Sumit Batra <sumit.batra@nxp.com>
Signed-off-by: Mayank Mahajan <mayankmahajan.x@nxp.com>
2024-03-26 09:28:36 +00:00
Tomi Fontanilles 7f9ed8695c samples: net: cellular_modem: add nRF9160 DK's nRF52840
The added configuration files allow the sample to be run
on the nRF52840 present on the nRF9160 DK with the nRF9160
(running SLM) being the modem and communication happening
through pins that are routed internally between the two.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-03-04 22:13:14 +01:00
Tomi Fontanilles 3813faa302 modem: backends: uart_async: log the number of bytes dropped
When a receive buffer overrun happens.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-03-04 22:13:14 +01:00
Tomi Fontanilles a6b710fce3 modem: chat: fix init assert
Use the proper variable.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-02-28 18:02:17 +00:00
Tomi Fontanilles 90f1ed6a81 modem: cmux: improve handling of receive buffer overrun
This allows to properly drop single CMUX frames that are
too big to fit in the receive buffer, keeping track of
where they end so that following frames are received
correctly regardless of the data contents.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-02-28 18:02:17 +00:00
Tomi Fontanilles bc64c91d62 modem: backend: uart_async: improve logging on UART_TX_ABORTED
Only log if the abort is not self-triggered,
and also print the number of bytes sent.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-02-28 18:02:17 +00:00
Tomi Fontanilles 11f22bf702 modem: ppp: fix receiving of successive frames when one 0x7E is omitted
The SOF delimiter byte may be omitted when a frame follows another
that just ended with that byte.
The parsing used to expect that second delimiter anyway,
which resulted in PPP frames going missing.

As an additional improvement, dropped bytes as well as the length
of received frames are now (debug) logged.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-02-28 18:02:17 +00:00
Tomi Fontanilles 6aef9d4d9a modem: ppp: fix crash when attaching to a pipe
modem_pipe_attach() can send events before returning, which could
provoke a crash as ppp->pipe, still NULL at that time, could be
used either in receiving (if the pipe had some data pending) or
in sending (if the PPP module had already been attached and had
some data to send in its transmit buffer).

ppp->pipe is now set before modem_pipe_attach().
Also, the ATTACHED_BIT is now set only after having actually attached.
And finally, the send_work is now scheduled on PIPE_EVENT_OPENED
so that data is flushed when the (closed) attached pipe is opened.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-02-28 18:02:17 +00:00
Tomi Fontanilles 5fc02e3501 modem: backends: use CONFIG_MODEM_MODULES_LOG_LEVEL
This makes the modem backends use the log level
set for the modem modules instead of the default one.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-02-28 18:02:17 +00:00
Tomi Fontanilles 825b676568 modem: small improvements
Changes include:
- Improving handling of frames for unconfigured DLCIs.
- Adding logs and outputting error codes in failure cases.
- Adding warnings to not reuse buffers.
- Fixing comments.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
2024-02-26 11:34:32 +00:00
Bjarki Arge Andreasen 793c507209 modem: cmux: Prevent NULL deref in dlci_close_handler
Prevent unlikely item == NULL scenario identifed by CID
323104

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-02-15 10:22:09 +01:00
Bjarki Arge Andreasen cc3076be1d modem: cmux: Prevent NULL deref in dlci_open_handler
Prevent unlikely item == NULL scenario (CID 323102) and remove
superflous cmux == NULL check (CID 334903)

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-02-15 10:22:09 +01:00
Bjarki Arge Andreasen 9633fb70a0 modem: cmux: Prevent NULL deref in connect_handler
Prevent unlikely item == NULL scenario (CID 323091) and remove
superflous cmux == NULL check (CID 334904)

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-02-15 10:22:09 +01:00
Bjarki Arge Andreasen 96e48c91b4 modem: cmux: Prevent NULL deref in connect_response_transmit
Prevent potential NULL dereference identified by CID 338026

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-02-15 10:22:09 +01:00
Bjarki Arge Andreasen d801e697dd modem: chat: Implement TRANSMIT_IDLE event
Refactors modem chat module to use TRANSMIT_IDLE event instead
of polling when transmitting, cleaning up the transmit handler
by combining duplicate code and using an internal state instead
of multiple iterators to track send state.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-01-25 11:22:42 +01:00
Bjarki Arge Andreasen 27b0d4e3fe modem: chat: remove receive and transmit timeouts
Remove receive and transmit timeouts which are no
longer useful as the RECEIVE_READY and
TRANSMIT_IDLE events will be used to efficiently
manage timeouts between transmit/receive calls.

Then update the the in-tree drivers using the
modem_chat module to omit the process timeout
parameter.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-01-25 11:22:42 +01:00
Bjarki Arge Andreasen a9779eca40 modem: ppp: Implement TRANSMIT_IDLE event
Implement TRANSMIT_IDLE event for modem_ppp module. This addition
optimizes the sys workque CPU time when performing a throughput
test from 36% to 5%, while only reducing the throughput by 12%.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-01-22 10:35:37 +00:00
Bjarki Arge Andreasen 358f1ff934 tests: modem: cmux: Implement TRANSMIT_IDLE into test suite
Use transmit idle event to synchronize with data transmitted
through DLCI pipes to test TRANSMIT_IDLE implementation.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-01-17 09:20:50 -06:00
Bjarki Arge Andreasen c114627853 modem: cmux: Implement TRANSMIT_IDLE event
Implement transmit idle event both for transmitting data to the
bus, and for the individual DLCI channels.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-01-17 09:20:50 -06:00
Bjarki Arge Andreasen 7c9f2872e1 modem: chat: Start waiting for response after request is sent
This PR makes the modem_chat wait until a chat script chat
request has been sent before accepting responses to said
request.

This helps ensure that an unsolicitet response is not mistaken
for a response to a request, which is especially problematic
if the chat script chat is using an any match.

For example, start chat script sending AT+CGMI, expecting the
modem name as a response followed by OK

> start script, waiting for response immediately
> start sending "AT+CGMI"
> receive "+CEREG 1,0" while sending
> script accepts "+CEREG 1,0" as the response to "AT+CGMI"
> "AT+CGMI" sent
> receive "QUECTEL BG95"
> receive "OK"

script handler got "+CEREG 1,0" instead of "QUECTEL BG95"

After this PR:

> start script
> start sending AT+CGMI
> receive "+CEREG 1,0" while sending
> "AT+CGMI" sent
> start waiting for response
> receive "QUECTEL BG95"
> script accepts "QUECTEL BG95" as response to "AT+CGMI"
> receive "OK"

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-01-15 15:11:42 +01:00
Pisit Sawangvonganan 1270bce4d8 modem: modem_cmux: optimize modem_cmux_transmit_frame to save ROM
This optimization aggregates frame headers before adding to the ring buffer
and computes the FCS of the frame header in one operation.
This approach improves execution efficiency and reduces memory footprint.
This code adjustment aligns with the changes proposed in https://github.com/zephyrproject-rtos/zephyr/pull/67062.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-01-11 16:10:37 +00:00
Bjarki Arge Andreasen 4d99c69e4a modem: backend: tty: Implement transmit idle event
Implement transmit idle notification for TTY backend. Since TTY
has an "infinite" transmit buffer, we invoke transmit idle
immediately after writing the data to the TTY file.

The test suite for the TTY backend has been updated to match the
new behavior.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-01-11 16:09:44 +00:00