Repository navigation
Expand file tree
/
Copy pathconnect.py
More file actions
61 lines (50 loc) · 2.01 KB
/
Copy pathconnect.py
File metadata and controls
61 lines (50 loc) · 2.01 KB
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
"""WAVEX SDK from Python (ctypes), Windows x64. Run as administrator, with wavex.dll and wintun.dll in this folder:
python connect.py phone.wavex
"""
import ctypes
import json
import os
import sys
here = os.path.dirname(os.path.abspath(__file__))
os.add_dll_directory(here)
lib = ctypes.CDLL(os.path.join(here, "wavex.dll"))
EVENT = ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_void_p)
lib.wavex_create.argtypes = [ctypes.c_char_p, ctypes.c_char_p, EVENT, ctypes.c_void_p]
lib.wavex_create.restype = ctypes.c_int64
lib.wavex_connect.argtypes = [ctypes.c_int64, ctypes.c_int]
lib.wavex_disconnect.argtypes = [ctypes.c_int64]
lib.wavex_destroy.argtypes = [ctypes.c_int64]
lib.wavex_status.argtypes = [ctypes.c_int64]
for f in (lib.wavex_status, lib.wavex_last_error, lib.wavex_version):
f.restype = ctypes.c_void_p
lib.wavex_free.argtypes = [ctypes.c_void_p]
def take(ptr) -> str:
"""A string the library returned, freed after reading."""
if not ptr:
return ""
try:
return ctypes.cast(ptr, ctypes.c_char_p).value.decode()
finally:
lib.wavex_free(ptr)
@EVENT
def on_event(event_json, _user): # kept at module level: the library calls it for the connection's lifetime
print("event:", json.loads(event_json))
def main(path: str) -> int:
print("WAVEX SDK", take(lib.wavex_version()))
with open(path, "rb") as f:
handle = lib.wavex_create(f.read(), json.dumps({"route_all": True, "device_info": "connect.py example"}).encode(), on_event, None)
if not handle:
print("create:", take(lib.wavex_last_error()))
return 1
try:
if not lib.wavex_connect(handle, 30000):
print("connect:", take(lib.wavex_last_error()))
return 1
print("status:", json.loads(take(lib.wavex_status(handle))))
input("connected; press Enter to disconnect ")
lib.wavex_disconnect(handle)
finally:
lib.wavex_destroy(handle)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1]) if len(sys.argv) > 1 else 2)