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.

390 lines
15 KiB

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. time_t ts = (time_t)uts;
  226. if( stime( &ts ) == 0 ){
  227. // Restert Server;
  228. m_bThreadStatus = TH_ENDED;
  229. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  230. };
  231. };
  232. break;
  233. };
  234. case CMD_KRNL_SYSTEM_TASK:{
  235. /*
  236. try{
  237. Json::Value root;
  238. JSONCPP_STRING err;
  239. Json::CharReaderBuilder builder;
  240. int len = strlen( tCMD->PARAM );
  241. if ( len ) {
  242. const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  243. if (!reader->parse( tCMD->PARAM, tCMD->PARAM + len, &root, &err)) {
  244. LOG_ERROR( "Error parsing the string" );
  245. LOG_ERROR( err.c_str() );
  246. } else {
  247. if (root.size()) {
  248. Json::Value tVal;
  249. tVal = root["action"];
  250. if (!tVal.isNull()) {
  251. LOG( tVal.asString().c_str() );
  252. //SYS_COMMAND_ENUMERATOR tENUM = SYS_CMD_NONE;
  253. CSTR pTemp = NULL;
  254. SYS_COMMAND_ENUMERATOR eType = SYS_CMD_NONE;
  255. for( int i = 0; i < 4; i++ ){
  256. pTemp = (CSTR) strstr( tVal.asString().c_str(), SYS_COMANDS_SCRIPTS[i] );
  257. if( pTemp ){
  258. eType = (SYS_COMMAND_ENUMERATOR)i;
  259. break;
  260. };
  261. };
  262. if( eType != SYS_CMD_NONE ){
  263. switch( eType ){
  264. case SYS_CMD_REBOOT: {
  265. int rv = system("./scripts/linux/reboot_system.sh");
  266. if( rv == -1 ) {
  267. LOG_ERROR("Error issuing System Command!");
  268. } else {
  269. m_bThreadStatus = TH_ENDED;
  270. m_bExitCause = 0;
  271. };
  272. break;
  273. };
  274. case SYS_CMD_SET_STATIC:{
  275. tVal = root["nif_id"];
  276. if (!tVal.isNull()) {
  277. LOG( tVal.asString().c_str() );
  278. };
  279. break;
  280. };
  281. };
  282. };
  283. };
  284. };
  285. };
  286. };
  287. } catch (exception e) {
  288. m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_FINISHED_ERROR );
  289. LOG_ERROR("EXCEPTION");
  290. };
  291. */
  292. break;
  293. };
  294. };
  295. free( tCMD );
  296. };
  297. m_pDBC->cleanNodePushedCMD();
  298. };
  299. SLEEP(SLEEP_KERNEL_INTERVAL);
  300. };
  301. };
  302. void mojoKernel::loadFilters(){
  303. if( m_pDBC == NULL ) return;
  304. DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID );
  305. if( tList != NULL ) {
  306. if( tList->size() > 0 ) {
  307. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  308. DBC_TARGET_FLITER_DATA *tData = NULL;
  309. tData = m_pDBC->getTargetFilterData( (*idx) );
  310. if( tData != NULL ) {
  311. switch( tData->type ) {
  312. case TF_AUTHORISED_PEOPLE_FILTER: {
  313. kernelEventFilterAPF *tFilter = new kernelEventFilterAPF( tData->aux_id );
  314. if( tFilter != NULL ) {
  315. if( tFilter->initFilter() ){
  316. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  317. } else {
  318. delete( tFilter );
  319. };
  320. };
  321. break;
  322. };
  323. };
  324. free( tData );
  325. };
  326. };
  327. tList->clear();
  328. };
  329. delete( tList );
  330. #ifdef DEBUG
  331. } else {
  332. LOG("No Target Filters Assigned!");
  333. #endif
  334. };
  335. };
  336. void mojoKernel::unloadFilters(){
  337. if( m_vTargetFilters.size() > 0 ) {
  338. #ifdef DEBUG
  339. LOG("Have Filters to Unload");
  340. #endif
  341. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  342. switch( (*idx)->getType() ) {
  343. case TF_AUTHORISED_PEOPLE_FILTER:{
  344. delete( (kernelEventFilterAPF*)(*idx) );
  345. break;
  346. };
  347. default:{
  348. char tBuffer[64];
  349. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  350. LOG( tBuffer );
  351. };
  352. };
  353. };
  354. m_vTargetFilters.clear();
  355. LOG("Unload Filters Done!");
  356. #ifdef DEBUG
  357. } else {
  358. LOG("No Filters to Unload!");
  359. #endif
  360. };
  361. };
  362. #undef LOG
  363. #undef LOG_ERROR