2020-01-02 11:41:07 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2021-04-24 19:09:30 +02:00
|
|
|
#define DT_DRV_COMPAT zephyr_gsm_ppp
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
2020-02-04 13:08:04 +01:00
|
|
|
LOG_MODULE_REGISTER(modem_gsm, CONFIG_MODEM_LOG_LEVEL);
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2020-10-16 09:47:11 +02:00
|
|
|
#include <stdlib.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/sys/ring_buffer.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/net/ppp.h>
|
|
|
|
#include <zephyr/drivers/modem/gsm_ppp.h>
|
|
|
|
#include <zephyr/drivers/uart.h>
|
|
|
|
#include <zephyr/drivers/console/uart_mux.h>
|
2020-01-02 11:41:07 +01:00
|
|
|
|
|
|
|
#include "modem_context.h"
|
|
|
|
#include "modem_iface_uart.h"
|
|
|
|
#include "modem_cmd_handler.h"
|
2020-04-01 11:51:59 +02:00
|
|
|
#include "../console/gsm_mux.h"
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-04-24 19:09:30 +02:00
|
|
|
#define GSM_UART_NODE DT_INST_BUS(0)
|
2020-12-10 11:38:43 +01:00
|
|
|
#define GSM_CMD_READ_BUF 128
|
|
|
|
#define GSM_CMD_AT_TIMEOUT K_SECONDS(2)
|
|
|
|
#define GSM_CMD_SETUP_TIMEOUT K_SECONDS(6)
|
2022-02-20 09:29:54 +01:00
|
|
|
/* GSM_CMD_LOCK_TIMEOUT should be longer than GSM_CMD_AT_TIMEOUT & GSM_CMD_SETUP_TIMEOUT,
|
|
|
|
* otherwise the gsm_ppp_stop might fail to lock tx.
|
|
|
|
*/
|
|
|
|
#define GSM_CMD_LOCK_TIMEOUT K_SECONDS(10)
|
2020-12-10 11:38:43 +01:00
|
|
|
#define GSM_RECV_MAX_BUF 30
|
|
|
|
#define GSM_RECV_BUF_SIZE 128
|
|
|
|
#define GSM_ATTACH_RETRY_DELAY_MSEC 1000
|
2022-02-20 15:56:17 +01:00
|
|
|
#define GSM_REGISTER_DELAY_MSEC 1000
|
2022-02-25 17:00:15 +01:00
|
|
|
#define GSM_RETRY_DELAY K_SECONDS(1)
|
2020-12-10 11:38:43 +01:00
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
#define GSM_RSSI_RETRY_DELAY_MSEC 2000
|
|
|
|
#define GSM_RSSI_RETRIES 10
|
|
|
|
#define GSM_RSSI_INVALID -1000
|
|
|
|
|
|
|
|
#if defined(CONFIG_MODEM_GSM_ENABLE_CESQ_RSSI)
|
|
|
|
#define GSM_RSSI_MAXVAL 0
|
|
|
|
#else
|
|
|
|
#define GSM_RSSI_MAXVAL -51
|
|
|
|
#endif
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2022-02-20 15:56:17 +01:00
|
|
|
/* Modem network registration state */
|
|
|
|
enum network_state {
|
|
|
|
GSM_NET_INIT = -1,
|
|
|
|
GSM_NET_NOT_REGISTERED,
|
|
|
|
GSM_NET_HOME_NETWORK,
|
|
|
|
GSM_NET_SEARCHING,
|
|
|
|
GSM_NET_REGISTRATION_DENIED,
|
|
|
|
GSM_NET_UNKNOWN,
|
|
|
|
GSM_NET_ROAMING,
|
|
|
|
};
|
|
|
|
|
2020-04-01 11:51:59 +02:00
|
|
|
/* During the modem setup, we first create DLCI control channel and then
|
|
|
|
* PPP and AT channels. Currently the modem does not create possible GNSS
|
|
|
|
* channel.
|
|
|
|
*/
|
|
|
|
enum setup_state {
|
|
|
|
STATE_INIT = 0,
|
|
|
|
STATE_CONTROL_CHANNEL = 0,
|
|
|
|
STATE_PPP_CHANNEL,
|
|
|
|
STATE_AT_CHANNEL,
|
|
|
|
STATE_DONE
|
|
|
|
};
|
|
|
|
|
2020-01-02 11:41:07 +01:00
|
|
|
static struct gsm_modem {
|
2022-02-23 16:19:37 +01:00
|
|
|
struct k_mutex lock;
|
2021-09-24 21:46:07 +02:00
|
|
|
const struct device *dev;
|
2020-01-02 11:41:07 +01:00
|
|
|
struct modem_context context;
|
|
|
|
|
|
|
|
struct modem_cmd_handler_data cmd_handler_data;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t cmd_match_buf[GSM_CMD_READ_BUF];
|
2020-01-02 11:41:07 +01:00
|
|
|
struct k_sem sem_response;
|
2022-01-21 16:17:07 +01:00
|
|
|
struct k_sem sem_if_down;
|
2020-01-02 11:41:07 +01:00
|
|
|
|
|
|
|
struct modem_iface_uart_data gsm_data;
|
2021-03-31 17:31:30 +02:00
|
|
|
struct k_work_delayable gsm_configure_work;
|
2020-01-02 11:41:07 +01:00
|
|
|
char gsm_rx_rb_buf[PPP_MRU * 3];
|
2020-01-24 08:02:33 +01:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t *ppp_recv_buf;
|
2020-01-24 08:02:33 +01:00
|
|
|
size_t ppp_recv_buf_len;
|
2020-04-01 11:51:59 +02:00
|
|
|
|
|
|
|
enum setup_state state;
|
2020-04-30 20:33:38 +02:00
|
|
|
const struct device *ppp_dev;
|
|
|
|
const struct device *at_dev;
|
|
|
|
const struct device *control_dev;
|
2020-04-01 11:51:59 +02:00
|
|
|
|
2020-10-05 13:58:55 +02:00
|
|
|
struct net_if *iface;
|
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
struct k_thread rx_thread;
|
|
|
|
struct k_work_q workq;
|
|
|
|
struct k_work_delayable rssi_work_handle;
|
|
|
|
struct gsm_ppp_modem_info minfo;
|
|
|
|
|
2022-02-20 15:56:17 +01:00
|
|
|
enum network_state net_state;
|
|
|
|
|
|
|
|
int register_retries;
|
2021-06-17 11:57:45 +02:00
|
|
|
int rssi_retries;
|
2020-12-10 11:38:43 +01:00
|
|
|
int attach_retries;
|
2021-06-17 11:57:45 +02:00
|
|
|
bool attached : 1;
|
2021-10-20 17:43:08 +02:00
|
|
|
bool modem_info_queried : 1;
|
2021-09-24 21:46:07 +02:00
|
|
|
|
|
|
|
void *user_data;
|
|
|
|
|
|
|
|
gsm_modem_power_cb modem_on_cb;
|
|
|
|
gsm_modem_power_cb modem_off_cb;
|
2022-01-21 16:17:07 +01:00
|
|
|
struct net_mgmt_event_callback gsm_mgmt_cb;
|
2020-01-02 11:41:07 +01:00
|
|
|
} gsm;
|
|
|
|
|
2022-02-25 17:00:15 +01:00
|
|
|
NET_BUF_POOL_DEFINE(gsm_recv_pool, GSM_RECV_MAX_BUF, GSM_RECV_BUF_SIZE, 0, NULL);
|
2022-03-14 15:29:29 +01:00
|
|
|
K_KERNEL_STACK_DEFINE(gsm_rx_stack, CONFIG_MODEM_GSM_RX_STACK_SIZE);
|
|
|
|
K_KERNEL_STACK_DEFINE(gsm_workq_stack, CONFIG_MODEM_GSM_WORKQ_STACK_SIZE);
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
static inline void gsm_ppp_lock(struct gsm_modem *gsm)
|
|
|
|
{
|
|
|
|
(void)k_mutex_lock(&gsm->lock, K_FOREVER);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void gsm_ppp_unlock(struct gsm_modem *gsm)
|
|
|
|
{
|
|
|
|
(void)k_mutex_unlock(&gsm->lock);
|
|
|
|
}
|
|
|
|
|
2021-12-28 15:25:27 +01:00
|
|
|
static inline int gsm_work_reschedule(struct k_work_delayable *dwork, k_timeout_t delay)
|
|
|
|
{
|
2022-01-03 16:21:17 +01:00
|
|
|
return k_work_reschedule_for_queue(&gsm.workq, dwork, delay);
|
2021-12-28 15:25:27 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
#if defined(CONFIG_MODEM_GSM_ENABLE_CESQ_RSSI)
|
|
|
|
/* helper macro to keep readability */
|
|
|
|
#define ATOI(s_, value_, desc_) modem_atoi(s_, value_, desc_, __func__)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convert string to long integer, but handle errors
|
|
|
|
*
|
|
|
|
* @param s: string with representation of integer number
|
|
|
|
* @param err_value: on error return this value instead
|
|
|
|
* @param desc: name the string being converted
|
|
|
|
* @param func: function where this is called (typically __func__)
|
|
|
|
*
|
|
|
|
* @retval return integer conversion on success, or err_value on error
|
|
|
|
*/
|
|
|
|
static int modem_atoi(const char *s, const int err_value,
|
|
|
|
const char *desc, const char *func)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
ret = (int)strtol(s, &endptr, 10);
|
|
|
|
if (!endptr || *endptr != '\0') {
|
|
|
|
LOG_ERR("bad %s '%s' in %s", log_strdup(s),
|
|
|
|
log_strdup(desc), log_strdup(func));
|
|
|
|
return err_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2020-02-06 14:14:01 +01:00
|
|
|
static void gsm_rx(struct gsm_modem *gsm)
|
2020-01-02 11:41:07 +01:00
|
|
|
{
|
|
|
|
LOG_DBG("starting");
|
|
|
|
|
|
|
|
while (true) {
|
2021-06-16 14:23:30 +02:00
|
|
|
(void)k_sem_take(&gsm->gsm_data.rx_sem, K_FOREVER);
|
2020-01-24 08:02:33 +01:00
|
|
|
|
2020-03-30 16:07:11 +02:00
|
|
|
/* The handler will listen AT channel */
|
|
|
|
gsm->context.cmd_handler.process(&gsm->context.cmd_handler,
|
|
|
|
&gsm->context.iface);
|
2020-01-02 11:41:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MODEM_CMD_DEFINE(gsm_cmd_ok)
|
|
|
|
{
|
|
|
|
modem_cmd_handler_set_error(data, 0);
|
|
|
|
LOG_DBG("ok");
|
|
|
|
k_sem_give(&gsm.sem_response);
|
2020-02-10 17:24:42 +01:00
|
|
|
return 0;
|
2020-01-02 11:41:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MODEM_CMD_DEFINE(gsm_cmd_error)
|
|
|
|
{
|
|
|
|
modem_cmd_handler_set_error(data, -EINVAL);
|
|
|
|
LOG_DBG("error");
|
|
|
|
k_sem_give(&gsm.sem_response);
|
2020-02-10 17:24:42 +01:00
|
|
|
return 0;
|
2020-01-02 11:41:07 +01:00
|
|
|
}
|
|
|
|
|
2022-02-20 10:09:39 +01:00
|
|
|
/* Handler: +CME Error: <err>[0] */
|
|
|
|
MODEM_CMD_DEFINE(gsm_cmd_exterror)
|
|
|
|
{
|
|
|
|
/* TODO: map extended error codes to values */
|
|
|
|
modem_cmd_handler_set_error(data, -EIO);
|
|
|
|
k_sem_give(&gsm.sem_response);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-19 22:39:18 +01:00
|
|
|
static const struct modem_cmd response_cmds[] = {
|
2020-01-02 11:41:07 +01:00
|
|
|
MODEM_CMD("OK", gsm_cmd_ok, 0U, ""),
|
|
|
|
MODEM_CMD("ERROR", gsm_cmd_error, 0U, ""),
|
2022-02-20 10:09:39 +01:00
|
|
|
MODEM_CMD("+CME ERROR: ", gsm_cmd_exterror, 1U, ""),
|
2020-01-02 11:41:07 +01:00
|
|
|
MODEM_CMD("CONNECT", gsm_cmd_ok, 0U, ""),
|
|
|
|
};
|
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
static int unquoted_atoi(const char *s, int base)
|
|
|
|
{
|
|
|
|
if (*s == '"') {
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strtol(s, NULL, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handler: +COPS: <mode>[0],<format>[1],<oper>[2]
|
|
|
|
*/
|
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_cops)
|
|
|
|
{
|
2021-11-01 12:20:33 +01:00
|
|
|
if (argc >= 1) {
|
2021-06-17 11:57:45 +02:00
|
|
|
#if defined(CONFIG_MODEM_CELL_INFO)
|
2021-11-01 12:20:33 +01:00
|
|
|
if (argc >= 3) {
|
|
|
|
gsm.context.data_operator = unquoted_atoi(argv[2], 10);
|
|
|
|
LOG_INF("operator: %u",
|
|
|
|
gsm.context.data_operator);
|
|
|
|
}
|
2021-06-17 11:57:45 +02:00
|
|
|
#endif
|
|
|
|
if (unquoted_atoi(argv[0], 10) == 0) {
|
|
|
|
gsm.context.is_automatic_oper = true;
|
|
|
|
} else {
|
|
|
|
gsm.context.is_automatic_oper = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-05 14:05:59 +01:00
|
|
|
/*
|
|
|
|
* Provide modem info if modem shell is enabled. This can be shown with
|
|
|
|
* "modem list" shell command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Handler: <manufacturer> */
|
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_manufacturer)
|
|
|
|
{
|
|
|
|
size_t out_len;
|
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
out_len = net_buf_linearize(gsm.minfo.mdm_manufacturer,
|
|
|
|
sizeof(gsm.minfo.mdm_manufacturer) - 1,
|
2020-02-05 14:05:59 +01:00
|
|
|
data->rx_buf, 0, len);
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_manufacturer[out_len] = '\0';
|
|
|
|
LOG_INF("Manufacturer: %s", log_strdup(gsm.minfo.mdm_manufacturer));
|
2020-02-10 17:24:42 +01:00
|
|
|
|
|
|
|
return 0;
|
2020-02-05 14:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handler: <model> */
|
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_model)
|
|
|
|
{
|
|
|
|
size_t out_len;
|
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
out_len = net_buf_linearize(gsm.minfo.mdm_model,
|
|
|
|
sizeof(gsm.minfo.mdm_model) - 1,
|
2020-02-05 14:05:59 +01:00
|
|
|
data->rx_buf, 0, len);
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_model[out_len] = '\0';
|
|
|
|
LOG_INF("Model: %s", log_strdup(gsm.minfo.mdm_model));
|
2020-02-10 17:24:42 +01:00
|
|
|
|
|
|
|
return 0;
|
2020-02-05 14:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handler: <rev> */
|
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_revision)
|
|
|
|
{
|
|
|
|
size_t out_len;
|
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
out_len = net_buf_linearize(gsm.minfo.mdm_revision,
|
|
|
|
sizeof(gsm.minfo.mdm_revision) - 1,
|
2020-02-05 14:05:59 +01:00
|
|
|
data->rx_buf, 0, len);
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_revision[out_len] = '\0';
|
|
|
|
LOG_INF("Revision: %s", log_strdup(gsm.minfo.mdm_revision));
|
2020-02-10 17:24:42 +01:00
|
|
|
|
|
|
|
return 0;
|
2020-02-05 14:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handler: <IMEI> */
|
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_imei)
|
|
|
|
{
|
|
|
|
size_t out_len;
|
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
out_len = net_buf_linearize(gsm.minfo.mdm_imei, sizeof(gsm.minfo.mdm_imei) - 1,
|
2020-02-05 14:05:59 +01:00
|
|
|
data->rx_buf, 0, len);
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_imei[out_len] = '\0';
|
|
|
|
LOG_INF("IMEI: %s", log_strdup(gsm.minfo.mdm_imei));
|
2020-02-10 17:24:42 +01:00
|
|
|
|
|
|
|
return 0;
|
2020-02-05 14:05:59 +01:00
|
|
|
}
|
2020-04-20 13:51:45 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_MODEM_SIM_NUMBERS)
|
|
|
|
/* Handler: <IMSI> */
|
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_imsi)
|
|
|
|
{
|
|
|
|
size_t out_len;
|
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
out_len = net_buf_linearize(gsm.minfo.mdm_imsi, sizeof(gsm.minfo.mdm_imsi) - 1,
|
2020-04-20 13:51:45 +02:00
|
|
|
data->rx_buf, 0, len);
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_imsi[out_len] = '\0';
|
|
|
|
LOG_INF("IMSI: %s", log_strdup(gsm.minfo.mdm_imsi));
|
2020-04-20 13:51:45 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handler: <ICCID> */
|
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_iccid)
|
|
|
|
{
|
|
|
|
size_t out_len;
|
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
out_len = net_buf_linearize(gsm.minfo.mdm_iccid, sizeof(gsm.minfo.mdm_iccid) - 1,
|
2020-04-20 13:51:45 +02:00
|
|
|
data->rx_buf, 0, len);
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_iccid[out_len] = '\0';
|
|
|
|
if (gsm.minfo.mdm_iccid[0] == '+') {
|
2020-04-20 13:51:45 +02:00
|
|
|
/* Seen on U-blox SARA: "+CCID: nnnnnnnnnnnnnnnnnnnn".
|
|
|
|
* Skip over the +CCID bit, which other modems omit.
|
|
|
|
*/
|
2022-01-03 16:21:17 +01:00
|
|
|
char *p = strchr(gsm.minfo.mdm_iccid, ' ');
|
2020-04-20 13:51:45 +02:00
|
|
|
|
|
|
|
if (p) {
|
|
|
|
size_t len = strlen(p+1);
|
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
memmove(gsm.minfo.mdm_iccid, p+1, len+1);
|
2020-04-20 13:51:45 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 16:21:17 +01:00
|
|
|
LOG_INF("ICCID: %s", log_strdup(gsm.minfo.mdm_iccid));
|
2020-04-20 13:51:45 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MODEM_SIM_NUMBERS */
|
2021-06-09 13:36:41 +02:00
|
|
|
|
2022-02-20 15:56:17 +01:00
|
|
|
MODEM_CMD_DEFINE(on_cmd_net_reg_sts)
|
|
|
|
{
|
|
|
|
gsm.net_state = (enum network_state)atoi(argv[1]);
|
|
|
|
|
|
|
|
switch (gsm.net_state) {
|
|
|
|
case GSM_NET_NOT_REGISTERED:
|
|
|
|
LOG_INF("Network %s.", "not registered");
|
|
|
|
break;
|
|
|
|
case GSM_NET_HOME_NETWORK:
|
|
|
|
LOG_INF("Network %s.", "registered, home network");
|
|
|
|
break;
|
|
|
|
case GSM_NET_SEARCHING:
|
|
|
|
LOG_INF("Searching for network...");
|
|
|
|
break;
|
|
|
|
case GSM_NET_REGISTRATION_DENIED:
|
|
|
|
LOG_INF("Network %s.", "registration denied");
|
|
|
|
break;
|
|
|
|
case GSM_NET_UNKNOWN:
|
|
|
|
LOG_INF("Network %s.", "unknown");
|
|
|
|
break;
|
|
|
|
case GSM_NET_ROAMING:
|
|
|
|
LOG_INF("Network %s.", "registered, roaming");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-09 13:36:41 +02:00
|
|
|
#if defined(CONFIG_MODEM_CELL_INFO)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handler: +CEREG: <n>[0],<stat>[1],<tac>[2],<ci>[3],<AcT>[4]
|
|
|
|
*/
|
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_cereg)
|
|
|
|
{
|
|
|
|
if (argc >= 4) {
|
|
|
|
gsm.context.data_lac = unquoted_atoi(argv[2], 16);
|
|
|
|
gsm.context.data_cellid = unquoted_atoi(argv[3], 16);
|
|
|
|
LOG_INF("lac: %u, cellid: %u",
|
|
|
|
gsm.context.data_lac,
|
|
|
|
gsm.context.data_cellid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct setup_cmd query_cellinfo_cmds[] = {
|
|
|
|
SETUP_CMD_NOHANDLE("AT+CEREG=2"),
|
|
|
|
SETUP_CMD("AT+CEREG?", "", on_cmd_atcmdinfo_cereg, 5U, ","),
|
|
|
|
SETUP_CMD_NOHANDLE("AT+COPS=3,2"),
|
|
|
|
SETUP_CMD("AT+COPS?", "", on_cmd_atcmdinfo_cops, 3U, ","),
|
|
|
|
};
|
|
|
|
|
|
|
|
static int gsm_query_cellinfo(struct gsm_modem *gsm)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = modem_cmd_handler_setup_cmds_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
query_cellinfo_cmds,
|
|
|
|
ARRAY_SIZE(query_cellinfo_cmds),
|
|
|
|
&gsm->sem_response,
|
|
|
|
GSM_CMD_SETUP_TIMEOUT);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_WRN("modem query for cell info returned %d", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MODEM_CELL_INFO */
|
2020-02-05 14:05:59 +01:00
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
#if defined(CONFIG_MODEM_GSM_ENABLE_CESQ_RSSI)
|
|
|
|
/*
|
|
|
|
* Handler: +CESQ: <rxlev>[0],<ber>[1],<rscp>[2],<ecn0>[3],<rsrq>[4],<rsrp>[5]
|
|
|
|
*/
|
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_rssi_cesq)
|
|
|
|
{
|
|
|
|
int rsrp, rscp, rxlev;
|
|
|
|
|
|
|
|
rsrp = ATOI(argv[5], 0, "rsrp");
|
|
|
|
rscp = ATOI(argv[2], 0, "rscp");
|
|
|
|
rxlev = ATOI(argv[0], 0, "rxlev");
|
|
|
|
|
|
|
|
if (rsrp >= 0 && rsrp <= 97) {
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_rssi = -140 + (rsrp - 1);
|
|
|
|
LOG_INF("RSRP: %d", gsm.minfo.mdm_rssi);
|
2021-06-17 11:57:45 +02:00
|
|
|
} else if (rscp >= 0 && rscp <= 96) {
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_rssi = -120 + (rscp - 1);
|
|
|
|
LOG_INF("RSCP: %d", gsm.minfo.mdm_rssi);
|
2021-06-17 11:57:45 +02:00
|
|
|
} else if (rxlev >= 0 && rxlev <= 63) {
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_rssi = -110 + (rxlev - 1);
|
|
|
|
LOG_INF("RSSI: %d", gsm.minfo.mdm_rssi);
|
2021-06-17 11:57:45 +02:00
|
|
|
} else {
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_rssi = GSM_RSSI_INVALID;
|
2021-06-17 11:57:45 +02:00
|
|
|
LOG_INF("RSRP/RSCP/RSSI not known");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Handler: +CSQ: <signal_power>[0],<qual>[1] */
|
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_rssi_csq)
|
|
|
|
{
|
|
|
|
/* Expected response is "+CSQ: <signal_power>,<qual>" */
|
|
|
|
if (argc) {
|
|
|
|
int rssi = atoi(argv[0]);
|
|
|
|
|
|
|
|
if (rssi >= 0 && rssi <= 31) {
|
|
|
|
rssi = -113 + (rssi * 2);
|
|
|
|
} else {
|
|
|
|
rssi = GSM_RSSI_INVALID;
|
|
|
|
}
|
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm.minfo.mdm_rssi = rssi;
|
2021-06-17 11:57:45 +02:00
|
|
|
LOG_INF("RSSI: %d", rssi);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_MODEM_GSM_ENABLE_CESQ_RSSI)
|
|
|
|
static const struct modem_cmd read_rssi_cmd =
|
|
|
|
MODEM_CMD("+CESQ:", on_cmd_atcmdinfo_rssi_cesq, 6U, ",");
|
|
|
|
#else
|
|
|
|
static const struct modem_cmd read_rssi_cmd =
|
|
|
|
MODEM_CMD("+CSQ:", on_cmd_atcmdinfo_rssi_csq, 2U, ",");
|
|
|
|
#endif
|
|
|
|
|
2021-10-20 17:43:08 +02:00
|
|
|
static const struct setup_cmd setup_modem_info_cmds[] = {
|
2020-02-05 14:05:59 +01:00
|
|
|
/* query modem info */
|
|
|
|
SETUP_CMD("AT+CGMI", "", on_cmd_atcmdinfo_manufacturer, 0U, ""),
|
|
|
|
SETUP_CMD("AT+CGMM", "", on_cmd_atcmdinfo_model, 0U, ""),
|
|
|
|
SETUP_CMD("AT+CGMR", "", on_cmd_atcmdinfo_revision, 0U, ""),
|
2021-10-20 17:43:08 +02:00
|
|
|
SETUP_CMD("AT+CGSN", "", on_cmd_atcmdinfo_imei, 0U, ""),
|
|
|
|
#if defined(CONFIG_MODEM_SIM_NUMBERS)
|
2020-04-20 13:51:45 +02:00
|
|
|
SETUP_CMD("AT+CIMI", "", on_cmd_atcmdinfo_imsi, 0U, ""),
|
|
|
|
SETUP_CMD("AT+CCID", "", on_cmd_atcmdinfo_iccid, 0U, ""),
|
2020-02-05 14:05:59 +01:00
|
|
|
#endif
|
2021-10-20 17:43:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct setup_cmd setup_cmds[] = {
|
|
|
|
/* no echo */
|
|
|
|
SETUP_CMD_NOHANDLE("ATE0"),
|
|
|
|
/* hang up */
|
|
|
|
SETUP_CMD_NOHANDLE("ATH"),
|
2022-02-20 10:09:39 +01:00
|
|
|
/* extended errors in numeric form */
|
2021-10-20 17:43:08 +02:00
|
|
|
SETUP_CMD_NOHANDLE("AT+CMEE=1"),
|
2020-01-02 11:41:07 +01:00
|
|
|
/* disable unsolicited network registration codes */
|
|
|
|
SETUP_CMD_NOHANDLE("AT+CREG=0"),
|
|
|
|
/* create PDP context */
|
|
|
|
SETUP_CMD_NOHANDLE("AT+CGDCONT=1,\"IP\",\"" CONFIG_MODEM_GSM_APN "\""),
|
2020-04-01 11:51:59 +02:00
|
|
|
};
|
|
|
|
|
2020-10-16 09:47:11 +02:00
|
|
|
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_attached)
|
|
|
|
{
|
|
|
|
/* Expected response is "+CGATT: 0|1" so simply look for '1' */
|
|
|
|
if (argc && atoi(argv[0]) == 1) {
|
|
|
|
LOG_INF("Attached to packet service!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
|
|
|
|
static const struct modem_cmd read_cops_cmd =
|
2021-11-01 12:20:33 +01:00
|
|
|
MODEM_CMD_ARGS_MAX("+COPS:", on_cmd_atcmdinfo_cops, 1U, 4U, ",");
|
2021-06-17 11:57:45 +02:00
|
|
|
|
2022-02-20 15:56:17 +01:00
|
|
|
static const struct modem_cmd check_net_reg_cmd =
|
|
|
|
MODEM_CMD("+CREG: ", on_cmd_net_reg_sts, 2U, ",");
|
|
|
|
|
2020-11-19 22:39:18 +01:00
|
|
|
static const struct modem_cmd check_attached_cmd =
|
2020-10-16 09:47:11 +02:00
|
|
|
MODEM_CMD("+CGATT:", on_cmd_atcmdinfo_attached, 1U, ",");
|
|
|
|
|
2020-11-19 22:39:18 +01:00
|
|
|
static const struct setup_cmd connect_cmds[] = {
|
2020-01-02 11:41:07 +01:00
|
|
|
/* connect to network */
|
2020-04-01 11:51:59 +02:00
|
|
|
SETUP_CMD_NOHANDLE("ATD*99#"),
|
2020-01-02 11:41:07 +01:00
|
|
|
};
|
|
|
|
|
2021-10-20 17:43:08 +02:00
|
|
|
static int gsm_query_modem_info(struct gsm_modem *gsm)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (gsm->modem_info_queried) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = modem_cmd_handler_setup_cmds_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
setup_modem_info_cmds,
|
|
|
|
ARRAY_SIZE(setup_modem_info_cmds),
|
|
|
|
&gsm->sem_response,
|
|
|
|
GSM_CMD_SETUP_TIMEOUT);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
gsm->modem_info_queried = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-07 09:42:30 +01:00
|
|
|
static int gsm_setup_mccmno(struct gsm_modem *gsm)
|
|
|
|
{
|
2021-06-17 11:57:45 +02:00
|
|
|
int ret = 0;
|
2020-02-07 09:42:30 +01:00
|
|
|
|
|
|
|
if (CONFIG_MODEM_GSM_MANUAL_MCCMNO[0]) {
|
|
|
|
/* use manual MCC/MNO entry */
|
2020-10-09 13:08:28 +02:00
|
|
|
ret = modem_cmd_send_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
NULL, 0,
|
|
|
|
"AT+COPS=1,2,\""
|
|
|
|
CONFIG_MODEM_GSM_MANUAL_MCCMNO
|
|
|
|
"\"",
|
|
|
|
&gsm->sem_response,
|
|
|
|
GSM_CMD_AT_TIMEOUT);
|
2020-02-07 09:42:30 +01:00
|
|
|
} else {
|
2021-06-17 11:57:45 +02:00
|
|
|
|
|
|
|
/* First AT+COPS? is sent to check if automatic selection for operator
|
|
|
|
* is already enabled, if yes we do not send the command AT+COPS= 0,0.
|
|
|
|
*/
|
|
|
|
|
2020-10-09 13:08:28 +02:00
|
|
|
ret = modem_cmd_send_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
2021-06-17 11:57:45 +02:00
|
|
|
&read_cops_cmd,
|
|
|
|
1, "AT+COPS?",
|
2020-10-09 13:08:28 +02:00
|
|
|
&gsm->sem_response,
|
2021-06-17 11:57:45 +02:00
|
|
|
GSM_CMD_SETUP_TIMEOUT);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gsm->context.is_automatic_oper) {
|
|
|
|
/* register operator automatically */
|
|
|
|
ret = modem_cmd_send_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
NULL, 0, "AT+COPS=0,0",
|
|
|
|
&gsm->sem_response,
|
|
|
|
GSM_CMD_AT_TIMEOUT);
|
|
|
|
}
|
2020-02-07 09:42:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("AT+COPS ret:%d", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:58:55 +02:00
|
|
|
static struct net_if *ppp_net_if(void)
|
|
|
|
{
|
|
|
|
return net_if_get_first_by_type(&NET_L2_GET_NAME(PPP));
|
|
|
|
}
|
|
|
|
|
2020-04-01 11:51:59 +02:00
|
|
|
static void set_ppp_carrier_on(struct gsm_modem *gsm)
|
|
|
|
{
|
2020-10-05 13:58:55 +02:00
|
|
|
static const struct ppp_api *api;
|
2021-09-08 13:20:16 +02:00
|
|
|
const struct device *ppp_dev = device_get_binding(CONFIG_NET_PPP_DRV_NAME);
|
2020-10-05 13:58:55 +02:00
|
|
|
struct net_if *iface = gsm->iface;
|
|
|
|
int ret;
|
2020-04-01 11:51:59 +02:00
|
|
|
|
|
|
|
if (!ppp_dev) {
|
2020-10-05 13:58:55 +02:00
|
|
|
LOG_ERR("Cannot find PPP %s!", CONFIG_NET_PPP_DRV_NAME);
|
2020-04-01 11:51:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:58:55 +02:00
|
|
|
if (!api) {
|
|
|
|
api = (const struct ppp_api *)ppp_dev->api;
|
|
|
|
|
|
|
|
/* For the first call, we want to call ppp_start()... */
|
|
|
|
ret = api->start(ppp_dev);
|
|
|
|
if (ret) {
|
|
|
|
LOG_ERR("ppp start returned %d", ret);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* ...but subsequent calls should be to ppp_enable() */
|
|
|
|
ret = net_if_l2(iface)->enable(iface, true);
|
|
|
|
if (ret) {
|
|
|
|
LOG_ERR("ppp l2 enable returned %d", ret);
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
2022-02-20 10:51:29 +01:00
|
|
|
static void query_rssi(struct gsm_modem *gsm, bool lock)
|
2020-04-01 11:51:59 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2022-02-20 10:51:29 +01:00
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
#if defined(CONFIG_MODEM_GSM_ENABLE_CESQ_RSSI)
|
2022-02-20 10:51:29 +01:00
|
|
|
ret = modem_cmd_send_ext(&gsm->context.iface, &gsm->context.cmd_handler, &read_rssi_cmd, 1,
|
|
|
|
"AT+CESQ", &gsm->sem_response, GSM_CMD_SETUP_TIMEOUT,
|
|
|
|
lock ? 0 : MODEM_NO_TX_LOCK);
|
2021-06-17 11:57:45 +02:00
|
|
|
#else
|
2022-02-20 10:51:29 +01:00
|
|
|
ret = modem_cmd_send_ext(&gsm->context.iface, &gsm->context.cmd_handler, &read_rssi_cmd, 1,
|
|
|
|
"AT+CSQ", &gsm->sem_response, GSM_CMD_SETUP_TIMEOUT,
|
|
|
|
lock ? 0 : MODEM_NO_TX_LOCK);
|
2021-06-17 11:57:45 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_DBG("No answer to RSSI readout, %s", "ignoring...");
|
|
|
|
}
|
2022-02-20 10:51:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void query_rssi_lock(struct gsm_modem *gsm)
|
|
|
|
{
|
|
|
|
query_rssi(gsm, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void query_rssi_nolock(struct gsm_modem *gsm)
|
|
|
|
{
|
|
|
|
query_rssi(gsm, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rssi_handler(struct k_work *work)
|
|
|
|
{
|
|
|
|
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
|
|
|
|
struct gsm_modem *gsm = CONTAINER_OF(dwork, struct gsm_modem, rssi_work_handle);
|
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_lock(gsm);
|
2022-02-20 10:51:29 +01:00
|
|
|
query_rssi_lock(gsm);
|
2021-06-17 11:57:45 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_MODEM_CELL_INFO)
|
2022-02-20 10:51:29 +01:00
|
|
|
(void)gsm_query_cellinfo(gsm);
|
2021-06-17 11:57:45 +02:00
|
|
|
#endif
|
2022-02-20 10:51:29 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->rssi_work_handle,
|
|
|
|
K_SECONDS(CONFIG_MODEM_GSM_RSSI_POLLING_PERIOD));
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_unlock(gsm);
|
2021-06-17 11:57:45 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 16:34:05 +01:00
|
|
|
static void gsm_finalize_connection(struct k_work *work)
|
2021-06-17 11:57:45 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2022-01-20 16:34:05 +01:00
|
|
|
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
|
|
|
|
struct gsm_modem *gsm = CONTAINER_OF(dwork, struct gsm_modem, gsm_configure_work);
|
2021-06-17 11:57:45 +02:00
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_lock(gsm);
|
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
/* If already attached, jump right to RSSI readout */
|
|
|
|
if (gsm->attached) {
|
|
|
|
goto attached;
|
|
|
|
}
|
2020-04-01 11:51:59 +02:00
|
|
|
|
2020-12-10 11:38:43 +01:00
|
|
|
/* If attach check failed, we should not redo every setup step */
|
|
|
|
if (gsm->attach_retries) {
|
|
|
|
goto attaching;
|
|
|
|
}
|
|
|
|
|
2022-02-20 15:56:17 +01:00
|
|
|
/* If modem is searching for network, we should skip the setup step */
|
|
|
|
if (gsm->register_retries) {
|
|
|
|
goto registering;
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:22:38 +01:00
|
|
|
if (IS_ENABLED(CONFIG_GSM_MUX)) {
|
2020-10-09 13:08:28 +02:00
|
|
|
ret = modem_cmd_send_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
&response_cmds[0],
|
|
|
|
ARRAY_SIZE(response_cmds),
|
|
|
|
"AT", &gsm->sem_response,
|
|
|
|
GSM_CMD_AT_TIMEOUT);
|
2020-04-01 11:51:59 +02:00
|
|
|
if (ret < 0) {
|
2021-09-16 13:20:37 +02:00
|
|
|
LOG_ERR("%s returned %d, %s", "AT", ret, "retrying...");
|
2022-02-25 17:00:15 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work, GSM_RETRY_DELAY);
|
2022-02-23 16:19:37 +01:00
|
|
|
goto unlock;
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 14:18:13 +01:00
|
|
|
if (IS_ENABLED(CONFIG_MODEM_GSM_FACTORY_RESET_AT_BOOT)) {
|
|
|
|
(void)modem_cmd_send_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
&response_cmds[0],
|
|
|
|
ARRAY_SIZE(response_cmds),
|
|
|
|
"AT&F", &gsm->sem_response,
|
|
|
|
GSM_CMD_AT_TIMEOUT);
|
|
|
|
k_sleep(K_SECONDS(1));
|
|
|
|
}
|
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
ret = gsm_setup_mccmno(gsm);
|
|
|
|
if (ret < 0) {
|
2021-09-16 13:20:37 +02:00
|
|
|
LOG_ERR("%s returned %d, %s", "gsm_setup_mccmno", ret, "retrying...");
|
2021-06-17 11:57:45 +02:00
|
|
|
|
2022-02-25 17:00:15 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work, GSM_RETRY_DELAY);
|
2022-02-23 16:19:37 +01:00
|
|
|
goto unlock;
|
2021-06-17 11:57:45 +02:00
|
|
|
}
|
2020-04-01 11:51:59 +02:00
|
|
|
|
2020-10-09 13:08:28 +02:00
|
|
|
ret = modem_cmd_handler_setup_cmds_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
setup_cmds,
|
|
|
|
ARRAY_SIZE(setup_cmds),
|
|
|
|
&gsm->sem_response,
|
|
|
|
GSM_CMD_SETUP_TIMEOUT);
|
2020-04-01 11:51:59 +02:00
|
|
|
if (ret < 0) {
|
2021-09-16 13:20:37 +02:00
|
|
|
LOG_DBG("%s returned %d, %s", "setup_cmds", ret, "retrying...");
|
2022-02-25 17:00:15 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work, GSM_RETRY_DELAY);
|
2022-02-23 16:19:37 +01:00
|
|
|
goto unlock;
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 17:43:08 +02:00
|
|
|
ret = gsm_query_modem_info(gsm);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_DBG("Unable to query modem information %d", ret);
|
2022-02-25 17:00:15 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work, GSM_RETRY_DELAY);
|
2022-02-23 16:19:37 +01:00
|
|
|
goto unlock;
|
2021-10-20 17:43:08 +02:00
|
|
|
}
|
|
|
|
|
2022-02-20 15:56:17 +01:00
|
|
|
registering:
|
|
|
|
/* Wait for cell tower registration */
|
|
|
|
ret = modem_cmd_send_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
&check_net_reg_cmd, 1,
|
|
|
|
"AT+CREG?",
|
|
|
|
&gsm->sem_response,
|
|
|
|
GSM_CMD_SETUP_TIMEOUT);
|
|
|
|
if ((ret < 0) || ((gsm->net_state != GSM_NET_ROAMING) &&
|
|
|
|
(gsm->net_state != GSM_NET_HOME_NETWORK))) {
|
|
|
|
if (!gsm->register_retries) {
|
|
|
|
gsm->register_retries = CONFIG_MODEM_GSM_REGISTER_TIMEOUT *
|
|
|
|
MSEC_PER_SEC / GSM_REGISTER_DELAY_MSEC;
|
|
|
|
} else {
|
|
|
|
gsm->register_retries--;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work,
|
|
|
|
K_MSEC(GSM_REGISTER_DELAY_MSEC));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gsm->register_retries = 0;
|
|
|
|
|
2020-12-10 11:38:43 +01:00
|
|
|
attaching:
|
2020-10-16 09:47:11 +02:00
|
|
|
/* Don't initialize PPP until we're attached to packet service */
|
|
|
|
ret = modem_cmd_send_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
&check_attached_cmd, 1,
|
|
|
|
"AT+CGATT?",
|
|
|
|
&gsm->sem_response,
|
|
|
|
GSM_CMD_SETUP_TIMEOUT);
|
|
|
|
if (ret < 0) {
|
2020-12-10 11:38:43 +01:00
|
|
|
/*
|
|
|
|
* attach_retries not set -> trigger N attach retries
|
|
|
|
* attach_retries set -> decrement and retry
|
|
|
|
* attach_retries set, becomes 0 -> trigger full retry
|
|
|
|
*/
|
|
|
|
if (!gsm->attach_retries) {
|
|
|
|
gsm->attach_retries = CONFIG_MODEM_GSM_ATTACH_TIMEOUT *
|
|
|
|
MSEC_PER_SEC / GSM_ATTACH_RETRY_DELAY_MSEC;
|
|
|
|
} else {
|
|
|
|
gsm->attach_retries--;
|
|
|
|
}
|
|
|
|
|
2020-10-16 09:47:11 +02:00
|
|
|
LOG_DBG("Not attached, %s", "retrying...");
|
2021-03-31 17:31:30 +02:00
|
|
|
|
2021-12-28 15:25:27 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work,
|
2021-03-31 17:31:30 +02:00
|
|
|
K_MSEC(GSM_ATTACH_RETRY_DELAY_MSEC));
|
2022-02-23 16:19:37 +01:00
|
|
|
goto unlock;
|
2020-10-16 09:47:11 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 11:38:43 +01:00
|
|
|
/* Attached, clear retry counter */
|
2021-06-17 11:57:45 +02:00
|
|
|
gsm->attached = true;
|
2020-12-10 11:38:43 +01:00
|
|
|
gsm->attach_retries = 0;
|
2020-10-16 09:47:11 +02:00
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
LOG_DBG("modem attach returned %d, %s", ret, "read RSSI");
|
|
|
|
gsm->rssi_retries = GSM_RSSI_RETRIES;
|
|
|
|
|
|
|
|
attached:
|
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_GSM_MUX)) {
|
|
|
|
/* Read connection quality (RSSI) before PPP carrier is ON */
|
2022-02-20 10:51:29 +01:00
|
|
|
query_rssi_nolock(gsm);
|
2021-06-17 11:57:45 +02:00
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
if (!(gsm->minfo.mdm_rssi && gsm->minfo.mdm_rssi != GSM_RSSI_INVALID &&
|
|
|
|
gsm->minfo.mdm_rssi < GSM_RSSI_MAXVAL)) {
|
2021-06-17 11:57:45 +02:00
|
|
|
|
|
|
|
LOG_DBG("Not valid RSSI, %s", "retrying...");
|
|
|
|
if (gsm->rssi_retries-- > 0) {
|
2021-12-28 15:25:27 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work,
|
2021-06-17 11:57:45 +02:00
|
|
|
K_MSEC(GSM_RSSI_RETRY_DELAY_MSEC));
|
2022-02-23 16:19:37 +01:00
|
|
|
goto unlock;
|
2021-06-17 11:57:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#if defined(CONFIG_MODEM_CELL_INFO)
|
2021-12-28 15:25:27 +01:00
|
|
|
(void)gsm_query_cellinfo(gsm);
|
2021-06-17 11:57:45 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:20:37 +02:00
|
|
|
LOG_DBG("modem RSSI: %d, %s", *gsm->context.data_rssi, "enable PPP");
|
2020-04-01 11:51:59 +02:00
|
|
|
|
2020-10-09 13:08:28 +02:00
|
|
|
ret = modem_cmd_handler_setup_cmds_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
connect_cmds,
|
|
|
|
ARRAY_SIZE(connect_cmds),
|
|
|
|
&gsm->sem_response,
|
|
|
|
GSM_CMD_SETUP_TIMEOUT);
|
2020-04-01 11:51:59 +02:00
|
|
|
if (ret < 0) {
|
2021-09-16 13:20:37 +02:00
|
|
|
LOG_DBG("%s returned %d, %s", "connect_cmds", ret, "retrying...");
|
2022-02-25 17:00:15 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work, GSM_RETRY_DELAY);
|
2022-02-23 16:19:37 +01:00
|
|
|
goto unlock;
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
set_ppp_carrier_on(gsm);
|
2020-03-31 15:49:09 +02:00
|
|
|
|
2022-01-20 16:22:38 +01:00
|
|
|
if (IS_ENABLED(CONFIG_GSM_MUX)) {
|
2020-03-31 15:49:09 +02:00
|
|
|
/* Re-use the original iface for AT channel */
|
|
|
|
ret = modem_iface_uart_init_dev(&gsm->context.iface,
|
2021-06-29 15:11:22 +02:00
|
|
|
gsm->at_dev);
|
2020-03-31 15:49:09 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
LOG_DBG("iface %suart error %d", "AT ", ret);
|
|
|
|
} else {
|
|
|
|
/* Do a test and try to send AT command to modem */
|
2020-10-09 13:08:28 +02:00
|
|
|
ret = modem_cmd_send_nolock(
|
|
|
|
&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
&response_cmds[0],
|
|
|
|
ARRAY_SIZE(response_cmds),
|
|
|
|
"AT", &gsm->sem_response,
|
|
|
|
GSM_CMD_AT_TIMEOUT);
|
2020-03-31 15:49:09 +02:00
|
|
|
if (ret < 0) {
|
2021-09-16 13:20:37 +02:00
|
|
|
LOG_WRN("%s returned %d, %s", "AT", ret, "iface failed");
|
2020-03-31 15:49:09 +02:00
|
|
|
} else {
|
|
|
|
LOG_INF("AT channel %d connected to %s",
|
2020-03-09 12:49:07 +01:00
|
|
|
DLCI_AT, gsm->at_dev->name);
|
2020-03-31 15:49:09 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-09 13:08:28 +02:00
|
|
|
modem_cmd_handler_tx_unlock(&gsm->context.cmd_handler);
|
2022-02-20 08:40:27 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->rssi_work_handle,
|
|
|
|
K_SECONDS(CONFIG_MODEM_GSM_RSSI_POLLING_PERIOD));
|
2020-03-31 15:49:09 +02:00
|
|
|
}
|
2022-02-23 16:19:37 +01:00
|
|
|
|
|
|
|
unlock:
|
|
|
|
gsm_ppp_unlock(gsm);
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mux_enable(struct gsm_modem *gsm)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Turn on muxing */
|
|
|
|
if (IS_ENABLED(CONFIG_MODEM_GSM_SIMCOM)) {
|
2020-10-09 13:08:28 +02:00
|
|
|
ret = modem_cmd_send_nolock(
|
2020-04-01 11:51:59 +02:00
|
|
|
&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
&response_cmds[0],
|
|
|
|
ARRAY_SIZE(response_cmds),
|
|
|
|
#if defined(SIMCOM_LTE)
|
|
|
|
/* FIXME */
|
|
|
|
/* Some SIMCOM modems can set the channels */
|
|
|
|
/* Control channel always at DLCI 0 */
|
|
|
|
"AT+CMUXSRVPORT=0,0;"
|
|
|
|
/* PPP should be at DLCI 1 */
|
|
|
|
"+CMUXSRVPORT=" STRINGIFY(DLCI_PPP) ",1;"
|
|
|
|
/* AT should be at DLCI 2 */
|
|
|
|
"+CMUXSRVPORT=" STRINGIFY(DLCI_AT) ",1;"
|
|
|
|
#else
|
|
|
|
"AT"
|
|
|
|
#endif
|
|
|
|
"+CMUX=0,0,5,"
|
|
|
|
STRINGIFY(CONFIG_GSM_MUX_MRU_DEFAULT_LEN),
|
|
|
|
&gsm->sem_response,
|
|
|
|
GSM_CMD_AT_TIMEOUT);
|
2021-09-19 12:02:18 +02:00
|
|
|
} else if (IS_ENABLED(CONFIG_MODEM_GSM_QUECTEL)) {
|
|
|
|
ret = modem_cmd_send_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
&response_cmds[0],
|
|
|
|
ARRAY_SIZE(response_cmds),
|
|
|
|
"AT+CMUX=0,0,5,"
|
|
|
|
STRINGIFY(CONFIG_GSM_MUX_MRU_DEFAULT_LEN),
|
|
|
|
&gsm->sem_response,
|
|
|
|
GSM_CMD_AT_TIMEOUT);
|
|
|
|
|
|
|
|
/* Arbitrary delay for Quectel modems to initialize the CMUX,
|
|
|
|
* without this the AT cmd will fail.
|
|
|
|
*/
|
|
|
|
k_sleep(K_SECONDS(1));
|
2020-04-01 11:51:59 +02:00
|
|
|
} else {
|
|
|
|
/* Generic GSM modem */
|
2020-10-09 13:08:28 +02:00
|
|
|
ret = modem_cmd_send_nolock(&gsm->context.iface,
|
2020-04-01 11:51:59 +02:00
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
&response_cmds[0],
|
|
|
|
ARRAY_SIZE(response_cmds),
|
|
|
|
"AT+CMUX=0", &gsm->sem_response,
|
|
|
|
GSM_CMD_AT_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("AT+CMUX ret:%d", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mux_setup_next(struct gsm_modem *gsm)
|
|
|
|
{
|
2021-12-28 15:25:27 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work, K_MSEC(1));
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void mux_attach_cb(const struct device *mux, int dlci_address,
|
2020-04-01 11:51:59 +02:00
|
|
|
bool connected, void *user_data)
|
|
|
|
{
|
2020-03-09 12:49:07 +01:00
|
|
|
LOG_DBG("DLCI %d to %s %s", dlci_address, mux->name,
|
2020-04-01 11:51:59 +02:00
|
|
|
connected ? "connected" : "disconnected");
|
|
|
|
|
|
|
|
if (connected) {
|
|
|
|
uart_irq_rx_enable(mux);
|
|
|
|
uart_irq_tx_enable(mux);
|
|
|
|
}
|
|
|
|
|
|
|
|
mux_setup_next(user_data);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mux_attach(const struct device *mux, const struct device *uart,
|
2020-04-01 11:51:59 +02:00
|
|
|
int dlci_address, void *user_data)
|
|
|
|
{
|
|
|
|
int ret = uart_mux_attach(mux, uart, dlci_address, mux_attach_cb,
|
|
|
|
user_data);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("Cannot attach DLCI %d (%s) to %s (%d)", dlci_address,
|
2020-03-09 12:49:07 +01:00
|
|
|
mux->name, uart->name, ret);
|
2020-04-01 11:51:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mux_setup(struct k_work *work)
|
|
|
|
{
|
2022-01-03 16:41:11 +01:00
|
|
|
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
|
|
|
|
struct gsm_modem *gsm = CONTAINER_OF(dwork, struct gsm_modem,
|
2020-04-01 11:51:59 +02:00
|
|
|
gsm_configure_work);
|
2021-04-24 19:09:30 +02:00
|
|
|
const struct device *uart = DEVICE_DT_GET(GSM_UART_NODE);
|
2020-04-01 11:51:59 +02:00
|
|
|
int ret;
|
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_lock(gsm);
|
|
|
|
|
2020-10-05 13:58:55 +02:00
|
|
|
/* We need to call this to reactivate mux ISR. Note: This is only called
|
|
|
|
* after re-initing gsm_ppp.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_GSM_MUX) &&
|
|
|
|
gsm->ppp_dev && gsm->state == STATE_CONTROL_CHANNEL) {
|
|
|
|
uart_mux_enable(gsm->ppp_dev);
|
|
|
|
}
|
|
|
|
|
2020-04-01 11:51:59 +02:00
|
|
|
switch (gsm->state) {
|
|
|
|
case STATE_CONTROL_CHANNEL:
|
|
|
|
/* Get UART device. There is one dev / DLCI */
|
|
|
|
if (gsm->control_dev == NULL) {
|
2020-10-05 13:58:55 +02:00
|
|
|
gsm->control_dev = uart_mux_alloc();
|
|
|
|
if (gsm->control_dev == NULL) {
|
|
|
|
LOG_DBG("Cannot get UART mux for %s channel",
|
|
|
|
"control");
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = mux_attach(gsm->control_dev, uart, DLCI_CONTROL, gsm);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:30:21 +01:00
|
|
|
gsm->state = STATE_PPP_CHANNEL;
|
|
|
|
|
2020-04-01 11:51:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_PPP_CHANNEL:
|
|
|
|
if (gsm->ppp_dev == NULL) {
|
2020-10-05 13:58:55 +02:00
|
|
|
gsm->ppp_dev = uart_mux_alloc();
|
|
|
|
if (gsm->ppp_dev == NULL) {
|
|
|
|
LOG_DBG("Cannot get UART mux for %s channel",
|
|
|
|
"PPP");
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = mux_attach(gsm->ppp_dev, uart, DLCI_PPP, gsm);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:30:21 +01:00
|
|
|
gsm->state = STATE_AT_CHANNEL;
|
|
|
|
|
2020-04-01 11:51:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_AT_CHANNEL:
|
|
|
|
if (gsm->at_dev == NULL) {
|
2020-10-05 13:58:55 +02:00
|
|
|
gsm->at_dev = uart_mux_alloc();
|
|
|
|
if (gsm->at_dev == NULL) {
|
|
|
|
LOG_DBG("Cannot get UART mux for %s channel",
|
|
|
|
"AT");
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = mux_attach(gsm->at_dev, uart, DLCI_AT, gsm);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:30:21 +01:00
|
|
|
gsm->state = STATE_DONE;
|
|
|
|
|
2020-04-01 11:51:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_DONE:
|
2020-03-31 15:49:09 +02:00
|
|
|
/* At least the SIMCOM modem expects that the Internet
|
|
|
|
* connection is created in PPP channel. We will need
|
|
|
|
* to attach the AT channel to context iface after the
|
|
|
|
* PPP connection is established in order to give AT commands
|
|
|
|
* to the modem.
|
|
|
|
*/
|
|
|
|
ret = modem_iface_uart_init_dev(&gsm->context.iface,
|
2021-06-29 15:11:22 +02:00
|
|
|
gsm->ppp_dev);
|
2020-04-01 11:51:59 +02:00
|
|
|
if (ret < 0) {
|
2020-03-31 15:49:09 +02:00
|
|
|
LOG_DBG("iface %suart error %d", "PPP ", ret);
|
2020-04-01 11:51:59 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-03-31 15:49:09 +02:00
|
|
|
LOG_INF("PPP channel %d connected to %s",
|
2020-03-09 12:49:07 +01:00
|
|
|
DLCI_PPP, gsm->ppp_dev->name);
|
2020-03-31 15:49:09 +02:00
|
|
|
|
2022-01-20 16:34:05 +01:00
|
|
|
k_work_init_delayable(&gsm->gsm_configure_work, gsm_finalize_connection);
|
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work, K_NO_WAIT);
|
2020-04-01 11:51:59 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
goto unlock;
|
2020-04-01 11:51:59 +02:00
|
|
|
fail:
|
|
|
|
gsm->state = STATE_INIT;
|
2022-02-23 16:19:37 +01:00
|
|
|
unlock:
|
|
|
|
gsm_ppp_unlock(gsm);
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
2020-01-02 11:41:07 +01:00
|
|
|
static void gsm_configure(struct k_work *work)
|
|
|
|
{
|
2022-01-03 16:41:11 +01:00
|
|
|
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
|
|
|
|
struct gsm_modem *gsm = CONTAINER_OF(dwork, struct gsm_modem,
|
2020-01-02 11:41:07 +01:00
|
|
|
gsm_configure_work);
|
2020-04-01 11:51:59 +02:00
|
|
|
int ret = -1;
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_lock(gsm);
|
|
|
|
|
2020-01-02 11:41:07 +01:00
|
|
|
LOG_DBG("Starting modem %p configuration", gsm);
|
|
|
|
|
2021-09-24 21:46:07 +02:00
|
|
|
if (gsm->modem_on_cb) {
|
|
|
|
gsm->modem_on_cb(gsm->dev, gsm->user_data);
|
|
|
|
}
|
|
|
|
|
2020-10-09 13:08:28 +02:00
|
|
|
ret = modem_cmd_send_nolock(&gsm->context.iface,
|
|
|
|
&gsm->context.cmd_handler,
|
|
|
|
&response_cmds[0],
|
|
|
|
ARRAY_SIZE(response_cmds),
|
|
|
|
"AT", &gsm->sem_response,
|
|
|
|
GSM_CMD_AT_TIMEOUT);
|
2020-04-01 11:51:59 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
LOG_DBG("modem not ready %d", ret);
|
2022-02-23 16:19:37 +01:00
|
|
|
goto reschedule;
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
2022-03-04 17:21:58 +01:00
|
|
|
if (IS_ENABLED(CONFIG_GSM_MUX)) {
|
2022-02-23 16:19:37 +01:00
|
|
|
if (mux_enable(gsm)) {
|
2022-01-20 16:10:04 +01:00
|
|
|
LOG_DBG("GSM muxing %s", "disabled");
|
2022-02-23 16:19:37 +01:00
|
|
|
goto reschedule;
|
2020-04-01 11:51:59 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 16:22:38 +01:00
|
|
|
LOG_DBG("GSM muxing %s", "enabled");
|
2020-04-01 11:51:59 +02:00
|
|
|
|
2022-01-20 16:22:38 +01:00
|
|
|
gsm->state = STATE_INIT;
|
2020-04-01 11:51:59 +02:00
|
|
|
|
2022-01-20 16:22:38 +01:00
|
|
|
k_work_init_delayable(&gsm->gsm_configure_work, mux_setup);
|
2022-02-23 16:19:37 +01:00
|
|
|
} else {
|
|
|
|
k_work_init_delayable(&gsm->gsm_configure_work, gsm_finalize_connection);
|
2020-01-02 11:41:07 +01:00
|
|
|
}
|
2020-01-24 08:02:33 +01:00
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
reschedule:
|
2022-01-20 16:34:05 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work, K_NO_WAIT);
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_unlock(gsm);
|
2020-01-02 11:41:07 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 15:28:25 +01:00
|
|
|
void gsm_ppp_start(const struct device *dev)
|
2020-10-05 13:58:55 +02:00
|
|
|
{
|
2022-02-23 16:19:37 +01:00
|
|
|
int ret;
|
2021-03-22 15:28:25 +01:00
|
|
|
struct gsm_modem *gsm = dev->data;
|
2020-10-05 13:58:55 +02:00
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_lock(gsm);
|
|
|
|
|
2020-10-05 13:58:55 +02:00
|
|
|
/* Re-init underlying UART comms */
|
2022-02-23 16:19:37 +01:00
|
|
|
ret = modem_iface_uart_init_dev(&gsm->context.iface, DEVICE_DT_GET(GSM_UART_NODE));
|
|
|
|
if (ret) {
|
|
|
|
LOG_ERR("modem_iface_uart_init returned %d", ret);
|
|
|
|
goto unlock;
|
2020-10-05 13:58:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 17:31:30 +02:00
|
|
|
k_work_init_delayable(&gsm->gsm_configure_work, gsm_configure);
|
2021-12-28 15:25:27 +01:00
|
|
|
(void)gsm_work_reschedule(&gsm->gsm_configure_work, K_NO_WAIT);
|
2022-02-23 16:19:37 +01:00
|
|
|
|
|
|
|
unlock:
|
|
|
|
gsm_ppp_unlock(gsm);
|
2020-10-05 13:58:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 15:28:25 +01:00
|
|
|
void gsm_ppp_stop(const struct device *dev)
|
2020-10-05 13:58:55 +02:00
|
|
|
{
|
2021-03-22 15:28:25 +01:00
|
|
|
struct gsm_modem *gsm = dev->data;
|
2020-10-05 13:58:55 +02:00
|
|
|
struct net_if *iface = gsm->iface;
|
2022-01-03 16:52:44 +01:00
|
|
|
struct k_work_sync work_sync;
|
|
|
|
|
|
|
|
(void)k_work_cancel_delayable_sync(&gsm->gsm_configure_work, &work_sync);
|
2022-02-20 08:54:00 +01:00
|
|
|
if (IS_ENABLED(CONFIG_GSM_MUX)) {
|
|
|
|
(void)k_work_cancel_delayable_sync(&gsm->rssi_work_handle, &work_sync);
|
|
|
|
}
|
2020-10-05 13:58:55 +02:00
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_lock(gsm);
|
|
|
|
|
2022-01-21 16:17:07 +01:00
|
|
|
/* wait for the interface to be properly down */
|
2022-06-07 11:42:54 +02:00
|
|
|
if (net_if_is_up(iface)) {
|
|
|
|
net_if_l2(iface)->enable(iface, false);
|
|
|
|
(void)k_sem_take(&gsm->sem_if_down, K_FOREVER);
|
|
|
|
}
|
2020-10-05 13:58:55 +02:00
|
|
|
if (IS_ENABLED(CONFIG_GSM_MUX)) {
|
|
|
|
|
|
|
|
if (gsm->ppp_dev) {
|
|
|
|
uart_mux_disable(gsm->ppp_dev);
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 13:08:28 +02:00
|
|
|
|
2022-02-20 09:29:54 +01:00
|
|
|
if (modem_cmd_handler_tx_lock(&gsm->context.cmd_handler, GSM_CMD_LOCK_TIMEOUT)) {
|
2020-10-09 13:08:28 +02:00
|
|
|
LOG_WRN("Failed locking modem cmds!");
|
|
|
|
}
|
2021-09-24 21:46:07 +02:00
|
|
|
|
|
|
|
if (gsm->modem_off_cb) {
|
|
|
|
gsm->modem_off_cb(gsm->dev, gsm->user_data);
|
|
|
|
}
|
2022-02-20 08:37:32 +01:00
|
|
|
|
|
|
|
gsm->attached = false;
|
2022-02-20 15:56:17 +01:00
|
|
|
gsm->net_state = GSM_NET_INIT;
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_unlock(gsm);
|
2021-09-24 21:46:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void gsm_ppp_register_modem_power_callback(const struct device *dev,
|
|
|
|
gsm_modem_power_cb modem_on,
|
|
|
|
gsm_modem_power_cb modem_off,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct gsm_modem *gsm = dev->data;
|
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_lock(gsm);
|
|
|
|
|
2021-09-24 21:46:07 +02:00
|
|
|
gsm->modem_on_cb = modem_on;
|
|
|
|
gsm->modem_off_cb = modem_off;
|
|
|
|
|
|
|
|
gsm->user_data = user_data;
|
2022-02-23 16:19:37 +01:00
|
|
|
gsm_ppp_unlock(gsm);
|
2020-10-05 13:58:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 17:43:08 +02:00
|
|
|
const struct gsm_ppp_modem_info *gsm_ppp_modem_info(const struct device *dev)
|
|
|
|
{
|
2022-01-03 16:21:17 +01:00
|
|
|
struct gsm_modem *gsm = dev->data;
|
2021-10-20 17:43:08 +02:00
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
return &gsm->minfo;
|
2021-10-20 17:43:08 +02:00
|
|
|
}
|
|
|
|
|
2022-01-21 16:17:07 +01:00
|
|
|
static void gsm_mgmt_event_handler(struct net_mgmt_event_callback *cb,
|
|
|
|
uint32_t mgmt_event, struct net_if *iface)
|
|
|
|
{
|
|
|
|
if ((mgmt_event & NET_EVENT_IF_DOWN) != mgmt_event) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Right now we only support 1 GSM instance */
|
|
|
|
if (iface != gsm.iface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mgmt_event == NET_EVENT_IF_DOWN) {
|
|
|
|
LOG_INF("GSM network interface down");
|
|
|
|
/* raise semaphore to indicate the interface is down */
|
|
|
|
k_sem_give(&gsm.sem_if_down);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 15:28:25 +01:00
|
|
|
static int gsm_init(const struct device *dev)
|
2020-01-02 11:41:07 +01:00
|
|
|
{
|
2021-03-22 15:28:25 +01:00
|
|
|
struct gsm_modem *gsm = dev->data;
|
2020-01-02 11:41:07 +01:00
|
|
|
int r;
|
|
|
|
|
2020-02-07 09:39:43 +01:00
|
|
|
LOG_DBG("Generic GSM modem (%p)", gsm);
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2022-02-23 16:19:37 +01:00
|
|
|
(void)k_mutex_init(&gsm->lock);
|
2021-09-24 21:46:07 +02:00
|
|
|
gsm->dev = dev;
|
|
|
|
|
2020-02-06 14:14:01 +01:00
|
|
|
gsm->cmd_handler_data.cmds[CMD_RESP] = response_cmds;
|
|
|
|
gsm->cmd_handler_data.cmds_len[CMD_RESP] = ARRAY_SIZE(response_cmds);
|
|
|
|
gsm->cmd_handler_data.match_buf = &gsm->cmd_match_buf[0];
|
|
|
|
gsm->cmd_handler_data.match_buf_len = sizeof(gsm->cmd_match_buf);
|
|
|
|
gsm->cmd_handler_data.buf_pool = &gsm_recv_pool;
|
2020-10-26 19:37:09 +01:00
|
|
|
gsm->cmd_handler_data.alloc_timeout = K_NO_WAIT;
|
2020-02-07 09:39:43 +01:00
|
|
|
gsm->cmd_handler_data.eol = "\r";
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2020-02-06 14:14:01 +01:00
|
|
|
k_sem_init(&gsm->sem_response, 0, 1);
|
2022-01-21 16:17:07 +01:00
|
|
|
k_sem_init(&gsm->sem_if_down, 0, 1);
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2020-02-06 14:14:01 +01:00
|
|
|
r = modem_cmd_handler_init(&gsm->context.cmd_handler,
|
|
|
|
&gsm->cmd_handler_data);
|
2020-01-02 11:41:07 +01:00
|
|
|
if (r < 0) {
|
|
|
|
LOG_DBG("cmd handler error %d", r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-02-05 14:05:59 +01:00
|
|
|
#if defined(CONFIG_MODEM_SHELL)
|
|
|
|
/* modem information storage */
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm->context.data_manufacturer = gsm->minfo.mdm_manufacturer;
|
|
|
|
gsm->context.data_model = gsm->minfo.mdm_model;
|
|
|
|
gsm->context.data_revision = gsm->minfo.mdm_revision;
|
|
|
|
gsm->context.data_imei = gsm->minfo.mdm_imei;
|
2020-04-20 13:51:45 +02:00
|
|
|
#if defined(CONFIG_MODEM_SIM_NUMBERS)
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm->context.data_imsi = gsm->minfo.mdm_imsi;
|
|
|
|
gsm->context.data_iccid = gsm->minfo.mdm_iccid;
|
2020-04-20 13:51:45 +02:00
|
|
|
#endif /* CONFIG_MODEM_SIM_NUMBERS */
|
2022-01-03 16:21:17 +01:00
|
|
|
gsm->context.data_rssi = &gsm->minfo.mdm_rssi;
|
2020-04-20 13:51:45 +02:00
|
|
|
#endif /* CONFIG_MODEM_SHELL */
|
2020-02-05 14:05:59 +01:00
|
|
|
|
2021-06-17 11:57:45 +02:00
|
|
|
gsm->context.is_automatic_oper = false;
|
2020-02-06 14:14:01 +01:00
|
|
|
gsm->gsm_data.rx_rb_buf = &gsm->gsm_rx_rb_buf[0];
|
|
|
|
gsm->gsm_data.rx_rb_buf_len = sizeof(gsm->gsm_rx_rb_buf);
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2020-04-01 11:51:59 +02:00
|
|
|
r = modem_iface_uart_init(&gsm->context.iface, &gsm->gsm_data,
|
2021-04-24 19:09:30 +02:00
|
|
|
DEVICE_DT_GET(GSM_UART_NODE));
|
2020-01-02 11:41:07 +01:00
|
|
|
if (r < 0) {
|
|
|
|
LOG_DBG("iface uart error %d", r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-02-06 14:14:01 +01:00
|
|
|
r = modem_context_register(&gsm->context);
|
2020-01-02 11:41:07 +01:00
|
|
|
if (r < 0) {
|
|
|
|
LOG_DBG("context error %d", r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2022-02-20 15:56:17 +01:00
|
|
|
gsm->net_state = GSM_NET_INIT;
|
|
|
|
|
2020-04-01 11:51:59 +02:00
|
|
|
LOG_DBG("iface->read %p iface->write %p",
|
|
|
|
gsm->context.iface.read, gsm->context.iface.write);
|
|
|
|
|
2022-01-03 16:21:17 +01:00
|
|
|
k_thread_create(&gsm->rx_thread, gsm_rx_stack,
|
2020-07-31 21:29:38 +02:00
|
|
|
K_KERNEL_STACK_SIZEOF(gsm_rx_stack),
|
2020-01-02 11:41:07 +01:00
|
|
|
(k_thread_entry_t) gsm_rx,
|
2020-02-06 14:14:01 +01:00
|
|
|
gsm, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
|
2022-01-03 16:21:17 +01:00
|
|
|
k_thread_name_set(&gsm->rx_thread, "gsm_rx");
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2021-12-28 15:25:27 +01:00
|
|
|
/* initialize the work queue */
|
2022-01-03 16:21:17 +01:00
|
|
|
k_work_queue_init(&gsm->workq);
|
2022-01-19 13:33:49 +01:00
|
|
|
k_work_queue_start(&gsm->workq, gsm_workq_stack, K_KERNEL_STACK_SIZEOF(gsm_workq_stack),
|
2021-12-28 15:25:27 +01:00
|
|
|
K_PRIO_COOP(7), NULL);
|
2022-01-03 16:21:17 +01:00
|
|
|
k_thread_name_set(&gsm->workq.thread, "gsm_workq");
|
2021-12-28 15:25:27 +01:00
|
|
|
|
2022-02-20 09:16:30 +01:00
|
|
|
if (IS_ENABLED(CONFIG_GSM_MUX)) {
|
|
|
|
k_work_init_delayable(&gsm->rssi_work_handle, rssi_handler);
|
|
|
|
}
|
2022-01-21 18:41:57 +01:00
|
|
|
|
2020-10-05 13:58:55 +02:00
|
|
|
gsm->iface = ppp_net_if();
|
|
|
|
if (!gsm->iface) {
|
|
|
|
LOG_ERR("Couldn't find ppp net_if!");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2020-01-02 11:41:07 +01:00
|
|
|
|
2022-01-21 16:17:07 +01:00
|
|
|
net_mgmt_init_event_callback(&gsm->gsm_mgmt_cb, gsm_mgmt_event_handler,
|
|
|
|
NET_EVENT_IF_DOWN);
|
|
|
|
net_mgmt_add_event_callback(&gsm->gsm_mgmt_cb);
|
|
|
|
|
2021-03-11 17:25:13 +01:00
|
|
|
if (IS_ENABLED(CONFIG_GSM_PPP_AUTOSTART)) {
|
2021-03-22 15:28:25 +01:00
|
|
|
gsm_ppp_start(dev);
|
2021-03-11 17:25:13 +01:00
|
|
|
}
|
2020-01-02 11:41:07 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-24 19:09:30 +02:00
|
|
|
DEVICE_DT_DEFINE(DT_INST(0, zephyr_gsm_ppp), gsm_init, NULL, &gsm, NULL,
|
|
|
|
POST_KERNEL, CONFIG_MODEM_GSM_INIT_PRIORITY, NULL);
|