test-sap-server 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/python
  2. # SPDX-License-Identifier: LGPL-2.1-or-later
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. from sap_client import *
  5. import time
  6. import sys
  7. def connect_disconnect_by_client(sap):
  8. print("[Test] Connect - Disconnect by client \n")
  9. try:
  10. if not sap.isConnected():
  11. sap.connect()
  12. if sap.proc_connect():
  13. if sap.proc_disconnectByClient():
  14. print("OK")
  15. return 0
  16. print("NOT OK")
  17. return 1
  18. except BluetoothError as e:
  19. print("Error " + str(e))
  20. def connect_disconnect_by_server_gracefully(sap, timeout=0):
  21. print("[Test] Connect - Disconnect by server with timer \n")
  22. try:
  23. if not sap.isConnected():
  24. sap.connect()
  25. if sap.proc_connect():
  26. if sap.proc_disconnectByServer(timeout):
  27. print("OK")
  28. return 0
  29. print("NOT OK")
  30. return 1
  31. except BluetoothError as e:
  32. print("Error " + str(e))
  33. def connect_txAPDU_disconnect_by_client(sap):
  34. print("[Test] Connect - TX APDU - Disconnect by client \n")
  35. try:
  36. if not sap.isConnected():
  37. sap.connect()
  38. if sap.proc_connect():
  39. if not sap.proc_transferAPDU():
  40. print("NOT OK 1")
  41. return 1
  42. if not sap.proc_transferAPDU():
  43. print("NOT OK 2")
  44. return 1
  45. if not sap.proc_transferAPDU():
  46. print("NOT OK 3")
  47. return 1
  48. if not sap.proc_transferAPDU():
  49. print("NOT OK 4")
  50. return 1
  51. if sap.proc_disconnectByClient():
  52. print("OK")
  53. return 0
  54. print("NOT OK")
  55. return 1
  56. except BluetoothError as e:
  57. print("Error " + str(e))
  58. def connect_rfcomm_only_and_wait_for_close_by_server(sap):
  59. print("[Test] Connect rfcomm only - Disconnect by server timeout \n")
  60. if not sap.isConnected():
  61. sap.connect()
  62. time.sleep(40)
  63. print("OK")
  64. def power_sim_off_on(sap):
  65. print("[Test] Powe sim off \n")
  66. try:
  67. if not sap.isConnected():
  68. sap.connect()
  69. if sap.proc_connect():
  70. if not sap.proc_resetSim():
  71. print("NOT OK")
  72. return 1
  73. if not sap.proc_powerSimOff():
  74. print("NOT OK")
  75. return 1
  76. if not sap.proc_powerSimOn():
  77. print("NOT OK")
  78. return 1
  79. if sap.proc_disconnectByClient():
  80. print("OK")
  81. return 0
  82. print("NOT OK")
  83. return 1
  84. except BluetoothError as e:
  85. print("Error " + str(e))
  86. if __name__ == "__main__":
  87. host = None # server bd_addr
  88. port = 8 # sap server port
  89. if (len(sys.argv) < 2):
  90. print("Usage: %s <address> [port]" % (sys.argv[0]))
  91. sys.exit(1)
  92. host = sys.argv[1]
  93. if (len(sys.argv) == 3):
  94. port = sys.argv[2]
  95. try:
  96. s = SAPClient(host, port)
  97. except BluetoothError as e:
  98. print("Error: " + str(e))
  99. sys.exit(1)
  100. connect_disconnect_by_client(s)
  101. connect_disconnect_by_server_gracefully(s)
  102. connect_disconnect_by_server_gracefully(s, 40) # wait 40 sec for srv to close rfcomm sock
  103. connect_rfcomm_only_and_wait_for_close_by_server(s)
  104. connect_txAPDU_disconnect_by_client(s)
  105. power_sim_off_on(s)