1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14+ #
15+ # ************************************************************** #
16+ # WHEN WHO WHAT, WHERE, WHY
17+ # ---------- ----------- -------------------------- #
18+ # 23/01/2024 Francis Add timeout handling
19+ # ************************************************************** #
1420
1521import usocket
1622import log
2127from queue import Queue
2228
2329# 设置日志输出级别
24- log .basicConfig (level = log .INFO )
25- ESP8266_log = log .getLogger ("ESP8266" )
30+ WLAN_log = log .getLogger ("WiFi" )
31+ # 打开日志打印
32+ #log.basicConfig(level=log.INFO)
33+
34+ class RET_CODE ():
35+ RET_SUCCESS_CODE = 0
36+ RET_RESPONSE_ERROR_CODE = - 1
37+ RET_SYS_ERROR_CODE = - 2
38+ RET_PARAM_ERROR_CODE = - 3
39+ RET_TIMEOUT_CODE = - 4 # wait respose timeout
40+ RET_NOT_SUPOORT_CODE = - 5 # this function not support now
41+ RET_NAT_NOT_OPEN_CODE = - 6 # nat not open, if this error,you should restart device.
2642
2743
2844class ESP8266 :
@@ -37,17 +53,17 @@ def __init__(self, uart=UART.UART2, mode=STA, callback=None):
3753 self .__callback = callback
3854 self .__value = None
3955 self .__wait_resp = 0
56+ self .__send_time = 0
4057 slip .destroy ()
4158
4259 ret = slip .construct (self .__uart , self .__mode , 0 )
43- if ret != 0 :
44- #print('slip netif construct fail' )
45- return None
46- #print('slip network card create success' )
60+ if ret == RET_CODE . RET_NAT_NOT_OPEN_CODE :
61+ return ValueError ( "nat is not open, you should restart device, and try again." )
62+ elif ret != RET_CODE . RET_SUCCESS_CODE :
63+ raise ValueError ( "slip init failure" )
4764 self .__queue = Queue (1 )
4865 self .__sock = self .__socket_init ()
4966 self .__threadid = _thread .start_new_thread (self .__Socket_Thread , ())
50- return None
5167
5268 def __socket_init (self ):
5369 # 创建一个socket实例
@@ -61,6 +77,15 @@ def __socket_init(self):
6177
6278 def __socket_reinit (self ):
6379 return self .__socket_init ()
80+
81+ def __socket_reponse (self ):
82+ if self .__err == 1 :
83+ return RET_CODE .RET_TIMEOUT_CODE
84+ if self .__value [2 ] == 'NOT SUPPORT' :
85+ return RET_CODE .RET_NOT_SUPOORT_CODE
86+ elif self .__value [2 ] != 'OK' :
87+ return RET_CODE .RET_RESPONSE_ERROR_CODE
88+ return RET_CODE .RET_SUCCESS_CODE
6489
6590 # station模式
6691 def station (self , user , password ):
@@ -71,12 +96,9 @@ def station(self, user, password):
7196 if self .__mode == self .STA :
7297 self .__message = self .user + ',' + self .password
7398 else :
74- #print('Not match station mode')
75- return - 1
99+ return RET_CODE .RET_PARAM_ERROR_CODE
76100 self .__Socket_UDP (__head , self .__message )
77- if self .__err == 1 or self .__value [2 ] != 'OK' :
78- return - 1
79- return 0
101+ return self .__socket_reponse ()
80102
81103 #ap 模式
82104 def ap (self , user , password ):
@@ -88,12 +110,9 @@ def ap(self, user, password):
88110 if self .__mode == self .AP :
89111 self .__message = self .user + ',' + self .password
90112 else :
91- #print('Not match ap mode')
92- return - 1
113+ return RET_CODE .RET_PARAM_ERROR_CODE
93114 self .__Socket_UDP (__head , self .__message )
94- if self .__err == 1 or self .__value [2 ] != 'OK' :
95- return - 1
96- return 0
115+ return self .__socket_reponse ()
97116
98117 # web配网模式
99118 def web_config (self , user , password ):
@@ -106,9 +125,7 @@ def web_config(self, user, password):
106125 self .password = str (password )
107126 self .__message = self .user + ',' + self .password
108127 self .__Socket_UDP (__head , self .__message )
109- if self .__err == 1 or self .__value [2 ] != 'OK' :
110- return - 1
111- return 0
128+ return self .__socket_reponse ()
112129
113130 def clear_remain (self ):
114131 if self .__queue .empty () == False :
@@ -121,15 +138,15 @@ def status(self):
121138 self .clear_remain ()
122139 self .__Socket_UDP ('F1' , '0' )
123140 if self .__err == 1 :
124- return 0
141+ return RET_CODE . RET_SUCCESS_CODE
125142 return int (self .__value [2 ])
126143
127144 # 查询网卡版本信息
128145 def version (self ):
129146 self .clear_remain ()
130147 self .__Socket_UDP ('F5' , '0' )
131148 if self .__err == 1 :
132- return - 1
149+ return RET_CODE . RET_TIMEOUT_CODE
133150 return self .__value [2 ]
134151
135152 # ota升级
@@ -138,18 +155,25 @@ def ota(self, url):
138155 __head = 'F4'
139156 self .__message = str (url )
140157 self .__Socket_UDP (__head , self .__message )
141- if self .__err == 1 or self .__value [2 ] != 'OK' :
142- return - 1
143- return 0
158+ return self .__socket_reponse ()
159+
144160 # smartconfig配置
145161 def smartconfig (self , mode ):
146162 self .clear_remain ()
147163 __head = 'F6'
148164 self .__message = str (mode )
149165 self .__Socket_UDP (__head , self .__message )
150- if self .__err == 1 or self .__value [2 ] != 'OK' :
151- return - 1
152- return 0
166+ return self .__socket_reponse ()
167+
168+ # 配置查询
169+ def config (self , param ):
170+ self .clear_remain ()
171+ __head = 'F7'
172+ self .__message = str (param )
173+ self .__Socket_UDP (__head , self .__message )
174+ if self .__err == 1 :
175+ return RET_CODE .RET_TIMEOUT_CODE
176+ return self .__value [2 ]
153177
154178 #添加路由信息
155179 def router_add (self , ip = '192.168.4.1' , mask = '255.255.255.0' ):
@@ -165,26 +189,31 @@ def ipconfig(self):
165189 def set_dns (self , pri_dns = '8.8.8.8' , sec_dns = '114.114.114.114' ):
166190 self .pri_dns = pri_dns
167191 self .sec_dns = sec_dns
168- return slip .set_dns (self .pri_dns ,self .sec_dns )
192+ ret = slip .set_dns (self .pri_dns ,self .sec_dns )
193+ if ret == 0 :
194+ return RET_CODE .RET_SUCCESS_CODE
195+ return RET_CODE .RET_PARAM_ERROR_CODE
169196
170197 # 设置默认网卡
171198 def set_default_NIC (self , ip_str ):
172199 self .ip_str = ip_str
173- return slip .set_default_netif (self .ip_str )
200+ ret = slip .set_default_netif (self .ip_str )
201+ if ret == 0 :
202+ return RET_CODE .RET_SUCCESS_CODE
203+ return RET_CODE .RET_PARAM_ERROR_CODE
174204
175205 # 释放slip网卡
176206 def stop (self ):
207+ _thread .stop_thread (self .__threadid )
177208 slip .destroy ()
178- return 0
179-
209+ return RET_CODE .RET_SUCCESS_CODE
180210
181211 # 封装tlv数据包
182212 def __pack_tlv_format (self , head , content ):
183213 self .head = head
184214 self .content = content
185215 if len (self .content ) == 0 or len (self .content ) > 9999 or len (self .head ) != 2 :
186- #print('illegal tlv content')
187- return - 1
216+ return RET_CODE .RET_PARAM_ERROR_CODE
188217 len_str = '%04d' % len (self .content )
189218 tlv_pack = self .head + len_str + self .content
190219 return tlv_pack
@@ -197,8 +226,7 @@ def __unpack_tlv_format(self, msg):
197226 value = self .msg [6 :]
198227 len_str = '%04d' % len (value )
199228 if len (value ) == 0 or len (value ) > 9999 or len (tag ) != 2 or len_str != self .msg [2 :6 ]:
200- #print('illegal tlv content')
201- return - 1
229+ return RET_CODE .RET_RESPONSE_ERROR_CODE
202230 unpack = (tag ,length ,value )
203231 return unpack
204232
@@ -212,10 +240,11 @@ def __Socket_UDP(self, head, content):
212240
213241 # 向服务端发送消息
214242 server_addr = ('172.16.1.5' ,1000 )
215- #print (msg)
243+ #WLAN_log.debug (msg)
216244 self .__err = 0
217245 self .__sock .sendto (msg ,server_addr )
218246 self .__wait_resp = 1
247+ self .__send_time = utime .ticks_ms ()
219248 data = self .__queue .get ()
220249 self .__wait_resp = 0
221250 return data
@@ -227,6 +256,8 @@ def __Socket_Thread(self):
227256 data = self .__sock .recv (1024 )
228257 except Exception as recverr :
229258 if "110" in str (recverr ):
259+ if utime .ticks_diff (utime .ticks_ms (), self .__send_time ) < 9000 :
260+ continue
230261 self .__err = 1
231262 if self .__wait_resp == 1 :
232263 self .__queue .put ('0' )
@@ -235,7 +266,7 @@ def __Socket_Thread(self):
235266 self .__sock = self .__socket_reinit ()
236267 else :
237268 value = self .__unpack_tlv_format (data .decode ())
238- #print ('mode: {0}, recv {1} bytes, Data: {2}'.format (value[0],len(data), value[2]))
269+ #WLAN_log.debug ('mode: {0}, recv {1} bytes, Data: {2}'.format (value[0],len(data), value[2]))
239270 if value [0 ] == 'FF' :
240271 if self .__callback != None :
241272 self .__callback (value [2 ])
0 commit comments