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.

401 lines
15 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2022. All Rights Reserved.
  4. // Version 1.0 11/19/2022
  5. // Last modification: 11/19/2022
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: mojoKernel.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // LRF_Kernel.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "mojoKernel.h"
  16. #include "../data/json/json_tool.h"
  17. #include <strings.h>
  18. #include <json/value.h>
  19. #include <json/writer.h>
  20. #include <json/reader.h>
  21. #include "../common/kernelEventFilterAPF.h"
  22. #define LOG( X ) kernelPrototype::m_pLog->Log( "KRNL", this, X )
  23. #ifdef DEBUG
  24. #define LOG_ERROR( X ) {\
  25. char _tBfrErrMSG234[256];\
  26. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  27. kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
  28. }
  29. #else
  30. #define LOG_ERROR( X ) kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  31. #endif
  32. mojoKernel *mojoKernel::_instance = NULL;
  33. mojoKernel *mojoKernel::getInstance( config_setting_t *ptr ){
  34. if ( _instance == NULL ) {
  35. _instance = new mojoKernel( ptr );
  36. };
  37. return _instance;
  38. };
  39. mojoKernel::mojoKernel( config_setting_t *ptr ):kernelPrototype( ptr ){
  40. m_pEventHeap = basicEventHeap::getInstance();
  41. memset( &m_sSysInfo, 0, sizeof( DBC_SYS_LOAD_INFO ));
  42. m_pShadow = NULL;
  43. m_pSystemAnchor = NULL;
  44. setWM( WM_NONE );
  45. };
  46. mojoKernel::~mojoKernel(){
  47. WaitFinish( true );
  48. #ifdef DEBUG
  49. LOG(" Deleting Node Factory...");
  50. #endif
  51. //if( m_pNodeFactory != NULL ) delete( m_pNodeFactory );
  52. #ifdef DEBUG
  53. LOG(" Deleting Module Factory...");
  54. #endif
  55. if( m_pModuleFactory != NULL ) delete( m_pModuleFactory );
  56. #ifdef DEBUG
  57. LOG(" Deleting Target Filters...");
  58. #endif
  59. unloadFilters();
  60. if( EXIT_CODE_KERNEL_CLEAR == getExitCause() ) {
  61. #ifdef DEBUG
  62. LOG("Unregister Server");
  63. #endif
  64. m_pDBC->unregisterServer();
  65. #ifdef DEBUG
  66. //LOG("Close DataBase Connection...");
  67. #endif
  68. //if( m_pDBC != NULL ) delete( m_pDBC );//Deleting in Backend
  69. //m_pDBC = NULL;
  70. } else {
  71. if( EXIT_CODE_DETECT_HARDWARE == getExitCause() ) {
  72. #ifdef DEBUG
  73. LOG("HW PNP Detection Requested...");
  74. #endif
  75. //m_pDBC->updateNodePushedCMD( tCMD->id, 30 );
  76. m_pHardwareManager->enumerateHardware();
  77. //m_pDBC->updateNodePushedCMD( tCMD->id, 60 );
  78. };
  79. #ifdef DEBUG
  80. //LOG("Close DataBase Connection...");
  81. #endif
  82. //if( m_pDBC != NULL ) delete( m_pDBC );
  83. //m_pDBC = NULL;
  84. };
  85. delete( m_pHardwareManager );
  86. _instance = NULL;
  87. };
  88. bool mojoKernel::create(){
  89. if( m_pSettings != NULL ) {
  90. config_setting_t * pSetting;
  91. int tVal;
  92. const char * tBuf;
  93. char tBuffer[128];
  94. LOG("Getting Settings ...");
  95. if( !config_setting_lookup_string( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  96. char * tVar;
  97. char * tTS;
  98. tTS = getTStr(NULL);
  99. tVar = MD5String(tTS);
  100. free(tTS);
  101. sprintf( m_sIdentity, tVar );
  102. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  103. config_setting_set_string( pSetting, tVar );
  104. free( tVar );
  105. basicSettingsProvider::getInstance()->compromise();
  106. LOG("Server Identity not defined - generating new!");
  107. } else {
  108. sprintf( m_sIdentity, tBuf );
  109. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  110. LOG( tBuffer );
  111. };
  112. if( !config_setting_lookup_int( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal ) ){
  113. tVal = m_nInstanceID = 1;
  114. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  115. config_setting_set_int( pSetting, tVal );
  116. basicSettingsProvider::getInstance()->compromise();
  117. LOG("Site ID not defined - using [1] as number!");
  118. } else {
  119. m_nInstanceID = tVal;
  120. sprintf( tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID );
  121. LOG( tBuffer );
  122. };
  123. } else {
  124. LOG_ERROR("Failed Read Config Settings!");
  125. return false;
  126. };
  127. basicSettingsProvider::getInstance()->validate();
  128. LOG("Register Server ... ");
  129. m_pDBC->registerServer( m_nInstanceID, m_sIdentity, getpid(), &m_nServerID, &m_nServerNodeID );
  130. char tBuffer[128];
  131. sprintf( tBuffer,"Server Registered as [%d] nodeID[%d]",m_nServerID, m_nServerNodeID );
  132. LOG( tBuffer );
  133. DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( m_nInstanceID );
  134. if( tSiteData ) {
  135. C3D_WGS84_LOC t;
  136. t.lat = tSiteData->lat;
  137. t.lon = tSiteData->lon;
  138. t.alt = tSiteData->alt;
  139. m_pSystemAnchor = systemAnchor::getInstance( tSiteData->SH, &t, tSiteData->local_binding );
  140. free( tSiteData );
  141. } else {
  142. LOG_ERROR("Error Loading Site DATA!");
  143. C3D_WGS84_LOC t;
  144. t.lat = 0;
  145. t.lon = 0;
  146. t.alt = 0;
  147. m_pSystemAnchor = systemAnchor::getInstance( false, &t, true );
  148. };
  149. m_pHardwareManager = hardwareManager::getInstance();
  150. loadFilters();
  151. m_pModuleFactory = kernelModuleFactory::getInstance( m_nInstanceID );
  152. if( !m_pModuleFactory ) {
  153. LOG_ERROR("Module Factory Failure!");
  154. return false;
  155. };
  156. //Autoloading Modules
  157. //if( !m_pModuleFactory->loadModules() ){
  158. // LOG_ERROR("Modules Loading Failure!");
  159. // return false;
  160. //};
  161. m_pNodeFactory = deviceNodeFactory::getInstance();
  162. if( !m_pNodeFactory ){
  163. LOG_ERROR("Node Factory Failure!");
  164. return false;
  165. };
  166. return init();
  167. };
  168. void mojoKernel::m_fThreadProc( void ){
  169. time_t tTimeTSeconds = 0;
  170. m_bThreadStatus = TH_WORKING;
  171. while ( m_bThreadStatus < TH_ENDED ) {
  172. m_tTimeTSeconds = time( NULL );
  173. //Each Second
  174. if( m_tTimeTSeconds != tTimeTSeconds ){
  175. tTimeTSeconds = m_tTimeTSeconds;
  176. m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  177. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD( m_nServerNodeID ); //Kernel
  178. if( tCMD != NULL ) {
  179. m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
  180. switch( tCMD->CMD ){
  181. case CMD_KRNL_RELOAD:{
  182. LOG("Reload Mojo Node!");
  183. m_bThreadStatus = TH_ENDED;
  184. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  185. break;
  186. };
  187. case CMD_KRNL_RESTART:{
  188. LOG("Restart Mojo Node!");
  189. m_bThreadStatus = TH_ENDED;
  190. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  191. break;
  192. };
  193. case CMD_KRNL_FACTORY_RESET:{
  194. LOG("Restart Mojo -> Factory Reset!");
  195. m_bThreadStatus = TH_ENDED;
  196. setExitCause( EXIT_CODE_FACTORY_RESET );
  197. break;
  198. };
  199. case CMD_NODES_RELOAD:{
  200. //if( m_pNodeFactory != NULL ) {
  201. // m_pNodeFactory->reloadNodes( m_nSiteID );
  202. //};
  203. break;
  204. };
  205. case CMD_NODE_RELOAD:{
  206. uint32_t tNodeID = atoi( tCMD->PARAM );
  207. //if( m_pNodeFactory != NULL ) {
  208. // m_pNodeFactory->reloadNode( tNodeID );
  209. //};
  210. break;
  211. };
  212. case CMD_KRNL_PNP:{
  213. if( m_pHardwareManager!= NULL ) {
  214. LOG_ERROR( "Restart and Detect HardWare!" );
  215. // Must be shutdowned all service that uses hardware prior to detection!
  216. // m_pHardwareManager->enumerateHardware();
  217. m_bThreadStatus = TH_ENDED;
  218. setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  219. };
  220. break;
  221. };
  222. case CMD_KRNL_SYNC_TIME:{
  223. uint32_t uts = atoi( tCMD->PARAM );
  224. if( uts ){
  225. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  226. time_t ts = (time_t)uts;
  227. if( stime( &ts ) == 0 ){
  228. // Restert Server;
  229. m_bThreadStatus = TH_ENDED;
  230. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  231. };
  232. #else
  233. timespec ts;
  234. ts.tv_sec = (time_t)uts;
  235. ts.tv_nsec = 0;
  236. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  237. // Restert Server;
  238. m_bThreadStatus = TH_ENDED;
  239. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  240. };
  241. #endif
  242. };
  243. break;
  244. };
  245. case CMD_KRNL_SYSTEM_TASK:{
  246. /*
  247. try{
  248. Json::Value root;
  249. JSONCPP_STRING err;
  250. Json::CharReaderBuilder builder;
  251. int len = strlen( tCMD->PARAM );
  252. if ( len ) {
  253. const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  254. if (!reader->parse( tCMD->PARAM, tCMD->PARAM + len, &root, &err)) {
  255. LOG_ERROR( "Error parsing the string" );
  256. LOG_ERROR( err.c_str() );
  257. } else {
  258. if (root.size()) {
  259. Json::Value tVal;
  260. tVal = root["action"];
  261. if (!tVal.isNull()) {
  262. LOG( tVal.asString().c_str() );
  263. //SYS_COMMAND_ENUMERATOR tENUM = SYS_CMD_NONE;
  264. CSTR pTemp = NULL;
  265. SYS_COMMAND_ENUMERATOR eType = SYS_CMD_NONE;
  266. for( int i = 0; i < 4; i++ ){
  267. pTemp = (CSTR) strstr( tVal.asString().c_str(), SYS_COMANDS_SCRIPTS[i] );
  268. if( pTemp ){
  269. eType = (SYS_COMMAND_ENUMERATOR)i;
  270. break;
  271. };
  272. };
  273. if( eType != SYS_CMD_NONE ){
  274. switch( eType ){
  275. case SYS_CMD_REBOOT: {
  276. int rv = system("./scripts/linux/reboot_system.sh");
  277. if( rv == -1 ) {
  278. LOG_ERROR("Error issuing System Command!");
  279. } else {
  280. m_bThreadStatus = TH_ENDED;
  281. m_bExitCause = 0;
  282. };
  283. break;
  284. };
  285. case SYS_CMD_SET_STATIC:{
  286. tVal = root["nif_id"];
  287. if (!tVal.isNull()) {
  288. LOG( tVal.asString().c_str() );
  289. };
  290. break;
  291. };
  292. };
  293. };
  294. };
  295. };
  296. };
  297. };
  298. } catch (exception e) {
  299. m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_FINISHED_ERROR );
  300. LOG_ERROR("EXCEPTION");
  301. };
  302. */
  303. break;
  304. };
  305. };
  306. free( tCMD );
  307. };
  308. m_pDBC->cleanNodePushedCMD();
  309. };
  310. SLEEP(SLEEP_KERNEL_INTERVAL);
  311. };
  312. };
  313. void mojoKernel::loadFilters(){
  314. if( m_pDBC == NULL ) return;
  315. DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID );
  316. if( tList != NULL ) {
  317. if( tList->size() > 0 ) {
  318. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  319. DBC_TARGET_FLITER_DATA *tData = NULL;
  320. tData = m_pDBC->getTargetFilterData( (*idx) );
  321. if( tData != NULL ) {
  322. switch( tData->type ) {
  323. case TF_AUTHORISED_PEOPLE_FILTER: {
  324. kernelEventFilterAPF *tFilter = new kernelEventFilterAPF( tData->aux_id );
  325. if( tFilter != NULL ) {
  326. if( tFilter->initFilter() ){
  327. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  328. } else {
  329. delete( tFilter );
  330. };
  331. };
  332. break;
  333. };
  334. };
  335. free( tData );
  336. };
  337. };
  338. tList->clear();
  339. };
  340. delete( tList );
  341. #ifdef DEBUG
  342. } else {
  343. LOG("No Target Filters Assigned!");
  344. #endif
  345. };
  346. };
  347. void mojoKernel::unloadFilters(){
  348. if( m_vTargetFilters.size() > 0 ) {
  349. #ifdef DEBUG
  350. LOG("Have Filters to Unload");
  351. #endif
  352. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  353. switch( (*idx)->getType() ) {
  354. case TF_AUTHORISED_PEOPLE_FILTER:{
  355. delete( (kernelEventFilterAPF*)(*idx) );
  356. break;
  357. };
  358. default:{
  359. char tBuffer[64];
  360. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  361. LOG( tBuffer );
  362. };
  363. };
  364. };
  365. m_vTargetFilters.clear();
  366. LOG("Unload Filters Done!");
  367. #ifdef DEBUG
  368. } else {
  369. LOG("No Filters to Unload!");
  370. #endif
  371. };
  372. };
  373. #undef LOG
  374. #undef LOG_ERROR