summaryrefslogtreecommitdiff
path: root/test/test-sap-server
blob: ddb1efe9bc8cb684c13ea0564f261011c7b32d86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
# SPDX-License-Identifier: LGPL-2.1-or-later

from __future__ import absolute_import, print_function, unicode_literals

from sap_client import *
import time
import sys

def connect_disconnect_by_client(sap):

    print("[Test] Connect - Disconnect by client \n")

    try:
        if not sap.isConnected():
           sap.connect()

        if sap.proc_connect():
            if sap.proc_disconnectByClient():
                print("OK")
                return 0

        print("NOT OK")
        return 1

    except BluetoothError as e:
        print("Error " + str(e))


def connect_disconnect_by_server_gracefully(sap, timeout=0):

    print("[Test] Connect - Disconnect by server with timer \n")

    try:
        if not sap.isConnected():
           sap.connect()

        if sap.proc_connect():
            if sap.proc_disconnectByServer(timeout):
                print("OK")
                return 0

        print("NOT OK")
        return 1

    except BluetoothError as e:
        print("Error " + str(e))


def connect_txAPDU_disconnect_by_client(sap):

    print("[Test] Connect - TX APDU - Disconnect by client \n")

    try:
        if not sap.isConnected():
           sap.connect()

        if sap.proc_connect():
            if not sap.proc_transferAPDU():
                print("NOT OK 1")
                return 1

            if not sap.proc_transferAPDU():
                print("NOT OK 2")
                return 1

            if not sap.proc_transferAPDU():
                print("NOT OK 3")
                return 1

            if not sap.proc_transferAPDU():
                print("NOT OK 4")
                return 1

            if sap.proc_disconnectByClient():
                print("OK")
                return 0

        print("NOT OK")
        return 1

    except BluetoothError as e:
        print("Error " + str(e))

def connect_rfcomm_only_and_wait_for_close_by_server(sap):

    print("[Test] Connect rfcomm only  - Disconnect by server timeout \n")

    if not sap.isConnected():
       sap.connect()

    time.sleep(40)
    print("OK")

def power_sim_off_on(sap):

    print("[Test] Powe sim off \n")

    try:
        if not sap.isConnected():
           sap.connect()

        if sap.proc_connect():
            if not sap.proc_resetSim():
                print("NOT OK")
                return 1

            if not sap.proc_powerSimOff():
                print("NOT OK")
                return 1

            if not sap.proc_powerSimOn():
                print("NOT OK")
                return 1

            if sap.proc_disconnectByClient():
                print("OK")
                return 0

        print("NOT OK")
        return 1

    except BluetoothError as e:
        print("Error " + str(e))


if __name__ == "__main__":

    host = None  # server bd_addr
    port = 8  # sap server port

    if (len(sys.argv) < 2):
        print("Usage: %s <address> [port]" % (sys.argv[0]))
        sys.exit(1)

    host = sys.argv[1]

    if (len(sys.argv) == 3):
        port = sys.argv[2]

    try:
        s = SAPClient(host, port)
    except BluetoothError as e:
        print("Error: " + str(e))
        sys.exit(1)

    connect_disconnect_by_client(s)
    connect_disconnect_by_server_gracefully(s)
    connect_disconnect_by_server_gracefully(s, 40)  #  wait 40 sec for srv to close rfcomm sock
    connect_rfcomm_only_and_wait_for_close_by_server(s)
    connect_txAPDU_disconnect_by_client(s)
    power_sim_off_on(s)