1 /*
2  * Copyright (c) 2020 Analog Life LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef QUECTEL_BG9X_H
8 #define QUECTEL_BG9X_H
9 
10 #include <zephyr/kernel.h>
11 #include <ctype.h>
12 #include <errno.h>
13 #include <zephyr/drivers/gpio.h>
14 #include <zephyr/device.h>
15 #include <zephyr/init.h>
16 
17 #include <zephyr/net/net_if.h>
18 #include <zephyr/net/offloaded_netdev.h>
19 #include <zephyr/net/net_offload.h>
20 #include <zephyr/net/socket_offload.h>
21 
22 #include "modem_context.h"
23 #include "modem_socket.h"
24 #include "modem_cmd_handler.h"
25 #include "modem_iface_uart.h"
26 
27 #define MDM_UART_NODE			  DT_INST_BUS(0)
28 #define MDM_UART_DEV			  DEVICE_DT_GET(MDM_UART_NODE)
29 #define MDM_CMD_TIMEOUT			  K_SECONDS(10)
30 #define MDM_CMD_CONN_TIMEOUT		  K_SECONDS(120)
31 #define MDM_REGISTRATION_TIMEOUT	  K_SECONDS(180)
32 #define MDM_SENDMSG_SLEEP		  K_MSEC(1)
33 #define MDM_MAX_DATA_LENGTH		  1024
34 #define MDM_RECV_MAX_BUF		  30
35 #define MDM_RECV_BUF_SIZE		  1024
36 #define MDM_MAX_SOCKETS			  5
37 #define MDM_BASE_SOCKET_NUM		  0
38 #define MDM_NETWORK_RETRY_COUNT		  10
39 #define MDM_INIT_RETRY_COUNT		  10
40 #define MDM_PDP_ACT_RETRY_COUNT		  3
41 #define MDM_WAIT_FOR_RSSI_COUNT		  10
42 #define MDM_WAIT_FOR_RSSI_DELAY		  K_SECONDS(2)
43 #define BUF_ALLOC_TIMEOUT		  K_SECONDS(1)
44 #define MDM_MAX_BOOT_TIME		  K_SECONDS(50)
45 
46 /* Default lengths of certain things. */
47 #define MDM_MANUFACTURER_LENGTH		  10
48 #define MDM_MODEL_LENGTH		  16
49 #define MDM_REVISION_LENGTH		  64
50 #define MDM_IMEI_LENGTH			  16
51 #define MDM_IMSI_LENGTH			  16
52 #define MDM_ICCID_LENGTH		  32
53 #define MDM_APN_LENGTH			  32
54 #define RSSI_TIMEOUT_SECS		  30
55 
56 #define MDM_UNSOL_RDY			CONFIG_MODEM_QUECTEL_BG9X_UNSOL_RDY
57 #define MDM_APN				  CONFIG_MODEM_QUECTEL_BG9X_APN
58 #define MDM_USERNAME			  CONFIG_MODEM_QUECTEL_BG9X_USERNAME
59 #define MDM_PASSWORD			  CONFIG_MODEM_QUECTEL_BG9X_PASSWORD
60 
61 /* Modem ATOI routine. */
62 #define ATOI(s_, value_, desc_)	  modem_atoi(s_, value_, desc_, __func__)
63 
64 /* pin settings */
65 enum mdm_control_pins {
66 	MDM_POWER = 0,
67 	MDM_RESET,
68 #if DT_INST_NODE_HAS_PROP(0, mdm_dtr_gpios)
69 	MDM_DTR,
70 #endif
71 #if DT_INST_NODE_HAS_PROP(0, mdm_wdisable_gpios)
72 	MDM_WDISABLE,
73 #endif
74 };
75 
76 /* driver data */
77 struct modem_data {
78 	struct net_if *net_iface;
79 	uint8_t mac_addr[6];
80 
81 	/* modem interface */
82 	struct modem_iface_uart_data iface_data;
83 	uint8_t iface_rb_buf[MDM_MAX_DATA_LENGTH];
84 
85 	/* modem cmds */
86 	struct modem_cmd_handler_data cmd_handler_data;
87 	uint8_t cmd_match_buf[MDM_RECV_BUF_SIZE + 1];
88 
89 	/* socket data */
90 	struct modem_socket_config socket_config;
91 	struct modem_socket sockets[MDM_MAX_SOCKETS];
92 
93 	/* RSSI work */
94 	struct k_work_delayable rssi_query_work;
95 
96 	/* modem data */
97 	char mdm_manufacturer[MDM_MANUFACTURER_LENGTH];
98 	char mdm_model[MDM_MODEL_LENGTH];
99 	char mdm_revision[MDM_REVISION_LENGTH];
100 	char mdm_imei[MDM_IMEI_LENGTH];
101 #if defined(CONFIG_MODEM_SIM_NUMBERS)
102 	char mdm_imsi[MDM_IMSI_LENGTH];
103 	char mdm_iccid[MDM_ICCID_LENGTH];
104 #endif /* #if defined(CONFIG_MODEM_SIM_NUMBERS) */
105 	int mdm_rssi;
106 
107 	/* bytes written to socket in last transaction */
108 	int sock_written;
109 
110 	/* Socket from which we are currently reading data. */
111 	int sock_fd;
112 
113 	/* Semaphore(s) */
114 	struct k_sem sem_response;
115 	struct k_sem sem_tx_ready;
116 	struct k_sem sem_sock_conn;
117 #if !DT_INST_NODE_HAS_PROP(0, mdm_reset_gpios)
118 	struct k_sem sem_pin_busy;
119 #endif
120 };
121 
122 /* Socket read callback data */
123 struct socket_read_data {
124 	char		 *recv_buf;
125 	size_t		 recv_buf_len;
126 	struct sockaddr	 *recv_addr;
127 	uint16_t	 recv_read_len;
128 };
129 
130 #endif /* QUECTEL_BG9X_H */
131