Skip to content

Commit

Permalink
Merge pull request #30 from ShimmyShaman/master
Browse files Browse the repository at this point in the history
bug fix for issue #28 and private/PythonFunctions/format function.
  • Loading branch information
pjueon authored Dec 12, 2021
2 parents b2c91fa + 1a3b08f commit cc002fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions include/private/PythonFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ std::string format(const std::string& fmt, Args... args)

// In C++11 and later, std::string is guaranteed to be null terminated.
// (https://stackoverflow.com/questions/11752705/does-stdstring-have-a-null-terminator)
// So do not need an extra space for the last '\0'
// So do not need an extra space for the null-terminator.
std::string ret(size, '\0');

std::snprintf(&ret[0], size, fmt.c_str(), args...);
// for snprintf, an extra space for the null-terminator MUST be included. (size + 1)
std::snprintf(&ret[0], size + 1, fmt.c_str(), args...);
return ret;
}

Expand Down
17 changes: 11 additions & 6 deletions src/gpio_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ int _write_sysfs_edge(int gpio, Edge edge, bool allow_none = true)
return write(edge_fd, "both", 7);
case Edge::NONE: {
if (!allow_none) {
close(edge_fd);
return (int)GPIO::EventResultCode::UnallowedEdgeNone;
}
return write(edge_fd, "none", 7);
Expand All @@ -130,18 +129,18 @@ int _write_sysfs_edge(int gpio, Edge edge, bool allow_none = true)

default:
std::cerr << format("Bad argument, edge=%i\n", (int)edge);
close(edge_fd);
return (int)GPIO::EventResultCode::IllegalEdgeArgument;
}
};

int result = get_result();

if (result == -1) {
std::perror("sysfs/edge write");
return (int)GPIO::EventResultCode::SysFD_EdgeWrite;
} else {
if (result >= 0)
result = 0;
else if (result == -1) {
// Print additional detail and label as a edge write error
std::perror("sysfs/edge write");
result = (int)GPIO::EventResultCode::SysFD_EdgeWrite;
}

close(edge_fd);
Expand Down Expand Up @@ -476,6 +475,12 @@ int _blocking_wait_for_edge(int gpio, int channel_id, Edge edge, uint64_t bounce
auto ftg_it = _fd_to_gpio_map.find(geo->fd);
if (ftg_it != _fd_to_gpio_map.end())
_fd_to_gpio_map.erase(ftg_it);

// Close the fd
if (close(geo->fd) == -1) {
std::cerr << "[WARNING] Failed to close Epoll_Thread file descriptor\n";
}

auto geo_it = _gpio_events.find(gpio);
if (geo_it != _gpio_events.end())
_gpio_events.erase(geo_it);
Expand Down

0 comments on commit cc002fc

Please sign in to comment.