You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

920 lines
33 KiB

6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
6 years ago
​
​
​
6 years ago
​
​
6 years ago
6 years ago
​
​
​
​
​
6 years ago
​
​
6 years ago
6 years ago
6 years ago
5 years ago
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
6 years ago
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
​
​
6 years ago
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
6 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
6 years ago
​
​
​
​
6 years ago
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
5 years ago
​
​
​
5 years ago
​
​
​
​
​
5 years ago
​
5 years ago
​
​
​
6 years ago
​
​
  1. /*******************************************************************
  2. *
  3. * DESCRIPTION: ARGUS Kernel source
  4. *
  5. * AUTHOR: gagua
  6. *
  7. * HISTORY:
  8. *
  9. * DATE:5/29/2019
  10. *
  11. *******************************************************************/
  12. #include "rgooseKernel.h"
  13. #include "rgooseFilterAPF.h"
  14. #include <sys/types.h>
  15. #include <sys/sysinfo.h>
  16. #include <sys/statvfs.h>
  17. #include "../data/json/json_tool.h"
  18. #include <strings.h>
  19. #include <json/value.h>
  20. #include <json/writer.h>
  21. #include <json/reader.h>
  22. #define LOG( X ) m_pLog->Log( "KRNL", this, X )
  23. #ifdef DEBUG
  24. #define LOG_ERROR( X ) {\
  25. char _tBfrErrMSG2w34[256];\
  26. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  27. m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  28. }
  29. #else
  30. #define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  31. #endif
  32. argusKernel *argusKernel::_instance = NULL;
  33. /**
  34. * Returns the single instance of the object.
  35. */
  36. argusKernel* argusKernel::getInstance( bool cold_start, config_setting_t *Settings ) {
  37. // check if the instance has been created yet
  38. if( 0 == _instance ){
  39. // if not, then create it
  40. _instance = new argusKernel( cold_start, Settings );
  41. };
  42. // return the single instance
  43. return _instance;
  44. };
  45. argusKernel::argusKernel( bool cold_start, config_setting_t *Settings ):basicThread(), basicEventHandler(){
  46. m_bExitCause = 0;
  47. m_nServerID = 0;
  48. m_nSiteID = 0;
  49. m_pDBC = NULL;
  50. m_bSkipResignComponents = cold_start;
  51. m_tTimeTSeconds = time( NULL );
  52. m_pLog = basicLogCollection::getInstance();
  53. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" );
  54. //m_pServiceAnnouncer = NULL;
  55. m_pEventHeap = NULL;
  56. //m_pOnvifMediaObserver = NULL;
  57. m_pNodeFactory = NULL;
  58. m_pModuleFactory = NULL;
  59. #ifdef SIMULATE_KERNEL_TARGETS
  60. sprintf( m_cCurrentMission,"" );
  61. m_bLandingTO = 0;
  62. m_bJamingTO = 0;
  63. #endif
  64. };
  65. argusKernel::~argusKernel(){
  66. WaitFinish( true );
  67. #ifdef DEBUG
  68. LOG(" Deleting Node Factory...");
  69. #endif
  70. if( m_pNodeFactory != NULL ) delete( m_pNodeFactory );
  71. #ifdef DEBUG
  72. LOG(" Deleting Module Factory...");
  73. #endif
  74. if( m_pModuleFactory != NULL ) delete( m_pModuleFactory );
  75. #ifdef DEBUG
  76. LOG(" Deleting Target Filters...");
  77. #endif
  78. unloadFilters();
  79. if( m_bExitCause == 0 ) {
  80. #ifdef DEBUG
  81. LOG("Unregister Server");
  82. #endif
  83. m_pDBC->unregisterServer( m_nSiteID, m_nServerNodeID );
  84. #ifdef DEBUG
  85. LOG("Close DataBase Connection...");
  86. #endif
  87. if( m_pDBC != NULL ) delete( m_pDBC );
  88. m_pDBC = NULL;
  89. } else {
  90. if( m_bExitCause == 4 ) {
  91. #ifdef DEBUG
  92. LOG("HW PNP Detection Requested...");
  93. #endif
  94. m_pHardwareManager->enumerateHardware();
  95. };
  96. #ifdef DEBUG
  97. LOG("Close DataBase Connection...");
  98. #endif
  99. if( m_pDBC != NULL ) delete( m_pDBC );
  100. m_pDBC = NULL;
  101. };
  102. _instance = NULL;
  103. };
  104. bool argusKernel::create( void ){
  105. char tBuffer[256];
  106. if( m_pSettings != NULL ) {
  107. config_setting_t * pSetting;
  108. int tVal;
  109. const char * tBuf;
  110. LOG("Getting Settings ...");
  111. if( !config_setting_lookup_string( m_pSettings, ARGUS_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  112. char * tVar;
  113. char * tTS;
  114. tTS = getTStr(NULL);
  115. tVar = MD5String(tTS);
  116. free(tTS);
  117. sprintf( m_sIdentity, tVar );
  118. pSetting = config_setting_add( m_pSettings, ARGUS_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  119. config_setting_set_string( pSetting, tVar );
  120. free( tVar );
  121. basicSettingsProvider::getInstance()->compromise();
  122. LOG("Server Identity not defined - generating new!");
  123. } else {
  124. sprintf( m_sIdentity, tBuf );
  125. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  126. LOG( tBuffer );
  127. };
  128. if( !config_setting_lookup_int( m_pSettings, ARGUS_KERNEL_TAG_SITE_ID, &tVal ) ){
  129. tVal = m_nSiteID = 1;
  130. pSetting = config_setting_add( m_pSettings, ARGUS_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  131. config_setting_set_int( pSetting, tVal );
  132. basicSettingsProvider::getInstance()->compromise();
  133. LOG("Site ID not defined - using [1] as number!");
  134. } else {
  135. m_nSiteID = tVal;
  136. sprintf( tBuffer, "Site ID Read From Settings: %d", m_nSiteID );
  137. LOG( tBuffer );
  138. };
  139. } else {
  140. LOG_ERROR("Failed Read Config Settings!");
  141. return false;
  142. };
  143. if( m_pDBC == NULL ) {
  144. LOG("Explicitly Create DBC Object");
  145. m_pDBC = argusDBC::getInstance( m_pSettings );
  146. };
  147. if( m_pDBC == NULL ){
  148. LOG_ERROR("DBC Object Creation Failure!");
  149. return false;
  150. } else LOG("DBC Object is OK");
  151. if(!m_pDBC->connect()){
  152. LOG_ERROR("DBC Connection Failure!");
  153. SLEEP(5000);
  154. //ToDO: Set Restart Kernel!
  155. return false;
  156. };
  157. #ifdef DEBUG
  158. LOG( "Settings Read Done" );
  159. #endif
  160. m_pDBC->registerServer( m_nSiteID, m_sIdentity, getpid() );
  161. m_nServerID = m_pDBC->getServerID( m_sIdentity );
  162. m_nServerNodeID = m_pDBC->getServerNode( m_nServerID );
  163. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( m_nSiteID );
  164. if( tSiteData ) {
  165. C3D_WGS84_LOC t;
  166. t.lat = tSiteData->lat;
  167. t.lon = tSiteData->lon;
  168. t.alt = tSiteData->alt;
  169. m_pSystemAnchor = systemAnchor::getInstance( tSiteData->SH, &t );
  170. free( tSiteData );
  171. } else {
  172. LOG_ERROR("Error Loading Site DATA!");
  173. };
  174. //Init Subcomponents
  175. m_pHardwareManager = argusHardwareManager::getInstance( m_bSkipResignComponents );
  176. if( m_pHardwareManager!= NULL ) {
  177. m_pHardwareManager->validate();
  178. } else {
  179. LOG_ERROR("Failure to get HardwareManager Instance!");
  180. return false;
  181. };
  182. m_pDBC->setNodeOnline( m_nServerNodeID, ARGUS_NODE_STATUS_WORK );
  183. //m_pServiceAnnouncer = new ssdpAnnouncer( m_pSettings );
  184. basicSettingsProvider::getInstance()->validate();
  185. m_pEventHeap = basicEventHeap::getInstance();
  186. m_pModuleFactory = argusModuleFactory::getInstance( m_nServerID );
  187. if( !m_pModuleFactory ) {
  188. LOG_ERROR("Failure to get Module Factory Instance!");
  189. return false;
  190. } else {
  191. #ifdef DEBUG
  192. LOG( "Module Factory Created" );
  193. #endif
  194. while( !m_pModuleFactory->isValidationDone() ) SLEEP( 10 );
  195. #ifdef DEBUG
  196. LOG( "Module Factory Validated" );
  197. #endif
  198. };
  199. #ifdef DEBUG
  200. LOG( "Load Filters" );
  201. #endif
  202. loadFilters();
  203. #ifdef DEBUG
  204. LOG( "Filters Loaded" );
  205. #endif
  206. m_pNodeFactory = argusNodeFactory::getInstance( m_nSiteID );
  207. if( !m_pNodeFactory ) {
  208. LOG_ERROR("Failure to get Node Factory Instance!");
  209. return false;
  210. }
  211. #ifdef DEBUG
  212. else {
  213. LOG( "Node Factory Created" );
  214. }
  215. #endif
  216. ;
  217. #ifdef DEBUG
  218. LOG( "Kernel Trhead is about to be Created" );
  219. #endif
  220. if( init() == false ){
  221. LOG_ERROR("Error in starting Kernel Thread!");
  222. return false;
  223. };
  224. #ifdef DEBUG
  225. LOG( "Kernel Thread Created" );
  226. sprintf( tBuffer, "Server is registered as id[%d]", m_nServerID );
  227. LOG( tBuffer );
  228. #endif
  229. return true;
  230. };
  231. void argusKernel::loadFilters(){
  232. if( m_pDBC == NULL ) return;
  233. DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID );
  234. if( tList != NULL ) {
  235. if( tList->size() > 0 ) {
  236. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  237. DBC_TARGET_FLITER_DATA *tData = NULL;
  238. tData = m_pDBC->getTargetFilterData( (*idx) );
  239. if( tData != NULL ) {
  240. switch( tData->type ) {
  241. case TF_AUTHORISED_PEOPLE_FILTER: {
  242. ARGUS_APF *tFilter = new ARGUS_APF( tData->aux_id );
  243. if( tFilter != NULL ) {
  244. if( tFilter->initFilter() ){
  245. m_vTargetFilters.push_back( (ARGUS_TARGET_FILTER*) tFilter );
  246. } else {
  247. delete( tFilter );
  248. };
  249. };
  250. break;
  251. };
  252. };
  253. free( tData );
  254. };
  255. };
  256. tList->clear();
  257. };
  258. delete( tList );
  259. } else {
  260. #ifdef DEBUG
  261. LOG("No Target Filters Assigned!");
  262. #endif
  263. };
  264. };
  265. void argusKernel::unloadFilters(){
  266. if( m_vTargetFilters.size() > 0 ) {
  267. #ifdef DEBUG
  268. LOG("Have Filters to Unload");
  269. #endif
  270. for( V_TARGET_FILTERS::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  271. switch( (*idx)->getType() ) {
  272. case TF_AUTHORISED_PEOPLE_FILTER:{
  273. delete( (ARGUS_APF*)(*idx) );
  274. break;
  275. };
  276. default:{
  277. char tBuffer[64];
  278. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  279. LOG( tBuffer );
  280. };
  281. };
  282. };
  283. m_vTargetFilters.clear();
  284. LOG("Unload Filters Done!");
  285. }
  286. #ifdef DEBUG
  287. else {
  288. LOG("No Filters to Unload!");
  289. }
  290. #endif
  291. ;
  292. };
  293. ARGUS_TARGET_FILTER *argusKernel::getFilter( uint32_t id ){
  294. ARGUS_TARGET_FILTER *RetVal = NULL;
  295. if( m_vTargetFilters.size() > 0 ) {
  296. for( V_TARGET_FILTERS::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  297. if( (*idx)->getID() == id ) {
  298. RetVal = (*idx);
  299. break;
  300. };
  301. };
  302. };
  303. return RetVal;
  304. };
  305. void argusKernel::WaitFinish( bool Force ){
  306. if( Force ){
  307. #ifdef DEBUG
  308. LOG( "Forced Exit" );
  309. #endif
  310. } else {
  311. #ifdef DEBUG
  312. LOG( "Start Waiting for Thread to End (usually at MAIN)." );
  313. #endif
  314. };
  315. waitThreadEnd( Force );
  316. };
  317. typedef enum E_SYS_COMMAND_ENUMERATOR{
  318. SYS_CMD_NONE = -1,
  319. SYS_CMD_REBOOT = 0,
  320. SYS_CMD_SET_STATIC,
  321. P_ST_MAX_OPTICAL_ZOOM,
  322. P_ST_PAN_REVERSE
  323. }SYS_COMMAND_ENUMERATOR;
  324. CSTR SYS_COMANDS_SCRIPTS[]={
  325. "REBOOT",
  326. "SET_STATIC",
  327. "SET_DYNAMIC",
  328. "SET_SSID",
  329. "SET_AP",
  330. "REFRESH_WIFI",
  331. "CONNECT_2AP"
  332. };
  333. /* Main Kernel Thread Procedure */
  334. void argusKernel::m_fThreadProc( void ){
  335. char Buffer[CTEXT];
  336. time_t tTimeTSeconds;
  337. int lastTSS = 0;
  338. int lastTMS = 0;
  339. DBC_EVT_DATA * tEvtPtr;
  340. uint32_t tLazyTimer = ARGUS_KERNEL_LAZY_SECONDS;
  341. char pidBuf[16];
  342. sprintf(pidBuf,"pid: %d tid: %d", getpid(), gettid() );
  343. LOG( pidBuf );
  344. #ifdef SIMULATE_KERNEL_TARGETS
  345. LOG("SIMULATION ENABLED");
  346. #endif
  347. m_bThreadStatus = TH_WORKING;
  348. while( m_bThreadStatus < TH_ENDED ){
  349. m_tTimeTSeconds = time( NULL );
  350. // Process Once per Second
  351. if( m_tTimeTSeconds != tTimeTSeconds ){
  352. tTimeTSeconds = m_tTimeTSeconds;
  353. struct sysinfo SI;
  354. struct statvfs DI;
  355. float cpu1 = 0.0f;
  356. float cpu5 = 0.0f;
  357. float cpu15 = 0.0f;
  358. float mem = 0.0f;
  359. float disk = 0.0f;
  360. uint64_t tMEM = 0;
  361. uint64_t tDISK = 0;
  362. uint64_t cMEM = 0;
  363. uint64_t cDISK = 0;
  364. uint32_t cCORES = get_nprocs();
  365. if( sysinfo( &SI ) == 0 ){
  366. float f_load = 100.f / (float)( cCORES * ( 1 << SI_LOAD_SHIFT ) );
  367. cpu1 = SI.loads[0] * f_load;
  368. cpu5 = SI.loads[1] * f_load;
  369. cpu15 = SI.loads[2] * f_load;
  370. tMEM = SI.totalram;
  371. cMEM = SI.totalram - SI.freeram;
  372. mem = (float)cMEM / (float)tMEM;
  373. } else {
  374. LOG_ERROR("Error while getting System Info!");
  375. };
  376. const uint32_t MB = 1 << 10;//(1024 * 1024);// * 1024;
  377. if( statvfs("/", &DI) == 0 ){
  378. tDISK = ( DI.f_blocks * DI.f_frsize ) / MB;
  379. cDISK = ( DI.f_bavail * DI.f_frsize ) / MB;
  380. disk = (float)cDISK / (float)tDISK;
  381. } else {
  382. LOG_ERROR("Error while getting Disk Info!");
  383. };
  384. if( !m_pDBC->updateServerUtilisation( m_nServerID, cCORES, cpu1, cpu5, cpu15, mem, disk, tMEM, cMEM, tDISK, cDISK ) ) LOG_ERROR("Cannot update System Utilisation Stats!");
  385. //Get Queued Commands
  386. DBC_PUSHED_CMD *tCMD = m_pDBC->getNodePushedCMD( m_nServerNodeID );
  387. //tCMD = m_pDBC->getActiveSiteCommand( m_nSiteID );
  388. if( tCMD != NULL ){
  389. switch( tCMD->CMD ) {
  390. case ARGUS_CMD_KRNL_RELOAD:{
  391. LOG("Reload ARGUS SERVER!");
  392. m_bThreadStatus = TH_ENDED;
  393. m_bExitCause = 1;
  394. break;
  395. };
  396. case ARGUS_CMD_KRNL_RESTART:{
  397. LOG("Restart ARGUS SERVER!");
  398. m_bThreadStatus = TH_ENDED;
  399. m_bExitCause = 0;
  400. break;
  401. };
  402. case ARGUS_CMD_NODES_RELOAD:{
  403. if( m_pNodeFactory != NULL ) {
  404. m_pNodeFactory->reloadNodes( m_nSiteID );
  405. };
  406. break;
  407. };
  408. case ARGUS_CMD_NODE_RELOAD:{
  409. uint32_t tNodeID = atoi( tCMD->PARAM );
  410. if( m_pNodeFactory != NULL ) {
  411. m_pNodeFactory->reloadNode( tNodeID );
  412. };
  413. break;
  414. };
  415. case ARGUS_CMD_KRNL_PNP:{
  416. if( m_pHardwareManager!= NULL ) {
  417. LOG_ERROR( "Restart and Detect HardWare!" );
  418. // Must be shutdowned all service that uses hardware prior to detection!
  419. // m_pHardwareManager->enumerateHardware();
  420. m_bThreadStatus = TH_ENDED;
  421. m_bExitCause = 4; // Go -> Detect Hardware in BACKEND
  422. };
  423. break;
  424. };
  425. case ARGUS_CMD_KRNL_SYSTEM_TASK:{
  426. try{
  427. Json::Value root;
  428. JSONCPP_STRING err;
  429. Json::CharReaderBuilder builder;
  430. int len = strlen( tCMD->PARAM );
  431. if ( len ) {
  432. const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  433. if (!reader->parse( tCMD->PARAM, tCMD->PARAM + len, &root, &err)) {
  434. LOG_ERROR( "Error parsing the string" );
  435. LOG_ERROR( err.c_str() );
  436. } else {
  437. if (root.size()) {
  438. Json::Value tVal;
  439. tVal = root["action"];
  440. if (!tVal.isNull()) {
  441. LOG( tVal.asString().c_str() );
  442. //SYS_COMMAND_ENUMERATOR tENUM = SYS_CMD_NONE;
  443. CSTR pTemp = NULL;
  444. SYS_COMMAND_ENUMERATOR eType = SYS_CMD_NONE;
  445. for( int i = 0; i < 4; i++ ){
  446. pTemp = (CSTR) strstr( tVal.asString().c_str(), SYS_COMANDS_SCRIPTS[i] );
  447. if( pTemp ){
  448. eType = (SYS_COMMAND_ENUMERATOR)i;
  449. break;
  450. };
  451. };
  452. if( eType != SYS_CMD_NONE ){
  453. switch( eType ){
  454. case SYS_CMD_REBOOT: {
  455. int rv = system("./scripts/linux/reboot_system.sh");
  456. if( rv == -1 ) {
  457. LOG_ERROR("Error issuing System Command!");
  458. } else {
  459. m_bThreadStatus = TH_ENDED;
  460. m_bExitCause = 0;
  461. };
  462. break;
  463. };
  464. case SYS_CMD_SET_STATIC:{
  465. tVal = root["nif_id"];
  466. if (!tVal.isNull()) {
  467. LOG( tVal.asString().c_str() );
  468. };
  469. break;
  470. };
  471. };
  472. };
  473. };
  474. };
  475. };
  476. };
  477. } catch (exception e) {
  478. LOG_ERROR("EXCEPTION");
  479. };
  480. break;
  481. };
  482. };
  483. free( tCMD );
  484. };
  485. // Validate Hardware
  486. if( m_pHardwareManager!= NULL ) {
  487. if( !m_pHardwareManager->validate() ){
  488. LOG_ERROR( "Restart Because of HW!" );
  489. m_bThreadStatus = TH_ENDED;
  490. m_bExitCause = 0; // 0 to real exit from app
  491. };
  492. };
  493. // Validate Modules
  494. if( m_pModuleFactory != NULL ) {
  495. m_pModuleFactory->validate();
  496. };
  497. // Validate Nodes
  498. if( m_pNodeFactory != NULL ) {
  499. m_pNodeFactory->validate();
  500. };
  501. // Archive Events;
  502. if( tLazyTimer ){
  503. tLazyTimer--;
  504. } else {
  505. tLazyTimer = ARGUS_KERNEL_LAZY_SECONDS;
  506. //m_pDBC->lazyEventArchiver( ARGUS_KERNEL_LAZY_SECONDS );
  507. };
  508. };// ops loop
  509. SLEEP( SLEEP_KERNEL_INTERVAL );
  510. };
  511. };
  512. /*
  513. int argusKernel::isCameraListed( CSTR uid ){
  514. if( m_vCamRecords.size() ){
  515. for( int index = 0; index < m_vCamRecords.size(); index++ ){
  516. if(strstr(m_vCamRecords.at(index)->uuid,uid)!=NULL){
  517. m_vCamRecords.at(index)->online = true;
  518. return index;
  519. };
  520. };
  521. } else {
  522. LOG("No Registered Cameras!");
  523. };
  524. return -1;
  525. };
  526. void argusKernel::validateCameras( void ){
  527. vectorWoWoResults *DiscoverResults = m_pOnvifMediaObserver->getResults();
  528. if( DiscoverResults->size() > 0 ) {
  529. for (int index = 0; index < DiscoverResults->size(); index++) {
  530. char tBuffer[256];
  531. sprintf( tBuffer,"Found Camwera [%s] %s", DiscoverResults->at(index)->XAddrs, DiscoverResults->at(index)->RefID );
  532. LOG( tBuffer );
  533. uint32_t cid = m_pDBC->getCamId( DiscoverResults->at(index)->RefID ); //isCameraListed( DiscoverResults->at(index)->RefID );
  534. if( cid ) {
  535. sprintf( tBuffer, "Camera is Listed as %d", cid );
  536. LOG( tBuffer );
  537. //if( strstr(DiscoverResults->at(index)->XAddrs,m_vCamRecords.at(cid)->ip)==NULL ) {
  538. // LOG( DiscoverResults->at(index)->XAddrs );
  539. m_pDBC->updateIPCam( m_nSiteID, cid, DiscoverResults->at(index)->XAddrs );// DiscoverResults->at(index));
  540. //};
  541. } else {
  542. // Uncknown camera - no checking
  543. LOG("Camera is Not Listed");
  544. m_pDBC->addIPCam( m_nSiteID, DiscoverResults->at(index) );
  545. };
  546. };
  547. } else {
  548. LOG("No Online IP Cameras in Network");
  549. };
  550. };
  551. void argusKernel::validateEvents( int &lastTSS, int &lastTMS ){
  552. DBC_EVT_DATA *tPtr = NULL;
  553. EVENT_CONTAINER *tEvt = NULL;
  554. if(m_vEvtRecords.size()) {
  555. lastTSS = m_vEvtRecords.at(m_vEvtRecords.size()-1)->ts_s;
  556. lastTMS = m_vEvtRecords.at(m_vEvtRecords.size()-1)->ts_ms;
  557. #ifndef ONE_EVENT_PER_LOOP
  558. while( m_vEvtRecords.size() ){
  559. #endif
  560. tPtr = *m_vEvtRecords.begin();
  561. if( tPtr != NULL ) {
  562. if( tEvt != NULL ) {
  563. m_pEventHeap->broadcast( tEvt );
  564. };
  565. free( tPtr );
  566. };
  567. m_vEvtRecords.erase( m_vEvtRecords.begin() );
  568. #ifndef ONE_EVENT_PER_LOOP
  569. };
  570. #endif
  571. };
  572. };
  573. */
  574. bool argusKernel::signalEvent( void ){
  575. //if( m_pDBC->getLastEvents( m_vEvtRecords, 0,0 ) ){
  576. //};
  577. return true;
  578. };
  579. int argusKernel::getNetNodes( ARGUS_NODE_TYPE type, argusNetNode **&RA ){
  580. RA = NULL;
  581. int cnt = 0;
  582. if( m_pNodeFactory != NULL ){
  583. cnt = m_pNodeFactory->getNodesCount( type );
  584. if( cnt ) {
  585. RA = (argusNetNode **) calloc( cnt, sizeof( argusNetNode * ) );
  586. m_pNodeFactory->fetchNodes(type, RA );
  587. };
  588. };
  589. return cnt;
  590. };
  591. argusNetNode *argusKernel::getNetNode( int id ){
  592. if( m_pNodeFactory != NULL ){
  593. return m_pNodeFactory->getNode( id );
  594. };
  595. return NULL;
  596. };
  597. /*
  598. int argusKernel::getModules( ARGUS_MODULE_TYPE type, argusKernelModule **&RA ){
  599. RA = NULL;
  600. int cnt = 0;
  601. if( m_pModuleFactory != NULL ){
  602. cnt = m_pModuleFactory->getModulesCount( type );
  603. if( cnt ) {
  604. RA = (argusKernelModule **) calloc( cnt, sizeof( argusKernelModule * ) );
  605. m_pModuleFactory->fetchModules( type, RA );
  606. };
  607. };
  608. return cnt;
  609. };
  610. argusKernelModule *argusKernel::getModule( int id ){
  611. if( m_pModuleFactory != NULL ){
  612. return m_pModuleFactory->getModule( id );
  613. };
  614. return NULL;
  615. };
  616. */
  617. void argusKernel::dispatchEvent( EVENT_CONTAINER *Evt ){
  618. // Register all events in Database;
  619. //LOG( "New EVENT!!" );
  620. if( m_pDBC == NULL ) return;
  621. if( Evt == NULL ) return;
  622. switch( Evt->m_eClass ) {
  623. case EVT_CLASS_EXTERNAL_CMD:
  624. switch( Evt->m_eType ){
  625. case EVT_FH_JAMER_ON:{
  626. uint32_t TS_A, TS_B;
  627. TS_A = Evt->m_UMSTS / 1000;
  628. TS_B = Evt->m_UMSTS % 1000;
  629. #ifdef SIMULATE_KERNEL_TARGETS
  630. m_bJamingTO = SIMULATE_JAMER_TO;
  631. #endif
  632. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  633. if( tData ) {
  634. if( m_pDBC->addEvent(
  635. 0,
  636. Evt->m_UUID,
  637. TS_A,
  638. TS_B,
  639. EVT_FH_JAMER_ON,
  640. 0,
  641. 0,
  642. 0,
  643. 0,
  644. 0,
  645. 0,
  646. 0,
  647. 0,
  648. 0,
  649. tData->evtUUID
  650. ) ){
  651. //Simulate FH Module Event;
  652. EVENT_CONTAINER *ttt;
  653. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  654. if( nnn ){
  655. nnn->location.lat = 0;
  656. nnn->location.lon = 0;
  657. nnn->location.alt = 0;
  658. sprintf( nnn->missionID, tData->evtUUID );
  659. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_JAMER_ON, NULL, NULL, nnn );
  660. if( ttt ) {
  661. m_pEventHeap->broadcast( ttt );
  662. } else {
  663. free( nnn );
  664. };
  665. };
  666. };
  667. free(tData);
  668. };
  669. break;
  670. };
  671. case EVT_FH_MISSION_END:{
  672. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  673. if( tData ) {
  674. //DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  675. if( 1 /*tEVT*/ ) {
  676. uint32_t TS_A, TS_B;
  677. TS_A = Evt->m_UMSTS / 1000;
  678. TS_B = Evt->m_UMSTS % 1000;
  679. if( m_pDBC->addEvent(
  680. 0,
  681. Evt->m_UUID,
  682. TS_A,
  683. TS_B,
  684. EVT_FH_LANDED ) ){
  685. //Simulate FH Module Event;
  686. EVENT_CONTAINER *ttt;
  687. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  688. if( nnn ){
  689. nnn->location.lat = 0;
  690. nnn->location.lon = 0;
  691. nnn->location.alt = 0;
  692. sprintf( nnn->missionID, tData->evtUUID );
  693. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_LANDED, NULL, NULL, nnn );
  694. if( ttt ) {
  695. m_pEventHeap->broadcast( ttt );
  696. } else {
  697. free( nnn );
  698. };
  699. };
  700. };
  701. //free( tEVT );
  702. } else {
  703. LOG_ERROR("Linked Event Was Not Found!");
  704. };
  705. };
  706. break;
  707. }
  708. case EVT_FH_MISSION_BEGIN:{
  709. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  710. #ifdef SIMULATE_KERNEL_TARGETS
  711. m_bLandingTO = SIMULATE_LANDING_TO;
  712. #endif
  713. if( tData ) {
  714. #ifdef SIMULATE_KERNEL_TARGETS
  715. sprintf( m_cCurrentMission, tData->evtUUID );
  716. #endif
  717. DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  718. if( tEVT ) {
  719. uint32_t TS_A, TS_B;
  720. TS_A = Evt->m_UMSTS / 1000;
  721. TS_B = Evt->m_UMSTS % 1000;
  722. if( Evt->m_db_id = m_pDBC->addEvent(
  723. 0,
  724. Evt->m_UUID,
  725. TS_A,
  726. TS_B,
  727. EVT_FH_FLY_TO,
  728. 0,
  729. 0,
  730. 0,
  731. 0,
  732. 0,
  733. tEVT->lat,
  734. tEVT->lon,
  735. tEVT->alt,
  736. tEVT->id,
  737. tEVT->uuid
  738. ) ){
  739. //Simulate FH Module Event;
  740. EVENT_CONTAINER *ttt;
  741. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  742. if( nnn ){
  743. nnn->location.lat = tEVT->lat;
  744. nnn->location.lon = tEVT->lon;
  745. nnn->location.alt = tEVT->alt;
  746. sprintf( nnn->missionID, tData->evtUUID );
  747. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_MISSION_BEGIN, NULL, NULL, nnn );
  748. if( ttt ) {
  749. m_pEventHeap->broadcast( ttt );
  750. } else {
  751. free( nnn );
  752. };
  753. };
  754. };
  755. free( tEVT );
  756. } else {
  757. LOG_ERROR("Linked Event Was Not Found!");
  758. };
  759. };
  760. break;
  761. };
  762. };
  763. break;
  764. case EVT_CLASS_SENSOR_CLD:
  765. case EVT_CLASS_SENSOR_LGS:
  766. case EVT_CLASS_SENSOR_LFS:
  767. case EVT_CLASS_SENSOR_MND:
  768. case EVT_CLASS_SENSOR_PLS_210:
  769. case EVT_CLASS_SENSOR_PLS_360:
  770. case EVT_CLASS_SENSOR_ALS:
  771. case EVT_CLASS_SENSOR_3D:
  772. case EVT_CLASS_SENSOR_ACC:
  773. switch( Evt->m_eType ) {
  774. case EVT_TARGET_1D:{
  775. TARGET_DATA *tDATA =(TARGET_DATA *) Evt->m_pEvtData;
  776. if( tDATA != NULL ) {
  777. uint32_t TS_A, TS_B;
  778. TS_A = Evt->m_UMSTS / 1000;
  779. TS_B = Evt->m_UMSTS % 1000;
  780. Evt->m_db_id = m_pDBC->addEvent(
  781. Evt->m_nSourceUID,
  782. Evt->m_UUID,
  783. TS_A,
  784. TS_B,
  785. Evt->m_eType,
  786. tDATA->digital,
  787. 0,
  788. 0,
  789. tDATA->local_target.r,
  790. tDATA->area,
  791. tDATA->global_target.lat,
  792. tDATA->global_target.lon,
  793. tDATA->global_target.alt
  794. );
  795. };
  796. break;
  797. };
  798. case EVT_TARGET_2D_BLACK:
  799. case EVT_TARGET_2D_WHITE:{
  800. TARGET_DATA *tDATA = (TARGET_DATA *)Evt->m_pEvtData;
  801. if( tDATA != NULL ) {
  802. uint32_t TS_A, TS_B;
  803. TS_A = Evt->m_UMSTS / 1000;
  804. TS_B = Evt->m_UMSTS % 1000;
  805. Evt->m_db_id = m_pDBC->addEvent(
  806. Evt->m_nSourceUID,
  807. Evt->m_UUID,
  808. TS_A,
  809. TS_B,
  810. Evt->m_eType,
  811. tDATA->digital,
  812. tDATA->local_target.tetta,
  813. tDATA->local_target.fi,
  814. tDATA->local_target.r,
  815. tDATA->area,
  816. tDATA->global_target.lat,
  817. tDATA->global_target.lon,
  818. tDATA->global_target.alt
  819. );
  820. };
  821. break;
  822. };
  823. case EVT_TARGET_3D_BLACK:
  824. case EVT_TARGET_3D_WHITE:{
  825. TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  826. if( tDATA != NULL ) {
  827. uint32_t TS_A, TS_B;
  828. TS_A = Evt->m_UMSTS / 1000;
  829. TS_B = Evt->m_UMSTS % 1000;
  830. Evt->m_db_id = m_pDBC->addEvent(
  831. Evt->m_nSourceUID,
  832. Evt->m_UUID,
  833. TS_A,
  834. TS_B,
  835. Evt->m_eType,
  836. tDATA->digital,
  837. tDATA->local_target.tetta,
  838. tDATA->local_target.fi,
  839. tDATA->local_target.r,
  840. tDATA->area,
  841. tDATA->global_target.lat,
  842. tDATA->global_target.lon,
  843. tDATA->global_target.alt
  844. );
  845. };
  846. break;
  847. };
  848. case EVT_FLIP:{
  849. uint32_t TS_A, TS_B;
  850. TS_A = Evt->m_UMSTS / 1000;
  851. TS_B = Evt->m_UMSTS % 1000;
  852. Evt->m_db_id = m_pDBC->addEvent( Evt->m_nSourceUID, Evt->m_UUID, TS_A, TS_B, Evt->m_eType);
  853. break;
  854. };
  855. };
  856. break;
  857. case EVT_CLASS_VIDEO:{
  858. switch( Evt->m_eType ) {
  859. case EVT_CAPTURE_START:{
  860. break;
  861. };
  862. };
  863. break;
  864. };
  865. };
  866. };
  867. #undef LOG
  868. #undef LOG_ERROR