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.

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