Design Pattern Examples
Overview of object-oriented design patterns
adapter_backendfunctions.py
Go to the documentation of this file.
5
6import os
7import os.path
8import sys
9from ctypes import *
10from enum import Enum
11
12
14class DDR_ErrorCode(Enum):
15
16 DDR_ErrorCode_Success = 0
17
18 DDR_ErrorCode_Block_Already_Opened = 1
19
20 DDR_ErrorCode_Block_Not_Opened = 2
21
22 DDR_ErrorCode_Invalid_Block_Name = 3
23
24 DDR_ErrorCode_Invalid_Handle = 4
25
26 DDR_ErrorCode_Invalid_Offset = 5
27
28 DDR_ErrorCode_Null_Argument = 6
29
30
31
32DDR_MAX_OFFSET = 32
33
34
35BLOCK_NAME_0 = "gorp"
36
37
38BLOCK_NAME_1 = "baba"
39
40
41BLOCK_NAME_2 = "yaga"
42
43
44
47class Handle:
48
49 def __init__(self):
50 self.value = -1
51
52
55
56
57
61
62 def __init__(self):
63 self.value = 0
64
65
68
69
70
72if sys.platform == "win32":
73 dll_path = os.path.join(os.path.dirname(__file__), "Adapter_BackEnd")
74else:
75 dll_path = os.path.join(os.path.dirname(__file__), "libAdapter_BackEnd.so")
76
77
78adapter_backend = cdll.LoadLibrary(dll_path)
79
80
81
95def ddr_openmemoryblock(blockName, dataHandle: Handle) -> DDR_ErrorCode:
96 handle = c_int()
97 name = blockName.encode("UTF8")
98 error_code = DDR_ErrorCode(adapter_backend.DDR_OpenMemoryBlock(name, byref(handle)))
99 if error_code == DDR_ErrorCode.DDR_ErrorCode_Success:
100 dataHandle.value = handle.value
101 return error_code
102
103
113def ddr_closememoryblock(dataHandle : Handle) -> DDR_ErrorCode:
114 return DDR_ErrorCode(adapter_backend.DDR_CloseMemoryBlock(dataHandle.value))
115
116
128def ddr_getmemorysize(dataHandle: Handle, memorySizeInChunks: ValueHandle) -> DDR_ErrorCode:
129 chunks = c_int()
130 error_code = DDR_ErrorCode(adapter_backend.DDR_GetMemorySize(dataHandle.value, byref(chunks)))
131 if error_code == DDR_ErrorCode.DDR_ErrorCode_Success:
132 memorySizeInChunks.value = chunks.value
133 return error_code
134
135
150def ddr_getdatachunk(dataHandle: Handle, chunkOffset: int, value: ValueHandle) -> DDR_ErrorCode:
151 chunk_value = c_uint32()
152 error_code = DDR_ErrorCode(adapter_backend.DDR_GetDataChunk(dataHandle.value, chunkOffset, byref(chunk_value)))
153 if error_code == DDR_ErrorCode.DDR_ErrorCode_Success:
154 value.value = chunk_value.value
155 return error_code
156
157
172def ddr_setdatachunk(dataHandle: Handle, chunkOffset: int, value: int) -> DDR_ErrorCode:
173 chunk_value = c_uint32(value)
174 error_code = DDR_ErrorCode(adapter_backend.DDR_SetDataChunk(dataHandle.value, chunkOffset, chunk_value))
175 return error_code
Represents the possible errors that can be returned from the memory block access functions.
value
The attribute to read to get the value passed back through the Handle class instance.
Represents a value that can be passed into or out of a function.
value
The attribute to read to get the value passed back through the ValueHandle class instance.
DDR_ErrorCode ddr_getdatachunk(Handle dataHandle, int chunkOffset, ValueHandle value)
Read a single 32-bit value at the given offset in the memory block indicated by the specified handle.
DDR_ErrorCode ddr_closememoryblock(Handle dataHandle)
Close access to a memory block previously opened by ddr_openmemoryblock(), thus releasing it for othe...
DDR_ErrorCode ddr_getmemorysize(Handle dataHandle, ValueHandle memorySizeInChunks)
Retrieve the number of chunks in the memory block indicated by the handle to the successfully opened ...
DDR_ErrorCode ddr_setdatachunk(Handle dataHandle, int chunkOffset, int value)
Writes a single 32-bit value to the given offset in the memory block indicated by the specified handl...
DDR_ErrorCode ddr_openmemoryblock(blockName, Handle dataHandle)
Open access to a memory block for exclusive use, given the name of the memory block.