I declare a structure type in swift, name as CmdStruct
struct CmdStruct {
var cmd:[UInt8]
init() {
cmd = [UInt8](repeating: 0, count:16)
}
}
In the case that the connection has been obtained. In sendCmd function, declare a variable which name as input which type of CmdStruct. After set data to input, call a DriverKit function IOConnectCallStructMethod.
Here xcode shows a warning tip for the parameter 'input' for call IOConnectCallStructMethod:
<Forming 'UnsafeRawPointer' to a variable of type 'CmdStruct'; this is likely incorrect because 'CmdStruct' may contain an object reference.>
func sendCmd() {
var ret:kern_return_t = kIOReturnSuccess
var input = cmdStruct()
var output:[UInt8] = [UInt8](repeating:0, count: 16)
var inputSize = MemoryLayout<CmdStruct>.size // print value 8
var outputSize = output.count // print value 16
input.cmd[0] = 0x12
input.cmd[1] = 0x34
ret = IOConnectCallStructMethod(Connection, selector, &input, inputSize, &output, &outputSize)
}
In C file, driverkit function ExternalMethod will receive the parameter from swift side. And check the value of input->cmd[0] and input->cmd[1] in console. However, the values are not the expected 0x12 and 0x34.
struct CmdStruct
{
uint8_t cmd[16];
};
kern_return_t myTest::ExternalMethod(uint64_t selector, IOUserClientMethodArguments* arguments, const IOuserClientMethodDispatch* dispatch, OSObject* target, void* reference)
{
int i = 0;
CmdStruct* input = nullptr;
if (arguments == nullptr) {
ret = KIOReturnBadArgument;
}
if (arguments->structureInput != nullptr) {
input = (CmdStruct*)arguments->structureInput->getBytestNoCopy();
}
/*
Here to print input->cmd[0] and input->cmd[1] data to console.
*/
}
I expect that the correct value of input will be show on the driverkit side. I'm not sure which part is wrong. Here, I list some factors that may lead to incorrect value.
-
How to fix the warning tip for parameter 'input' when call IOConnectCallStructMethod in swift.
-
I get the inputSize value 8 by using MemoryLayout<CmdStruct>.size. But this structure contains a 16 bytes array. How to get the correct struct size in swift
-
In C file side, using (CmdStruct*)arguments->structureInput->getBytestNoCopy() to transform data to C struct is correct?