-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.dart
59 lines (45 loc) · 1.77 KB
/
hello.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// https://github.com/dart-lang/samples/tree/main/ffi
// https://stackoverflow.com/questions/61541354/dart-flutter-ffi-foreign-function-interface-native-callbacks-eg-sqlite3-exec
import 'dart:ffi' as ffi;
import 'dart:io' show Directory;
import 'dart:typed_data';
import 'package:path/path.dart' as path;
typedef HelloWorldFunc = ffi.Void Function();
typedef HelloWorld = void Function();
typedef OnSensorUpdateFunc = ffi.Int32 Function(
ffi.Pointer<ffi.UnsignedChar>, ffi.Int32);
typedef RegisterCallbackFunc = ffi.Int32 Function(
ffi.Pointer<ffi.NativeFunction<OnSensorUpdateFunc>>);
typedef RegisterCallback = int Function(
ffi.Pointer<ffi.NativeFunction<OnSensorUpdateFunc>>);
const except = -1;
void main() {
var libraryPath = path.join(
Directory.current.path, 'hello_library', 'build', 'libhello.so');
final dylib = ffi.DynamicLibrary.open(libraryPath);
final HelloWorld hello = dylib
.lookup<ffi.NativeFunction<HelloWorldFunc>>('hello_world')
.asFunction();
final RegisterCallback reg = dylib
.lookup<ffi.NativeFunction<RegisterCallbackFunc>>('registerCallback')
.asFunction();
reg(ffi.Pointer.fromFunction<OnSensorUpdateFunc>(callback, except));
hello();
}
int callback(ffi.Pointer<ffi.UnsignedChar> data, int size) {
print("Hello World in dart called by c lib");
var sensorValues = data.toUint8List(size);
for (var i = 0; i < size; i++) {
print("Sensor $i Value ${sensorValues![i]}");
}
return 0;
}
// https://stackoverflow.com/questions/60879616/dart-flutter-getting-data-array-from-c-c-using-ffi
extension UnsignedCharPointerExtension on ffi.Pointer<ffi.UnsignedChar> {
Uint8List? toUint8List(int length) {
if (this == ffi.nullptr) {
return null;
}
return cast<ffi.Uint8>().asTypedList(length);
}
}