1 /**
2 * Copyright (c) 2018 Linaro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #ifndef ZEPHYR_DRIVERS_WIFI_ESWIFI_ESWIFI_H_
8 #define ZEPHYR_DRIVERS_WIFI_ESWIFI_ESWIFI_H_
9
10 #include <zephyr/kernel.h>
11 #include <stdio.h>
12 #include <zephyr/kernel_structs.h>
13 #include <zephyr/drivers/gpio.h>
14
15 #include <zephyr/net/wifi_mgmt.h>
16
17 #include "eswifi_offload.h"
18
19 #define MAX_DATA_SIZE 1600
20
21 #define AT_OK_STR "\r\nOK\r\n> "
22 #define AT_OK_STR_LEN 8
23 #define AT_RSP_DELIMITER "\r\n"
24 #define AT_RSP_DELIMITER_LEN 2
25
26 enum eswifi_security_type {
27 ESWIFI_SEC_OPEN,
28 ESWIFI_SEC_WEP,
29 ESWIFI_SEC_WPA,
30 ESWIFI_SEC_WPA2_AES,
31 ESWIFI_SEC_WPA2_MIXED,
32 ESWIFI_SEC_MAX
33 };
34
35 enum eswifi_request {
36 ESWIFI_REQ_SCAN,
37 ESWIFI_REQ_CONNECT,
38 ESWIFI_REQ_DISCONNECT,
39 ESWIFI_REQ_NONE
40 };
41
42 enum eswifi_role {
43 ESWIFI_ROLE_CLIENT,
44 ESWIFI_ROLE_AP,
45 };
46
47 struct eswifi_sta {
48 char ssid[WIFI_SSID_MAX_LEN + 1];
49 enum eswifi_security_type security;
50 char pass[65];
51 bool connected;
52 uint8_t channel;
53 int rssi;
54 };
55
56 struct eswifi_bus_ops;
57
58 struct eswifi_cfg {
59 struct gpio_dt_spec resetn;
60 struct gpio_dt_spec wakeup;
61 };
62
63 struct eswifi_dev {
64 struct net_if *iface;
65 struct eswifi_bus_ops *bus;
66 scan_result_cb_t scan_cb;
67 struct k_work_q work_q;
68 struct k_work request_work;
69 struct k_work_delayable status_work;
70 struct eswifi_sta sta;
71 enum eswifi_request req;
72 enum eswifi_role role;
73 uint8_t mac[6];
74 char buf[MAX_DATA_SIZE];
75 struct k_mutex mutex;
76 atomic_val_t mutex_owner;
77 unsigned int mutex_depth;
78 void *bus_data;
79 struct eswifi_off_socket socket[ESWIFI_OFFLOAD_MAX_SOCKETS];
80 };
81
82 struct eswifi_bus_ops {
83 int (*init)(struct eswifi_dev *eswifi);
84 int (*request)(struct eswifi_dev *eswifi, char *cmd, size_t clen,
85 char *rsp, size_t rlen);
86 };
87
eswifi_request(struct eswifi_dev * eswifi,char * cmd,size_t clen,char * rsp,size_t rlen)88 static inline int eswifi_request(struct eswifi_dev *eswifi, char *cmd,
89 size_t clen, char *rsp, size_t rlen)
90 {
91 return eswifi->bus->request(eswifi, cmd, clen, rsp, rlen);
92 }
93
eswifi_lock(struct eswifi_dev * eswifi)94 static inline void eswifi_lock(struct eswifi_dev *eswifi)
95 {
96 /* Nested locking */
97 if (atomic_get(&eswifi->mutex_owner) != (atomic_t)(uintptr_t)_current) {
98 k_mutex_lock(&eswifi->mutex, K_FOREVER);
99 atomic_set(&eswifi->mutex_owner, (atomic_t)(uintptr_t)_current);
100 eswifi->mutex_depth = 1;
101 } else {
102 eswifi->mutex_depth++;
103 }
104 }
105
eswifi_unlock(struct eswifi_dev * eswifi)106 static inline void eswifi_unlock(struct eswifi_dev *eswifi)
107 {
108 if (!--eswifi->mutex_depth) {
109 atomic_set(&eswifi->mutex_owner, -1);
110 k_mutex_unlock(&eswifi->mutex);
111 }
112 }
113
114 int eswifi_at_cmd(struct eswifi_dev *eswifi, char *cmd);
__select_socket(struct eswifi_dev * eswifi,uint8_t idx)115 static inline int __select_socket(struct eswifi_dev *eswifi, uint8_t idx)
116 {
117 snprintk(eswifi->buf, sizeof(eswifi->buf), "P0=%d\r", idx);
118 return eswifi_at_cmd(eswifi, eswifi->buf);
119 }
120
121 static inline
eswifi_socket_to_dev(struct eswifi_off_socket * socket)122 struct eswifi_dev *eswifi_socket_to_dev(struct eswifi_off_socket *socket)
123 {
124 return CONTAINER_OF(socket - socket->index, struct eswifi_dev, socket[0]);
125 }
126
127 struct eswifi_bus_ops *eswifi_get_bus(void);
128 int eswifi_offload_init(struct eswifi_dev *eswifi);
129 struct eswifi_dev *eswifi_by_iface_idx(uint8_t iface);
130 int eswifi_at_cmd_rsp(struct eswifi_dev *eswifi, char *cmd, char **rsp);
131 void eswifi_async_msg(struct eswifi_dev *eswifi, char *msg, size_t len);
132 void eswifi_offload_async_msg(struct eswifi_dev *eswifi, char *msg, size_t len);
133 int eswifi_socket_create(int family, int type, int proto);
134
135 int eswifi_socket_type_from_zephyr(int proto, enum eswifi_transport_type *type);
136
137 int __eswifi_socket_free(struct eswifi_dev *eswifi,
138 struct eswifi_off_socket *socket);
139 int __eswifi_socket_new(struct eswifi_dev *eswifi, int family, int type,
140 int proto, void *context);
141 int __eswifi_off_start_client(struct eswifi_dev *eswifi,
142 struct eswifi_off_socket *socket);
143 int __eswifi_listen(struct eswifi_dev *eswifi, struct eswifi_off_socket *socket, int backlog);
144 int __eswifi_accept(struct eswifi_dev *eswifi, struct eswifi_off_socket *socket);
145 int __eswifi_bind(struct eswifi_dev *eswifi, struct eswifi_off_socket *socket,
146 const struct sockaddr *addr, socklen_t addrlen);
147 #if defined(CONFIG_NET_SOCKETS_OFFLOAD)
148 int eswifi_socket_offload_init(struct eswifi_dev *leswifi);
149 #endif
150
151 #if defined(CONFIG_WIFI_ESWIFI_SHELL)
152 void eswifi_shell_register(struct eswifi_dev *dev);
153 #else
154 #define eswifi_shell_register(dev)
155 #endif
156
157 #endif
158