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