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.

334 lines
11 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
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. m_pNodeFactory = deviceNodeFactory::getInstance();
  157. if( !m_pNodeFactory ){
  158. LOG_ERROR("Node Factory Failure!");
  159. return false;
  160. };
  161. return init();
  162. };
  163. void mojoKernel::m_fThreadProc( void ){
  164. time_t tTimeTSeconds = 0;
  165. m_bThreadStatus = TH_WORKING;
  166. while ( m_bThreadStatus < TH_ENDED ) {
  167. m_tTimeTSeconds = time( NULL );
  168. //Each Second
  169. if( m_tTimeTSeconds != tTimeTSeconds ){
  170. tTimeTSeconds = m_tTimeTSeconds;
  171. m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  172. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD( m_nServerNodeID ); //Kernel
  173. if( tCMD != NULL ) {
  174. m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
  175. switch( tCMD->CMD ){
  176. case CMD_KRNL_RELOAD:{
  177. LOG("Reload Mojo Node!");
  178. m_bThreadStatus = TH_ENDED;
  179. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  180. break;
  181. };
  182. case CMD_KRNL_RESTART:{
  183. LOG("Restart Mojo Node!");
  184. m_bThreadStatus = TH_ENDED;
  185. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  186. break;
  187. };
  188. case CMD_KRNL_FACTORY_RESET:{
  189. LOG("Restart Mojo -> Factory Reset!");
  190. m_bThreadStatus = TH_ENDED;
  191. setExitCause( EXIT_CODE_FACTORY_RESET );
  192. break;
  193. };
  194. case CMD_NODES_RELOAD:{
  195. if( m_pNodeFactory != NULL ) {
  196. m_pNodeFactory->reloadNodes();
  197. };
  198. break;
  199. };
  200. case CMD_NODE_RELOAD:{
  201. uint32_t tNodeID = atoi( tCMD->PARAM );
  202. if( m_pNodeFactory != NULL ) {
  203. m_pNodeFactory->reloadNode( tNodeID );
  204. };
  205. break;
  206. };
  207. case CMD_KRNL_PNP:{
  208. if( m_pHardwareManager!= NULL ) {
  209. LOG_ERROR( "Restart and Detect HardWare!" );
  210. // Must be shutdowned all service that uses hardware prior to detection!
  211. m_bThreadStatus = TH_ENDED;
  212. setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  213. };
  214. break;
  215. };
  216. case CMD_KRNL_SYNC_TIME:{
  217. uint32_t uts = atoi( tCMD->PARAM );
  218. if( uts ){
  219. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  220. time_t ts = (time_t)uts;
  221. if( stime( &ts ) == 0 ){
  222. // Restert Server;
  223. m_bThreadStatus = TH_ENDED;
  224. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  225. };
  226. #else
  227. timespec ts;
  228. ts.tv_sec = (time_t)uts;
  229. ts.tv_nsec = 0;
  230. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  231. // Restert Server;
  232. m_bThreadStatus = TH_ENDED;
  233. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  234. };
  235. #endif
  236. };
  237. break;
  238. };
  239. case CMD_KRNL_SYSTEM_TASK:{
  240. break;
  241. };
  242. };
  243. free( tCMD );
  244. };
  245. m_pDBC->cleanNodePushedCMD();
  246. };
  247. SLEEP(SLEEP_KERNEL_INTERVAL);
  248. };
  249. };
  250. void mojoKernel::loadFilters(){
  251. if( m_pDBC == NULL ) return;
  252. DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID );
  253. if( tList != NULL ) {
  254. if( tList->size() > 0 ) {
  255. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  256. DBC_TARGET_FLITER_DATA *tData = NULL;
  257. tData = m_pDBC->getTargetFilterData( (*idx) );
  258. if( tData != NULL ) {
  259. switch( tData->type ) {
  260. case TF_AUTHORISED_PEOPLE_FILTER: {
  261. kernelEventFilterAPF *tFilter = new kernelEventFilterAPF( tData->aux_id );
  262. if( tFilter != NULL ) {
  263. if( tFilter->initFilter() ){
  264. m_vTargetFilters.push_back( (kernelEventFilter*) tFilter );
  265. } else {
  266. delete( tFilter );
  267. };
  268. };
  269. break;
  270. };
  271. };
  272. free( tData );
  273. };
  274. };
  275. tList->clear();
  276. };
  277. delete( tList );
  278. #ifdef DEBUG
  279. } else {
  280. LOG("No Target Filters Assigned!");
  281. #endif
  282. };
  283. };
  284. void mojoKernel::unloadFilters(){
  285. if( m_vTargetFilters.size() > 0 ) {
  286. #ifdef DEBUG
  287. LOG("Have Filters to Unload");
  288. #endif
  289. for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){
  290. switch( (*idx)->getType() ) {
  291. case TF_AUTHORISED_PEOPLE_FILTER:{
  292. delete( (kernelEventFilterAPF*)(*idx) );
  293. break;
  294. };
  295. default:{
  296. char tBuffer[64];
  297. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  298. LOG( tBuffer );
  299. };
  300. };
  301. };
  302. m_vTargetFilters.clear();
  303. LOG("Unload Filters Done!");
  304. #ifdef DEBUG
  305. } else {
  306. LOG("No Filters to Unload!");
  307. #endif
  308. };
  309. };
  310. #undef LOG
  311. #undef LOG_ERROR