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.

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