#include "kernelModuleFactory.h" #include "kernelModuleEmbedVideo.h" #include "kernelModuleFlyHunter.h" #include "kernelModuleKafka.h" #include "kernelModuleModemManager.h" #include "kernelModuleOnvifObserver.h" #include "kernelModuleSSDP.h" #include "kernelModuleUSBRelay.h" #include "kernelModuleBosch.h" #include "kernelModuleAttentionCloud.h" #ifdef MEM_DEBUG #include "../alg/generic/heapDebug.h" #endif #ifdef USE_MYSQL #include "MySQL_dataBinder.h" #else #include "SQLite_dataBinder.h" #endif #define LOG(X) m_pLog->Log("MFAC", this, X) #define LOG_ERROR(X) m_pLog->Log("MFAC", this, _LOG_ERROR, X) kernelModuleFactory * kernelModuleFactory::_instance = NULL; /** * Returns the single instance of the object. */ kernelModuleFactory * kernelModuleFactory::getInstance(uint32_t InstanceID) { if (NULL == _instance) { _instance = new kernelModuleFactory(InstanceID); }; return _instance; } kernelModuleFactory::kernelModuleFactory(uint32_t InstanceID) : basicThread() { m_nInstanceID = InstanceID; m_bInitialValidationDone = false; m_bInitialLoadModulesDone = false; m_pLog = basicLogCollection::getInstance(); #ifdef USE_MYSQL m_pDBC = mysqlDBC::getInstance(); #else m_pDBC = sqliteDBC::getInstance(); #endif init(); } kernelModuleFactory::~kernelModuleFactory() { waitThreadEnd(true); shutdownServices(true); _instance = NULL; } void kernelModuleFactory::shutdownServices(bool force) { #ifdef DEBUG LOG("shutdownServices"); #endif m_mModulesProtector.lock(); if (m_vKernelModules.size() > 0) { for (vectorKernelModules::iterator index = m_vKernelModules.begin(); index != m_vKernelModules.end(); index++) { #ifdef DEBUG char tB[128]; switch ((*index)->getType()) { // case KERNEL_MODULE_ZLMEDIA: sprintf( tB, "Stop Service of ZlMedia Module" ); break; case KERNEL_MODULE_DCBUSB : sprintf(tB, "Stop Service of DCUSB Module"); break; case KERNEL_MODULE_KAFKA : sprintf(tB, "Stop Service of KAFKA Module"); break; case KERNEL_MODULE_GPS : sprintf(tB, "Stop Service of GPS Module"); break; case KERNEL_MODULE_NETWORK : sprintf(tB, "Stop Service of Network Module"); break; case KERNEL_MODULE_FLYHUNTER : sprintf(tB, "Stop Service of FlyHunter Module"); break; case KERNEL_MODULE_SSDP_ANONCE : sprintf(tB, "Stop Service of SSDP Module"); break; case KERNEL_MODULE_ONVIF_OBSERVER: sprintf(tB, "Stop Service of OnVif Observer Module"); break; case KERNEL_MODULE_EMBED_VIDEO : sprintf(tB, "Stop Service of EmbedVideo Module"); break; case KERNEL_MODULE_MODEM_MANAGER : sprintf(tB, "Stop Service of Modem Manager Module"); break; case KERNEL_MODULE_BOSCH_REST : sprintf(tB, "Stop Service of Bosch REST Module"); break; case KERNEL_MODULE_ATTENTION_CLOUD: sprintf(tB, "Stop Service of Attention Cloud Module"); break; default : sprintf(tB, "Stop Service of UNKNOWN[%d] Module", (*index)->getType()); break; }; LOG(tB); #endif (*index)->stopService( force ); #ifdef DEBUG LOG("Done"); #endif deleteModule(*index); #ifdef DEBUG LOG("Module is Deleted"); #endif }; m_vKernelModules.clear(); }; m_mModulesProtector.unlock(); #ifdef DEBUG LOG("Done shutdownServices"); #endif } bool kernelModuleFactory::loadModules() { DBC_INT_LIST * tList = NULL; if (m_pDBC == NULL) { return false; } tList = m_pDBC->listModules(); if (tList != NULL) { if (tList->size()) { #ifdef DEBUG char tB[128]; sprintf(tB, "Have %lu Modules to Load!", tList->size()); LOG(tB); #endif for (DBC_INT_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++) { createModule(*idx); }; tList->clear(); } #ifdef DEBUG else { LOG("No Modules Defined"); } #endif delete(tList); } #ifdef DEBUG else { LOG("No Modules Defined"); } #endif return m_bInitialLoadModulesDone = true; } kernelModule * kernelModuleFactory::createModule(int id) { kernelModule * tVal = NULL; uint8_t cType = 0; if (m_pDBC != NULL) { DBC_MODULE_DATA * tData = NULL; tData = m_pDBC->getModuleData(id); if (tData != NULL) { cType = tData->type; switch (cType) { case KERNEL_MODULE_DCBUSB: #ifdef DEBUG LOG("Creating Dry Contact USB Module"); #endif tVal = new kernelModuleUSBRelay(tData->module_id); break; case KERNEL_MODULE_KAFKA: #ifdef DEBUG LOG("Creating KAFKA Module"); #endif tVal = new kernelModuleKafka(tData->module_id); break; case KERNEL_MODULE_FLYHUNTER: #ifdef DEBUG LOG("Creating Fly Hunter Module"); #endif tVal = new kernelModuleFlyHunter(tData->module_id); break; case KERNEL_MODULE_SSDP_ANONCE: #ifdef DEBUG LOG("Creating SSDP Announcer Module"); #endif tVal = new kernelModuleSSDPAnnouncer(tData->module_id); break; case KERNEL_MODULE_ONVIF_OBSERVER: #ifdef DEBUG LOG("Creating OnVif Observer Module"); #endif if (m_pDBC->isLocal()) { tVal = new kernelModuleOnvifObserver(tData->module_id); #ifdef DEBUG } else { LOG("Module is Disabled Since Remote Database!"); #endif }; break; case KERNEL_MODULE_EMBED_VIDEO: #ifdef DEBUG char tT[129]; sprintf(tT, "Creating Embed Video Module %d [%d] {%p}", id, tData->module_id, (void *)tData); LOG(tT); #endif if (m_pDBC->isLocal()) { tVal = new kernelModuleEmbedVideo(tData->module_id); #ifdef DEBUG } else { LOG("Module is Disabled Since Remote Database!"); #endif }; break; case KERNEL_MODULE_MODEM_MANAGER: #ifdef DEBUG LOG("Creating Modem Manager Module"); #endif tVal = new kernelModuleModemManager(tData->module_id); break; case KERNEL_MODULE_BOSCH_REST: #ifdef DEBUG LOG("Creating Bosch REST Module"); #endif tVal = new kernelModuleBosch(tData->module_id); break; case KERNEL_MODULE_ATTENTION_CLOUD: #ifdef DEBUG LOG("Creating Attention Cloud Module"); #endif tVal = new kernelAttentionCloud(tData->module_id); break; }; free(tData); } else { LOG_ERROR("No Module Data Fetched!"); }; } else { LOG_ERROR("No Database Object Assigned!"); }; if (tVal != NULL) { if (tVal->startService()) { m_mModulesProtector.lock(); m_vKernelModules.push_back(tVal); m_mModulesProtector.unlock(); } else { switch (cType) { case KERNEL_MODULE_DCBUSB : delete((kernelModuleUSBRelay *) tVal); break; case KERNEL_MODULE_KAFKA : delete((kernelModuleKafka *) tVal); break; case KERNEL_MODULE_FLYHUNTER : delete((kernelModuleFlyHunter *) tVal); break; case KERNEL_MODULE_SSDP_ANONCE: delete((kernelModuleSSDPAnnouncer *) tVal); break; case KERNEL_MODULE_ONVIF_OBSERVER: if (m_pDBC->isLocal()) { delete((kernelModuleOnvifObserver *) tVal); }; break; case KERNEL_MODULE_EMBED_VIDEO: if (m_pDBC->isLocal()) { delete((kernelModuleEmbedVideo *) tVal); }; break; case KERNEL_MODULE_MODEM_MANAGER: delete((kernelModuleModemManager *) tVal); break; case KERNEL_MODULE_BOSCH_REST: delete((kernelModuleBosch *) tVal); break; case KERNEL_MODULE_ATTENTION_CLOUD: delete((kernelAttentionCloud *) tVal); break; // kernelModule has a virtual destructor: plain delete is correct for any other type default: delete(tVal); break; }; // (tVal was deleted above - a second delete here was a double free) tVal = NULL; LOG_ERROR("Error Starting Module Service!"); }; }; return tVal; } void kernelModuleFactory::deleteModule(kernelModule * M) { if (M) { M->stopService(true); switch (M->getType()) { case KERNEL_MODULE_DCBUSB : delete((kernelModuleUSBRelay *) M); break; case KERNEL_MODULE_KAFKA : delete((kernelModuleKafka *) M); break; case KERNEL_MODULE_FLYHUNTER : delete((kernelModuleFlyHunter *) M); break; case KERNEL_MODULE_SSDP_ANONCE : delete((kernelModuleSSDPAnnouncer *) M); break; case KERNEL_MODULE_ONVIF_OBSERVER: delete((kernelModuleOnvifObserver *) M); break; case KERNEL_MODULE_EMBED_VIDEO : delete((kernelModuleEmbedVideo *) M); break; case KERNEL_MODULE_MODEM_MANAGER : delete((kernelModuleModemManager *) M); break; case KERNEL_MODULE_BOSCH_REST : delete((kernelModuleBosch *) M); break; case KERNEL_MODULE_ATTENTION_CLOUD: delete((kernelAttentionCloud *) M); break; default: LOG("Can not Delete Unknown Type Module!"); }; }; } void kernelModuleFactory::deleteModule(int id) { if (m_vKernelModules.size() > (size_t) id) { deleteModule(m_vKernelModules.at(id)); m_vKernelModules.erase(m_vKernelModules.begin() + id); m_vKernelModules.shrink_to_fit(); }; } uint32_t kernelModuleFactory::getModulesCount(KERNEL_MODULE_TYPE t) { uint32_t cnt = 0; m_mModulesProtector.lock(); if (m_vKernelModules.size()) { for (size_t i = 0; i < m_vKernelModules.size(); i++) { if (m_vKernelModules.at(i)->getType() == t) { cnt++; } }; }; m_mModulesProtector.unlock(); return cnt; } uint32_t kernelModuleFactory::fetchModules(KERNEL_MODULE_TYPE t, kernelModule **& RA) { uint32_t cnt = getModulesCount(t); RA = NULL; if (cnt > 0) { RA = (kernelModule **) calloc(cnt, sizeof(kernelModule *)); if (RA != NULL) { m_mModulesProtector.lock(); if (m_vKernelModules.size()) { uint32_t idx = 0; for (size_t i = 0; i < m_vKernelModules.size(); i++) { if (m_vKernelModules.at(i)->getType() == t) { RA[idx] = m_vKernelModules.at(i); idx++; }; }; }; m_mModulesProtector.unlock(); } else { cnt = 0; } }; return cnt; } kernelModule * kernelModuleFactory::getModule(uint32_t id) { kernelModule * RetVal = NULL; m_mModulesProtector.lock(); if (m_vKernelModules.size()) { for (vectorKernelModules::iterator idx = m_vKernelModules.begin(); idx != m_vKernelModules.end(); idx++) { if ((*idx)->getId() == id) { RetVal = (*idx); break; }; }; }; m_mModulesProtector.unlock(); return RetVal; } kernelModule * kernelModuleFactory::getModule(KERNEL_MODULE_TYPE t, uint32_t id) { kernelModule * RetVal = NULL; m_mModulesProtector.lock(); if (m_vKernelModules.size()) { for (vectorKernelModules::iterator idx = m_vKernelModules.begin(); idx != m_vKernelModules.end(); idx++) { if (id) { if (((*idx)->getType() == t) && ((*idx)->getId() == id)) { RetVal = (*idx); break; }; } else { if ((*idx)->getType() == t) { RetVal = (*idx); break; }; }; }; }; m_mModulesProtector.unlock(); return RetVal; } bool kernelModuleFactory::validate(void) { return (m_bThreadStatus < TH_ENDED); } void kernelModuleFactory::m_fThreadProc(void) { if (m_pDBC == NULL) { m_bThreadStatus = TH_ENDED; } else { DBC_INT_LIST * tList = NULL; #ifdef MEM_DEBUG size_t memStart, memEnd, memDiff; char MEM_MSG[256]; #endif pthread_setname_np( pthread_self(), "ModuleFactory" ); m_bThreadStatus = TH_WORKING; while (m_bThreadStatus < TH_ENDED){ if (m_bInitialLoadModulesDone){ #ifdef MEM_DEBUG memStart = getCurrentRSS(); #endif m_mModulesProtector.lock(); if (m_vKernelModules.size()){ DBC_INT_LIST tL; for ( size_t cm = 0; cm < m_vKernelModules.size(); cm++){ tL.push_back(m_vKernelModules.at(cm)->getId()); } tList = m_pDBC->listNewModules(&tL); tL.clear(); } else { tList = m_pDBC->listNewModules(NULL); }; m_mModulesProtector.unlock(); if (tList != NULL) { #ifdef DEBUG LOG("New Modules Exists!"); #endif for (size_t index = 0; index < tList->size(); index++){ createModule(tList->at(index)); } tList->clear(); delete(tList); }; #ifdef MEM_DEBUG memEnd = getCurrentRSS(); memDiff = memEnd - memStart; sprintf(MEM_MSG, "MEM START: %d MEM DIF1: %d", memStart, memDiff); LOG(MEM_MSG); #endif m_mModulesProtector.lock(); if (m_vKernelModules.size()) { // Get Full List // tList = NULL; tList = m_pDBC->listModules(); if (tList != NULL){ size_t cm = 0; while (cm < m_vKernelModules.size()) { // check if module removed bool tRemoved = true; for ( size_t lm = 0; lm < tList->size(); lm++) { if (tList->at(lm) == (int) m_vKernelModules.at(cm)->getId()) { tRemoved = false; break; }; }; if (! tRemoved) { // validate module m_vKernelModules.at(cm)->validate(); cm++; } else { #ifdef DEBUG LOG("Delete Module!"); #endif deleteModule(cm); }; }; tList->clear(); delete(tList); } else { #ifdef DEBUG LOG("All Modules Must be Deleted!"); #endif while (m_vKernelModules.size()) { deleteModule(0); } }; } #ifdef DEBUG_EXTRA else { LOG("No Modules to Validate!"); } #endif ; m_mModulesProtector.unlock(); #ifdef MEM_DEBUG memEnd = getCurrentRSS(); memDiff = memEnd - memStart; sprintf(MEM_MSG, "MEM START: %lu MEM DIF2: %lu", memStart, memDiff); LOG(MEM_MSG); #endif if (! m_bInitialValidationDone) { m_bInitialValidationDone = true; } }; for (int i = (5000 / DEFAULT_THREAD_SLEEP_TIME); i && (m_bThreadStatus < TH_ENDED); i--) { SLEEP(DEFAULT_THREAD_SLEEP_TIME); } }; }; } //--enable-gpl --enable-libx264 --enable-nonfree --extra-ldflags=-latomic --enable-omx --enable-omx-rpi //-rtsp_transport tcp #undef LOG #undef LOG_ERROR