// // Created by Matthew on 2023/12/27. // #include #include #include #include #include #include #include #include #include "GPIOControl.h" #ifdef _DEBUG #include #endif #define IOT_PARAM_WRITE 0xAE #define IOT_PARAM_READ 0xAF std::mutex GpioControl::m_locker; std::vector> GpioControl::m_references; int GpioControl::turnOnImpl(const IOT_PARAM& param) { uint32_t references = 1; std::vector >::iterator it; int res = 0; int fd = -1; 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; 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) { 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); } } 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(); 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) { param.cmd = *it; turnOnImpl(param); } m_locker.unlock(); } else { m_locker.lock(); for (it = cmds.cbegin(); it != cmds.cend(); ++it) { param.cmd = *it; turnOffImpl(param); } m_locker.unlock(); } } int GpioControl::getInt(int cmd) { int fd = open(GPIO_NODE_MP, O_RDONLY); // LOGE("get_int fd=%d,cmd=%d\r\n",fd, cmd); if( fd > 0 ) { IOT_PARAM param; param.cmd = cmd; ioctl(fd, IOT_PARAM_READ, ¶m); #ifdef _DEBUG ALOGI("getInt cmd=%d,value=%d,result=%d\r\n",param.cmd, param.value, param.result); #endif close(fd); return param.value; } return -1; } void GpioControl::setLong(int cmd, long value) { int fd = open(GPIO_NODE_MP, O_RDONLY); IOT_PARAM param; param.cmd = cmd; param.value2 = value; // LOGE("set_long fd=%d,cmd=%d,value2=%ld\r\n",fd, param.cmd, param.value2); if( fd > 0 ) { ioctl(fd, IOT_PARAM_WRITE, ¶m); // LOGE("set_long22 cmd=%d,value2=%ld,result=%d\r\n",param.cmd, param.value2, param.result); close(fd); } } long GpioControl::getLong(int cmd) { int fd = open(GPIO_NODE_MP, O_RDONLY); // LOGE("get_long fd=%d,cmd=%d\r\n",fd, cmd); if( fd > 0 ) { IOT_PARAM param; param.cmd = cmd; ioctl(fd, IOT_PARAM_READ, ¶m); // LOGE("get_long22 cmd=%d,value2=%ld,result=%d\r\n",param.cmd, param.value2, param.result); close(fd); return param.value2; } return -1; } void GpioControl::setString(int cmd, const std::string& value) { IOT_PARAM param; int fd = open(GPIO_NODE_MP, O_RDONLY); int len = MAX_STRING_LEN < value.size() ? MAX_STRING_LEN : value.size(); param.cmd = cmd; memset(param.str, 0, MAX_STRING_LEN); memcpy(param.str, value.c_str(), len); // LOGE("set_string fd=%d,cmd=%d,str=%s\r\n",fd, param.cmd, param.str); if( fd > 0 ) { ioctl(fd, IOT_PARAM_WRITE, ¶m); // LOGE("set_string22 cmd=%d,str=%s,result=%d\r\n",param.cmd, param.str, param.result); close(fd); } return; } std::string GpioControl::getString(int cmd) { int fd = open(GPIO_NODE_MP, O_RDONLY); // LOGE("get_string fd=%d,cmd=%d\r\n",fd, cmd); if( fd > 0 ) { IOT_PARAM param; param.cmd = cmd; ioctl(fd, IOT_PARAM_READ, ¶m); // LOGE("get_string22 cmd=%d,str=%s,result=%d\r\n",param.cmd, param.str, param.result); close(fd); return std::string(param.str); } return ""; }