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.

1492 lines
58 KiB

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