Skip to content

Commit c4f0fb6

Browse files
committed
subsys: ctr_lte_v2 and ctr_gnss: Upgrade lte v2 to state maschine and add gnss support
1 parent eef64d1 commit c4f0fb6

34 files changed

+2468
-1030
lines changed

applications/demo/prj.conf

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ CONFIG_SETTINGS_SHELL=y
3434
CONFIG_IMG_MANAGER=y
3535
CONFIG_BOOTLOADER_MCUBOOT=y
3636

37+
CONFIG_CTR_LTE_V2_GNSS=y
38+
CONFIG_CTR_GNSS=y
39+
3740
# CONFIG_MCUBOOT_EXTRA_IMGTOOL_ARGS="--custom-tlv 0xff00 tralala"
3841

3942
CONFIG_PM_PARTITION_SIZE_SETTINGS_STORAGE=0x8000
@@ -47,8 +50,9 @@ CONFIG_DFU_TARGET_LOG_LEVEL_DBG=y
4750
CONFIG_STREAM_FLASH_ERASE=y
4851
CONFIG_STREAM_FLASH=y
4952

53+
# CONFIG_CTR_LTE_LINK_LOG_LEVEL_DBG=y
54+
CONFIG_CTR_LTE_LINK_LOG_LEVEL_INF=y
5055
# CONFIG_CTR_LTE_LINK_LOG_LEVEL_ERR=y
51-
CONFIG_CTR_LTE_LINK_LOG_LEVEL_DBG=y
5256
# CONFIG_CTR_LTE_V2_LOG_LEVEL_ERR=y
5357
CONFIG_CTR_LTE_V2_LOG_LEVEL_DBG=y
5458

@@ -60,8 +64,10 @@ CONFIG_FILE_SYSTEM=y
6064
CONFIG_FILE_SYSTEM_LITTLEFS=y
6165
CONFIG_FILE_SYSTEM_MKFS=y
6266
CONFIG_FILE_SYSTEM_SHELL=y
67+
# CONFIG_FILE_SYSTEM_SHELL_TEST_COMMANDS=y
6368
CONFIG_SETTINGS_FILE=y
6469
CONFIG_SETTINGS_FILE_PATH="/lfs1/settings/run"
65-
6670
# Use external flash for littlefs file system
6771
CONFIG_PM_PARTITION_REGION_LITTLEFS_EXTERNAL=y
72+
73+

applications/demo/src/app_init.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int app_init(void)
8585
LOG_ERR("Call `ctr_cloud_set_callback` failed: %d", ret);
8686
}
8787

88-
ret = ctr_cloud_set_poll_interval(K_SECONDS(30));
88+
ret = ctr_cloud_set_poll_interval(K_SECONDS(2 * 60));
8989
if (ret) {
9090
LOG_ERR("Call `ctr_cloud_set_pull_interval` failed: %d", ret);
9191
return ret;

applications/demo/src/app_work.c

+31
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
/* CHESTER includes */
1515
#include <chester/ctr_buf.h>
1616
#include <chester/ctr_cloud.h>
17+
#include <chester/ctr_gnss.h>
1718

1819
/* Zephyr includes */
1920
#include <zephyr/device.h>
@@ -147,6 +148,34 @@ static void power_timer_handler(struct k_timer *timer)
147148

148149
static K_TIMER_DEFINE(m_power_timer, power_timer_handler, NULL);
149150

151+
static void gnss_handler(enum ctr_gnss_event event, union ctr_gnss_event_data *data,
152+
void *user_data)
153+
{
154+
switch (event) {
155+
case CTR_GNSS_EVENT_START_OK:
156+
LOG_INF("GNSS started");
157+
break;
158+
case CTR_GNSS_EVENT_START_ERR:
159+
LOG_ERR("GNSS start failed");
160+
break;
161+
case CTR_GNSS_EVENT_STOP_OK:
162+
LOG_INF("GNSS stopped");
163+
break;
164+
case CTR_GNSS_EVENT_STOP_ERR:
165+
LOG_ERR("GNSS stop failed");
166+
break;
167+
case CTR_GNSS_EVENT_UPDATE:
168+
LOG_INF("GNSS update: fix_quality=%d, satellites_tracked=%d, latitude=%.6f, "
169+
"longitude=%.6f, altitude=%.2f",
170+
data->update.fix_quality, data->update.satellites_tracked,
171+
data->update.latitude, data->update.longitude, data->update.altitude);
172+
break;
173+
case CTR_GNSS_EVENT_FAILURE:
174+
LOG_ERR("GNSS failure");
175+
break;
176+
}
177+
}
178+
150179
int app_work_init(void)
151180
{
152181
k_work_queue_start(&m_work_q, m_work_q_stack, K_THREAD_STACK_SIZEOF(m_work_q_stack),
@@ -158,6 +187,8 @@ int app_work_init(void)
158187
k_timer_start(&m_sample_timer, K_NO_WAIT, K_SECONDS(g_app_config.interval_sample));
159188
k_timer_start(&m_power_timer, K_SECONDS(60), K_HOURS(12));
160189

190+
ctr_gnss_set_handler(gnss_handler, NULL);
191+
161192
return 0;
162193
}
163194

drivers/ctr_lte_link/ctr_lte_link.c

+20-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ LOG_MODULE_REGISTER(ctr_lte_link, CONFIG_CTR_LTE_LINK_LOG_LEVEL);
3434
#define RESET_PAUSE K_SECONDS(3)
3535
#define WAKE_UP_DELAY K_MSEC(100)
3636
#define WAKE_UP_PAUSE K_MSEC(100)
37+
#define TX_GUARD_TIME K_MSEC(10)
3738

3839
#define TX_LINE_PREFIX ""
3940
#define TX_LINE_SUFFIX "\r\n"
@@ -75,6 +76,7 @@ struct ctr_lte_link_data {
7576
struct k_pipe rx_pipe;
7677
struct k_sem rx_disabled_sem;
7778
struct k_sem tx_finished_sem;
79+
k_timepoint_t tx_timepoint;
7880
struct k_work rx_loss_work;
7981
struct k_work rx_receive_work;
8082
struct k_work rx_restart_work;
@@ -630,7 +632,7 @@ static int ctr_lte_link_disable_uart_(const struct device *dev)
630632

631633
static int ctr_lte_link_enter_data_mode_(const struct device *dev)
632634
{
633-
LOG_INF("Enter data mode");
635+
LOG_DBG("Enter data mode");
634636

635637
if (k_is_in_isr()) {
636638
return -EWOULDBLOCK;
@@ -654,7 +656,7 @@ static int ctr_lte_link_enter_data_mode_(const struct device *dev)
654656

655657
static int ctr_lte_link_exit_data_mode_(const struct device *dev)
656658
{
657-
LOG_INF("Exit data mode");
659+
LOG_DBG("Exit data mode");
658660

659661
if (k_is_in_isr()) {
660662
return -EWOULDBLOCK;
@@ -678,7 +680,7 @@ static int ctr_lte_link_exit_data_mode_(const struct device *dev)
678680

679681
static int ctr_lte_link_enter_dialog_(const struct device *dev)
680682
{
681-
LOG_INF("Enter dialog");
683+
LOG_DBG("Enter dialog");
682684

683685
if (k_is_in_isr()) {
684686
return -EWOULDBLOCK;
@@ -700,7 +702,7 @@ static int ctr_lte_link_enter_dialog_(const struct device *dev)
700702

701703
static int ctr_lte_link_exit_dialog_(const struct device *dev)
702704
{
703-
LOG_INF("Exit dialog");
705+
LOG_DBG("Exit dialog");
704706

705707
if (k_is_in_isr()) {
706708
return -EWOULDBLOCK;
@@ -770,6 +772,10 @@ static int ctr_lte_link_send_line_(const struct device *dev, k_timeout_t timeout
770772

771773
strcat(get_data(dev)->tx_line_buf, TX_LINE_SUFFIX);
772774

775+
if (!sys_timepoint_expired(get_data(dev)->tx_timepoint)) {
776+
k_sleep(sys_timepoint_timeout(get_data(dev)->tx_timepoint));
777+
}
778+
773779
ret = uart_tx(get_config(dev)->uart_dev, get_data(dev)->tx_line_buf,
774780
strlen(get_data(dev)->tx_line_buf), SYS_FOREVER_US);
775781
if (ret) {
@@ -785,6 +791,8 @@ static int ctr_lte_link_send_line_(const struct device *dev, k_timeout_t timeout
785791
return ret;
786792
}
787793

794+
get_data(dev)->tx_timepoint = sys_timepoint_calc(TX_GUARD_TIME);
795+
788796
k_mutex_unlock(&get_data(dev)->lock);
789797

790798
return 0;
@@ -800,10 +808,10 @@ static int ctr_lte_link_recv_line_(const struct device *dev, k_timeout_t timeout
800808

801809
k_mutex_lock(&get_data(dev)->lock, K_FOREVER);
802810

803-
if (!get_data(dev)->enabled) {
804-
k_mutex_unlock(&get_data(dev)->lock);
805-
return -EBUSY;
806-
}
811+
// if (!get_data(dev)->enabled) {
812+
// k_mutex_unlock(&get_data(dev)->lock);
813+
// return -EBUSY;
814+
// }
807815

808816
if (get_data(dev)->in_data_mode) {
809817
k_mutex_unlock(&get_data(dev)->lock);
@@ -850,10 +858,10 @@ static int ctr_lte_link_free_line_(const struct device *dev, char *line)
850858

851859
k_mutex_lock(&get_data(dev)->lock, K_FOREVER);
852860

853-
if (!get_data(dev)->enabled) {
854-
k_mutex_unlock(&get_data(dev)->lock);
855-
return -EBUSY;
856-
}
861+
// if (!get_data(dev)->enabled) {
862+
// k_mutex_unlock(&get_data(dev)->lock);
863+
// return -EBUSY;
864+
// }
857865

858866
k_heap_free(&get_data(dev)->rx_heap, line);
859867
LOG_DBG("Released from heap: %p", (void *)line);

include/chester/ctr_lte_v2.h

+42-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern "C" {
1515

1616
struct ctr_lte_v2_conn_param {
1717
bool valid;
18+
int result;
1819
int eest;
1920
int ecl;
2021
int rsrp;
@@ -51,33 +52,60 @@ struct ctr_lte_v2_cereg_param {
5152
};
5253

5354
struct ctr_lte_v2_send_recv_param {
55+
bool rai;
5456
const void *send_buf;
5557
size_t send_len;
5658
void *recv_buf;
5759
size_t recv_size;
5860
size_t *recv_len;
59-
int64_t *send_duration; // in milliseconds
60-
int64_t *recv_duration; // in milliseconds
61-
int64_t *total_duration; // in milliseconds
61+
k_timeout_t timeout;
6262
};
6363

64+
struct ctr_lte_v2_metrics {
65+
uint32_t uplink_count;
66+
uint32_t uplink_bytes;
67+
uint32_t uplink_errors;
68+
int64_t uplink_last_ts;
69+
uint32_t downlink_count;
70+
uint32_t downlink_bytes;
71+
uint32_t downlink_errors;
72+
int64_t downlink_last_ts;
73+
};
74+
75+
struct ctr_lte_v2_gnss_update {
76+
float latitude;
77+
float longitude;
78+
float altitude;
79+
float accuracy;
80+
float speed;
81+
float heading;
82+
char datetime[20];
83+
};
84+
85+
typedef void (*ctr_lte_v2_gnss_cb)(const struct ctr_lte_v2_gnss_update *update, void *user_data);
86+
87+
int ctr_lte_v2_enable(void);
88+
int ctr_lte_v2_wait_for_connected(k_timeout_t timeout);
89+
int ctr_lte_v2_send_recv(const struct ctr_lte_v2_send_recv_param *param);
90+
91+
// get information
6492
int ctr_lte_v2_get_imei(uint64_t *imei);
6593
int ctr_lte_v2_get_imsi(uint64_t *imsi);
6694
int ctr_lte_v2_get_iccid(char **iccid);
6795
int ctr_lte_v2_get_modem_fw_version(char **version);
68-
int ctr_lte_v2_is_prepared(bool *prepared);
69-
int ctr_lte_v2_is_attached(bool *attached);
70-
int ctr_lte_v2_start(void);
71-
int ctr_lte_v2_start(void);
72-
int ctr_lte_v2_stop(void);
73-
int ctr_lte_v2_wait_on_modem_sleep(k_timeout_t delay);
74-
int ctr_lte_v2_prepare(void);
75-
int ctr_lte_v2_attach(void);
76-
int ctr_lte_v2_detach(void);
77-
int ctr_lte_v2_send_recv(const struct ctr_lte_v2_send_recv_param *param, bool rai);
78-
int ctr_lte_v2_eval_conn(void);
7996
int ctr_lte_v2_get_conn_param(struct ctr_lte_v2_conn_param *param);
8097
int ctr_lte_v2_get_cereg_param(struct ctr_lte_v2_cereg_param *param);
98+
int ctr_lte_v2_get_metrics(struct ctr_lte_v2_metrics *metrics);
99+
100+
const char *ctr_lte_v2_coneval_result_str(int result);
101+
102+
int ctr_lte_v2_is_attached(bool *attached);
103+
104+
#if defined(CONFIG_CTR_LTE_V2_GNSS)
105+
int ctr_lte_v2_gnss_set_enable(bool enable);
106+
int ctr_lte_v2_gnss_get_enable(bool *enable);
107+
int ctr_lte_v2_gnss_set_handler(ctr_lte_v2_gnss_cb callback, void *user_data);
108+
#endif
81109

82110
#ifdef __cplusplus
83111
}

subsys/ctr_cloud/ctr_cloud.c

+4-66
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <chester/ctr_buf.h>
1515
#include <chester/ctr_cloud.h>
1616
#include <chester/ctr_info.h>
17-
#include <chester/ctr_lte_v2.h>
1817
#include <chester/ctr_rtc.h>
1918

2019
/* Zephyr includes */
@@ -187,53 +186,6 @@ static int process_downlink(struct ctr_buf *buf, struct ctr_buf *upbuf)
187186
return 0;
188187
}
189188

190-
static int lte_attach(void)
191-
{
192-
int ret;
193-
int res = 0;
194-
195-
k_mutex_lock(&m_lock, K_FOREVER);
196-
197-
ret = ctr_lte_v2_attach();
198-
if (ret) {
199-
LOG_ERR("Call `ctr_lte_v2_attach` failed: %d", ret);
200-
k_mutex_unlock(&m_lock);
201-
res = ret;
202-
goto exit;
203-
}
204-
205-
exit:
206-
ret = ctr_lte_v2_stop();
207-
if (ret) {
208-
LOG_ERR("Call `ctr_lte_v2_stop` failed: %d", ret);
209-
k_mutex_unlock(&m_lock);
210-
res = res ? res : ret;
211-
return ret;
212-
}
213-
214-
k_mutex_unlock(&m_lock);
215-
216-
return res;
217-
}
218-
219-
static int lte_detach(void)
220-
{
221-
int ret;
222-
223-
k_mutex_lock(&m_lock, K_FOREVER);
224-
225-
ret = ctr_lte_v2_detach();
226-
if (ret) {
227-
LOG_ERR("Call `ctr_lte_v2_detach` failed: %d", ret);
228-
k_mutex_unlock(&m_lock);
229-
return ret;
230-
}
231-
232-
k_mutex_unlock(&m_lock);
233-
234-
return 0;
235-
}
236-
237189
static void poll_work_handler(struct k_work *work)
238190
{
239191
int ret = ctr_cloud_recv();
@@ -585,30 +537,22 @@ static void init_work_handler(struct k_work *work)
585537
ctr_buf_reset(&m_transfer_buf);
586538

587539
for (;;) {
588-
// k_sleep(K_SECONDS(5));
589540

590-
LOG_INF("Running LTE ATTACH");
591-
592-
ret = lte_attach();
593-
if (ret) {
594-
LOG_ERR("Call `lte_attach` failed: %d", ret);
595-
goto error;
596-
}
541+
ctr_cloud_transfer_wait_for_ready(K_FOREVER);
597542

598543
LOG_INF("Running CREATE SESSION");
599-
600544
ret = create_session();
601545
if (ret) {
602546
LOG_ERR("Call `create_session` failed: %d", ret);
603-
goto error;
547+
continue;
604548
}
605549

606550
LOG_INF("Running UPLOAD ENCODER");
607551

608552
ret = upload_decoder();
609553
if (ret) {
610554
LOG_ERR("Call `upload_decoder` failed: %d", ret);
611-
goto error;
555+
continue;
612556
}
613557

614558
LOG_INF("Running UPLOAD ENCODER");
@@ -617,7 +561,7 @@ static void init_work_handler(struct k_work *work)
617561

618562
if (ret) {
619563
LOG_ERR("Call `upload_encoder` failed: %d", ret);
620-
goto error;
564+
continue;
621565
}
622566

623567
LOG_INF("Running UPLOAD CONFIG");
@@ -633,12 +577,6 @@ static void init_work_handler(struct k_work *work)
633577
#endif
634578

635579
break;
636-
error:
637-
ret = lte_detach();
638-
if (ret) {
639-
LOG_ERR("Call `lte_detach` failed: %d", ret);
640-
goto error;
641-
}
642580
}
643581

644582
k_mutex_unlock(&m_lock);

0 commit comments

Comments
 (0)