WifiConnUtility.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. from NativeLog import NativeLog
  2. import time
  3. import random
  4. ERROR_AP_PROP = {"ssid": "123456789012345678901234567890",
  5. "ssid_len": 30,
  6. "pwd": "12345678901234567890",
  7. "pwd_len": 20,
  8. "channel": 10,
  9. "enc": 3,
  10. "apc": 9, # invalid apc count
  11. }
  12. class WifiConnUtilError(StandardError):
  13. pass
  14. class WifiConnUtility(object):
  15. def __init__(self, tc_action):
  16. self.tc_action = tc_action
  17. self.target_type = tc_action.target_type
  18. pass
  19. def set_mode(self, mode):
  20. ret = True
  21. fail_string = "set mode fail"
  22. cmd = []
  23. checker_stings = []
  24. for i in range(2):
  25. if self.target_type[0] == "SSC":
  26. cmd.append("SSCC SSC%d op -S -o %d" % (i+1, mode[i]))
  27. checker_stings.append("SSCP SSC%d C +MODE:OK" % (i+1))
  28. pass
  29. else:
  30. cmd.append("ATC AT%d CWMODE %d" % (i+1, mode[i]))
  31. checker_stings.append("ATP AT%d L OK" % (i+1))
  32. pass
  33. if self.tc_action.load_and_exe_one_step(checker_stings, cmd,
  34. fail_string, check_time=50) is False:
  35. NativeLog.add_trace_critical("Failed to set mode")
  36. ret = False
  37. return ret
  38. pass
  39. def _apc_switch(self, outlet_list, action_list):
  40. checker_stings = ["R PC_COM C OK"]
  41. switch_cmd = "APC <APC1>"
  42. fail_string = "Error when switching APC"
  43. ret = True
  44. for [_outlet, _action] in zip(action_list, outlet_list):
  45. switch_cmd += " %s %d" % (_action, _outlet)
  46. if self.tc_action.load_and_exe_one_step(checker_stings, [switch_cmd],
  47. fail_string, check_time=50) is False:
  48. NativeLog.add_trace_critical("Error when switching APC")
  49. ret = False
  50. return ret
  51. pass
  52. def _set_target_ap(self, ap_prop):
  53. ret = True
  54. fail_string = "set target ap fail, %s, %s" % (ap_prop["ssid"], ap_prop["pwd"])
  55. if self.target_type[1] == "SSC":
  56. if ap_prop["pwd"] == "":
  57. cmd = ["SSCC SSC2 ap -S -s %s -t %d" % (ap_prop["ssid"],
  58. ap_prop["enc"])
  59. ]
  60. else:
  61. cmd = ["SSCC SSC2 ap -S -s %s -p %s -t %d" % (ap_prop["ssid"],
  62. ap_prop["pwd"],
  63. ap_prop["enc"])
  64. ]
  65. checker_stings = ["SSCP SSC2 C +SAP:OK"]
  66. pass
  67. else:
  68. cmd = ["ATC AT2 CWSAP \"%s\" \"%s\" %d %d" % (ap_prop["ssid"],
  69. ap_prop["pwd"],
  70. ap_prop["channel"],
  71. ap_prop["enc"])
  72. ]
  73. checker_stings = ["ATR AT2 L OK"]
  74. pass
  75. if self.tc_action.load_and_exe_one_step(checker_stings, cmd,
  76. fail_string, check_time=50) is False:
  77. NativeLog.add_trace_critical("set target ap fail")
  78. ret = False
  79. return ret
  80. pass
  81. def setup_ap(self, ap_type, ap_prop):
  82. if ap_type == "target":
  83. ret = self._set_target_ap(ap_prop)
  84. pass
  85. else:
  86. ret = self._apc_switch(["ON"], [ap_prop["apc"]])
  87. # delay for 5 seconds, wait AP ready
  88. time.sleep(5)
  89. pass
  90. return ret
  91. def do_scan(self, ap_prop):
  92. fail_string = "Scan fail"
  93. ret = True
  94. # do not check if the set AP can be scanned
  95. if self.target_type[1] == "SSC":
  96. cmd = ["SSCC SSC1 sta -S"]
  97. checker_stings = ["SSCR SSC1 C +SCANDONE"]
  98. pass
  99. else:
  100. cmd = ["ATS AT1 AT+CWLAP"]
  101. checker_stings = ["ATR AT1 L OK"]
  102. pass
  103. if self.tc_action.load_and_exe_one_step(checker_stings, cmd,
  104. fail_string, check_time=100) is False:
  105. NativeLog.add_trace_critical("Scan fail")
  106. ret = False
  107. return ret
  108. pass
  109. def _switch_off_target_ap(self, delay):
  110. time.sleep(delay)
  111. self._set_target_ap(ERROR_AP_PROP)
  112. pass
  113. def _switch_on_target_ap(self, ap_prop, delay):
  114. time.sleep(delay)
  115. self._set_target_ap(ap_prop)
  116. pass
  117. def _switch_off_ap(self, ap_type, ap_prop, delay_range):
  118. delay = random.randint(delay_range[0]*10, delay_range[1]*10)/10
  119. if ap_type == "target":
  120. self._switch_off_target_ap(delay)
  121. else:
  122. delay -= 1.5
  123. time.sleep(delay if delay > 0 else 0)
  124. self._apc_switch(["OFF"], [ap_prop["apc"]])
  125. pass
  126. def _switch_on_ap(self, ap_type, ap_prop, delay_range):
  127. delay = random.randint(delay_range[0]*10, delay_range[1]*10)/10
  128. if ap_type == "target":
  129. self._switch_on_target_ap(ap_prop, delay)
  130. else:
  131. delay -= 1.5
  132. time.sleep(delay if delay > 0 else 0)
  133. self._apc_switch(["ON"], [ap_prop["apc"]])
  134. pass
  135. def _join_ap(self, ap_prop, test_method):
  136. fail_string = "join target ap fail, %s, %s" % (ap_prop["ssid"], ap_prop["pwd"])
  137. if self.target_type[1] == "SSC":
  138. cmd = ["SSCC SSC1 ap -C -s %s -p %s" % (ap_prop["ssid"],
  139. ap_prop["pwd"],)
  140. ]
  141. checker_stings = ["SSCR SSC1 C +JAP:CONNECTED"]
  142. pass
  143. else:
  144. cmd = ["ATC AT1 CWJAP \"%s\" \"%s\"" % (ap_prop["ssid"],
  145. ap_prop["pwd"])
  146. ]
  147. checker_stings = ["ATR AT1 NC ERROR NC FAIL L OK"]
  148. pass
  149. if test_method == "Normal":
  150. ret = self.tc_action.load_and_exe_one_step(checker_stings, cmd,
  151. fail_string, check_freq=0.1, check_time=350)
  152. if ret is not False:
  153. ret *= 0.1
  154. else:
  155. ret = self.tc_action.load_and_exe_one_step([], cmd, fail_string)
  156. return ret
  157. pass
  158. def _check_join_ap_result(self, ap_prop):
  159. ret = False
  160. fail_string = "join ap fail, %s, %s" % (ap_prop["ssid"], ap_prop["pwd"])
  161. if self.target_type[1] == "SSC":
  162. checker_stings = ["SSCR SSC1 C +JAP:CONNECTED"]
  163. ret = self.tc_action.load_and_exe_one_step(checker_stings, ["DELAY 0"],
  164. fail_string, check_freq=1, check_time=120)
  165. pass
  166. else:
  167. cmd = ["ATS AT1 AT+CWJAP?"]
  168. checker_stings = ["ATR AT1 NC busy NC No%20AP C +CWJAP"]
  169. for i in range(3):
  170. ret = self.tc_action.load_and_exe_one_step(checker_stings, cmd,
  171. fail_string, check_freq=1, check_time=2)
  172. if ret is not False:
  173. break
  174. time.sleep(15)
  175. return ret
  176. pass
  177. def join_ap(self, join_test_method, ap_type, ap_prop, delay):
  178. if join_test_method == "WRONG_PROP":
  179. _prop = ERROR_AP_PROP
  180. else:
  181. _prop = ap_prop
  182. ret = self._join_ap(_prop, join_test_method)
  183. if join_test_method == "OFF_ON":
  184. self._switch_off_ap(ap_type, ap_prop, delay[0])
  185. self._switch_on_ap(ap_type, ap_prop, delay[1])
  186. ret = self._check_join_ap_result(_prop)
  187. pass
  188. elif join_test_method == "OFF":
  189. self._switch_off_ap(ap_type, ap_prop, delay[0])
  190. time.sleep(25)
  191. pass
  192. return ret
  193. pass
  194. def do_reconnect(self, reconnect_test_method, ap_type, ap_prop, delay):
  195. ret = True
  196. if reconnect_test_method == "OFF_ON":
  197. self._switch_off_ap(ap_type, ap_prop, delay[0])
  198. self._switch_on_ap(ap_type, ap_prop, delay[1])
  199. ret = self._check_join_ap_result(ap_prop)
  200. pass
  201. elif reconnect_test_method == "OFF":
  202. self._switch_off_ap(ap_type, ap_prop, delay[0])
  203. pass
  204. return ret
  205. pass
  206. def main():
  207. pass
  208. if __name__ == '__main__':
  209. main()