1 #!/usr/bin/python 2 3 from sap import * 4 import time 5 6 def connect_disconnect_by_client(sap): 7 8 print "[Test] Connect - Disconnect by client \n" 9 10 try: 11 if not sap.isConnected(): 12 sap.connect() 13 14 if sap.proc_connect(): 15 if sap.proc_disconnectByClient(): 16 print "OK" 17 return 0 18 19 print "NOT OK" 20 return 1 21 22 except BluetoothError , e: 23 print "Error " + str(e) 24 25 26 def connect_disconnect_by_server_gracefully(sap, timeout=0): 27 28 print "[Test] Connect - Disconnect by server with timer \n" 29 30 try: 31 if not sap.isConnected(): 32 sap.connect() 33 34 if sap.proc_connect(): 35 if sap.proc_disconnectByServer(timeout): 36 print "OK" 37 return 0 38 39 print "NOT OK" 40 return 1 41 42 except BluetoothError , e: 43 print "Error " + str(e) 44 45 46 def connect_txAPDU_disconnect_by_client(sap): 47 48 print "[Test] Connect - TX APDU - Disconnect by client \n" 49 50 try: 51 if not sap.isConnected(): 52 sap.connect() 53 54 if sap.proc_connect(): 55 if not sap.proc_transferAPDU(): 56 print "NOT OK 1" 57 return 1 58 59 if not sap.proc_transferAPDU(): 60 print "NOT OK 2" 61 return 1 62 63 if not sap.proc_transferAPDU(): 64 print "NOT OK 3" 65 return 1 66 67 if not sap.proc_transferAPDU(): 68 print "NOT OK 4" 69 return 1 70 71 if sap.proc_disconnectByClient(): 72 print "OK" 73 return 0 74 75 print "NOT OK" 76 return 1 77 78 except BluetoothError , e: 79 print "Error " + str(e) 80 81 def connect_rfcomm_only_and_wait_for_close_by_server(sap): 82 83 print "[Test] Connect rfcomm only - Disconnect by server timeout \n" 84 85 if not sap.isConnected(): 86 sap.connect() 87 88 time.sleep(40) 89 print "OK" 90 91 def power_sim_off_on(sap): 92 93 print "[Test] Powe sim off \n" 94 95 try: 96 if not sap.isConnected(): 97 sap.connect() 98 99 if sap.proc_connect(): 100 if not sap.proc_resetSim(): 101 print "NOT OK" 102 return 1 103 104 if not sap.proc_powerSimOff(): 105 print "NOT OK" 106 return 1 107 108 if not sap.proc_powerSimOn(): 109 print "NOT OK" 110 return 1 111 112 if sap.proc_disconnectByClient(): 113 print "OK" 114 return 0 115 116 print "NOT OK" 117 return 1 118 119 except BluetoothError , e: 120 print "Error " + str(e) 121 122 123 if __name__ == "__main__": 124 125 host = "00:00:00:00:00:0" # server bd_addr 126 port = 8 # sap server port 127 128 try: 129 s = SAPClient(host, port) 130 except BluetoothError , e: 131 print "Error " + str(e) 132 133 connect_disconnect_by_client(s) 134 connect_disconnect_by_server_gracefully(s) 135 connect_disconnect_by_server_gracefully(s, 40) # wait 40 sec for srv to close rfcomm sock 136 connect_rfcomm_only_and_wait_for_close_by_server(s) 137 connect_txAPDU_disconnect_by_client(s) 138 power_sim_off_on(s) 139