WifiSmartConfig.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import random
  2. import os
  3. import time
  4. import copy
  5. from TCAction import TCActionBase
  6. from NativeLog import NativeLog
  7. from Utility import Encoding
  8. from Utility import MakeFolder
  9. AP_PROP = ("ssid", "ssid_len", "pwd",
  10. "pwd_len", "channel", "enc", "apc")
  11. SMART_TYPE = ("esp-touch", "airkiss")
  12. TEST_METHOD = ("ssid_broadcast", "ssid_hidden")
  13. HT = ("ht20", "ht40")
  14. TEST_STAT = ("total count", "fail count", "total time", "longest time")
  15. _TEST_STAT_INIT_DICT = {"total count": 0,
  16. "fail count": 0,
  17. "total time": 0,
  18. "longest time": 0,
  19. }
  20. LOG_FOLDER = os.path.join("Performance", "SmartConfig")
  21. SSID_LEN_RANGE = (1, 32) # in bytes
  22. ENC_TYPE = (0, 2, 3, 4) # do not support WEP for 8266 soft AP
  23. PWD_RANGE = {0: [0, 0],
  24. 1: [5, 5],
  25. 2: [8, 32],
  26. 3: [8, 32],
  27. 4: [8, 32],
  28. }
  29. class TestCase(TCActionBase.CommonTCActionBase):
  30. def __init__(self, test_case, test_env, timeout=30, log_path=TCActionBase.LOG_PATH):
  31. TCActionBase.CommonTCActionBase.__init__(self, test_case, test_env, timeout=timeout, log_path=log_path)
  32. self.performance_folder_path = log_path
  33. # default value for optional configurable params
  34. self.test_method = ["ssid_hidden", "ssid_broadcast"]
  35. self.bssid = "ff:ff:ff:ff:ff:ff"
  36. self.ht_ap = dict(zip(HT, [("<ht20_ap_ssid>", "<ht20_ap_password>"),
  37. ("<ht40_ap_ssid>", "<ht40_ap_password>")]))
  38. self.ap_channel = {"ht20": 1, "ht40": 1}
  39. self.delay_time = 3 # default 3s, wait for scan done
  40. # load param from excel
  41. cmd_set = test_case["cmd set"]
  42. for i in range(1, len(cmd_set)):
  43. if cmd_set[i][0] != "dummy" and cmd_set[i][0] != "":
  44. cmd_string = "self." + cmd_set[i][0]
  45. exec cmd_string
  46. folder_path = MakeFolder.make_folder(self.performance_folder_path + "\\" + LOG_FOLDER)
  47. file_name = "SmartConfig_log_%s.log" % (time.strftime("%m%d%H%M%S", time.localtime()))
  48. self._performance_log_file = os.path.join(folder_path, file_name)
  49. # type
  50. self.target_type = ["SSC" if test_env.get_port_by_name("AT1") is None else "AT"]
  51. self.target_type.append("SSC" if test_env.get_port_by_name("AT2") is None else "AT")
  52. # test statistics
  53. # better ways to create?
  54. _test_stat = dict.fromkeys(TEST_STAT, 0)
  55. _test_method = dict.fromkeys(TEST_METHOD)
  56. _test_ht = dict.fromkeys(HT)
  57. self.test_stat = dict.fromkeys(SMART_TYPE)
  58. for i in SMART_TYPE:
  59. self.test_stat[i] = copy.deepcopy(_test_ht)
  60. for j in HT:
  61. self.test_stat[i][j] = copy.deepcopy(_test_method)
  62. for k in TEST_METHOD:
  63. self.test_stat[i][j][k] = copy.deepcopy(_test_stat)
  64. self.result_cntx = TCActionBase.ResultCheckContext(self, test_env, self.tc_name)
  65. pass
  66. def _generate_random_ap_prop(self, ht_type):
  67. ap_prop = dict.fromkeys(AP_PROP)
  68. # generate target ap_value
  69. ap_prop["ssid_len"] = random.randint(SSID_LEN_RANGE[0], SSID_LEN_RANGE[1])
  70. ap_prop["channel"] = self.ap_channel[ht_type]
  71. ap_prop["enc"] = random.choice(ENC_TYPE)
  72. ap_prop["pwd_len"] = random.randint(PWD_RANGE[ap_prop["enc"]][0], PWD_RANGE[ap_prop["enc"]][1])
  73. ap_prop["ssid"] = Encoding.generate_random_printable_str(ap_prop["ssid_len"])
  74. ap_prop["pwd"] = Encoding.generate_random_printable_str(ap_prop["pwd_len"])
  75. return ap_prop
  76. def _logging_performance(self, time_cost, ssid, password, smart_type, test_method, ht_type):
  77. # update test statistics
  78. stat = self.test_stat[smart_type][ht_type][test_method]
  79. stat["total count"] += 1
  80. # log performance to performance log file
  81. with open(self._performance_log_file, "ab+") as f:
  82. # log time and ssid
  83. if time_cost is not False:
  84. time_tmp = float(time_cost)/10
  85. f.write("\r\n[%s]:\r\n[Succeed] [%.2f]\r\n" %
  86. (time.strftime("%m-%d %H:%M:%S", time.localtime()), time_tmp))
  87. stat["total time"] += time_tmp
  88. stat["longest time"] = time_tmp if time_tmp > stat["longest time"] else stat["longest time"]
  89. else:
  90. f.write("\r\n[%s]:\r\n[Fail]\r\n" %
  91. time.strftime("%m-%d %H:%M:%S", time.localtime()))
  92. stat["fail count"] += 1
  93. f.write("[%s] [%s] [%s]\r\n" %
  94. (smart_type, test_method, ht_type))
  95. f.write("[ssid] %s \r\n[password] %s\r\n" %
  96. (ssid, password))
  97. pass
  98. def _generate_performance_report(self):
  99. with open(self._performance_log_file, "ab+") as f:
  100. for i in SMART_TYPE:
  101. for j in HT:
  102. for k in TEST_METHOD:
  103. stat = self.test_stat[i][j][k]
  104. f.write("\r\n[Test report] [%s] [%s] [%s]\r\n" % (i, j, k))
  105. if stat["total count"] > 0:
  106. f.write("[Total]: %d\r\n" % stat["total count"])
  107. f.write("[Failed]: %d\r\n" % stat["fail count"])
  108. f.write("[Fail ratio]: %.2f%%\r\n" %
  109. (float(stat["fail count"])/stat["total count"] * 100))
  110. f.write("[Longest time cost]: %.2f\r\n" % stat["longest time"])
  111. if (stat["total count"] - stat["fail count"]) > 0:
  112. f.write("[Average time cost]: %.2f\r\n" %
  113. (stat["total time"]/(stat["total count"]-stat["fail count"])))
  114. @staticmethod
  115. def cmd_exception_catcher(e):
  116. raise e
  117. pass
  118. def execute(self):
  119. TCActionBase.TCActionBase.execute(self)
  120. self.result_cntx.start()
  121. # mandatory configurable params
  122. try:
  123. test_count = self.test_count
  124. delay_time = self.delay_time
  125. except StandardError, e:
  126. NativeLog.add_trace_critical("Error configuration for WifiJAP script, error is %s" % e)
  127. raise StandardError("Error configuration")
  128. # step 0 : set AT1 mode
  129. fail_string = "Fail to restore init condition"
  130. if self.target_type[0] == "AT":
  131. cmd = ["ATS AT1 AT+CWMODE=1"]
  132. checker_stings = ["R AT1 L OK"]
  133. else:
  134. cmd = ["SSC SSC1 op -S -o 1"]
  135. checker_stings = ["R SSC1 C +MODE:OK"]
  136. if self.target_type[1] == "AT":
  137. cmd.append("ATS AT2 AT+CWMODE=2")
  138. checker_stings.append("R AT2 L OK")
  139. else:
  140. cmd.append("SSC SSC2 op -S -o 2")
  141. checker_stings.append("R SSC2 C +MODE:OK")
  142. if self.load_and_exe_one_step(checker_stings, cmd,
  143. fail_string, check_time=150) is False:
  144. NativeLog.add_trace_critical(fail_string)
  145. return
  146. for i in xrange(test_count):
  147. _method = random.choice(self.test_method)
  148. _ht = random.choice(self.ht)
  149. _ap_prop = self._generate_random_ap_prop(_ht)
  150. _smart_type = random.choice(self.smart_type)
  151. _ht_ap = self.ht_ap[_ht]
  152. is_hidden = 0 if _method == "ssid_broadcast" else 1
  153. # get ip and
  154. # step 1 : restore init condition
  155. fail_string = "Fail to restore init condition"
  156. if self.target_type[0] == "AT":
  157. cmd = ["ATS AT1 AT+CWSTOPSMART", "WIFI <pc_wifi_nic> CONN %s %s <pc_ip_sc>" % (_ht_ap[0], _ht_ap[1])]
  158. checker_stings = ["P AT1 L OK", "P PC_COM L OK"]
  159. else:
  160. cmd = ["SSC SSC1 smart -E", "WIFI <pc_wifi_nic> CONN %s %s <pc_ip_sc>" % (_ht_ap[0], _ht_ap[1])]
  161. checker_stings = ["P SSC1 C +SC:OK", "P PC_COM L OK"]
  162. if self.load_and_exe_one_step(checker_stings, cmd,
  163. fail_string, check_time=200) is False:
  164. NativeLog.add_trace_critical(fail_string)
  165. continue
  166. NativeLog.add_prompt_trace("Step1 Done")
  167. # step 2 : test method is ssid_broadcast, then set AP on target 2
  168. if _method == "ssid_broadcast":
  169. fail_string = "Fail to set AP"
  170. if self.target_type[1] == "AT":
  171. cmd = ["ATS AT2 AT+CWSAP=\"%s\",\"%s\",%d,%d" % (_ap_prop["ssid"], _ap_prop["pwd"],
  172. _ap_prop["channel"], _ap_prop["enc"])]
  173. checker_stings = ["R AT2 L OK"]
  174. else:
  175. cmd = ["SSC SSC2 ap -S -s %s -p %s -n %d -t %d" % (_ap_prop["ssid"], _ap_prop["pwd"],
  176. _ap_prop["channel"], _ap_prop["enc"])]
  177. checker_stings = ["R SSC2 C +SAP:OK"]
  178. if self.load_and_exe_one_step(checker_stings, cmd,
  179. fail_string, check_time=50) is False:
  180. NativeLog.add_trace_critical(fail_string)
  181. continue
  182. NativeLog.add_prompt_trace("Step2 Done")
  183. # step 3 : start SMART
  184. fail_string = "Fail to start smart config"
  185. if self.target_type[0] == "AT":
  186. cmd = ["ATS AT1 AT+CWSTARTSMART"]
  187. checker_stings = ["R AT1 L OK"]
  188. else:
  189. cmd = ["SSC SSC1 smart -S -a 0"]
  190. checker_stings = ["R SSC1 C +SC:OK"]
  191. if self.load_and_exe_one_step(checker_stings, cmd,
  192. fail_string, check_time=50) is False:
  193. NativeLog.add_trace_critical(fail_string)
  194. continue
  195. # sleep for delay_time seconds to wait scan done or simulate delay config situation
  196. time.sleep(delay_time)
  197. NativeLog.add_prompt_trace("Step3 Done")
  198. # step 4 : do smart config
  199. fail_string = "Fail in smart config"
  200. cmd = ["SMART %s <pc_ip_sc> %s %s %s %d"
  201. % (_smart_type, _ap_prop["ssid"], _ap_prop["pwd"], self.bssid, is_hidden)]
  202. if self.target_type[0] == "AT":
  203. checker_stings = ["P AT1 C Smart%20get%20wifi%20info",
  204. "P LOG1 C %s C %s" % (_ap_prop["ssid"], _ap_prop["pwd"])]
  205. else:
  206. checker_stings = ["P SSC1 C %s C %s" % (_ap_prop["ssid"], _ap_prop["pwd"])]
  207. try:
  208. time_cost = self.load_and_exe_one_step(checker_stings, cmd,
  209. fail_string, check_time=400,
  210. cmd_exception_catcher=self.cmd_exception_catcher)
  211. except StandardError:
  212. NativeLog.add_prompt_trace("Exception occurred during executing cmd")
  213. continue
  214. pass
  215. self._logging_performance(time_cost, _ap_prop["ssid"], _ap_prop["pwd"],
  216. _smart_type, _method, _ht)
  217. if time_cost is False:
  218. NativeLog.add_prompt_trace(fail_string)
  219. continue
  220. # continue to next loop
  221. NativeLog.add_prompt_trace("[WifiSmartConfig] Test count %d done" % i)
  222. # generate report and cleanup
  223. self._generate_performance_report()
  224. self.result_cntx.set_result("Succeed")
  225. def result_check(self, port_name, data):
  226. TCActionBase.CommonTCActionBase.result_check(self, port_name, data)
  227. self.result_cntx.append_data(port_name, data)
  228. def main():
  229. pass
  230. if __name__ == '__main__':
  231. main()