aboutsummaryrefslogtreecommitdiff
path: root/Marlin/src/lcd/extui/lib/mks_ui/wifi_module.h
blob: 856c6196fe346ae8e479c23d3d1cfe98f0858ef9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
 * Marlin 3D Printer Firmware
 * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
 *
 * Based on Sprinter and grbl.
 * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */
#pragma once

#ifdef __cplusplus
  extern "C" { /* C-declarations for C++ */
#endif

#include "../../../../inc/MarlinConfigPre.h"

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define UART_RX_BUFFER_SIZE   1024
#define UART_FIFO_BUFFER_SIZE 1024

#define WIFI_DECODE_TYPE      1

#define IP_DHCP_FLAG          1

#define WIFI_AP_NAME          "TP-LINK_MKS"
#define WIFI_KEY_CODE         "makerbase"

#define IP_ADDR               "192.168.3.100"
#define IP_MASK               "255.255.255.0"
#define IP_GATE               "192.168.3.1"
#define IP_DNS                "192.168.3.1"

#define AP_IP_DHCP_FLAG       1
#define AP_IP_ADDR            "192.168.3.100"
#define AP_IP_MASK            "255.255.255.0"
#define AP_IP_GATE            "192.168.3.1"
#define AP_IP_DNS             "192.168.3.1"
#define IP_START_IP           "192.168.3.1"
#define IP_END_IP             "192.168.3.255"

#define UDISKBUFLEN           1024

typedef enum{
  udisk_buf_empty = 0,
  udisk_buf_full,
} UDISK_DATA_BUFFER_STATE;

#define TRANS_RCV_FIFO_BLOCK_NUM  14

typedef struct {
  bool receiveEspData;
  unsigned char *bufferAddr[TRANS_RCV_FIFO_BLOCK_NUM];
  unsigned char *p;
  UDISK_DATA_BUFFER_STATE state[TRANS_RCV_FIFO_BLOCK_NUM];
  unsigned char read_cur;
  unsigned char write_cur;
} WIFI_DMA_RCV_FIFO;

typedef struct {
  uint8_t flag; // 0x0: no error;  0x01: error
  uint32_t start_tick; //error start time
  uint32_t now_tick;
} WIFI_TRANS_ERROR;

extern volatile WIFI_TRANS_ERROR wifiTransError;

typedef struct {
  char ap_name[32];   //wifi-name
  char keyCode[64]; //wifi password
  int  decodeType;
  int  baud;
  int  mode;
} WIFI_PARA;

typedef struct {
  char state;
  char hostUrl[96];
  int  port;
  char id[21];
} CLOUD_PARA;

typedef struct {
  char  dhcp_flag;
  char  ip_addr[16];
  char  mask[16];
  char  gate[16];
  char  dns[16];

  char  dhcpd_flag;
  char  dhcpd_ip[16];
  char  dhcpd_mask[16];
  char  dhcpd_gate[16];
  char  dhcpd_dns[16];
  char  start_ip_addr[16];
  char  end_ip_addr[16];
} IP_PARA;

typedef enum {
  WIFI_NOT_CONFIG,
  WIFI_CONFIG_MODE,
  WIFI_CONFIG_DHCP,
  WIFI_CONFIG_AP,
  WIFI_CONFIG_IP_INF,
  WIFI_CONFIG_DNS,
  WIFI_CONFIG_TCP,
  WIFI_CONFIG_SERVER,
  WIFI_CONFIG_REMOTE_PORT,
  WIFI_CONFIG_BAUD,
  WIFI_CONFIG_COMMINT,
  WIFI_CONFIG_OK,
  WIFI_GET_IP_OK,
  WIFI_RECONN,
  WIFI_CONNECTED,
  WIFI_WAIT_TRANS_START,
  WIFI_TRANS_FILE,
  WIFI_CONFIG_DHCPD,
  WIFI_COFIG_DHCPD_IP,
  WIFI_COFIG_DHCPD_DNS,
  WIFI_EXCEPTION,
} WIFI_STATE;

typedef enum {
  TRANSFER_IDLE,
  TRANSFERING,
  TRANSFER_STORE,
} TRANSFER_STATE;
extern volatile TRANSFER_STATE esp_state;

typedef struct {
  char buf[20][80];
  int  rd_index;
  int  wt_index;
} QUEUE;

typedef enum {
  WIFI_PARA_SET,      // 0x0:net parameter
  WIFI_PRINT_INF,     // 0x1:print message
  WIFI_TRANS_INF,     // 0x2:Pass through information
  WIFI_EXCEP_INF,     // 0x3:Exception information
  WIFI_CLOUD_CFG,     // 0x4:cloud config
  WIFI_CLOUD_UNBIND,  // 0x5:Unbind ID
} WIFI_RET_TYPE;

typedef struct {
  uint32_t uart_read_point;
  uint32_t uart_write_point;
  //uint8_t uartTxBuffer[UART_FIFO_BUFFER_SIZE];
} SZ_USART_FIFO;

#define WIFI_GCODE_BUFFER_LEAST_SIZE    96
#define WIFI_GCODE_BUFFER_SIZE  (WIFI_GCODE_BUFFER_LEAST_SIZE * 3)
typedef struct {
  uint8_t wait_tick;
  uint8_t Buffer[WIFI_GCODE_BUFFER_SIZE];
  uint32_t r;
  uint32_t w;
} WIFI_GCODE_BUFFER;

extern volatile WIFI_STATE wifi_link_state;
extern WIFI_PARA wifiPara;
extern IP_PARA ipPara;
extern CLOUD_PARA cloud_para;

extern WIFI_GCODE_BUFFER espGcodeFifo;

extern uint32_t  getWifiTick();
extern uint32_t  getWifiTickDiff(int32_t lastTick, int32_t curTick);

extern void mks_esp_wifi_init();
extern int  cfg_cloud_flag;
extern int  send_to_wifi(uint8_t *buf, int len);
extern void wifi_looping();
extern int  raw_send_to_wifi(uint8_t *buf, int len);
extern int  package_to_wifi(WIFI_RET_TYPE type, uint8_t *buf, int len);
extern void get_wifi_list_command_send();
extern void get_wifi_commands();
extern int  readWifiBuf(int8_t *buf, int32_t len);
extern void mks_wifi_firmware_upddate();
extern int usartFifoAvailable(SZ_USART_FIFO *fifo);
extern int readUsartFifo(SZ_USART_FIFO *fifo, int8_t *buf, int32_t len);
extern void esp_port_begin(uint8_t interrupt);

#ifdef __cplusplus
  } /* C-declarations for C++ */
#endif