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