custom AT command fails on second use with error

Hei,

I want to implement custom AT commands using a nrf9160 and ncs 2.8.0. The device should be able to process custom AT commands via UART console.

I followed this documentation https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/libraries/modem/at_cmd_custom.html

It builds successfully and the parser calls my custom AT cmd callback as expected when using the custom AT cmd using nrf_modem_at_cmd() (also multiple times in a row!). But as I said I want to send the command via console to the device. This is where the problem arises: I receive only once a "OK" when sending "AT+CMD1". All subsequent posts will be denied with "Error". The command "AT" works (Response "OK") after all.

I also tested it when sending "AT" as first command. Then I get no "OK", only "Error" when sending "AT+CMD1".

Why does the system allow me to send the command via console only once after rebooting it?

This is a minimal example (derived from at_client sample), which should reproduce the error:

main.c

#include <zephyr/kernel.h>
#include <stdio.h>
#include <string.h>
#include <modem/nrf_modem_lib.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/drivers/clock_control/nrf_clock_control.h>

#include <nrf_modem_at.h>
#include <modem/at_cmd_custom.h>

static char response[64];

/* AT filter function declarations. */
static int at_cmd_callback_cmd1(char *buf, size_t len, char *at_cmd)
{
	return at_cmd_custom_respond(buf, len, "\r\n+CMD1: OK\r\n");
}

AT_CMD_CUSTOM(CMD1, "AT+CMD1", at_cmd_callback_cmd1);

int main(void)
{
	int err;

	printk("The AT host sample started\n");

	err = nrf_modem_lib_init();
	if (err) {
		printk("Modem library initialization failed, error: %d\n", err);
		return 0;
	}
	
	printk("Ready\n");

	err = nrf_modem_at_cmd(response, sizeof(response), "AT+CMD1");
	if (err){
		printk("Error during sending cmd via nrf_modem_at_cmd E:%i\n", err);
	}
	printk("Response using nrf_modem_at_cmd for first time: %s\n", response);

	err = nrf_modem_at_cmd(response, sizeof(response), "AT+CMD1");
	if (err){
		printk("Error during sending cmd via nrf_modem_at_cmd E:%i\n", err);
	}
	printk("Response using nrf_modem_at_cmd for second time: %s\n", response);

	return 0;
}

proj.conf

# General config
CONFIG_ASSERT=y

# Network
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_POSIX_API=y

# Modem library
CONFIG_NRF_MODEM_LIB=y

# AT host library
CONFIG_AT_HOST_LIBRARY=y
CONFIG_UART_INTERRUPT_DRIVEN=y

# Stacks and heaps
CONFIG_MAIN_STACK_SIZE=3072
CONFIG_HEAP_MEM_POOL_SIZE=16384
CONFIG_AT_MONITOR_HEAP_SIZE=512

CONFIG_UART_NRFX_UARTE_LEGACY_SHIM=n

# addition for custom AT commands
CONFIG_LOG=y
CONFIG_AT_CMD_CUSTOM=y
CONFIG_AT_CMD_CUSTOM_LOG_LEVEL_DBG=y

Sending "AT+CMD1" twice and "AT" once gives:

*** Booting nRF Connect SDK v2.8.0-a2386bfc8401 ***
*** Using Zephyr OS v3.7.99-0bc3393fb112 ***
[00:00:00.301,269] <inf> at_cmd_custom: Custom AT commands enabled with 1 entry.
The AT host sample started
Ready
Response using nrf_modem_at_cmd for first time: 
+CMD1: OK

Response using nrf_modem_at_cmd for second time: 
+CMD1: OK

---- Sent utf8 encoded message: "AT+CMD1\r\n" ----

+CMD1: OK
---- Sent utf8 encoded message: "AT+CMD1\r\n" ----
ERROR
---- Sent utf8 encoded message: "AT\r\n" ----
OK

Related