diff --git a/app/src/main/cpp/GPIOControl.cpp b/app/src/main/cpp/GPIOControl.cpp index 6310b983..c2587925 100644 --- a/app/src/main/cpp/GPIOControl.cpp +++ b/app/src/main/cpp/GPIOControl.cpp @@ -31,20 +31,73 @@ typedef struct char str[MAX_STRING_LEN]; }IOT_PARAM; + +std::mutex GpioControl::m_locker; +std::vector> GpioControl::m_references; + void GpioControl::setInt(int cmd, int value) { - int fd = open(GPIO_NODE_MP, O_RDONLY); + int fd = -1; IOT_PARAM param; param.cmd = cmd; param.value = value; + int res = 0; // LOGE("set_int fd=%d,cmd=%d,value=%d\r\n",fd, cmd, value); - if( fd > 0 ) + + uint32_t references = (value != 0) ? 1 : 0; + std::vector >::iterator it; + + if (value) { - int res = ioctl(fd, IOT_PARAM_WRITE, ¶m); - // LOGE("set_int22 cmd=%d,value=%d,result=%d\r\n",param.cmd, param.value, param.result); - close(fd); + m_locker.lock(); + fd = open(GPIO_NODE_MP, O_RDONLY); + if( fd > 0 ) + { + res = ioctl(fd, IOT_PARAM_WRITE, ¶m); + close(fd); + // check res??? + for (it = m_references.begin(); it != m_references.end(); ++it) + { + if (it->first == cmd) + { + it->second++; + references = it->second; + break; + } + } + if (it == m_references.end()) + { + m_references.push_back(std::pair(cmd, references)); + } + } + m_locker.unlock(); + } + else + { + m_locker.lock(); + for (it = m_references.begin(); it != m_references.end(); ++it) + { + if (it->first == cmd) + { + if (it->second > 0) + { + it->second--; + } + references = it->second; + break; + } + } + + if (references == 0) + { + fd = open(GPIO_NODE_MP, O_RDONLY); + if (fd > 0) { + res = ioctl(fd, IOT_PARAM_WRITE, ¶m); + close(fd); + } + } + m_locker.unlock(); } - return; } int GpioControl::getInt(int cmd) diff --git a/app/src/main/cpp/GPIOControl.h b/app/src/main/cpp/GPIOControl.h index 920c114b..047d0f08 100644 --- a/app/src/main/cpp/GPIOControl.h +++ b/app/src/main/cpp/GPIOControl.h @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include #define CMD_GET_LIGHT_ADC 101 #define CMD_SET_LIGHT_ADC 102 @@ -176,6 +179,10 @@ class GpioControl { +private: + static std::mutex m_locker; + static std::vector> m_references; + public: static void setInt(int cmd, int value);