Skip to content

Commit

Permalink
finally covered issue #4
Browse files Browse the repository at this point in the history
  • Loading branch information
sombriks committed Jun 30, 2024
1 parent f577de5 commit aa86093
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 49 deletions.
88 changes: 88 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
"build": "node-gyp build",
"clean": "node-gyp clean",
"configure-gpio-sim": "sh test/prepare-gpio-sim.sh",
"test": "mocha",
"test": "mocha test/*.spec.js test/**/*.spec.js",
"test:coverage": "nyc npm run test",
"lint": "xo --fix"
},
"devDependencies": {
"chai": "^4.4.1",
"mocha": "10.1.0",
"node-gyp": "^10.0.1",
"nyc": "^15.1.0",
Expand All @@ -53,4 +54,4 @@
"unicorn/prefer-module": 0
}
}
}
}
16 changes: 9 additions & 7 deletions src/chip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ NAN_MODULE_INIT(Chip::Init) {

Chip::Chip(const char *device) {
chip = gpiod_chip_open_lookup(device);
if (!chip) Nan::ThrowError("Unable to open device");
std::string msg = "Chip::new - Unable to open device ";
msg = msg + device;
if (!chip) Nan::ThrowError(Nan::ErrnoException(errno, msg.c_str()));
}

Chip::~Chip() {
Expand All @@ -44,38 +46,38 @@ NAN_METHOD(Chip::New) {
NAN_METHOD(Chip::getNumberOfLines) {
Chip *obj = Nan::ObjectWrap::Unwrap<Chip>(info.This());
if (!obj->chip) {
Nan::ThrowError("::getNumberOfLines() for chip==NULL");
Nan::ThrowError(Nan::ErrnoException(errno, "::getNumberOfLines() for chip==NULL"));
return;
}
int ret = gpiod_chip_num_lines(obj->getNativeChip());
if (-1 == ret) {
Nan::ThrowError("::getNumberOfLines() failed");
Nan::ThrowError(Nan::ErrnoException(errno, "::getNumberOfLines() failed"));
} else
info.GetReturnValue().Set(ret);
}

NAN_METHOD(Chip::getChipName) {
Chip *obj = Nan::ObjectWrap::Unwrap<Chip>(info.This());
if (!obj->chip) {
Nan::ThrowError("::getChipName() for chip==NULL");
Nan::ThrowError(Nan::ErrnoException(errno, "::getChipName() for chip==NULL"));
return;
}
const char *name = gpiod_chip_name(obj->getNativeChip());
if (!name) {
Nan::ThrowError("::getChipName() failed");
Nan::ThrowError(Nan::ErrnoException(errno, "::getChipName() failed"));
} else
info.GetReturnValue().Set(Nan::New<v8::String>(name).ToLocalChecked());
}

NAN_METHOD(Chip::getChipLabel) {
Chip *obj = Nan::ObjectWrap::Unwrap<Chip>(info.This());
if (!obj->chip) {
Nan::ThrowError("::getChipLabel() for chip==NULL");
Nan::ThrowError(Nan::ErrnoException(errno, "::getChipLabel() for chip==NULL"));
return;
}
const char *label = gpiod_chip_label(obj->getNativeChip());
if (!label) {
Nan::ThrowError("::getChipLabel() failed");
Nan::ThrowError(Nan::ErrnoException(errno, "::getChipLabel() failed"));
} else
info.GetReturnValue().Set(Nan::New<v8::String>(label).ToLocalChecked());
}
Expand Down
20 changes: 11 additions & 9 deletions src/line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ NAN_MODULE_INIT(Line::Init) {

Line::Line(Chip *chip, unsigned int pin) {
line = gpiod_chip_get_line(chip->getNativeChip(), pin);
if (!line) Nan::ThrowError("Unable to open GPIO line ");
std::string msg = "Line::new - Unable to open GPIO line ";
msg = msg + std::to_string(pin);
if (!line) Nan::ThrowError(Nan::ErrnoException(errno, msg.c_str()));
}

Line::~Line() {
Expand Down Expand Up @@ -51,7 +53,7 @@ NAN_METHOD(Line::New) {
NAN_METHOD(Line::getLineOffset) {
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if (!obj->line) {
Nan::ThrowError("::getLineOffset() for line==NULL");
Nan::ThrowError(Nan::ErrnoException(errno, "::getLineOffset() for line==NULL"));
return;
}
int ret = gpiod_line_offset(obj->getNativeLine());
Expand All @@ -64,7 +66,7 @@ NAN_METHOD(Line::getLineOffset) {
NAN_METHOD(Line::getLineName) {
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if (!obj->line) {
Nan::ThrowError("::getLineName() for line==NULL");
Nan::ThrowError(Nan::ErrnoException(errno, "::getLineName() for line==NULL"));
return;
}
const char *name = gpiod_line_name(obj->getNativeLine());
Expand All @@ -77,7 +79,7 @@ NAN_METHOD(Line::getLineName) {
NAN_METHOD(Line::getLineConsumer) {
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if (!obj->line) {
Nan::ThrowError("::getLineConsumer() for line==NULL");
Nan::ThrowError(Nan::ErrnoException(errno, "::getLineConsumer() for line==NULL"));
return;
}
const char *name = gpiod_line_consumer(obj->getNativeLine());
Expand All @@ -90,7 +92,7 @@ NAN_METHOD(Line::getLineConsumer) {
NAN_METHOD(Line::getValue) {
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if (!obj->line) {
Nan::ThrowError("::getValue() for line==NULL");
Nan::ThrowError(Nan::ErrnoException(errno, "::getValue() for line==NULL"));
return;
}
int ret = gpiod_line_get_value(obj->getNativeLine());
Expand All @@ -112,7 +114,7 @@ NAN_METHOD(Line::setValue) {
NAN_METHOD(Line::requestInputMode) {
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if (!obj->line) {
Nan::ThrowError("::requestInputMode() for line==NULL");
Nan::ThrowError(Nan::ErrnoException(errno, "::requestInputMode() for line==NULL"));
return;
}
Nan::Utf8String consumer(info[0]);
Expand All @@ -123,7 +125,7 @@ NAN_METHOD(Line::requestInputMode) {
NAN_METHOD(Line::requestInputModeFlags) {
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if (!obj->line) {
Nan::ThrowError("::requestInputModeFlags for line==NULL");
Nan::ThrowError(Nan::ErrnoException(errno, "::requestInputModeFlags for line==NULL"));
return;
}
Nan::Utf8String consumer(info[0]);
Expand All @@ -135,15 +137,15 @@ NAN_METHOD(Line::requestInputModeFlags) {
NAN_METHOD(Line::requestOutputMode) {
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if (!obj->line) {
Nan::ThrowError("::requestOutputMode() for line==NULL");
Nan::ThrowError(Nan::ErrnoException(errno, "::requestOutputMode() for line==NULL"));
return;
}
unsigned int value = 0;
v8::Local<v8::Value> defaultValue = info[0];
if (!defaultValue->IsUndefined() && defaultValue->IsNumber()) {
unsigned int val = Nan::To<unsigned int>(defaultValue).FromJust();
if (val > 1) {
Nan::ThrowError("::requestOutputMode() value is not in {0,1} range");
Nan::ThrowError(Nan::ErrnoException(errno, "::requestOutputMode() value is not in {0,1} range"));
return;
}
value = val;
Expand Down
4 changes: 2 additions & 2 deletions src/misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ NAN_METHOD(getInstantLineValue) {

int value = -1;
if (-1 == (value = gpiod_ctxless_get_value(*device, offset, active_low, *consumer))) {
Nan::ThrowError("Unable to get instant value");
Nan::ThrowError(Nan::ErrnoException(errno, "::getInstantLineValue - Unable to get instant value"));
return;
}

Expand All @@ -29,7 +29,7 @@ NAN_METHOD(setInstantLineValue) {
Nan::Utf8String consumer(info[4]);

if (-1 == gpiod_ctxless_set_value(*device, offset, value, active_low, *consumer, NULL, NULL)) {
Nan::ThrowError("Unable to get instant value");
Nan::ThrowError(Nan::ErrnoException(errno, "::setInstantLineValue - Unable to get instant value"));
return;
}

Expand Down
11 changes: 7 additions & 4 deletions test/00-misc.js → test/00-misc.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
const assert = require('node:assert');
const { expect } = require('chai');
const gpiod = require('..');

describe('libgpiod miscellaneous bindings', () => {
it('should get libgpiod version', done => {
assert.ok(gpiod.version());
expect(gpiod.version()).to.be.ok;
done();
});

it('should get line instant value', done => {
const value = gpiod.getInstantLineValue(0, 17);
assert.equal(0, value);
expect(value).to.eq(0);
done();
});

it('should NOT get line instant value due wrong chip name', done => {
try {
gpiod.getInstantLineValue('/dev/gpiochipZero', 17);
} catch {
} catch (e) {
expect(e.errno).eq(2)
expect(e.code).eq('ENOENT')
expect(e.syscall).eq('::getInstantLineValue - Unable to get instant value')
done();
}
});
Expand Down
Loading

0 comments on commit aa86093

Please sign in to comment.