1 /*
2  * Copyright (C) 2021 metraTec GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef SIMCOM_SIM7080_H
8 #define SIMCOM_SIM7080_H
9 
10 #include <zephyr/kernel.h>
11 #include <ctype.h>
12 #include <inttypes.h>
13 #include <errno.h>
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/drivers/modem/simcom-sim7080.h>
16 #include <zephyr/device.h>
17 #include <zephyr/devicetree.h>
18 #include <zephyr/init.h>
19 #include <string.h>
20 
21 #include <zephyr/net/net_if.h>
22 #include <zephyr/net/net_offload.h>
23 #include <zephyr/net/socket_offload.h>
24 
25 #include "modem_context.h"
26 #include "modem_cmd_handler.h"
27 #include "modem_iface_uart.h"
28 #include "modem_socket.h"
29 
30 #define MDM_UART_NODE DT_INST_BUS(0)
31 #define MDM_UART_DEV DEVICE_DT_GET(MDM_UART_NODE)
32 #define MDM_MAX_DATA_LENGTH 1024
33 #define MDM_RECV_BUF_SIZE 1024
34 #define MDM_MAX_SOCKETS 5
35 #define MDM_BASE_SOCKET_NUM 0
36 #define MDM_RECV_MAX_BUF 30
37 #define BUF_ALLOC_TIMEOUT K_SECONDS(1)
38 #define MDM_CMD_TIMEOUT K_SECONDS(10)
39 #define MDM_REGISTRATION_TIMEOUT K_SECONDS(180)
40 #define MDM_CONNECT_TIMEOUT K_SECONDS(90)
41 #define MDM_PDP_TIMEOUT K_SECONDS(120)
42 #define MDM_DNS_TIMEOUT K_SECONDS(210)
43 #define MDM_WAIT_FOR_RSSI_DELAY K_SECONDS(2)
44 #define MDM_WAIT_FOR_RSSI_COUNT 30
45 #define MDM_MAX_AUTOBAUD 5
46 #define MDM_MAX_CEREG_WAITS 40
47 #define MDM_MAX_CGATT_WAITS 40
48 #define MDM_BOOT_TRIES 4
49 #define MDM_GNSS_PARSER_MAX_LEN 128
50 #define MDM_APN CONFIG_MODEM_SIMCOM_SIM7080_APN
51 #define MDM_LTE_BANDS CONFIG_MODEM_SIMCOM_SIM7080_LTE_BANDS
52 #define RSSI_TIMEOUT_SECS 30
53 
54 /*
55  * Default length of modem data.
56  */
57 #define MDM_MANUFACTURER_LENGTH 12
58 #define MDM_MODEL_LENGTH 16
59 #define MDM_REVISION_LENGTH 64
60 #define MDM_IMEI_LENGTH 16
61 #define MDM_IMSI_LENGTH 16
62 #define MDM_ICCID_LENGTH 32
63 
64 enum sim7080_state {
65 	SIM7080_STATE_INIT = 0,
66 	SIM7080_STATE_NETWORKING,
67 	SIM7080_STATE_GNSS,
68 	SIM7080_STATE_OFF,
69 };
70 
71 /* Possible states of the ftp connection. */
72 enum sim7080_ftp_connection_state {
73 	/* Not connected yet. */
74 	SIM7080_FTP_CONNECTION_STATE_INITIAL = 0,
75 	/* Connected and still data available. */
76 	SIM7080_FTP_CONNECTION_STATE_CONNECTED,
77 	/* All data transferred. */
78 	SIM7080_FTP_CONNECTION_STATE_FINISHED,
79 	/* Something went wrong. */
80 	SIM7080_FTP_CONNECTION_STATE_ERROR,
81 };
82 
83 /*
84  * Driver data.
85  */
86 struct sim7080_data {
87 	/*
88 	 * Network interface of the sim module.
89 	 */
90 	struct net_if *netif;
91 	uint8_t mac_addr[6];
92 	/*
93 	 * Uart interface of the modem.
94 	 */
95 	struct modem_iface_uart_data iface_data;
96 	uint8_t iface_rb_buf[MDM_MAX_DATA_LENGTH];
97 	/*
98 	 * Modem command handler.
99 	 */
100 	struct modem_cmd_handler_data cmd_handler_data;
101 	uint8_t cmd_match_buf[MDM_RECV_BUF_SIZE + 1];
102 	/*
103 	 * Modem socket data.
104 	 */
105 	struct modem_socket_config socket_config;
106 	struct modem_socket sockets[MDM_MAX_SOCKETS];
107 	/*
108 	 * Current state of the modem.
109 	 */
110 	enum sim7080_state state;
111 	/*
112 	 * RSSI work
113 	 */
114 	struct k_work_delayable rssi_query_work;
115 	/*
116 	 * Information over the modem.
117 	 */
118 	char mdm_manufacturer[MDM_MANUFACTURER_LENGTH];
119 	char mdm_model[MDM_MODEL_LENGTH];
120 	char mdm_revision[MDM_REVISION_LENGTH];
121 	char mdm_imei[MDM_IMEI_LENGTH];
122 #if defined(CONFIG_MODEM_SIM_NUMBERS)
123 	char mdm_imsi[MDM_IMSI_LENGTH];
124 	char mdm_iccid[MDM_ICCID_LENGTH];
125 #endif /* #if defined(CONFIG_MODEM_SIM_NUMBERS) */
126 	int mdm_rssi;
127 	/*
128 	 * Current operating socket and statistics.
129 	 */
130 	int current_sock_fd;
131 	int current_sock_written;
132 	/*
133 	 * Network registration of the modem.
134 	 */
135 	uint8_t mdm_registration;
136 	/*
137 	 * Whether gprs is attached or detached.
138 	 */
139 	uint8_t mdm_cgatt;
140 	/*
141 	 * If the sim card is ready or not.
142 	 */
143 	bool cpin_ready;
144 	/*
145 	 * Flag if the PDP context is active.
146 	 */
147 	bool pdp_active;
148 	/* SMS buffer structure provided by read. */
149 	struct sim7080_sms_buffer *sms_buffer;
150 	/* Position in the sms buffer. */
151 	uint8_t sms_buffer_pos;
152 	/* Ftp related variables. */
153 	struct {
154 		/* User buffer for ftp data. */
155 		char *read_buffer;
156 		/* Length of the read buffer/number of bytes read. */
157 		size_t nread;
158 		/* State of the ftp connection. */
159 		enum sim7080_ftp_connection_state state;
160 	} ftp;
161 	/*
162 	 * Semaphore(s).
163 	 */
164 	struct k_sem sem_response;
165 	struct k_sem sem_tx_ready;
166 	struct k_sem sem_dns;
167 	struct k_sem sem_ftp;
168 };
169 
170 /*
171  * Socket read callback data.
172  */
173 struct socket_read_data {
174 	char *recv_buf;
175 	size_t recv_buf_len;
176 	struct sockaddr *recv_addr;
177 	uint16_t recv_read_len;
178 };
179 
180 #endif /* SIMCOM_SIM7080_H */
181