diff --git a/app/src/main/cpp/GPIOControl.cpp b/app/src/main/cpp/GPIOControl.cpp index b76b814c..b20d939c 100644 --- a/app/src/main/cpp/GPIOControl.cpp +++ b/app/src/main/cpp/GPIOControl.cpp @@ -20,86 +20,122 @@ #define IOT_PARAM_WRITE 0xAE #define IOT_PARAM_READ 0xAF -#define MAX_STRING_LEN 32 - -typedef struct -{ - int cmd; - int value; - int result; - long value2; - 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 GpioControl::turnOnImpl(const IOT_PARAM& param) { - int fd = -1; - IOT_PARAM param = { cmd, value, 0 }; - // param.cmd = cmd; - // param.value = value; + uint32_t references = 1; + std::vector >::iterator it; int res = 0; + int fd = -1; - uint32_t references = (value != 0) ? 1 : 0; + 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 == param.cmd) + { + it->second++; + references = it->second; + break; + } + } + if (it == m_references.end()) + { + m_references.push_back(std::pair(cmd, references)); + } + } + + return references; +} + +int GpioControl::turnOffImpl(const IOT_PARAM& param) +{ + uint32_t references = 0; std::vector >::iterator it; + int res = 0; + int fd = -1; - if (value) + for (it = m_references.begin(); it != m_references.end(); ++it) + { + if (it->first == param.cmd) + { + if (it->second > 0) + { + it->second--; + } + references = it->second; + break; + } + } + + if (references == 0) { - m_locker.lock(); fd = open(GPIO_NODE_MP, O_RDONLY); - if( fd > 0 ) + if (fd > 0) { res = ioctl(fd, IOT_PARAM_WRITE, ¶m); #ifdef _DEBUG ALOGI("setInt cmd=%d,value=%d,result=%d\r\n",param.cmd, param.value, param.result); #endif 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)); - } } + } + + return references; +} + +void GpioControl::setInt(int cmd, int value) +{ + IOT_PARAM param = { cmd, value, 0 }; + // param.cmd = cmd; + // param.value = value; + + if (value) + { + m_locker.lock(); + turnOnImpl(param); m_locker.unlock(); } else { m_locker.lock(); - for (it = m_references.begin(); it != m_references.end(); ++it) + turnOffImpl(param); + m_locker.unlock(); + } +} + +static void GpioControl::setInt(const std::vector& cmds, int value) +{ + IOT_PARAM param = { 0, value, 0 }; + // param.cmd = cmd; + // param.value = value; + + std::vector::const_iterator it; + + if (value) + { + m_locker.lock(); + for (it = cmds.cbegin(); it != cmds.cend(); ++it) { - if (it->first == cmd) - { - if (it->second > 0) - { - it->second--; - } - references = it->second; - break; - } + param.cmd = *it; + turnOnImpl(param); } - - if (references == 0) + m_locker.unlock(); + } + else + { + m_locker.lock(); + for (it = cmds.cbegin(); it != cmds.cend(); ++it) { - fd = open(GPIO_NODE_MP, O_RDONLY); - if (fd > 0) { - res = ioctl(fd, IOT_PARAM_WRITE, ¶m); -#ifdef _DEBUG - ALOGI("setInt cmd=%d,value=%d,result=%d\r\n",param.cmd, param.value, param.result); -#endif - close(fd); - } + param.cmd = *it; + turnOffImpl(param); } m_locker.unlock(); } diff --git a/app/src/main/cpp/GPIOControl.h b/app/src/main/cpp/GPIOControl.h index c7bfc084..95eca7f7 100644 --- a/app/src/main/cpp/GPIOControl.h +++ b/app/src/main/cpp/GPIOControl.h @@ -113,15 +113,30 @@ #define GPIO_NODE_MP "/dev/mtkgpioctrl" +#define MAX_STRING_LEN 32 +typedef struct +{ + int cmd; + int value; + int result; + long value2; + char str[MAX_STRING_LEN]; +}IOT_PARAM; + class GpioControl { private: static std::mutex m_locker; static std::vector> m_references; -public: +protected: + static int turnOnImpl(const IOT_PARAM& param); + static int turnOffImpl(const IOT_PARAM& param); +public: static void setInt(int cmd, int value); + static void setInt(const std::vector& cmds, int value); + static int getInt(int cmd); static void setLong(int cmd, long value); static long getLong(int cmd);