Skip to content

Commit

Permalink
unify string buffer handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zariiii9003 committed Oct 26, 2023
1 parent 35607b8 commit d8dfccf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 55 deletions.
3 changes: 2 additions & 1 deletion src/pycanape/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"CANapeDll",
"CANapeVersion",
"CurveCalibrationObject",
"DllVersion",
"MapCalibrationObject",
"ScalarCalibrationObject",
"ValueBlockCalibrationObject",
Expand Down Expand Up @@ -69,7 +70,7 @@
ScalarCalibrationObject,
ValueBlockCalibrationObject,
)
from .canape import AppVersion, CANape
from .canape import AppVersion, CANape, DllVersion
from .cnp_api.cnp_constants import (
Channels,
DriverType,
Expand Down
41 changes: 18 additions & 23 deletions src/pycanape/canape.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,19 @@ def get_dll_version(self) -> DllVersion:

def get_project_directory(self) -> str:
"""Get the current project Directory."""
c_buffer = ctypes.create_string_buffer(1024)
c_length = ctypes.c_ulong(1024)
length = ctypes.c_ulong()
self._dll.Asap3GetProjectDirectory(
self.asap3_handle,
ctypes.cast(c_buffer, ctypes.c_char_p),
ctypes.byref(c_length),
None,
ctypes.byref(length),
)
return c_buffer.value.decode(RC["ENCODING"])
buffer = ctypes.create_string_buffer(length.value)
self._dll.Asap3GetProjectDirectory(
self.asap3_handle,
buffer,
ctypes.byref(length),
)
return buffer.value.decode(RC["ENCODING"])

def create_module(
self,
Expand Down Expand Up @@ -459,28 +464,18 @@ def get_cna_filename(self) -> str:
:return:
Path to .cna file
"""
# call function first time to determine max_size
size = ctypes.c_ulong(0)
try:
self._dll.Asap3GetCNAFilename(
self.asap3_handle,
None,
ctypes.byref(size),
)
except CANapeError:
if size.value > 0:
pass
else:
raise

# call function again to retrieve data
buffer = ctypes.create_string_buffer(size.value)
length = ctypes.c_ulong()
self._dll.Asap3GetCNAFilename(
self.asap3_handle,
None,
ctypes.byref(length),
)
buffer = ctypes.create_string_buffer(length.value)
self._dll.Asap3GetCNAFilename(
self.asap3_handle,
buffer,
ctypes.byref(size),
ctypes.byref(length),
)

return buffer.value.decode(RC["ENCODING"])

def load_cna_file(self, cna_file: Union[str, Path]) -> None:
Expand Down
28 changes: 14 additions & 14 deletions src/pycanape/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,31 +167,25 @@ def get_database_objects(self) -> List[str]:
List of object names
"""
if self._objects_cache is None:
# call function first time to determine max_size
max_size = ctypes.c_ulong(0)
length = ctypes.c_ulong(0)
self._dll.Asap3GetDatabaseObjects(
self.asap3_handle,
self.module_handle,
None,
ctypes.byref(max_size),
ctypes.byref(length),
TAsap3DBOType.DBTYPE_ALL,
)

# call function again to retrieve data
buffer = ctypes.create_string_buffer(max_size.value)
buffer = ctypes.create_string_buffer(length.value)
self._dll.Asap3GetDatabaseObjects(
self.asap3_handle,
self.module_handle,
buffer,
ctypes.byref(max_size),
ctypes.byref(length),
TAsap3DBOType.DBTYPE_ALL,
)
self._objects_cache = (
buffer.value.strip(b";")[: max_size.value]
.decode(RC["ENCODING"])
.split(";")
buffer.value.strip(b";").decode(RC["ENCODING"]).split(";")
)

return copy.copy(self._objects_cache)

def get_ecu_tasks(self) -> Dict[str, EcuTask]:
Expand Down Expand Up @@ -225,15 +219,21 @@ def get_task_instance(task_info: TTaskInfo2) -> EcuTask:

def get_network_name(self) -> str:
"""Receives the name of the used network."""
c_name = ctypes.create_string_buffer(256)
size = ctypes.c_uint()
self._dll.Asap3GetNetworkName(
self.asap3_handle,
self.module_handle,
ctypes.cast(ctypes.byref(c_name), ctypes.c_char_p),
None,
ctypes.byref(size),
)
buffer = ctypes.create_string_buffer(size.value)
self._dll.Asap3GetNetworkName(
self.asap3_handle,
self.module_handle,
buffer,
ctypes.byref(size),
)
return c_name.value[: size.value].decode(RC["ENCODING"])
return buffer.value.decode(RC["ENCODING"])

def get_ecu_driver_type(self) -> DriverType:
"""Retrieves the drivertype of an ECU.
Expand Down
28 changes: 20 additions & 8 deletions src/pycanape/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,21 @@ def __init__(

def get_name(self) -> str:
"""Get the name of the recorder."""
length = ctypes.c_long(256)
c_name = ctypes.create_string_buffer(length.value)
length = ctypes.c_long()
self._dll.Asap3GetRecorderName(
self._asap3_handle,
self._recorder_id,
ctypes.cast(c_name, ctypes.c_char_p),
None,
ctypes.byref(length),
)
return c_name.value.decode(RC["ENCODING"])
buffer = ctypes.create_string_buffer(length.value)
self._dll.Asap3GetRecorderName(
self._asap3_handle,
self._recorder_id,
buffer,
ctypes.byref(length),
)
return buffer.value.decode(RC["ENCODING"])

def get_state(self) -> cnp_constants.RecorderState:
"""Return the state of a Recorder"""
Expand Down Expand Up @@ -87,15 +93,21 @@ def disable(self) -> None:

def get_mdf_filename(self) -> str:
"""Retrieve the MDF Filename of a Recorder."""
length = wintypes.DWORD(1024)
c_name = ctypes.create_string_buffer(length.value)
length = wintypes.DWORD()
self._dll.Asap3GetRecorderMdfFileName(
self._asap3_handle,
self._recorder_id,
None,
ctypes.byref(length),
)
buffer = ctypes.create_string_buffer(length.value)
self._dll.Asap3GetRecorderMdfFileName(
self._asap3_handle,
self._recorder_id,
ctypes.cast(ctypes.byref(c_name), ctypes.c_char_p),
buffer,
ctypes.byref(length),
)
return c_name.value.decode(RC["ENCODING"])
return buffer.value.decode(RC["ENCODING"])

def set_mdf_filename(self, filename: Union[str, Path]) -> None:
"""Set the MDF Filename for a Recorder.
Expand Down
15 changes: 6 additions & 9 deletions src/pycanape/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,18 @@ def get_script_result_string(self) -> str:
:return:
result string of the script
"""
# call function first time to determine max_size
max_size = ctypes.c_ulong(0)
length = ctypes.c_ulong()
self._dll.Asap3GetScriptResultString(
self.asap3_handle,
self.script_handle,
None,
ctypes.byref(max_size),
ctypes.byref(length),
)

# call function again to retrieve data
c_buffer = ctypes.create_string_buffer(max_size.value)
buffer = ctypes.create_string_buffer(length.value)
self._dll.Asap3GetScriptResultString(
self.asap3_handle,
self.script_handle,
c_buffer,
ctypes.byref(max_size),
buffer,
ctypes.byref(length),
)
return c_buffer.value.decode(RC["ENCODING"])
return buffer.value.decode(RC["ENCODING"])

0 comments on commit d8dfccf

Please sign in to comment.