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