65 lines · python
1#!/usr/bin/python32# SPDX-License-Identifier: GPL-2.03 4import ctypes5import os6 7DBC_UID_SIZE = 168DBC_NONCE_SIZE = 169DBC_SIG_SIZE = 3210 11PARAM_GET_FMAX_CAP = (0x3,)12PARAM_SET_FMAX_CAP = (0x4,)13PARAM_GET_PWR_CAP = (0x5,)14PARAM_SET_PWR_CAP = (0x6,)15PARAM_GET_GFX_MODE = (0x7,)16PARAM_SET_GFX_MODE = (0x8,)17PARAM_GET_CURR_TEMP = (0x9,)18PARAM_GET_FMAX_MAX = (0xA,)19PARAM_GET_FMAX_MIN = (0xB,)20PARAM_GET_SOC_PWR_MAX = (0xC,)21PARAM_GET_SOC_PWR_MIN = (0xD,)22PARAM_GET_SOC_PWR_CUR = (0xE,)23 24DEVICE_NODE = "/dev/dbc"25 26lib = ctypes.CDLL("./dbc_library.so", mode=ctypes.RTLD_GLOBAL)27 28 29def handle_error(code):30 raise OSError(code, os.strerror(code))31 32 33def get_nonce(device, signature):34 if not device:35 raise ValueError("Device required")36 buf = ctypes.create_string_buffer(DBC_NONCE_SIZE)37 ret = lib.get_nonce(device.fileno(), ctypes.byref(buf), signature)38 if ret:39 handle_error(ret)40 return buf.value41 42 43def set_uid(device, new_uid, signature):44 if not signature:45 raise ValueError("Signature required")46 if not new_uid:47 raise ValueError("UID required")48 ret = lib.set_uid(device.fileno(), new_uid, signature)49 if ret:50 handle_error(ret)51 return True52 53 54def process_param(device, message, signature, data=None):55 if not signature:56 raise ValueError("Signature required")57 if type(message) != tuple:58 raise ValueError("Expected message tuple")59 arg = ctypes.c_int(data if data else 0)60 sig = ctypes.create_string_buffer(signature, len(signature))61 ret = lib.process_param(device.fileno(), message[0], ctypes.pointer(sig), ctypes.pointer(arg))62 if ret:63 handle_error(ret)64 return arg.value, sig.value65