@@ -31,7 +31,14 @@ LOG_MODULE_REGISTER(sm_at_client, CONFIG_SM_AT_CLIENT_LOG_LEVEL);
3131
3232static const struct device * uart_dev = DEVICE_DT_GET (DT_CHOSEN (ncs_sm_uart ));
3333
34- static struct k_work_delayable rx_process_work ;
34+ static void rx_process (struct k_work * work );
35+ static void dtr_uart_enable_work_fn (struct k_work * work );
36+ static void dtr_uart_disable_work_fn (struct k_work * work );
37+
38+ K_WORK_DELAYABLE_DEFINE (rx_process_work , rx_process );
39+ K_WORK_DEFINE (dtr_uart_enable_work , dtr_uart_enable_work_fn );
40+ K_WORK_DELAYABLE_DEFINE (dtr_uart_disable_work , dtr_uart_disable_work_fn );
41+
3542struct rx_buf_t {
3643 atomic_t ref_counter ;
3744 uint8_t buf [CONFIG_SM_AT_CLIENT_UART_RX_BUF_SIZE ];
@@ -73,6 +80,7 @@ static K_SEM_DEFINE(at_rsp, 0, 1);
7380
7481static sm_data_handler_t data_handler ;
7582static enum at_cmd_state sm_at_state ;
83+ static bool initialized ;
7684
7785#if defined(CONFIG_SM_AT_CLIENT_SHELL )
7886static const struct shell * global_shell ;
@@ -97,10 +105,6 @@ struct dtr_config {
97105
98106 /* Current DTR state. */
99107 bool active ;
100-
101- /* Work items for enabling and disabling DTR UART. */
102- struct k_work dtr_uart_enable_work ;
103- struct k_work_delayable dtr_uart_disable_work ;
104108};
105109
106110static struct dtr_config dtr_config = {
@@ -446,10 +450,10 @@ static void reschedule_disable(void)
446450{
447451 if (dtr_config .active && dtr_config .automatic ) {
448452 /* Restart the inactivity timer. */
449- k_work_reschedule (& dtr_config . dtr_uart_disable_work , dtr_config .inactivity );
453+ k_work_reschedule (& dtr_uart_disable_work , dtr_config .inactivity );
450454 } else {
451455 /* Stop the inactivity timer. */
452- k_work_cancel_delayable (& dtr_config . dtr_uart_disable_work );
456+ k_work_cancel_delayable (& dtr_uart_disable_work );
453457 }
454458}
455459
@@ -713,7 +717,7 @@ static void gpio_cb_func(const struct device *dev, struct gpio_callback *gpio_cb
713717
714718 if (dtr_config .automatic && !dtr_config .active ) {
715719 /* Wake up the application */
716- k_work_submit (& dtr_config . dtr_uart_enable_work );
720+ k_work_submit (& dtr_uart_enable_work );
717721 }
718722
719723 if (ri_handler ) {
@@ -787,7 +791,6 @@ static int gpio_init(void)
787791int sm_at_client_init (sm_data_handler_t handler , bool automatic , k_timeout_t inactivity )
788792{
789793 int err ;
790- static bool initialized ;
791794
792795 if (initialized ) {
793796 return - EALREADY ;
@@ -810,11 +813,6 @@ int sm_at_client_init(sm_data_handler_t handler, bool automatic, k_timeout_t ina
810813 return - EFAULT ;
811814 }
812815
813- k_work_init_delayable (& rx_process_work , rx_process );
814-
815- k_work_init (& dtr_config .dtr_uart_enable_work , dtr_uart_enable_work_fn );
816- k_work_init_delayable (& dtr_config .dtr_uart_disable_work , dtr_uart_disable_work_fn );
817-
818816 /* Initialize shell pointer so it's available for printing in callbacks */
819817#if defined(CONFIG_SHELL_BACKEND_SERIAL )
820818 global_shell = shell_backend_uart_get_ptr ();
@@ -848,6 +846,7 @@ int sm_at_client_uninit(void)
848846 data_handler = NULL ;
849847 ri_handler = NULL ;
850848 sm_at_state = AT_CMD_OK ;
849+ initialized = false;
851850
852851 return 0 ;
853852}
@@ -868,6 +867,11 @@ int sm_at_client_send_cmd(const char *const command, uint32_t timeout)
868867{
869868 int ret ;
870869
870+ if (!initialized ) {
871+ LOG_ERR ("AT client not initialized" );
872+ return - EPERM ;
873+ }
874+
871875 sm_at_state = AT_CMD_PENDING ;
872876 ret = tx_write (command , strlen (command ), false);
873877 if (ret < 0 ) {
@@ -900,32 +904,52 @@ int sm_at_client_send_cmd(const char *const command, uint32_t timeout)
900904
901905int sm_at_client_send_data (const uint8_t * const data , size_t datalen )
902906{
907+ if (!initialized ) {
908+ LOG_ERR ("AT client not initialized" );
909+ return - EPERM ;
910+ }
911+
903912 return tx_write (data , datalen , true);
904913}
905914
906915void sm_at_client_configure_dtr_uart (bool automatic , k_timeout_t inactivity )
907916{
917+ if (!initialized ) {
918+ LOG_ERR ("AT client not initialized" );
919+ return ;
920+ }
921+
908922 dtr_config .automatic = automatic ;
909923 dtr_config .inactivity = inactivity ;
910924
911925 if (dtr_config .automatic && !dtr_config .active && !ring_buf_is_empty (& tx_buf )) {
912926 /* If automatic DTR UART is enabled and there is data to send, enable DTR UART. */
913- k_work_submit (& dtr_config . dtr_uart_enable_work );
927+ k_work_submit (& dtr_uart_enable_work );
914928 } else {
915929 reschedule_disable ();
916930 }
917931}
918932
919933void sm_at_client_disable_dtr_uart (void )
920934{
935+ if (!initialized ) {
936+ LOG_ERR ("AT client not initialized" );
937+ return ;
938+ }
939+
921940 sm_at_client_configure_dtr_uart (false, K_NO_WAIT );
922- k_work_reschedule (& dtr_config . dtr_uart_disable_work , K_NO_WAIT );
941+ k_work_reschedule (& dtr_uart_disable_work , K_NO_WAIT );
923942}
924943
925944void sm_at_client_enable_dtr_uart (void )
926945{
946+ if (!initialized ) {
947+ LOG_ERR ("AT client not initialized" );
948+ return ;
949+ }
950+
927951 sm_at_client_configure_dtr_uart (false, K_NO_WAIT );
928- k_work_submit (& dtr_config . dtr_uart_enable_work );
952+ k_work_submit (& dtr_uart_enable_work );
929953}
930954
931955#if defined(CONFIG_SM_AT_CLIENT_SHELL )
0 commit comments