Skip to content

Commit

Permalink
Remove unnecessary print statements
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseRiemens committed Nov 29, 2023
1 parent 15a5aa5 commit 74bba88
Showing 1 changed file with 91 additions and 47 deletions.
138 changes: 91 additions & 47 deletions packages/flutter_gpiod/lib/src/gpiod.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,24 @@ int _syscall4<A0, A1, A2, A3>(
return ok < 0 ? -errno : ok;
}

typedef native_epoll_wait = ffi.NativeFunction<ffi.Int32 Function(ffi.Int32, ffi.Pointer, ffi.Int32, ffi.Int32)>;
typedef native_epoll_wait = ffi.NativeFunction<
ffi.Int32 Function(ffi.Int32, ffi.Pointer, ffi.Int32, ffi.Int32)>;
typedef dart_epoll_wait = int Function(int, ffi.Pointer, int, int);

typedef native_epoll_ctl = ffi.NativeFunction<ffi.Int32 Function(ffi.Int32, ffi.Int32, ffi.Int32, ffi.Pointer)>;
typedef native_epoll_ctl = ffi.NativeFunction<
ffi.Int32 Function(ffi.Int32, ffi.Int32, ffi.Int32, ffi.Pointer)>;
typedef dart_epoll_ctl = int Function(int, int, int, ffi.Pointer);

typedef native_read = ffi.NativeFunction<ffi.Int32 Function(ffi.Int32, ffi.Pointer<ffi.Void>, ffi.Uint32)>;
typedef native_read = ffi.NativeFunction<
ffi.Int32 Function(ffi.Int32, ffi.Pointer<ffi.Void>, ffi.Uint32)>;
typedef dart_read = int Function(int, ffi.Pointer<ffi.Void>, int);

typedef native_read64 = ffi.NativeFunction<ffi.Int64 Function(ffi.Int32, ffi.Pointer<ffi.Void>, ffi.Uint64)>;
typedef native_read64 = ffi.NativeFunction<
ffi.Int64 Function(ffi.Int32, ffi.Pointer<ffi.Void>, ffi.Uint64)>;
typedef dart_read64 = dart_read;

typedef native_errno_location = ffi.NativeFunction<ffi.Pointer<ffi.Int32> Function()>;
typedef native_errno_location
= ffi.NativeFunction<ffi.Pointer<ffi.Int32> Function()>;
typedef dart_errno_location = ffi.Pointer<ffi.Int32> Function();

Future<void> _eventIsolateEntry2(List args) async {
Expand All @@ -164,7 +169,8 @@ Future<void> _eventIsolateEntry2(List args) async {
final libc = LibC(ffi.DynamicLibrary.process());

final maxEpollEvents = 64;
final epollEvents = ffi.calloc.allocate<epoll_event>(ffi.sizeOf<epoll_event>() * maxEpollEvents);
final epollEvents = ffi.calloc
.allocate<epoll_event>(ffi.sizeOf<epoll_event>() * maxEpollEvents);

final maxEvents = 16;
final events = newGpioEventData(count: maxEvents);
Expand Down Expand Up @@ -201,7 +207,8 @@ Future<void> _eventIsolateEntry2(List args) async {
if (ok < 0) {
freeStruct(epollEvents, allocator: ffi.calloc);
freeStruct(events);
throw LinuxError("Could not read GPIO events from event line fd", "read", -ok);
throw LinuxError(
"Could not read GPIO events from event line fd", "read", -ok);
} else if (ok == 0) {
throw LinuxError(
'read(${epollEvent.ref.data.u64}, ${events}, ${maxEvents * ffi.sizeOf<gpioevent_data>()}) returned 0',
Expand Down Expand Up @@ -263,22 +270,22 @@ class PlatformInterface {

if (fd < 0) {
chipIndexToFd.values.forEach((fd) => libc.close(fd));
throw FileSystemException('Could not open GPIO chip $i', '/dev/gpiochip$i');
throw FileSystemException(
'Could not open GPIO chip $i', '/dev/gpiochip$i');
}

chipIndexToFd[i] = fd;
}

final epollFd = libc.epoll_create1(0);
if (epollFd < 0) {
throw LinuxError("Could not create epoll instance", "epoll_create1", libc.errno);
throw LinuxError(
"Could not create epoll instance", "epoll_create1", libc.errno);
}

final receivePort = ReceivePort();
final errorReceivePort = ReceivePort();

print('before isolate.spawn');

Isolate.spawn(
_eventIsolateEntry2,
[
Expand All @@ -289,13 +296,12 @@ class PlatformInterface {
debugName: 'flutter_gpiod event listener',
);

print('after isolate.spawn');

errorReceivePort.listen((message) {
throw RemoteError(message[0], message[1]);
});

return PlatformInterface._construct(libc, numChips, chipIndexToFd, epollFd, receivePort);
return PlatformInterface._construct(
libc, numChips, chipIndexToFd, epollFd, receivePort);
}

final LibC libc;
Expand Down Expand Up @@ -330,7 +336,8 @@ class PlatformInterface {
}

int _lineFdFromLineHandle(int lineHandle) {
return _lineHandleToLineHandleFd[lineHandle] ?? _lineHandleToLineEventFd[lineHandle]!;
return _lineHandleToLineHandleFd[lineHandle] ??
_lineHandleToLineEventFd[lineHandle]!;
}

int _chipIndexFromLineHandle(int lineHandle) {
Expand Down Expand Up @@ -365,12 +372,15 @@ class PlatformInterface {
)
.key;

final edge = list[1] == GPIOEVENT_EVENT_RISING_EDGE ? SignalEdge.rising : SignalEdge.falling;
final edge = list[1] == GPIOEVENT_EVENT_RISING_EDGE
? SignalEdge.rising
: SignalEdge.falling;
final timestampNanos = list[2];
final timestamp = Duration(microseconds: timestampNanos ~/ 1000);
final time = DateTime.fromMicrosecondsSinceEpoch(list[3]);

return GlobalSignalEvent(lineHandle, SignalEvent(edge, timestampNanos, timestamp, time));
return GlobalSignalEvent(
lineHandle, SignalEvent(edge, timestampNanos, timestamp, time));
})
.where((event) => event.lineHandle != -1)
.asBroadcastStream();
Expand All @@ -384,13 +394,15 @@ class PlatformInterface {
}

Map<String, dynamic> getChipDetails(int chipIndex) {
final structPtr = newStruct<gpiochip_info>(elementSize: ffi.sizeOf<gpiochip_info>());
final structPtr =
newStruct<gpiochip_info>(elementSize: ffi.sizeOf<gpiochip_info>());
final struct = structPtr.ref;

Map<String, dynamic> map;

try {
_ioctl(_chipFdFromChipIndex(chipIndex), GPIO_GET_CHIPINFO_IOCTL, structPtr);
_ioctl(
_chipFdFromChipIndex(chipIndex), GPIO_GET_CHIPINFO_IOCTL, structPtr);

map = <String, dynamic>{
'name': stringFromInlineArray(32, (i) => struct.name[i]),
Expand All @@ -412,7 +424,8 @@ class PlatformInterface {
final fd = _chipFdFromLineHandle(lineHandle);
final offset = _lineIndexFromLineHandle(lineHandle);

final structPtr = newStruct<gpioline_info>(elementSize: ffi.sizeOf<gpioline_info>());
final structPtr =
newStruct<gpioline_info>(elementSize: ffi.sizeOf<gpioline_info>());
final struct = structPtr.ref;

struct.line_offset = offset;
Expand Down Expand Up @@ -463,7 +476,8 @@ class PlatformInterface {
final isEvent = triggers.isNotEmpty;

if (isEvent && !isInput) {
throw ArgumentError('Line must be requested as input when triggers are requested.');
throw ArgumentError(
'Line must be requested as input when triggers are requested.');
}

if (isInput) {
Expand All @@ -490,7 +504,8 @@ class PlatformInterface {
final offset = _lineIndexFromLineHandle(lineHandle);

if (!isEvent) {
final requestPtr = newStruct<gpiohandle_request>(elementSize: ffi.sizeOf<gpiohandle_request>());
final requestPtr = newStruct<gpiohandle_request>(
elementSize: ffi.sizeOf<gpiohandle_request>());
final request = requestPtr.ref;

request.lines = 1;
Expand All @@ -502,7 +517,9 @@ class PlatformInterface {
(i, v) => request.consumer_label[i] = v,
);

request.flags = (direction == LineDirection.input ? GPIOHANDLE_REQUEST_INPUT : GPIOHANDLE_REQUEST_OUTPUT) |
request.flags = (direction == LineDirection.input
? GPIOHANDLE_REQUEST_INPUT
: GPIOHANDLE_REQUEST_OUTPUT) |
(outputMode == OutputMode.openDrain
? GPIOHANDLE_REQUEST_OPEN_DRAIN
: outputMode == OutputMode.openSource
Expand All @@ -529,11 +546,13 @@ class PlatformInterface {
freeStruct(requestPtr);
}
} else {
final requestPtr = newStruct<gpioevent_request>(elementSize: ffi.sizeOf<gpioevent_request>());
final requestPtr = newStruct<gpioevent_request>(
elementSize: ffi.sizeOf<gpioevent_request>());
final request = requestPtr.ref;

request.lineoffset = offset;
writeStringToArrayHelper(consumer, 32, (i, v) => request.consumer_label[i] = v);
writeStringToArrayHelper(
consumer, 32, (i, v) => request.consumer_label[i] = v);
request.handleflags = GPIOHANDLE_REQUEST_INPUT |
(bias == Bias.disable
? GPIOHANDLE_REQUEST_BIAS_DISABLE
Expand All @@ -555,16 +574,19 @@ class PlatformInterface {
_requestedLines.add(lineHandle);
_lineHandleToLineEventFd[lineHandle] = request.fd;

final epollEvent = ffi.calloc.allocate<epoll_event>(ffi.sizeOf<epoll_event>());
final epollEvent =
ffi.calloc.allocate<epoll_event>(ffi.sizeOf<epoll_event>());
epollEvent.ref.events = EPOLL_EVENTS.EPOLLIN | EPOLL_EVENTS.EPOLLPRI;
epollEvent.ref.data.u64 = request.fd;
final result = libc.epoll_ctl(_epollFd, EPOLL_CTL_ADD, request.fd, epollEvent);
final result =
libc.epoll_ctl(_epollFd, EPOLL_CTL_ADD, request.fd, epollEvent);
ffi.calloc.free(epollEvent);

if (result < 0) {
final errno = libc.errno;
releaseLine(lineHandle);
throw LinuxError("Could not add GPIO line event fd to epoll instance", "epoll_ctl", errno);
throw LinuxError("Could not add GPIO line event fd to epoll instance",
"epoll_ctl", errno);
}
} finally {
freeStruct(requestPtr);
Expand All @@ -579,7 +601,10 @@ class PlatformInterface {
var ok = libc.close(fd);
var errno = libc.errno;
if (ok != 0) {
throw LinuxError("Couldn't release line by closing line handle file descriptor.", "close", errno);
throw LinuxError(
"Couldn't release line by closing line handle file descriptor.",
"close",
errno);
}

_requestedLines.remove(lineHandle);
Expand Down Expand Up @@ -617,10 +642,13 @@ class PlatformInterface {
ArgumentError.checkNotNull(initialValue, 'initialValue');
}

final requestPtr = newStruct<gpiohandle_config>(elementSize: ffi.sizeOf<gpiohandle_config>());
final requestPtr = newStruct<gpiohandle_config>(
elementSize: ffi.sizeOf<gpiohandle_config>());
final request = requestPtr.ref;

request.flags = (direction == LineDirection.input ? GPIOHANDLE_REQUEST_INPUT : GPIOHANDLE_REQUEST_OUTPUT) |
request.flags = (direction == LineDirection.input
? GPIOHANDLE_REQUEST_INPUT
: GPIOHANDLE_REQUEST_OUTPUT) |
(outputMode == OutputMode.openDrain
? GPIOHANDLE_REQUEST_OPEN_DRAIN
: outputMode == OutputMode.openSource
Expand All @@ -640,7 +668,8 @@ class PlatformInterface {
}

try {
_ioctl(_lineFdFromLineHandle(lineHandle), GPIOHANDLE_SET_CONFIG_IOCTL, requestPtr);
_ioctl(_lineFdFromLineHandle(lineHandle), GPIOHANDLE_SET_CONFIG_IOCTL,
requestPtr);
} finally {
freeStruct(requestPtr);
}
Expand All @@ -651,7 +680,8 @@ class PlatformInterface {

final fd = _lineFdFromLineHandle(lineHandle);

final structPtr = newStruct<gpiohandle_data>(elementSize: ffi.sizeOf<gpiohandle_data>());
final structPtr =
newStruct<gpiohandle_data>(elementSize: ffi.sizeOf<gpiohandle_data>());
final struct = structPtr.ref;

bool result;
Expand All @@ -670,12 +700,14 @@ class PlatformInterface {
assert(_requestedLines.contains(lineHandle));
assert(_lineHandleToLineHandleFd.containsKey(lineHandle));

final structPtr = newStruct<gpiohandle_data>(elementSize: ffi.sizeOf<gpiohandle_data>());
final structPtr =
newStruct<gpiohandle_data>(elementSize: ffi.sizeOf<gpiohandle_data>());
final struct = structPtr.ref;

struct.values[0] = value ? 1 : 0;
try {
_ioctl(_lineFdFromLineHandle(lineHandle), GPIOHANDLE_SET_LINE_VALUES_IOCTL, structPtr);
_ioctl(_lineFdFromLineHandle(lineHandle),
GPIOHANDLE_SET_LINE_VALUES_IOCTL, structPtr);
} finally {
freeStruct(structPtr);
}
Expand All @@ -688,7 +720,8 @@ class PlatformInterface {
throw StateError("Unsupported OS: ${Platform.operatingSystem}");
}

final matches = RegExp("^Linux (\\d*)\\.(\\d*)").firstMatch(Platform.operatingSystemVersion)!;
final matches = RegExp("^Linux (\\d*)\\.(\\d*)")
.firstMatch(Platform.operatingSystemVersion)!;
if (matches.groupCount == 2) {
final major = int.parse(matches.group(1)!);
final minor = int.parse(matches.group(2)!);
Expand Down Expand Up @@ -720,7 +753,8 @@ class PlatformInterface {
/// Starting-point for querying GPIO chips or lines,
/// and finding the line you want to control.
class FlutterGpiod {
FlutterGpiod._internal(this.chips, this.supportsBias, this.supportsLineReconfiguration);
FlutterGpiod._internal(
this.chips, this.supportsBias, this.supportsLineReconfiguration);

static FlutterGpiod? _instance;

Expand All @@ -745,13 +779,15 @@ class FlutterGpiod {
/// If none exists, one will be constructed.
static FlutterGpiod get instance {
if (_instance == null) {
final chips =
List.generate(PlatformInterface.instance.getNumChips(), (i) => GpioChip._fromIndex(i), growable: false);
final chips = List.generate(PlatformInterface.instance.getNumChips(),
(i) => GpioChip._fromIndex(i),
growable: false);

final bias = PlatformInterface.instance.supportsBias();
final reconfig = PlatformInterface.instance.supportsLineReconfiguration();

_instance = FlutterGpiod._internal(List.unmodifiable(chips), bias, reconfig);
_instance =
FlutterGpiod._internal(List.unmodifiable(chips), bias, reconfig);
}

return _instance!;
Expand All @@ -763,7 +799,9 @@ class FlutterGpiod {
}

Stream<SignalEvent> _onSignalEvent(int lineHandle) {
return _onGlobalSignalEvent.where((e) => e.lineHandle == lineHandle).map((e) => e.signalEvent);
return _onGlobalSignalEvent
.where((e) => e.lineHandle == lineHandle)
.map((e) => e.signalEvent);
}
}

Expand Down Expand Up @@ -993,7 +1031,8 @@ class GpioLine {
final info = PlatformInterface.instance.getLineInfo(lineHandle);

if (info.isRequested) {
return GpioLine._internal(lineHandle, true, info, const {}, PlatformInterface.instance.getLineValue(lineHandle));
return GpioLine._internal(lineHandle, true, info, const {},
PlatformInterface.instance.getLineValue(lineHandle));
} else {
return GpioLine._internal(lineHandle, false, null, const {}, null);
}
Expand Down Expand Up @@ -1109,7 +1148,8 @@ class GpioLine {

void _checkSupportsLineReconfiguration() {
if (!FlutterGpiod.instance.supportsLineReconfiguration) {
throw UnsupportedError("Can't reconfigure line because that's not supported by "
throw UnsupportedError(
"Can't reconfigure line because that's not supported by "
"the underlying version of libgpiod. "
"You need to check `FlutterGpiod.supportsLineReconfiguration` "
"to make sure you can reconfigure.");
Expand All @@ -1126,7 +1166,8 @@ class GpioLine {
///
/// You can't specify triggers here because of platform
/// limitations.
void reconfigureInput({Bias? bias, ActiveState activeState = ActiveState.high}) {
void reconfigureInput(
{Bias? bias, ActiveState activeState = ActiveState.high}) {
ArgumentError.checkNotNull(activeState, "activeState");
_checkSupportsBiasValue(bias);
_checkSupportsLineReconfiguration();
Expand Down Expand Up @@ -1209,11 +1250,13 @@ class GpioLine {
ArgumentError.checkNotNull(value, "value");

if (_requested == false) {
throw StateError("Can't set line value because line is not requested and configured as output.");
throw StateError(
"Can't set line value because line is not requested and configured as output.");
}

if (_info!.direction != LineDirection.output) {
throw StateError("Can't set line value because line is not configured as output.");
throw StateError(
"Can't set line value because line is not configured as output.");
}

if (_value == value) return;
Expand Down Expand Up @@ -1256,7 +1299,8 @@ class GpioLine {
/// like this: `rising`, `rising`, `rising`, `falling`, `rising`,
/// even though that doesn't seem to make any sense
/// at first glance.
Stream<SignalEvent> get onEvent => FlutterGpiod.instance._onSignalEvent(_lineHandle);
Stream<SignalEvent> get onEvent =>
FlutterGpiod.instance._onSignalEvent(_lineHandle);

/// Broadcast stream of signal edges.
///
Expand Down

0 comments on commit 74bba88

Please sign in to comment.