dbc.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. import ctypes
  4. import os
  5. DBC_UID_SIZE = 16
  6. DBC_NONCE_SIZE = 16
  7. DBC_SIG_SIZE = 32
  8. PARAM_GET_FMAX_CAP = (0x3,)
  9. PARAM_SET_FMAX_CAP = (0x4,)
  10. PARAM_GET_PWR_CAP = (0x5,)
  11. PARAM_SET_PWR_CAP = (0x6,)
  12. PARAM_GET_GFX_MODE = (0x7,)
  13. PARAM_SET_GFX_MODE = (0x8,)
  14. PARAM_GET_CURR_TEMP = (0x9,)
  15. PARAM_GET_FMAX_MAX = (0xA,)
  16. PARAM_GET_FMAX_MIN = (0xB,)
  17. PARAM_GET_SOC_PWR_MAX = (0xC,)
  18. PARAM_GET_SOC_PWR_MIN = (0xD,)
  19. PARAM_GET_SOC_PWR_CUR = (0xE,)
  20. DEVICE_NODE = "/dev/dbc"
  21. lib = ctypes.CDLL("./dbc_library.so", mode=ctypes.RTLD_GLOBAL)
  22. def handle_error(code):
  23. raise OSError(code, os.strerror(code))
  24. def get_nonce(device, signature):
  25. if not device:
  26. raise ValueError("Device required")
  27. buf = ctypes.create_string_buffer(DBC_NONCE_SIZE)
  28. ret = lib.get_nonce(device.fileno(), ctypes.byref(buf), signature)
  29. if ret:
  30. handle_error(ret)
  31. return buf.value
  32. def set_uid(device, new_uid, signature):
  33. if not signature:
  34. raise ValueError("Signature required")
  35. if not new_uid:
  36. raise ValueError("UID required")
  37. ret = lib.set_uid(device.fileno(), new_uid, signature)
  38. if ret:
  39. handle_error(ret)
  40. return True
  41. def process_param(device, message, signature, data=None):
  42. if not signature:
  43. raise ValueError("Signature required")
  44. if type(message) != tuple:
  45. raise ValueError("Expected message tuple")
  46. arg = ctypes.c_int(data if data else 0)
  47. sig = ctypes.create_string_buffer(signature, len(signature))
  48. ret = lib.process_param(device.fileno(), message[0], ctypes.pointer(sig), ctypes.pointer(arg))
  49. if ret:
  50. handle_error(ret)
  51. return arg.value, sig.value