【发布时间】:2021-10-06 05:58:58
【问题描述】:
linux 内核的 ioctl 将结构放入结构中。我正在尝试弄清楚如何对 ctypes 做同样的事情。
我从 ioctl 收到 ParentRq。我想要一种将 Message.data 中的数据复制到另一个结构中的通用方法,例如 EventID。
from ctypes import Structure, c_uint8, c_uint16, POINTER
class EventID(Structure):
_pack_ = 1
_fields_ = [
('recent_addition_timestamp', c_uint8 * 4),
('last_recid', c_uint8 * 2),
('last_sw_processed_id', c_uint8 * 2),
('last_processed_id', c_uint8 * 2),
]
_eventid_sampledata = [0x88, 0x29, 0xfd, 0x60, 0x3c, 0x02, 0xff, 0xff, 0x3c, 0x02]
class Message(Structure):
_pack_ = 1
_fields_ = [
('data_len', c_uint16),
('data', POINTER(c_uint8))
]
class ParentRq(Structure):
_pack_ = 1
_fields_ = [
('msg', Message)
]
p = ParentRq()
p.msg.data_len = len(EventID._eventid_sampledata)
p.msg.data = (c_uint8 * p.msg.data_len)(*EventID._eventid_sampledata)
# Proof that items got copied into the pointer correctly.
items = []
for i in range(p.msg.data_len):
items.append(hex(p.msg.data[i]))
# items -> ['0x88', '0x29', '0xfd', '0x60', '0x3c', '0x2', '0xff', '0xff', '0x3c', '0x2']
# Copy p.msg.data into EventID
# How do I copy p.msg.data into EventID?
eid = EventID()
【问题讨论】:
标签: python-3.x ctypes ioctl