SoftAPNSTA.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import random
  2. import time
  3. import string
  4. import threading
  5. from TCAction import TCActionBase
  6. from NativeLog import NativeLog
  7. from TCAction import PerformanceTCBase
  8. from Utility import Encoding
  9. class STAJAPThread(threading.Thread):
  10. def __init__(self, test_action, port_name, ssid, password, delay1, delay2, change_mac):
  11. threading.Thread.__init__(self)
  12. self.setDaemon(True)
  13. self.test_action = test_action
  14. self.port_name = port_name
  15. self.ssid = ssid
  16. self.password = password
  17. self.delay1 = delay1
  18. self.delay2 = delay2
  19. self.change_mac = change_mac
  20. self.exit_flag = threading.Event()
  21. pass
  22. def exit(self):
  23. self.exit_flag.set()
  24. pass
  25. def run(self):
  26. total_test_count = 0
  27. fail_count = 0
  28. while self.exit_flag.isSet() is False:
  29. # change mac
  30. if self.change_mac is True:
  31. mac = Encoding.generate_random_mac()
  32. self.test_action.serial_write_line(self.port_name, "mac -S -o 1 -m %s" % mac)
  33. self.test_action.check_response(self.port_name, "+MAC:STA,OK")
  34. time.sleep(1)
  35. # JAP
  36. total_test_count += 1
  37. # flush current port data
  38. self.test_action.flush_data(self.port_name)
  39. self.test_action.serial_write_line(self.port_name, "sta -C -s %s -p %s" % (self.ssid, self.password))
  40. if self.test_action.check_response(self.port_name, "+JAP:CONNECTED", 45) is False:
  41. fail_count += 1
  42. NativeLog.add_trace_critical("[%s] Failed to JAP, Failed/Total : %d/%d"
  43. % (self.port_name, fail_count, total_test_count))
  44. continue
  45. time.sleep(random.randint(self.delay1[0], self.delay1[1]))
  46. # QAP
  47. self.test_action.serial_write_line(self.port_name, "sta -D")
  48. if self.test_action.check_response(self.port_name, "+QAP:OK", 5) is False:
  49. NativeLog.add_trace_critical("[%s] Failed to QAP" % self.port_name)
  50. time.sleep(random.randint(self.delay2[0], self.delay2[1]))
  51. # make sure quit AP
  52. self.test_action.serial_write_line(self.port_name, "sta -D")
  53. pass
  54. pass
  55. class TestCase(PerformanceTCBase.PerformanceTCBase):
  56. def __init__(self, test_case, test_env, timeout=30, log_path=TCActionBase.LOG_PATH):
  57. PerformanceTCBase.PerformanceTCBase.__init__(self, test_case, test_env, timeout=timeout, log_path=log_path)
  58. self.sta_num = 0
  59. self.max_sta = 4
  60. self.test_time = 60
  61. self.delay1 = [5, 30]
  62. self.delay2 = [5, 10]
  63. self.change_mac = True
  64. self.channel = 11
  65. # load param from excel
  66. cmd_set = test_case["cmd set"]
  67. for i in range(1, len(cmd_set)):
  68. if cmd_set[i][0] != "dummy" and cmd_set[i][0] != "":
  69. cmd_string = "self." + cmd_set[i][0]
  70. exec cmd_string
  71. pass
  72. def process(self):
  73. # configurable parameters
  74. try:
  75. sta_num = self.sta_num
  76. max_sta = self.max_sta
  77. test_time = self.test_time
  78. # delay between JAP succeed and QAP
  79. delay1 = self.delay1
  80. # delay between QAP and JAP
  81. delay2 = self.delay2
  82. # if change mac each time before JAP
  83. change_mac = self.change_mac
  84. # channel
  85. channel = self.channel
  86. except StandardError, e:
  87. raise StandardError("miss mandatory parameters")
  88. # step 0, set op mode and enable dhcp
  89. self.serial_write_line("SSC1", "op -S -o 2")
  90. if self.check_response("SSC1", "+MODE:OK", 2) is False:
  91. NativeLog.add_trace_critical("Failed to set ap mode")
  92. return
  93. self.serial_write_line("SSC1", "dhcp -E -o 2")
  94. if self.check_response("SSC1", "+DHCP:AP,OK", 2) is False:
  95. NativeLog.add_trace_critical("Failed to enable ap dhcp")
  96. return
  97. self.serial_write_line("SSC1", "dhcp -L -s 192.168.4.2 -e 192.168.4.100 -t 1")
  98. if self.check_response("SSC1", "+DHCP:LEASE,OK", 2) is False:
  99. NativeLog.add_trace_critical("Failed to enable ap dhcp")
  100. return
  101. self.serial_write_line("SSC1", "dhcp -S -o 2")
  102. if self.check_response("SSC1", "+DHCP:AP,OK", 2) is False:
  103. NativeLog.add_trace_critical("Failed to enable ap dhcp")
  104. return
  105. for i in range(sta_num):
  106. self.serial_write_line("SSC%d" % (i+2), "op -S -o 1")
  107. if self.check_response("SSC%d" % (i+2), "+MODE:OK", 2) is False:
  108. NativeLog.add_trace_critical("Failed to set sta mode")
  109. return
  110. self.serial_write_line("SSC%d" % (i+2), "dhcp -S -o 1")
  111. if self.check_response("SSC%d" % (i+2), "+DHCP:STA,OK", 2) is False:
  112. NativeLog.add_trace_critical("Failed to enable sta dhcp")
  113. # step 1, set ap config and load
  114. ap_ssid = "".join([random.choice(string.uppercase) for m in range(15)])
  115. ap_password = "".join([random.choice(string.lowercase) for m in range(15)])
  116. self.serial_write_line("SSC1", "ap -S -s %s -p %s -t 3 -m %s -n %s"
  117. % (ap_ssid, ap_password, max_sta, channel))
  118. if self.check_response("SSC1", "+SAP:OK", 2) is False:
  119. NativeLog.add_trace_critical("Failed to set AP")
  120. return
  121. # step 2, start thread to let STA JAP
  122. sta_thread_list = []
  123. for i in range(sta_num):
  124. sta_thread_list.append(STAJAPThread(self, "SSC%d" % (i+2),
  125. ap_ssid, ap_password, delay1, delay2, change_mac))
  126. for sta_thread in sta_thread_list:
  127. sta_thread.start()
  128. # step 3, sleep for test time
  129. for i in range(test_time):
  130. self.flush_data("SSC1")
  131. time.sleep(60)
  132. # step 4, close all thread, will disconnect when exit thread
  133. for sta_thread in sta_thread_list:
  134. sta_thread.exit()
  135. for sta_thread in sta_thread_list:
  136. sta_thread.join()
  137. # wait and make sure disconnect done
  138. time.sleep(1)
  139. # step 5, join AP and check
  140. sta_num_temp = max_sta if sta_num > max_sta else sta_num
  141. for i in range(sta_num_temp):
  142. self.serial_write_line("SSC%d" % (i+2), "sta -C -s %s -p %s" % (ap_ssid, ap_password))
  143. if self.check_response("SSC%d" % (i+2), "+JAP:CONNECTED", 45) is False:
  144. self.set_result("Fail")
  145. break
  146. pass
  147. else:
  148. self.set_result("Succeed")
  149. def main():
  150. pass
  151. if __name__ == '__main__':
  152. main()