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.

1536 lines
60 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
2 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
4 years ago
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
2 years ago
​
4 years ago
2 years ago
2 years ago
​
​
2 years ago
2 years ago
​
​
2 years ago
​
2 years ago
​
2 years ago
​
2 years ago
2 years ago
​
​
​
2 years ago
2 years ago
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
4 years ago
​
​
​
​
  1. #include "hardwareManager.h"
  2. #include <sys/types.h>
  3. #include <sys/sysinfo.h>
  4. #include <sys/statvfs.h>
  5. #include <time.h>
  6. #ifdef USE_MYSQL
  7. #include "MySQL_dataBinder.h"
  8. #else
  9. #include "SQLite_dataBinder.h"
  10. #endif
  11. //#include "rgooseKernel.h"
  12. //#include "rgooseDBConnector.h"
  13. #include "hardwareResourceSerialProvider.h"
  14. #include "hardwareResourceNetworkProvider.h"
  15. #include "hardwareResourceVideoProvider.h"
  16. #include "../hardware/hid/hid_dc.h"
  17. #include "../alg/generic/heapDebug.h"
  18. #include <sys/time.h>
  19. #include <sys/resource.h>
  20. #include <sys/types.h>
  21. #include <sys/ioctl.h>
  22. #include <hidapi/hidapi.h>
  23. #include <ifaddrs.h>
  24. #include <linux/serial.h>
  25. #include <linux/videodev2.h>
  26. //#define NO_EXEC_EXTERNALS
  27. #define LOG( X ) m_pLog->Log( "HWM", this, X )
  28. #ifdef DEBUG
  29. #define LOG_ERROR( X ) {\
  30. char _tBfrErrMSG2w34[256];\
  31. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  32. m_pLog->Log( "HWM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  33. }
  34. #else
  35. #define LOG_ERROR( X ) m_pLog->Log( "HWM", this, _LOG_ERROR, X );
  36. #endif
  37. hardwareManager *hardwareManager::_instance = NULL;
  38. /**
  39. * Returns the single instance of the object.
  40. */
  41. hardwareManager* hardwareManager::getInstance( bool cold_start ) {
  42. // check if the instance has been created yet
  43. if( 0 == _instance ){
  44. // if not, then create it
  45. _instance = new hardwareManager( cold_start );
  46. };
  47. // return the single instance
  48. return _instance;
  49. };
  50. hardwareManager::hardwareManager( bool cold_start ){
  51. m_pLog = basicLogCollection::getInstance();
  52. //m_bHardwareEnumerated = false;
  53. m_nServerNodeID = 0;
  54. m_nServerID = 0;
  55. m_nSiteID = 0;
  56. #ifdef MEMORY_CONSUMPTION_LIMIT
  57. m_bCacheCleanCounter = 3;
  58. #endif
  59. memset( &m_sSYS_INFO, 0, sizeof(DBC_SYS_INFO) );
  60. strncpy( m_sSYS_INFO.SW_INFO, _BUILD_STRING_, 128 );
  61. config_setting_t * m_pSettings = basicSettingsProvider::getInstance()->getSubNode( NULL, "HARDWARE" );
  62. if ( m_pSettings ) {
  63. config_setting_t * pSetting;
  64. long long int tVal;
  65. const char * tBuf;
  66. char tBuffer[128];
  67. #ifdef MEMORY_CONSUMPTION_LIMIT
  68. if( !config_setting_lookup_int64( m_pSettings, "MEMORY_LIMIT", &tVal ) ){
  69. tVal = m_nMemoryLimitHeapSize = MEMORY_CONSUMPTION_LIMIT;
  70. pSetting = config_setting_add( m_pSettings, "MEMORY_LIMIT", CONFIG_TYPE_INT64 );
  71. config_setting_set_int64( pSetting, tVal );
  72. basicSettingsProvider::getInstance()->compromise();
  73. LOG("MEMORY LIMIT NOT DEFINED - Using 2Gb Default!");
  74. } else {
  75. m_nMemoryLimitHeapSize = tVal;
  76. sprintf( tBuffer, "MEMORY LIMIT Read From Settings: %lld bytes", m_nMemoryLimitHeapSize );
  77. LOG( tBuffer );
  78. };
  79. #endif
  80. basicSettingsProvider::getInstance()->validate();
  81. } else {
  82. #ifdef MEMORY_CONSUMPTION_LIMIT
  83. m_nMemoryLimitHeapSize = MEMORY_CONSUMPTION_LIMIT;
  84. #endif
  85. };
  86. #ifndef NO_EXEC_EXTERNALS
  87. FILE *fd = popen("/bin/uname -s -r -v -m","r");
  88. if( fd!= NULL ) {
  89. fgets( m_sSYS_INFO.OS_INFO, 127, fd);
  90. m_sSYS_INFO.OS_INFO[127] = (char)0;
  91. trim( m_sSYS_INFO.OS_INFO );
  92. memset( m_sSYS_INFO.HW_INFO, 0, 128 );
  93. fclose( fd );
  94. fd = NULL;
  95. };
  96. #else
  97. FILE *fd = NULL;
  98. #endif
  99. fd = fopen("/proc/cpuinfo","r");
  100. if ( fd!= NULL ) {
  101. char tBB[1024];
  102. bool bARM = false;
  103. while( !feof( fd ) ) {
  104. fgets( tBB, 1024, fd );
  105. trim( tBB );
  106. if( strcasestr(tBB, "model") != NULL ) {
  107. if( strcasestr(tBB, "model name") != NULL ) {
  108. if ( !bARM ) {
  109. if( strcasestr( tBB, "ARM" ) != NULL){
  110. bARM = true;
  111. } else {
  112. CArray *tA = explode(':', tBB );
  113. if ( tA ) {
  114. strncpy( m_sSYS_INFO.HW_INFO, tA->at(1), 127 );
  115. delete tA;
  116. };
  117. break;
  118. };
  119. };
  120. } else {
  121. if( bARM ) {
  122. CArray *tA = explode(':', tBB);
  123. if ( tA ) {
  124. strncpy( m_sSYS_INFO.HW_INFO, tA->at(1), 127 );
  125. delete tA;
  126. };
  127. break;
  128. };
  129. };
  130. };
  131. };
  132. m_sSYS_INFO.HW_INFO[127] = (char)0;
  133. trim( m_sSYS_INFO.HW_INFO );
  134. fclose( fd );
  135. fd = NULL;
  136. };
  137. #ifdef USE_MYSQL
  138. m_pDBC = mysqlDBC::getInstance();
  139. #else
  140. m_pDBC = sqliteDBC::getInstance();
  141. #endif
  142. if( m_pDBC == NULL ) {
  143. LOG_ERROR( "Failure to get DBC Instance -> Forced HW Enumeration!" );
  144. enumerateHardware();
  145. } else {
  146. char tB[128];
  147. struct rlimit rl={0};
  148. getrlimit( RLIMIT_STACK, &rl );
  149. sprintf( tB, "Stack Size: %d", rl.rlim_cur ); //RLIM_INFINITY
  150. LOG( tB );
  151. sprintf( tB, "MAX Stack Size: %d", rl.rlim_max );
  152. LOG( tB );
  153. //enumerateHardware( !cold_start );
  154. enumerateNIFS();
  155. #ifdef DEBUG
  156. LOG("Load Ressources...");
  157. #endif
  158. loadResources();
  159. #ifdef DEBUG
  160. LOG("Load Resources Done");
  161. #endif
  162. };
  163. };
  164. hardwareManager::~hardwareManager(){
  165. #ifdef DEBUG
  166. LOG("Destructor Begin");
  167. #endif
  168. unloadResources();
  169. #ifdef DEBUG
  170. LOG("Destructor End");
  171. #endif
  172. _instance = NULL;
  173. };
  174. void hardwareManager::unloadResources( void ){
  175. #ifdef DEBUG
  176. LOG("Unload Resources ...");
  177. #endif
  178. //m_mVectorProtector.lock();
  179. if( m_vResources.size() ) {
  180. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  181. switch( (*idx)->getHardwareType() ) {
  182. case HARDWARE_UART:
  183. case HARDWARE_IP_UART: delete( ( hardwareResourceSerialProvider*)(*idx) ); break;
  184. case HARDWARE_NIF_LOOPBACK:
  185. case HARDWARE_NIF_ETHERNET:
  186. case HARDWARE_NIF_WIFI:
  187. case HARDWARE_NIF_VIRTUAL:
  188. case HARDWARE_NIF_TUNEL: delete( ( hardwareResourceNetwork*)(*idx) ); break;
  189. };
  190. };
  191. m_vResources.clear();
  192. };
  193. //m_mVectorProtector.unlock();
  194. #ifdef DEBUG
  195. LOG("Unload Resources Done");
  196. #endif
  197. };
  198. void hardwareManager::loadResources( void ){
  199. /** @todo Valgrind is showing several memory leaks from this function.
  200. *
  201. * ~~~~
  202. ==00:00:04:04.207 26021== 513 (480 direct, 33 indirect) bytes in 1 blocks are definitely lost in loss record 3,863 of 3,972
  203. ==00:00:04:04.207 26021== at 0x4838DEF: operator new(unsigned long) (vg_replace_malloc.c:342)
  204. ==00:00:04:04.207 26021== by 0x16D0EF: hardwareManager::loadResources() (hardwareManager.cpp:318)
  205. ==00:00:04:04.207 26021== by 0x16C9CE: hardwareManager::hardwareManager(bool) (hardwareManager.cpp:174)
  206. ==00:00:04:04.207 26021== by 0x16C470: hardwareManager::getInstance(bool) (hardwareManager.cpp:55)
  207. ==00:00:04:04.207 26021== by 0x1D6E89: mojoKernel::create() (mojoKernel.cpp:264)
  208. ==00:00:04:04.207 26021== by 0x1D5C60: main (mojoBackend.cpp:476)
  209. ==00:00:04:04.207 26021== by 0x779CD79: __libc_start_main (libc-start.c:308)
  210. ==00:00:04:04.207 26021== by 0x124CF9: _start (in /home/stephane/src/argus/Projects/la_mojo/Debug86/mojo_d)
  211. ==00:00:04:04.207 26021== by 0x1FFF000497: ???
  212. ==00:00:04:04.207 26021== by 0x1B: ???
  213. * ~~~~
  214. *
  215. * And:
  216. *
  217. * ~~~~
  218. ==00:00:04:04.198 26021== 240 bytes in 3 blocks are definitely lost in loss record 3,796 of 3,972
  219. ==00:00:04:04.198 26021== at 0x483877F: malloc (vg_replace_malloc.c:307)
  220. ==00:00:04:04.198 26021== by 0x233EAA: sqliteDBC::getResourceData(unsigned int) (SQLite_dataBinder.cpp:2897)
  221. ==00:00:04:04.198 26021== by 0x16CD43: hardwareManager::loadResources() (hardwareManager.cpp:260)
  222. ==00:00:04:04.198 26021== by 0x16C9CE: hardwareManager::hardwareManager(bool) (hardwareManager.cpp:174)
  223. ==00:00:04:04.198 26021== by 0x16C470: hardwareManager::getInstance(bool) (hardwareManager.cpp:55)
  224. ==00:00:04:04.198 26021== by 0x1D6E89: mojoKernel::create() (mojoKernel.cpp:264)
  225. ==00:00:04:04.198 26021== by 0x1D5C60: main (mojoBackend.cpp:476)
  226. ==00:00:04:04.198 26021== by 0x779CD79: __libc_start_main (libc-start.c:308)
  227. ==00:00:04:04.198 26021== by 0x124CF9: _start (in /home/stephane/src/argus/Projects/la_mojo/Debug86/mojo_d)
  228. ==00:00:04:04.198 26021== by 0x1FFF000497: ???
  229. ==00:00:04:04.198 26021== by 0x1B: ???
  230. * ~~~~
  231. */
  232. if( m_pDBC == NULL ) return;
  233. DBC_INT_LIST *tVal;
  234. tVal = m_pDBC->listResources( );//load whole site resources
  235. //tVal = m_pDBC->listResources( m_nServerNodeID );//Load Only Server Node resources
  236. if( tVal!= NULL ) {
  237. if( tVal->size() ) {
  238. for (DBC_INT_LIST::iterator index = tVal->begin(); index < tVal->end(); index++) {
  239. DBC_RESOURCE_DATA *tData = m_pDBC->getResourceData( *index );
  240. if( tData != NULL ) {
  241. hardwareResource *tPtr = NULL;
  242. switch( tData->type ){
  243. case HARDWARE_UART:{
  244. #ifdef DEBUG
  245. LOG("Construct HW UART");
  246. #endif
  247. hardwareResourceSerialProvider *tRes = new hardwareResourceSerialProvider( tData->id );
  248. if( tRes != NULL ) {
  249. if( tRes->create() ) {
  250. tPtr = ( hardwareResource *) tRes;
  251. } else {
  252. LOG_ERROR("Error Creating HW UART!");
  253. delete( tRes );
  254. };
  255. };
  256. break;
  257. };
  258. case HARDWARE_NIF_LOOPBACK:
  259. case HARDWARE_NIF_ETHERNET:
  260. case HARDWARE_NIF_WIFI:
  261. case HARDWARE_NIF_VIRTUAL:
  262. case HARDWARE_NIF_TUNEL:{
  263. #ifdef DEBUG
  264. LOG("Construct NetIF");
  265. #endif
  266. hardwareResourceNetwork *tRes = new hardwareResourceNetwork( tData->id );
  267. if( tRes != NULL ) {
  268. if( tRes->create() ) {
  269. tPtr = ( hardwareResource *) tRes;
  270. } else {
  271. LOG_ERROR("Error Creating Network Ressource!");
  272. delete( tRes );
  273. };
  274. };
  275. break;
  276. };
  277. case HARDWARE_IP_UART:{
  278. #ifdef DEBUG
  279. LOG("Construct IP UART");
  280. #endif
  281. hardwareResourceSerialProvider *tRes = new hardwareResourceSerialProvider( tData->id );
  282. if( tRes != NULL ) {
  283. if( tRes->create() ) {
  284. tPtr = ( hardwareResource *) tRes;
  285. } else {
  286. LOG_ERROR("Error Creating IP UART!");
  287. delete( tRes );
  288. };
  289. };
  290. break;
  291. };
  292. case HARDWARE_ANALOG_VIDEO_IN:
  293. case HARDWARE_DIGITAL_VIDEO_IN:{
  294. #ifdef DEBUG
  295. LOG("Construct VIDEO IN");
  296. #endif
  297. hardwareResourceVideoProvider *tRes = new hardwareResourceVideoProvider( tData->id );
  298. if( tRes != NULL ) {
  299. if( tRes->create() ) {
  300. tPtr = ( hardwareResource *) tRes;
  301. } else {
  302. LOG_ERROR("Error Creating Video IN!");
  303. delete( tRes );
  304. };
  305. };
  306. break;
  307. };
  308. #ifdef DEBUG
  309. default:{
  310. char tB[128];
  311. sprintf( tB, "Unknown Ressource[%d] Type[%d]!", *index, tData->type );
  312. LOG_ERROR(tB);
  313. break;
  314. };
  315. #endif
  316. };
  317. if( tPtr!= NULL ) {
  318. #ifdef DEBUG
  319. LOG("Resource Sucessfully Created!");
  320. #endif
  321. m_mVectorProtector.lock();
  322. m_vResources.push_back( tPtr );
  323. m_mVectorProtector.unlock();
  324. m_pDBC->setRessourceActive( *index );
  325. } else {
  326. m_pDBC->setRessourceActive( *index, false );
  327. };
  328. } else {
  329. LOG_ERROR("Can not fetch Resource Data from Database!");
  330. };
  331. };
  332. #ifdef DEBUG
  333. } else {
  334. LOG("Fetched Empty List!");
  335. #endif
  336. };
  337. tVal->clear();
  338. delete( tVal );
  339. #ifdef DEBUG
  340. } else {
  341. LOG("Fetched NULL!");
  342. #endif
  343. };
  344. };
  345. bool hardwareManager::validate( void ){
  346. bool RetVal = true;
  347. m_mVectorProtector.lock();
  348. if( m_vResources.size() ) {
  349. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  350. if( !(*idx)->validate() ) {
  351. // recreate Resource
  352. LOG_ERROR( "HW Resource Invalid!" );
  353. m_pDBC->setRessourceActive( (*idx)->getDbId(), false );
  354. #ifdef DEBUG
  355. char tB[256];
  356. sprintf( tB, "Info: DBID[%d] R-Type: %d Provider: %d ", (*idx)->getDbId(), (*idx)->getHardwareType(), (*idx)->getProviderType() );
  357. LOG( tB );
  358. #endif
  359. if( !(*idx)->create()){
  360. LOG_ERROR( "HW Resource Creation Failure!" );
  361. } else {
  362. m_pDBC->setRessourceActive( (*idx)->getDbId() );
  363. #ifdef DEBUG
  364. LOG("Re-Created OK");
  365. #endif
  366. };
  367. #ifdef DEBUG_EXTRA
  368. } else {
  369. LOG("Resource OK!");
  370. #endif
  371. };
  372. };
  373. #ifdef DEBUG_EXTRA
  374. } else {
  375. LOG("No Resources!");
  376. #endif
  377. };
  378. m_mVectorProtector.unlock();
  379. //Mem Validator
  380. #ifdef MEMORY_CONSUMPTION_LIMIT
  381. if( m_nMemoryLimitHeapSize ) {
  382. size_t memStat = getCurrentRSS();
  383. #ifdef DEBUG_EXTRA
  384. char MEM_MSG[128];
  385. sprintf( MEM_MSG, "MEM: %" PRIu64 " TARG: %" PRIu64, memStat, m_nMemoryLimitHeapSize );
  386. LOG( MEM_MSG );
  387. #endif
  388. //https://www.ebay.com/itm/274974088159?
  389. RetVal = ( memStat < m_nMemoryLimitHeapSize );
  390. if ( !RetVal ) {
  391. LOG_ERROR("Memory Consumption too BIG!");
  392. };
  393. };
  394. if ( m_bCacheCleanCounter == 0 ) {
  395. LOG_ERROR("System Cache Fas Too Big Too Many Times!");
  396. RetVal = false;
  397. };
  398. #endif
  399. return RetVal;
  400. };
  401. void hardwareManager::getMAC( CSTR if_name, CSTR buf ){
  402. char tB[128];
  403. FILE * fd=NULL;
  404. if ( buf ) {
  405. sprintf(tB, "/sys/class/net/%s/address", if_name);
  406. #ifdef DEBUG_EXTRA
  407. LOG( tB );
  408. #endif
  409. fd=fopen(tB,"r");
  410. if ( fd ) {
  411. fread( buf, 1, INET6_ADDRSTRLEN, fd );
  412. fclose( fd );
  413. buf = trim( buf );
  414. } else {
  415. memset( buf, 0, INET6_ADDRSTRLEN );
  416. };
  417. #ifdef DEBUG_EXTRA
  418. LOG( buf );
  419. #endif
  420. };
  421. };
  422. void hardwareManager::getGW( CSTR if_name, CSTR buf ){
  423. memset( buf, 0, INET6_ADDRSTRLEN );
  424. #ifndef NO_EXEC_EXTERNALS
  425. FILE * fd = popen( "route -n", "r" );
  426. char tB[1024];
  427. //char expBuf[1024];
  428. if ( fd ) {
  429. while (!feof(fd)) {
  430. fgets((CSTR)tB, 1024, fd);
  431. trim((CSTR)tB);
  432. if( strlen(tB) ) {
  433. if( strstr( tB, if_name ) != NULL ){
  434. //strncpy( expBuf, tB, 1024 );
  435. CArray *CA = explode( ' ', tB );
  436. if ( CA ) {
  437. //for (int i=0; i< CA->size(); i++ ) {
  438. // char tB[128];
  439. // trim( CA->at(i) );
  440. // sprintf( tB, "GW# %d: %s", i, CA->at(i) );
  441. // LOG( tB );
  442. //};
  443. trim( CA->at(1) );
  444. if( strstr(CA->at(1), "0.0.0.0") == NULL ) {
  445. strncpy( buf, CA->at(1), INET6_ADDRSTRLEN );
  446. };
  447. delete(CA);
  448. };
  449. };
  450. };
  451. };
  452. fclose( fd );
  453. };
  454. #endif
  455. if( strlen( buf ) == 0 ) strncpy( buf, "0.0.0.0", INET6_ADDRSTRLEN );
  456. };
  457. void hardwareManager::getTTyDesc( CSTR dev_name, CSTR buf ){
  458. memset( buf, 0, DBC_RESOURCE_DESC_SIZE );
  459. #ifndef NO_EXEC_EXTERNALS
  460. FILE * fd = popen( "dmesg | grep tty", "r" );
  461. char tB[1024];
  462. //char expBuf[1024];
  463. if ( fd ) {
  464. while (!feof(fd)) {
  465. fgets((CSTR)tB, 1024, fd);
  466. trim((CSTR)tB);
  467. if( strlen(tB) ) {
  468. if( strstr( tB, dev_name ) != NULL ){
  469. CArray *CA = explode( ':', tB );
  470. if ( CA ) {
  471. if( CA->size() ) {
  472. trim( CA->at(CA->size()-1) );
  473. strncpy( buf, CA->at(CA->size()-1), CTEXT );
  474. buf[CTEXT-1]=(char)0;
  475. };
  476. delete(CA);
  477. };
  478. };
  479. };
  480. };
  481. fclose( fd );
  482. };
  483. #endif
  484. if( strlen( buf ) == 0 ){
  485. strncpy( buf, "--Unknown--", DBC_RESOURCE_DESC_SIZE );
  486. buf[DBC_RESOURCE_DESC_SIZE-1]=(char)0;
  487. };
  488. };
  489. PARSE_RESULT_NET_INTERFACES *hardwareManager::parse_neconfig( CSTR if_name ){
  490. PARSE_RESULT_NET_INTERFACES *tVal = NULL;
  491. FILE *fd;
  492. bool cConfig = false;
  493. fd = fopen("/etc/network/interfaces","r");
  494. if ( fd ) {
  495. char tB[1024];
  496. char expBuf[1024];
  497. tVal = (PARSE_RESULT_NET_INTERFACES *) malloc( sizeof(PARSE_RESULT_NET_INTERFACES));
  498. if ( tVal ) {
  499. memset(tVal, 0 , sizeof(PARSE_RESULT_NET_INTERFACES));
  500. while (!feof(fd)) {
  501. fgets((CSTR)tB, 1024, fd);
  502. trim((CSTR)tB);
  503. if( strlen(tB) ) {
  504. strncpy( expBuf, tB, 1024 );
  505. CArray *CA = explode( ' ', expBuf );
  506. if( CA ) {
  507. for( CArray::iterator idx = CA->begin(); idx < CA->end(); idx++ ) {
  508. if((*idx)[0]=='#') break;
  509. if(strstr((*idx), "auto")!= NULL ) if( strstr( tB, if_name )!= NULL ) tVal->enabled = true;
  510. if(strstr((*idx), "allow-hotplug")!= NULL ) if( strstr( tB, if_name )!= NULL ) tVal->hot_plug = true;
  511. if(strstr((*idx), "iface")!= NULL ) if( strstr( tB, if_name )!= NULL ){
  512. if (strstr( tB, "static") != NULL) tVal->if_static = true;
  513. cConfig = true;
  514. } else cConfig = false;
  515. if(strstr((*idx), "address")!= NULL ) if( cConfig ){
  516. idx++;
  517. if(strstr((*idx),"/")!= NULL ){
  518. int8_t sh = strstr((*idx),"/") - (*idx);
  519. strncpy( tVal->address, (*idx), sh );
  520. tVal->address[sh]=(char)0;
  521. int8_t bm = atoi( (*idx) + sh + 1 );
  522. union{
  523. uint32_t mm;
  524. uint8_t nm[4];
  525. } nmu = {0};
  526. for ( sh = 0; sh < bm; sh++ ) nmu.mm |= (1 << sh);
  527. sprintf(tVal->netmask, "%d.%d.%d.%d", nmu.nm[0], nmu.nm[1], nmu.nm[2], nmu.nm[3]);
  528. } else {
  529. strncpy( tVal->address, (*idx), INET6_ADDRSTRLEN );
  530. };
  531. };
  532. if(strstr((*idx), "netmask")!= NULL ) if( cConfig ){
  533. LOG("netMSK");
  534. idx++;
  535. LOG(*idx);
  536. strncpy(tVal->netmask, (*idx), INET6_ADDRSTRLEN);
  537. };
  538. //if(strstr((*idx), "bridge_ports")!= NULL ) cMode = 6;
  539. //if(strstr((*idx), "bridge_stp")!= NULL ) cMode = 7;
  540. //if(strstr((*idx), "bridge_waitport")!= NULL ) cMode = 8;
  541. //if(strstr((*idx), "bridge_fd")!= NULL ) cMode = 9;
  542. };
  543. delete( CA );
  544. };
  545. };
  546. };
  547. };
  548. fclose( fd );
  549. };
  550. #ifdef DEBUG_EXTRA
  551. if ( tVal ) {
  552. if( tVal->enabled ) LOG("NIF Enabled"); else LOG("NIF Disabled");
  553. if (tVal->hot_plug) LOG("NIF Hot Plugable"); else LOG("NIF not Hot Plugable");
  554. if (tVal->if_static) LOG("Address is static"); else LOG("Address is Dynamic");
  555. if(strlen(tVal->address))LOG(tVal->address);
  556. if(strlen(tVal->netmask))LOG(tVal->netmask);
  557. };
  558. #endif
  559. return tVal;
  560. };
  561. PARSE_RESULT_WIFI_SETTINGS *hardwareManager::parse_wifi( CSTR if_name ){
  562. PARSE_RESULT_WIFI_SETTINGS * tVal = NULL;
  563. FILE * fd;
  564. bool cConfig = false;
  565. bool is_wifi = false;
  566. tVal = (PARSE_RESULT_WIFI_SETTINGS *) malloc( sizeof(PARSE_RESULT_WIFI_SETTINGS));
  567. if ( tVal ) {
  568. memset(tVal, 0 , sizeof(PARSE_RESULT_WIFI_SETTINGS));
  569. fd = fopen("/etc/dnsmasq.conf","r");
  570. if ( fd ) {
  571. char tB[1024];
  572. char expBuf[1024];
  573. while (!feof(fd)) {
  574. fgets((CSTR)tB, 1024, fd);
  575. trim((CSTR)tB);
  576. if( strlen(tB) ) {
  577. strncpy( expBuf, tB, 1024 );
  578. CArray *CA = explode( '=', expBuf );
  579. if( CA ) {
  580. if( CA->size() ){
  581. if( CA->at(0)[0] == '#' ) continue;
  582. if( strstr(CA->at(0), "interface")!= NULL ) {
  583. if( strstr( tB, if_name)!= NULL ) {
  584. cConfig = true;
  585. tVal->dhcp_enabled = true;
  586. };
  587. };
  588. if( cConfig ){
  589. if ( strstr(CA->at(0), "dhcp-range")!= NULL ) {
  590. char tT[1024];
  591. strncpy( tT, CA->at(1), 1024 );
  592. CArray *CB = explode(',', tT );
  593. if( CB ){
  594. if( CB->size() > 3 ){
  595. strncpy( tVal->range_start, CB->at(0), INET6_ADDRSTRLEN);
  596. strncpy( tVal->range_stop, CB->at(1), INET6_ADDRSTRLEN);
  597. strncpy( tVal->netmask, CB->at(2), INET6_ADDRSTRLEN);
  598. strncpy( tVal->lease_period, CB->at(3), INET6_ADDRSTRLEN);
  599. };
  600. delete( CB );
  601. };
  602. };
  603. };
  604. };
  605. delete( CA );
  606. };
  607. };
  608. };
  609. fclose( fd );
  610. };
  611. #ifndef NO_EXEC_EXTERNALS
  612. //Check WiFi
  613. if( strstr(if_name,"w")!= NULL ) {
  614. char tName[128];
  615. is_wifi = true;
  616. sprintf( tName, "iwconfig %s", if_name );
  617. fd = popen( tName, "r" );
  618. if ( fd ) {
  619. char tB[1024];
  620. while (!feof(fd)) {
  621. fgets((CSTR)tB, 1024, fd );
  622. trim( tB );
  623. if (strstr(tB, "Mode") != NULL) if (strstr(tB, "Master") != NULL) tVal->access_point = true;
  624. };
  625. fclose( fd );
  626. };
  627. };
  628. #endif
  629. if ( tVal->access_point ) {
  630. fd = fopen("/etc/hostapd/hostapd.conf", "r");
  631. if ( fd ) {
  632. char tB[1024];
  633. char expBuf[1024];
  634. while (!feof(fd)) {
  635. fgets((CSTR)tB, 1024, fd);
  636. trim( tB );
  637. if( strlen(tB) ) {
  638. if( tB[0]=='#' ) continue;
  639. strncpy(expBuf, tB, 1024);
  640. CArray *CA = explode( '=', expBuf );
  641. if( CA ) {
  642. if( CA->size() ){
  643. if (strstr(CA->at(0), "ssid") != NULL) if (strstr(CA->at(0), "ignore_broadcast_ssid") == NULL){
  644. strncpy( tVal->SSID, CA->at(1), 64 );
  645. } else tVal->hiden_ssid = (bool) atoi(CA->at(1));
  646. if (strstr(CA->at(0), "wpa_passphrase") != NULL) strncpy( tVal->password, CA->at(1), 64 );
  647. if (strstr(CA->at(0), "country_code") != NULL) strncpy(tVal->country_code, CA->at(1), 3);
  648. if (strstr(CA->at(0), "channel") != NULL) tVal->channel = atoi(CA->at(1));
  649. };
  650. delete( CA );
  651. };
  652. };
  653. };
  654. fclose( fd );
  655. };
  656. } else {
  657. fd = fopen("/etc/wpa_supplicant/wpa_supplicant.conf", "r");
  658. if ( fd ) {
  659. fclose( fd );
  660. };
  661. };
  662. fd = fopen("/etc/rc.local","r");
  663. if ( fd ) {
  664. char tB[1024];
  665. char expBuf[1024];
  666. while (!feof(fd)) {
  667. fgets((CSTR)tB, 1024, fd);
  668. trim((CSTR)tB);
  669. if( strlen(tB) ) {
  670. strncpy( expBuf, tB, 1024 );
  671. CArray *CA = explode( ' ', expBuf );
  672. if( CA ) {
  673. if ( CA->size() ) {
  674. if (CA->at(0)[0] == '#') continue;
  675. if( strstr(CA->at(0), "iptables-restore")!= NULL ) {
  676. char zB[1024];
  677. FILE *afd = fopen( CA->at(2), "r" );
  678. if ( afd ) {
  679. while( !feof( afd ) ){
  680. fgets((CSTR)zB, 1024, afd );
  681. if( strstr( zB, "POSTROUTING" )!= NULL ) if( strstr( zB, "MASQUERADE" )!= NULL ) if( strstr( zB, if_name )== NULL ){
  682. CArray *CB = explode( ' ', zB );
  683. if ( CB ) {
  684. if ( CB->size() > 3 ) {
  685. strncpy(tVal->router_outbond, CB->at(3), INET6_ADDRSTRLEN);
  686. };
  687. delete( CB );
  688. };
  689. tVal->router_enabled = true;
  690. };
  691. };
  692. fclose(afd);
  693. } else LOG("Error Opening Firewall Rules!");
  694. };
  695. };
  696. delete( CA );
  697. };
  698. };
  699. };
  700. fclose(fd);
  701. } else LOG_ERROR("Can not open rc.local!");
  702. #ifdef DEBUG_EXTRA
  703. char zz[128];
  704. if ( is_wifi ) {
  705. if (tVal->access_point){
  706. LOG("WiFi is in AP Mode");
  707. sprintf( zz, "\tSSID:%s PWD:%s", tVal->SSID, tVal->password );
  708. LOG( zz );
  709. if (tVal->hiden_ssid) LOG("SSID is hidden");
  710. } else LOG("WiFi is in Client Mode");
  711. };
  712. if (tVal->dhcp_enabled){
  713. LOG("DHCP Service - Enabled");
  714. sprintf( zz, "\tRange: %s - %s Lease Time: %s", tVal->range_start, tVal->range_stop, tVal->lease_period );
  715. LOG( zz );
  716. } else {
  717. LOG("DHCP Service - Disabled");
  718. };
  719. if (tVal->router_enabled){
  720. sprintf( zz, "Routing enabled to [%s] NIF", tVal->router_outbond );
  721. LOG( zz );
  722. } else {
  723. LOG("Routing Disabled");
  724. };
  725. #endif
  726. };
  727. return tVal;
  728. };
  729. void hardwareManager::enumerateNIFS( void ){
  730. struct ifaddrs * ifaddr = NULL;
  731. char tBuffer[128];
  732. //if( m_pKernel == NULL ) return;
  733. if( m_pDBC == NULL ) return;
  734. if( -1 == getifaddrs(&ifaddr)) {
  735. LOG_ERROR("No NET INTERFACES?!");
  736. } else {
  737. #ifdef DEBUG
  738. LOG("Enumerating Network Interfaces");
  739. #endif
  740. struct ifaddrs * ifa;
  741. for( ifa = ifaddr; ifa; ifa = ifa->ifa_next ){
  742. char addr[INET6_ADDRSTRLEN];
  743. char mask[INET6_ADDRSTRLEN];
  744. char gw[INET6_ADDRSTRLEN];
  745. char bc[INET6_ADDRSTRLEN];
  746. char mac[INET6_ADDRSTRLEN];
  747. char device[INET6_ADDRSTRLEN];
  748. uint32_t nif_id = 0;
  749. uint32_t nif_aux_id = 0;
  750. uint32_t router_id = 0;
  751. NIF_TYPE nif_type = NIF_LOOPBACK;
  752. NETWORK_TYPE net_type = NETWORK_IPV4;
  753. RESOURCE_TYPE res_type = HARDWARE_NIF_LOOPBACK;
  754. NIF_MODE nif_mode = ADDRESS_DISABLED;
  755. #ifdef DEBUG_EXTRA
  756. LOG("*** Network Interface ***");
  757. #endif
  758. strncpy( device, ifa->ifa_name, INET6_ADDRSTRLEN );
  759. strncpy( mac,"--no-mac--", INET6_ADDRSTRLEN );
  760. #ifdef DEBUG_EXTRA
  761. LOG(device);
  762. #endif
  763. // Filter Loopback if
  764. if( strstr( device, "lo" ) != NULL ){
  765. nif_type = NIF_LOOPBACK;
  766. res_type = HARDWARE_NIF_LOOPBACK;
  767. //Not Implemented
  768. #ifdef DEBUG_EXTRA
  769. LOG("Loopback Device -> SKIP");
  770. #endif
  771. continue;
  772. };
  773. // Filter vpn TUN if
  774. if( strstr( device, "tun" ) != NULL ){
  775. nif_type = NIF_VPN_TUNEL;
  776. res_type = HARDWARE_NIF_TUNEL;
  777. //Not Implemented Yet
  778. #ifdef DEBUG_EXTRA
  779. LOG("Tunel Device -> SKIP");
  780. #endif
  781. continue;
  782. };
  783. if( strstr( device, "e" ) != NULL ){
  784. nif_type = NIF_ETHERNET;
  785. res_type = HARDWARE_NIF_ETHERNET;
  786. };
  787. if( strstr( device, "wl" ) != NULL ){
  788. nif_type = NIF_WIFI;
  789. res_type = HARDWARE_NIF_WIFI;
  790. };
  791. if( strstr( device, "ww" ) != NULL ){
  792. nif_type = NIF_WAN;
  793. res_type = HARDWARE_NIF_WAN;
  794. };
  795. if( ifa->ifa_addr != NULL ){
  796. if (ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) {
  797. #ifdef DEBUG_EXTRA
  798. LOG("No INET ADDRESSES -> SKIP");
  799. #endif
  800. continue;
  801. }
  802. } else {
  803. #ifdef DEBUG_EXTRA
  804. LOG("ifa->ifa_addr is NULL -> SKIP");
  805. #endif
  806. continue;
  807. };
  808. #ifdef DEBUG_EXTRA
  809. LOG("Getting Info....");
  810. #endif
  811. getMAC( device, mac );
  812. getGW( device, gw );
  813. #ifdef DEBUG_EXTRA
  814. LOG( mac );
  815. LOG( gw );
  816. #endif
  817. PARSE_RESULT_NET_INTERFACES * tt = NULL;
  818. PARSE_RESULT_WIFI_SETTINGS * tw = NULL;
  819. #ifdef DEBUG_EXTRA
  820. LOG("Parse NetConfig Info....");
  821. #endif
  822. tt = parse_neconfig( device );
  823. if( tt ) {
  824. if ( tt->enabled ) {
  825. if( tt->if_static ){
  826. nif_mode = ADDRESS_STATIC;
  827. } else {
  828. nif_mode = ADDRESS_DYNAMIC;
  829. };
  830. } else {
  831. nif_mode = ADDRESS_DISABLED;
  832. };
  833. } else {
  834. LOG_ERROR("Error Parsing System NIF Settings!");
  835. continue;
  836. };
  837. #ifdef DEBUG_EXTRA
  838. LOG("Parse Routing/DHCP/AP Info....");
  839. #endif
  840. tw = parse_wifi( device );
  841. if ( tw ){
  842. #ifdef DEBUG_EXTRA
  843. LOG("Routing/AP Info OK!");
  844. #endif
  845. if( nif_mode == ADDRESS_STATIC ) {
  846. if( tw->dhcp_enabled ){
  847. #ifdef DEBUG_EXTRA
  848. LOG("Address is Router");
  849. #endif
  850. nif_mode = ADDRESS_STATIC_RAP;
  851. };
  852. };
  853. nif_id = m_pDBC->addLocalNIF( device, mac, nif_type, nif_mode );
  854. #ifdef DEBUG_EXTRA
  855. sprintf( tBuffer, "NIF(%s) added as [%d]", device, nif_id );
  856. LOG( tBuffer );
  857. #endif
  858. if ( nif_type == NIF_WIFI ){
  859. if( nif_id == 0 ){
  860. LOG_ERROR("Error Adding NIF in DB!");
  861. continue;
  862. };
  863. WIFI_MODE w_mode;
  864. if( !tt->enabled ) {
  865. w_mode = WIFI_DISABLED;
  866. } else {
  867. if( tt->if_static ){
  868. if( tw->access_point ) {
  869. w_mode = WIFI_IS_AP;
  870. uint32_t outbond_id = 0;
  871. if( tw->router_enabled ){
  872. outbond_id = m_pDBC->getNIF_IDbyDevice( tw->router_outbond );
  873. };
  874. if( tw->dhcp_enabled ) {
  875. #ifdef DEBUG_EXTRA
  876. LOG("ADD Router With DHCP");
  877. #endif
  878. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period );
  879. } else {
  880. #ifdef DEBUG_EXTRA
  881. LOG("ADD Router ONLY");
  882. #endif
  883. router_id = m_pDBC->addLocalNetworkInfrastructure( nif_id, outbond_id );
  884. };
  885. #ifdef DEBUG_EXTRA
  886. sprintf( tBuffer, "Router added as [%d]", router_id );
  887. LOG( tBuffer );
  888. #endif
  889. } else {
  890. w_mode = WIFI_CONNECTED_TO_AP;
  891. };
  892. };
  893. };
  894. #ifdef DEBUG_EXTRA
  895. LOG("ADD WiFi Record");
  896. #endif
  897. nif_aux_id = m_pDBC->addLocalWiFi( nif_id, w_mode, tw->SSID, tw->password, ENCRIPTION_WPA_PSK, router_id );
  898. };
  899. if( nif_type == NIF_ETHERNET || nif_type == NIF_ETHERNET_VIRTUAL ){
  900. if( tt->if_static ){
  901. uint32_t outbond_id = 0;
  902. nif_mode = ADDRESS_STATIC;
  903. if( tw->router_enabled ){
  904. nif_mode = ADDRESS_STATIC_RAP;
  905. outbond_id = m_pDBC->getNIF_IDbyDevice( tw->router_outbond );
  906. };
  907. if( tw->dhcp_enabled ) {
  908. #ifdef DEBUG_EXTRA
  909. LOG("ADD Router With DHCP");
  910. #endif
  911. router_id = m_pDBC->addLocalNetworkInfrastructure( nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period );
  912. } else {
  913. #ifdef DEBUG_EXTRA
  914. LOG("ADD Router ONLY");
  915. #endif
  916. router_id = m_pDBC->addLocalNetworkInfrastructure( nif_id, outbond_id );
  917. };
  918. #ifdef DEBUG_EXTRA
  919. sprintf( tBuffer, "Router added as [%d]", router_id );
  920. LOG( tBuffer );
  921. #endif
  922. } else {
  923. nif_mode = ADDRESS_DYNAMIC;
  924. };
  925. };
  926. free(tt);
  927. free(tw);
  928. } else {
  929. LOG_ERROR("Error Parsing System Router/AP Settings!");
  930. free(tt);
  931. continue;
  932. };
  933. if( nif_aux_id != 0 ) {
  934. nif_id = m_pDBC->addLocalNIF(device, mac, nif_type, nif_mode, nif_aux_id );
  935. #ifdef DEBUG_EXTRA
  936. sprintf( tBuffer, "2 NIF(%s) Updated as [%d]", device, nif_id );
  937. LOG( tBuffer );
  938. #endif
  939. if( nif_id == 0 ) {
  940. LOG_ERROR("Error Adding NIF in DB!");
  941. continue;
  942. };
  943. #ifdef DEBUG_EXTRA
  944. LOG("NIF added!");
  945. } else {
  946. LOG("NIF Already added (regular Mode)!");
  947. #endif
  948. };
  949. m_pDBC->addResource( /*m_nServerNodeID,*/ res_type, device, nif_id );
  950. if( ifa->ifa_addr->sa_family == AF_INET ){
  951. net_type = NETWORK_IPV4;
  952. // create IPv4 string
  953. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  954. inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
  955. sprintf( mask, "%d.%d.%d.%d",
  956. (uint8_t)ifa->ifa_netmask->sa_data[2],
  957. (uint8_t)ifa->ifa_netmask->sa_data[3],
  958. (uint8_t)ifa->ifa_netmask->sa_data[4],
  959. (uint8_t)ifa->ifa_netmask->sa_data[5]
  960. );
  961. sprintf( bc, "%d.%d.%d.%d",
  962. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  963. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  964. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  965. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  966. );
  967. } else { // AF_INET6
  968. net_type = NETWORK_IPV6;
  969. // create IPv6 string
  970. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  971. inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
  972. sprintf( mask, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  973. (uint8_t)ifa->ifa_netmask->sa_data[2],
  974. (uint8_t)ifa->ifa_netmask->sa_data[3],
  975. (uint8_t)ifa->ifa_netmask->sa_data[4],
  976. (uint8_t)ifa->ifa_netmask->sa_data[5],
  977. (uint8_t)ifa->ifa_netmask->sa_data[6],
  978. (uint8_t)ifa->ifa_netmask->sa_data[7],
  979. (uint8_t)ifa->ifa_netmask->sa_data[8],
  980. (uint8_t)ifa->ifa_netmask->sa_data[9],
  981. (uint8_t)ifa->ifa_netmask->sa_data[10],
  982. (uint8_t)ifa->ifa_netmask->sa_data[11],
  983. (uint8_t)ifa->ifa_netmask->sa_data[12],
  984. (uint8_t)ifa->ifa_netmask->sa_data[13],
  985. (uint8_t)ifa->ifa_netmask->sa_data[14],
  986. (uint8_t)ifa->ifa_netmask->sa_data[15],
  987. (uint8_t)ifa->ifa_netmask->sa_data[16],
  988. (uint8_t)ifa->ifa_netmask->sa_data[17]
  989. );
  990. if( ifa->ifa_ifu.ifu_broadaddr != NULL ){
  991. sprintf( bc, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  992. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  993. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  994. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  995. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5],
  996. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[6],
  997. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[7],
  998. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[8],
  999. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[9],
  1000. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[10],
  1001. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[11],
  1002. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[12],
  1003. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[13],
  1004. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[14],
  1005. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[15],
  1006. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[16],
  1007. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[17]
  1008. );
  1009. } else {
  1010. sprintf( bc, "" );
  1011. };
  1012. };
  1013. m_pDBC->addLocalNetwork( nif_id, net_type, addr, mask, gw, bc );
  1014. #ifdef DEBUG
  1015. sprintf( tBuffer, "Detected Network Interface: %s [%s]", ifa->ifa_name, addr );
  1016. LOG(tBuffer);
  1017. #endif
  1018. nif_id = 0;
  1019. nif_aux_id = 0;
  1020. router_id = 0;
  1021. nif_type = NIF_LOOPBACK;
  1022. net_type = NETWORK_IPV4;
  1023. res_type = HARDWARE_NIF_LOOPBACK;
  1024. nif_mode = ADDRESS_DISABLED;
  1025. };
  1026. freeifaddrs( ifaddr );
  1027. };
  1028. };
  1029. void hardwareManager::enumerateHardware( void ){
  1030. char tBuffer[CTEXT];
  1031. DIR * dFD = NULL;
  1032. struct dirent * pDR = NULL;
  1033. //ToDo: Check Ability to work without Kernel and DBC
  1034. //if( m_pKernel == NULL ) return;
  1035. if( m_pDBC == NULL ) return;
  1036. #ifdef DEBUG
  1037. LOG("HW Enumeration Started!");
  1038. #endif
  1039. // Moved to a constructor!!
  1040. //m_nServerNodeID = m_pKernel->getServerNodeId();
  1041. //m_nServerID = m_pKernel->getServerId();
  1042. //m_nSiteID = m_pKernel->getSiteId();
  1043. /* Add Network Interfaces */
  1044. //if( !m_pDBC->cleanResources( true ) ){
  1045. // LOG_ERROR("Cannot DeActivate Old Ressources!");
  1046. // return false;
  1047. //};
  1048. if( !m_pDBC->wipeServerResources() ){
  1049. LOG_ERROR("Cannot Wipe Old Ressources!");
  1050. return;
  1051. };
  1052. enumerateNIFS();
  1053. /* Check HID Devices */
  1054. struct hid_device_info * pDevs;
  1055. uint8_t DCBCount;
  1056. //Enumerate USB devices with the given IDs
  1057. pDevs = hid_enumerate( 0x16c0, 0x05df );
  1058. if( pDevs!=NULL ){
  1059. struct hid_device_info * pCDev = pDevs;
  1060. //Count the number of returned devices
  1061. for( DCBCount = 0; pCDev!=NULL; DCBCount++ ) pCDev = pCDev->next;
  1062. //Allocate a buffer for the relays
  1063. if( DCBCount > 0 ){
  1064. DC_BOARD tDCB;
  1065. //Fill the relay structs
  1066. pCDev = pDevs;
  1067. for( int idx = 0; idx < DCBCount; idx++ ){
  1068. //Save the path to this device
  1069. memset( tDCB.path, 0, MAX_PATH_LEN );
  1070. strncpy( tDCB.path, pCDev->path, MAX_PATH_LEN );
  1071. // The product string is USBRelayx where x is number of relays read to the \0 in case there are more than 9
  1072. tDCB.relay_count = atoi((const char *)&pCDev->product_string[8]);
  1073. //Open it to get more details
  1074. hid_device * handle;
  1075. handle = hid_open_path( tDCB.path );
  1076. if( handle ){
  1077. unsigned char buf[9];
  1078. //Get the features of the device
  1079. buf[0] = 0x01;
  1080. if( hid_get_feature_report( handle, buf, sizeof(buf)) == -1 ){
  1081. LOG_ERROR("hid_get_feature_report");
  1082. pCDev = pCDev->next;
  1083. hid_close( handle );
  1084. continue;
  1085. };
  1086. //Set the serial number (0x0 for termination)
  1087. memset( tDCB.serial, 0x0, (HID_DCR_SERIIAL_LENGTH+1) );
  1088. memcpy( tDCB.serial, buf, HID_DCR_SERIIAL_LENGTH );
  1089. //Byte 7 in the response contains the target_state of the relays
  1090. tDCB.state = buf[7];
  1091. hid_close( handle );
  1092. #ifdef DEBUG
  1093. //Output the device enumeration details if verbose is on
  1094. LOG("HID DCB Found");
  1095. sprintf(tBuffer, " Type: %04hx::%04hx", pCDev->vendor_id, pCDev->product_id);
  1096. LOG(tBuffer);
  1097. sprintf(tBuffer, " Path: %s", pCDev->path );
  1098. LOG(tBuffer);
  1099. sprintf(tBuffer, " Serial_number: %ls", pCDev->serial_number );
  1100. LOG(tBuffer);
  1101. sprintf(tBuffer, " Manufacturer: %ls", pCDev->manufacturer_string);
  1102. LOG(tBuffer);
  1103. sprintf(tBuffer, " Product: %ls", pCDev->product_string);
  1104. LOG(tBuffer);
  1105. sprintf(tBuffer, " Release: %hx", pCDev->release_number);
  1106. LOG(tBuffer);
  1107. sprintf(tBuffer, " Interface: %d", pCDev->interface_number);
  1108. LOG(tBuffer);
  1109. sprintf(tBuffer, " Number of Relays = %d", tDCB.relay_count);
  1110. LOG(tBuffer);
  1111. //If debug are on, output individual relay details
  1112. for( int k = 0; k < tDCB.relay_count; k++ ){
  1113. if( tDCB.state & 1 << k ){
  1114. sprintf(tBuffer, " %s_%d=1", tDCB.serial, k + 1);
  1115. } else {
  1116. sprintf(tBuffer, " %s_%d=0", tDCB.serial, k + 1);
  1117. };
  1118. LOG(tBuffer);
  1119. };
  1120. #endif
  1121. m_pDBC->addLocalDCB( m_nSiteID, pCDev->path, tDCB.serial, tDCB.relay_count, tDCB.state );
  1122. } else {
  1123. LOG_ERROR("unable to open HID device");
  1124. };
  1125. pCDev = pCDev->next;
  1126. };
  1127. };
  1128. hid_free_enumeration( pDevs );
  1129. };
  1130. /* Add devices from DEV */
  1131. dFD = opendir("/dev");
  1132. if( dFD != NULL ) {
  1133. while( ( pDR = readdir( dFD ))!= NULL ){
  1134. if( strcasestr( pDR->d_name, "video" ) == pDR->d_name ) {
  1135. createResourceVIDEO( pDR->d_name );
  1136. continue;
  1137. };
  1138. if( strcasestr( pDR->d_name, "ttyS" ) == pDR->d_name ){
  1139. createResourceUART( pDR->d_name );
  1140. continue;
  1141. };
  1142. // RaspberyPI Hardware
  1143. if( strcasestr( pDR->d_name, "ttyAMA" ) == pDR->d_name ){
  1144. createResourceUART( pDR->d_name );
  1145. continue;
  1146. };
  1147. if( strcasestr( pDR->d_name, "ttyUSB" ) == pDR->d_name ){
  1148. createResourceUART( pDR->d_name );
  1149. continue;
  1150. };
  1151. };
  1152. closedir( dFD );
  1153. };
  1154. //m_bHardwareEnumerated = true;
  1155. m_pDBC->updateSystemInfo( &m_sSYS_INFO );
  1156. };
  1157. uint32_t hardwareManager::createResourceUART( CSTR devName ){
  1158. uint32_t RetVal = 0;
  1159. char tBuffer[CTEXT];
  1160. /* Enumerate BuiltIn Serial */
  1161. #ifdef DEBUG
  1162. sprintf( tBuffer, "Serial Device: %s", devName );
  1163. LOG( tBuffer );
  1164. #endif
  1165. char dev_name[CTEXT];
  1166. int fd = -1;
  1167. struct serial_struct ss;
  1168. sprintf( dev_name, "/dev/%s", devName );
  1169. fd = open( dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK );
  1170. if (-1 == fd) {
  1171. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1172. LOG_ERROR( tBuffer );
  1173. } else {
  1174. if ( 0 && ( -1 == xioctl(fd, TIOCGSERIAL, &ss) )){
  1175. LOG_ERROR("TIOCGSERIAL");
  1176. } else {
  1177. #ifdef DEBUG
  1178. sprintf( tBuffer, "Serial Port[Port:0x%X IRQ:%d BB:%d Line:%d Type:%d Flags:%X]",
  1179. ss.port,
  1180. ss.irq,
  1181. ss.baud_base,
  1182. ss.line,
  1183. ss.type,
  1184. ss.flags
  1185. );
  1186. LOG( tBuffer );
  1187. #endif
  1188. memset( tBuffer, 0, CTEXT );
  1189. getTTyDesc( devName, tBuffer );
  1190. if( tBuffer[0] ) {
  1191. LOG( tBuffer );
  1192. uint32_t tID = m_pDBC->addLocalUART(/*m_nServerNodeID,*/1, 19200, 8, 'N', 1.0f, dev_name );
  1193. if ( tID ) {
  1194. m_pDBC->addResource( /*m_nServerNodeID,*/ HARDWARE_UART, tBuffer, tID );
  1195. };
  1196. } else {
  1197. LOG_ERROR("Device Not Initialized at Boot Time!");
  1198. };
  1199. };
  1200. close(fd);
  1201. };
  1202. return RetVal;
  1203. };
  1204. uint32_t hardwareManager::createResourceVIDEO( CSTR devName ){
  1205. uint32_t RetVal = 0;
  1206. char tBuffer[256];
  1207. /* Enumerate Video Capture */
  1208. #ifdef DEBUG_EXTRA
  1209. sprintf( tBuffer, "Video Source: %s", devName );
  1210. LOG( tBuffer );
  1211. #endif
  1212. struct v4l2_capability cap;
  1213. //struct v4l2_cropcap cropcap;
  1214. //struct v4l2_crop crop;
  1215. struct v4l2_format fmt;
  1216. struct v4l2_frmivalenum sprm;
  1217. char dev_name[CTEXT];
  1218. int fd = -1;
  1219. struct stat st;
  1220. sprintf( dev_name, "/dev/%s", devName );
  1221. if (-1 == stat( dev_name, &st )) {
  1222. sprintf( tBuffer, "Cannot identify '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1223. LOG_ERROR( tBuffer );
  1224. return 0;
  1225. };
  1226. if (!S_ISCHR(st.st_mode)) {
  1227. sprintf( tBuffer, "%s is no devicen", dev_name);
  1228. LOG_ERROR( tBuffer );
  1229. return 0;
  1230. };
  1231. #ifdef DEBUG
  1232. LOG( dev_name );
  1233. #endif
  1234. fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
  1235. if (-1 == fd) {
  1236. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1237. LOG_ERROR( tBuffer );
  1238. } else {
  1239. if( -1 == xioctl(fd, VIDIOC_QUERYCAP, &cap) ){
  1240. if (EINVAL == errno) {
  1241. sprintf( tBuffer, "%s is no V4L2 device", dev_name );
  1242. LOG_ERROR( tBuffer );
  1243. } else {
  1244. LOG_ERROR( "VIDIOC_QUERYCAP" );
  1245. };
  1246. close(fd);
  1247. return 0;
  1248. };
  1249. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  1250. #ifdef DEBUG
  1251. sprintf(tBuffer, "%s is no video capture device", dev_name);
  1252. LOG_ERROR( tBuffer );
  1253. #endif
  1254. close(fd);
  1255. return 0;
  1256. };
  1257. CLEAR(fmt);
  1258. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1259. /* Preserve original settings as set by v4l2-ctl for example */
  1260. if( -1 == xioctl(fd, VIDIOC_G_FMT, &fmt)){
  1261. LOG_ERROR("VIDIOC_G_FMT");
  1262. } else {
  1263. CLEAR( sprm );
  1264. if(-1 == xioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &sprm) && 0 ){
  1265. LOG_ERROR("VIDIOC_ENUM_FRAMEINTERVALS");
  1266. } else {
  1267. uint16_t fps = 15;
  1268. switch ( sprm.type ) {
  1269. case V4L2_FRMIVAL_TYPE_DISCRETE: LOG("1DISCRETE"); break;
  1270. case V4L2_FRMIVAL_TYPE_CONTINUOUS: LOG("2DISCRETE"); break;
  1271. case V4L2_FRMIVAL_TYPE_STEPWISE: LOG("3DISCRETE"); break;
  1272. };
  1273. #ifdef DEBUG
  1274. sprintf( tBuffer, "Grabber{%s}{%s}[%dx%d@%d]", cap.card, cap.driver, fmt.fmt.pix.width,fmt.fmt.pix.height, fps );
  1275. LOG( tBuffer );
  1276. #endif
  1277. uint32_t mid = m_pDBC->addLocalCam( (CSTR)cap.card, (CSTR)cap.driver, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height, fps );
  1278. //uint64_t mid = m_pDBC->addLocalMediaSource( m_nSiteID, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height );
  1279. //m_pDBC->addNodeSocket( m_nServerNodeID, mid, NODE_SOCK_TYPE_ANALOG_VIDEO_GRABBER, NODE_SOCK_MODE_OUT, false, dev_name, tBuffer );
  1280. RetVal = m_pDBC->addResource( HARDWARE_ANALOG_VIDEO_IN, dev_name, mid );
  1281. };
  1282. };
  1283. close(fd);
  1284. };
  1285. return RetVal;
  1286. };
  1287. uint32_t hardwareManager::createResourceUDCB( CSTR devName ){
  1288. uint32_t RetVal = 0;
  1289. char tBuffer[256];
  1290. return RetVal;
  1291. };
  1292. bool hardwareManager::assignResource( hardwareResource *ptr, bool protected_mode ){
  1293. LOG("ASSIGN HWR");
  1294. if ( ptr == NULL ) return false;
  1295. if( protected_mode ) m_mVectorProtector.lock();
  1296. LOG("LOCKED");
  1297. m_vResources.push_back( ptr );
  1298. LOG("ADDED");
  1299. if( protected_mode ) m_mVectorProtector.unlock();
  1300. return true;
  1301. };
  1302. bool hardwareManager::resignResource( hardwareResource *ptr, bool protected_mode ){
  1303. LOG("RESIGN HWR");
  1304. if( ptr == NULL ) return false;
  1305. if( protected_mode ) m_mVectorProtector.lock();
  1306. LOG("LOCKED");
  1307. if( m_vResources.size() ) {
  1308. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  1309. if( (*idx) == ptr ) {
  1310. LOG("FOUND");
  1311. m_vResources.erase( idx );
  1312. LOG("ERASED");
  1313. break;
  1314. };
  1315. };
  1316. } else {
  1317. LOG("NO RES!");
  1318. };
  1319. LOG("UNLOCKED");
  1320. if( protected_mode ) m_mVectorProtector.unlock();
  1321. return true;
  1322. };
  1323. hardwareResource *hardwareManager::requestResource( HARDWARE_PROVIDERS type, uint32_t id ){
  1324. hardwareResource *RetVal = NULL;
  1325. m_mVectorProtector.lock();
  1326. if( m_vResources.size() ) {
  1327. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  1328. #ifdef DEBUG_EXTRA
  1329. char tB[256];
  1330. sprintf( tB, "Check Resources RR[%d] RI[%d] ?? CR[%d] CI[%d]", type, id, (*idx)->getProviderType(), (*idx)->getDbId() );
  1331. LOG( tB );
  1332. #endif
  1333. if( ((*idx)->getProviderType() == type ) && ( (*idx)->getDbId() == id ) ) {
  1334. RetVal = (*idx);
  1335. break;
  1336. };
  1337. };
  1338. } else {
  1339. LOG("No Resources!!");
  1340. };
  1341. m_mVectorProtector.unlock();
  1342. return RetVal;
  1343. };
  1344. DBC_INT_LIST *hardwareManager::requestResourcesByType( HARDWARE_PROVIDERS type ){
  1345. DBC_INT_LIST *RetVal = NULL;
  1346. #ifdef DEBUG
  1347. char tBuffer[128];
  1348. sprintf( tBuffer, "Requested Resource type: %d", type );
  1349. LOG( tBuffer );
  1350. #endif
  1351. m_mVectorProtector.lock();
  1352. if( m_vResources.size() ) {
  1353. #ifdef DEBUG
  1354. LOG("Have Some Resources!");
  1355. #endif
  1356. RetVal = new DBC_INT_LIST();
  1357. if( RetVal ) {
  1358. #ifdef DEBUG
  1359. LOG("RetVal Created!");
  1360. #endif
  1361. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  1362. #ifdef DEBUG
  1363. sprintf( tBuffer, "Have Resource type: %d", (*idx)->getHardwareType() );
  1364. LOG( tBuffer );
  1365. #endif
  1366. if( (*idx)->getProviderType() == type ) {
  1367. #ifdef DEBUG
  1368. LOG("Matched!");
  1369. #endif
  1370. RetVal->push_back((*idx)->getDbId());
  1371. };
  1372. };
  1373. } else {
  1374. LOG_ERROR("Cannot Create DBC_INT_LIST");
  1375. };
  1376. };
  1377. m_mVectorProtector.unlock();
  1378. return RetVal;
  1379. };
  1380. #include <sys/types.h>
  1381. #include <signal.h>
  1382. bool hardwareManager::updateSysInfo( DBC_SYS_LOAD_INFO *ptr ){
  1383. if ( ptr == NULL) return false;
  1384. struct sysinfo SI;
  1385. struct statvfs DI;
  1386. ptr->CPU_CORES = get_nprocs();
  1387. if( sysinfo( &SI ) == 0 ){
  1388. float f_load = 100.f / (float)( ptr->CPU_CORES * ( 1 << SI_LOAD_SHIFT ) );
  1389. ptr->cpu1 = SI.loads[0] * f_load;
  1390. ptr->cpu5 = SI.loads[1] * f_load;
  1391. ptr->cpu15 = SI.loads[2] * f_load;
  1392. ptr->T_MEM = SI.totalram * SI.mem_unit;
  1393. // "freeram" is obvious, and "bufferram" is memory temporarily used by the kernel to cache some files
  1394. ptr->C_MEM = ( SI.totalram - SI.freeram - SI.bufferram ) * SI.mem_unit;
  1395. ptr->mem = 100.0f * (float)ptr->C_MEM / (float)ptr->T_MEM;
  1396. #ifdef DEBUG_EXTRA
  1397. char tB[128];
  1398. sprintf( tB, "Total Mem: %" PRIu64 " Free: %" PRIu64 " Perc: %.2f", SI.totalram, SI.freeram, ptr->mem );
  1399. LOG( tB );
  1400. #endif
  1401. #ifdef MEMORY_CONSUMPTION_LIMIT
  1402. if ( ptr->mem > 80.0f ) {
  1403. LOG_ERROR( "Try to free some caches!" );
  1404. /*
  1405. To free pagecache:
  1406. # echo 1 > /proc/sys/vm/drop_caches
  1407. To free dentries and inodes:
  1408. # echo 2 > /proc/sys/vm/drop_caches
  1409. To free pagecache, dentries and inodes:
  1410. # echo 3 > /proc/sys/vm/drop_caches
  1411. */
  1412. //system( "echo 1 > /proc/sys/vm/drop_caches" );
  1413. if( m_bCacheCleanCounter ) {
  1414. m_bCacheCleanCounter--;
  1415. } else {
  1416. // KillSellf
  1417. // kill( getpid(), 9 );
  1418. };
  1419. };
  1420. #endif
  1421. } else {
  1422. LOG_ERROR("Error while getting System Info!");
  1423. };
  1424. //const uint32_t MB = 1 << 10;//(1024 * 1024);// * 1024;
  1425. if( statvfs("/", &DI) == 0 ){
  1426. ptr->T_DISK = ( DI.f_blocks * (unsigned long long) DI.f_frsize ) >> 10;// / MB;
  1427. //cDISK = ( DI.f_bavail * (unsigned long long) DI.f_bsize ) >> 10;// / MB;
  1428. ptr->C_DISK = ptr->T_DISK - ( ( DI.f_bfree * (unsigned long long) DI.f_bsize ) >> 10 );
  1429. ptr->disk = 100.0f * (float)ptr->C_DISK / (float)ptr->T_DISK;
  1430. } else {
  1431. LOG_ERROR("Error while getting Disk Info!");
  1432. };
  1433. //Temperature reading
  1434. ptr->temperature = -1;
  1435. if( fileExist( "/sys/class/thermal" ) ) {
  1436. char tB[128];
  1437. // Check first 8 zones to find CPU one
  1438. for ( uint8_t i = 0; i < 8; i++ ) {
  1439. sprintf( tB, "/sys/class/thermal/thermal_zone%d", i );
  1440. if ( fileExist( tB ) ) {
  1441. sprintf( tB, "/sys/class/thermal/thermal_zone%d/type", i );
  1442. if ( fileExist( tB ) ) {
  1443. FILE *fd = fopen( tB, "r" );
  1444. readLine( fd, tB, 128 );
  1445. fclose(fd);
  1446. if ( strcasestr(tB,"cpu")!=NULL || strcasestr(tB,"x86_pkg")!=NULL || strcasestr(tB,"acpitz")!=NULL ) {
  1447. sprintf( tB, "/sys/class/thermal/thermal_zone%d/temp", i );
  1448. FILE *fd = fopen( tB, "r" );
  1449. readLine( fd, tB, 128 );
  1450. fclose(fd);
  1451. ptr->temperature = ((float) atoll( tB )) / 1000.0f;
  1452. break;
  1453. };
  1454. } else continue;
  1455. } else break;
  1456. };
  1457. };
  1458. return m_pDBC->updateServerUtilisation( ptr );
  1459. };
  1460. void hardwareManager::clearAllResources( void ){
  1461. };
  1462. #undef CLEAR
  1463. #undef LOG
  1464. #undef LOG_ERROR