From 4e19523db885042754c69ae47b891fd80d8dad88 Mon Sep 17 00:00:00 2001 From: NoHomey Date: Thu, 29 Sep 2016 17:58:12 +0300 Subject: [PATCH] adding disposing function for all resources opening a file descriptor --- src/wiringPiI2C.cc | 25 ++++++++++++++++++++++++- src/wiringPiSPI.cc | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/wiringPiI2C.cc b/src/wiringPiI2C.cc index 3172e58..ed3927b 100644 --- a/src/wiringPiI2C.cc +++ b/src/wiringPiI2C.cc @@ -1,5 +1,6 @@ #include "wiringPiI2C.h" #include +#include DECLARE(wiringPiI2CRead); DECLARE(wiringPiI2CReadReg8); @@ -9,6 +10,7 @@ DECLARE(wiringPiI2CWriteReg8); DECLARE(wiringPiI2CWriteReg16); DECLARE(wiringPiI2CSetupInterface); DECLARE(wiringPiI2CSetup); +DECLARE(wiringPiI2CClose); // Func : int wiringPiI2CRead (int fd); // Simple device read. Some devices present data when you read them without having to do any register transactions. @@ -188,6 +190,26 @@ IMPLEMENT(wiringPiI2CSetup) { SCOPE_CLOSE(INT32(res)); } +// Func : void wiringPiI2CClose(const int fd) +// Description : This closes opened I2C file descriptor +// fd is file descriptor returned either from wiringPiI2CSetup or wiringPiI2CSetupInterface + +IMPLEMENT(wiringPiI2CClose) { + SCOPE_OPEN(); + + SET_ARGUMENT_NAME(0, fd); + + CHECK_ARGUMENTS_LENGTH_EQUAL(1); + + CHECK_ARGUMENT_TYPE_INT32(0); + + int fd = GET_ARGUMENT_AS_INT32(0); + + ::close(fd); + + SCOPE_CLOSE(UNDEFINED()); +} + IMPLEMENT_EXPORT_INIT(wiringPiI2C) { EXPORT_FUNCTION(wiringPiI2CRead); EXPORT_FUNCTION(wiringPiI2CReadReg8); @@ -197,4 +219,5 @@ IMPLEMENT_EXPORT_INIT(wiringPiI2C) { EXPORT_FUNCTION(wiringPiI2CWriteReg16); EXPORT_FUNCTION(wiringPiI2CSetupInterface); EXPORT_FUNCTION(wiringPiI2CSetup); -} + EXPORT_FUNCTION(wiringPiI2CClose); +} \ No newline at end of file diff --git a/src/wiringPiSPI.cc b/src/wiringPiSPI.cc index c0a0ee5..e477fb6 100644 --- a/src/wiringPiSPI.cc +++ b/src/wiringPiSPI.cc @@ -1,10 +1,12 @@ #include "wiringPiSPI.h" #include +#include DECLARE(wiringPiSPIGetFd); DECLARE(wiringPiSPIDataRW); DECLARE(wiringPiSPISetup); DECLARE(wiringPiSPISetupMode); +DECLARE(wiringPiSPIClose); // Func : int wiringPiSPIGetFd(int channel) @@ -98,9 +100,30 @@ IMPLEMENT(wiringPiSPISetupMode) { SCOPE_CLOSE(INT32(res)); } +// Func : void wiringPiSPIClose(const int fd) +// Description : This closes opened SPI file descriptor +// fd is file descriptor returned either from wiringPiSPISetup or wiringPiSPISetupMode + +IMPLEMENT(wiringPiSPIClose) { + SCOPE_OPEN(); + + SET_ARGUMENT_NAME(0, fd); + + CHECK_ARGUMENTS_LENGTH_EQUAL(1); + + CHECK_ARGUMENT_TYPE_INT32(0); + + int fd = GET_ARGUMENT_AS_INT32(0); + + ::close(fd); + + SCOPE_CLOSE(UNDEFINED()); +} + IMPLEMENT_EXPORT_INIT(wiringPiSPI) { EXPORT_FUNCTION(wiringPiSPIGetFd); EXPORT_FUNCTION(wiringPiSPIDataRW); EXPORT_FUNCTION(wiringPiSPISetup); EXPORT_FUNCTION(wiringPiSPISetupMode); + EXPORT_FUNCTION(wiringPiSPIClose); } \ No newline at end of file