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.

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