A swift function name as getDataFromC which can connect with C function.
A C function name as ExternalMethod which will receive the connect from swift.
The parameter will be fill by call setData function.
I can confirm that the entire structure return to getDataFromC was successful.
Because I print the data of other structure members to check the correctness of the data.
However, I don't know how to translate the largeData to [UInt8].
I try UnsafeMutableRawPointer type. I don't know what to do for the following action. I expected that output.largeData can be translate to [UInt8] type.
//Bridging header file
struct myStruct
{
void *largeData;
int largeDataLength;
int smallData[3];
}
//C file
void setData(myStruct* data)
{
int i = 0;
uint8_t array[1024];
memset(array,0x66,1024);
data->largeData = array;
data->largeDataLength = 1024;
data->smalData[0] = 0x99;
data->smalData[0] = 0x88;
data->smalData[0] = 0x77;
}
void ExternalMethod(myStruct* data)
{
setData(&output);
}
//Swift file
func test() {
var output = myStruct()
getDataFromC(&output);
var myPointer: UnsafeMutableRawPointer = output.largeData
/* Here: I don't know how to translate data from myPointer to [UInt8] */
print("smallData: \(output.largeDataLength)") // 1024 transmission is correct
print("smallData: \(output.smallData.0)") // 0x99 transmission is correct
print("smallData: \(output.smallData.1)") // 0x88 transmission is correct
print("smallData: \(output.smallData.2)") // 0x77 transmission is correct
}
Post
Replies
Boosts
Views
Activity
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.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?
Run the command below can disable DriverKit-specific entitlement checks. How can I enable the DriverKit-specific entitlement checks again?
sudo nvram boot-args="dk=0x8001"
Hi all,
Whenever I install my kernel extension at first time,
it shows "***.kext was blocked from opening because it is not from an identified developer" in System Preference > Security & Policy .
I've download Developer ID Application in keychain, and it is not expired yet.
In my Xcode > Build Settings > Signing:
Code Signing Entitlements: empty
Code Signing Identity: Developer ID Application
Code Signing Inject Base Entitlements: No
Code Signing Style: Automatic
Development Team: My team
Enable Hardened Runtime: Yes
Other Code Signing Flags: --timestamp
Provisioning Profile: Automatic
And I've notarized my kernel extension, and its status is approval.
Does someone encounter the same issue?
Or does someone know which step I missed when I build and sign my kernel extension?
Thanks a lot.
Sean