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.

1078 lines
44 KiB

6 years ago
​
​
​
5 years ago
5 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
6 years ago
6 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
5 years ago
​
5 years ago
​
​
​
5 years ago
​
​
​
5 years ago
​
5 years ago
​
​
5 years ago
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
6 years ago
​
6 years ago
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
  1. #include "rgooseHardwareManager.h"
  2. #include "rgooseKernel.h"
  3. #include "rgooseDBConnector.h"
  4. #include "rgooseHardwareResourceSerialProvider.h"
  5. #include "rgooseHardwareResourceNetworkProvider.h"
  6. #include "../hardware/hid/hid_dc.h"
  7. #include "../alg/generic/heapDebug.h"
  8. #include <hidapi/hidapi.h>
  9. #define LOG( X ) m_pLog->Log( "HWM", this, X )
  10. #ifdef DEBUG
  11. #define LOG_ERROR( X ) {\
  12. char _tBfrErrMSG2w34[256];\
  13. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  14. m_pLog->Log( "HWM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  15. }
  16. #else
  17. #define LOG_ERROR( X ) m_pLog->Log( "HWM", this, _LOG_ERROR, X );
  18. #endif
  19. argusHardwareManager *argusHardwareManager::_instance = NULL;
  20. /**
  21. * Returns the single instance of the object.
  22. */
  23. argusHardwareManager* argusHardwareManager::getInstance( bool cold_start ) {
  24. // check if the instance has been created yet
  25. if( 0 == _instance ){
  26. // if not, then create it
  27. _instance = new argusHardwareManager( cold_start );
  28. };
  29. // return the single instance
  30. return _instance;
  31. };
  32. #include <sys/time.h>
  33. #include <sys/resource.h>
  34. argusHardwareManager::argusHardwareManager( bool cold_start ){
  35. m_pLog = basicLogCollection::getInstance();
  36. //m_bHardwareEnumerated = false;
  37. m_nServerNodeID = 0;
  38. m_nServerID = 0;
  39. m_nSiteID = 0;
  40. m_nMemoryLimitHeapSize = 524288000; // 50 MB //524288000; //500 MB ~ 1 Hour
  41. m_pKernel = argusKernel::getInstance();
  42. if( m_pKernel == NULL ){
  43. LOG_ERROR( "Failure to get Kernel Instance!" );
  44. } else {
  45. m_nServerNodeID = m_pKernel->getServerNodeId();
  46. m_nServerID = m_pKernel->getServerId();
  47. m_nSiteID = m_pKernel->getSiteId();
  48. };
  49. m_pDBC = argusDBC::getInstance();
  50. if( m_pDBC == NULL ) {
  51. LOG_ERROR( "Failure to get DBC Instance -> Forced HW Enumeration!" );
  52. enumerateHardware();
  53. } else {
  54. char tB[128];
  55. struct rlimit rl={0};
  56. getrlimit( RLIMIT_STACK, &rl );
  57. sprintf( tB, "Stack Size: %d", rl.rlim_cur ); //RLIM_INFINITY
  58. LOG( tB );
  59. sprintf( tB, "MAX Stack Size: %d", rl.rlim_max );
  60. LOG( tB );
  61. //enumerateHardware( !cold_start );
  62. enumerateNIFS();
  63. #ifdef DEBUG
  64. LOG("Loading Ressources...");
  65. #endif
  66. loadResources();
  67. };
  68. };
  69. argusHardwareManager::~argusHardwareManager(){
  70. unloadResources();
  71. };
  72. void argusHardwareManager::unloadResources( void ){
  73. if( m_vResources.size() ) {
  74. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  75. switch( (*idx)->getHardwareType() ) {
  76. case ARGUS_HARDWARE_UART:
  77. case ARGUS_HARDWARE_IP_UART: delete( (argusHardwareResourceSerialProvider*)(*idx) ); break;
  78. case ARGUS_HARDWARE_NIF_LOOPBACK:
  79. case ARGUS_HARDWARE_NIF_ETHERNET:
  80. case ARGUS_HARDWARE_NIF_WIFI:
  81. case ARGUS_HARDWARE_NIF_VIRTUAL:
  82. case ARGUS_HARDWARE_NIF_TUNEL: delete( (argusHardwareResourceNetwork*)(*idx) ); break;
  83. };
  84. };
  85. m_vResources.clear();
  86. };
  87. };
  88. void argusHardwareManager::loadResources( void ){
  89. if( m_pDBC == NULL ) return;
  90. DBC_INT_LIST *tVal;
  91. //tVal = m_pDBC->listResources( );//load whole site resources
  92. tVal = m_pDBC->listResources( m_nServerNodeID );//Load Only Server Node resources
  93. if( tVal!= NULL ) {
  94. for( DBC_INT_LIST::iterator index = tVal->begin(); index < tVal->end(); index++ ) {
  95. DBC_RESOURCE_DATA *tData = m_pDBC->getResourceData( *index );
  96. if( tData != NULL ) {
  97. argusHardwareResource *tPtr = NULL;
  98. switch( tData->type ){
  99. case ARGUS_HARDWARE_UART:{
  100. #ifdef DEBUG
  101. LOG("Construct HW UART");
  102. #endif
  103. argusHardwareResourceSerialProvider *tRes = new argusHardwareResourceSerialProvider( tData->id );
  104. if( tRes != NULL ) {
  105. if( tRes->create() ) {
  106. tPtr = ( argusHardwareResource *) tRes;
  107. } else {
  108. LOG_ERROR("Error Creating HW UART!");
  109. delete( tRes );
  110. };
  111. };
  112. break;
  113. };
  114. case ARGUS_HARDWARE_NIF_LOOPBACK:
  115. case ARGUS_HARDWARE_NIF_ETHERNET:
  116. case ARGUS_HARDWARE_NIF_WIFI:
  117. case ARGUS_HARDWARE_NIF_VIRTUAL:
  118. case ARGUS_HARDWARE_NIF_TUNEL:{
  119. #ifdef DEBUG
  120. LOG("Construct NetIF");
  121. #endif
  122. argusHardwareResourceNetwork *tRes = new argusHardwareResourceNetwork( tData->id );
  123. if( tRes != NULL ) {
  124. if( tRes->create() ) {
  125. tPtr = ( argusHardwareResource *) tRes;
  126. } else {
  127. LOG_ERROR("Error Creating Network Ressource!");
  128. delete( tRes );
  129. };
  130. };
  131. break;
  132. };
  133. case ARGUS_HARDWARE_IP_UART:{
  134. #ifdef DEBUG
  135. LOG("Construct IP UART");
  136. #endif
  137. argusHardwareResourceSerialProvider *tRes = new argusHardwareResourceSerialProvider( tData->id );
  138. if( tRes != NULL ) {
  139. if( tRes->create() ) {
  140. tPtr = ( argusHardwareResource *) tRes;
  141. } else {
  142. LOG_ERROR("Error Creating IP UART!");
  143. delete( tRes );
  144. };
  145. };
  146. break;
  147. };
  148. #ifdef DEBUG
  149. default:{
  150. char tB[128];
  151. sprintf( tB, "Unknown Ressource[%d] Type[%d]!", *index, tData->type );
  152. LOG_ERROR(tB);
  153. break;
  154. };
  155. #endif
  156. };
  157. if( tPtr!= NULL ) {
  158. #ifdef DEBUG
  159. LOG("Resource Sucessfully Created!");
  160. #endif
  161. m_vResources.push_back( tPtr );
  162. };
  163. } else {
  164. LOG_ERROR("Can not fetch Resource Data from Database!");
  165. };
  166. };
  167. tVal->clear();
  168. delete( tVal );
  169. };
  170. };
  171. bool argusHardwareManager::validate( void ){
  172. bool RetVal = true;
  173. #ifdef DEBUG_EXTRA
  174. LOG("Validator");
  175. #endif
  176. //if( !m_bHardwareEnumerated ) return;// RetVal;
  177. //enumerateHardware( m_bHardwareEnumerated );
  178. if( m_vResources.size() ) {
  179. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  180. if( !(*idx)->validate() ) {
  181. // recreate Resource
  182. LOG_ERROR( "HW Resource Invalid!" );
  183. if( !(*idx)->create()){
  184. LOG_ERROR( "HW Resource Creation Failure!" );
  185. } else LOG("Re-Created OK");
  186. }
  187. #ifdef DEBUG_EXTRA
  188. else LOG("Resource OK!")
  189. #endif
  190. ;
  191. };
  192. }
  193. #ifdef DEBUG_EXTRA
  194. else LOG("No Resources!")
  195. #endif
  196. ;
  197. //Mem Validator
  198. if( m_nMemoryLimitHeapSize ) {
  199. size_t memStat = getCurrentRSS();
  200. #ifdef DEBUG_EXTRA
  201. char MEM_MSG[128];
  202. sprintf( MEM_MSG, "MEM: %d TARG: %d", memStat, m_nMemoryLimitHeapSize );
  203. LOG( MEM_MSG );
  204. #endif
  205. //https://www.ebay.com/itm/274974088159?
  206. RetVal = ( memStat < m_nMemoryLimitHeapSize );
  207. };
  208. return RetVal;
  209. };
  210. int xioctl(int fh, int request, void *arg){
  211. int r;
  212. do {
  213. r = ioctl( fh, request, arg );
  214. } while (-1 == r && EINTR == errno);
  215. return r;
  216. };
  217. #define CLEAR(x) memset(&(x), 0, sizeof(x))
  218. #include <sys/types.h>
  219. #include <ifaddrs.h>
  220. void argusHardwareManager::getMAC( CSTR if_name, CSTR buf ){
  221. char tB[128];
  222. FILE * fd=NULL;
  223. if ( buf ) {
  224. sprintf(tB, "/sys/class/net/%s/address", if_name);
  225. #ifdef DEBUG
  226. LOG( tB );
  227. #endif
  228. fd=fopen(tB,"r");
  229. if ( fd ) {
  230. fread( buf, 1, INET6_ADDRSTRLEN, fd );
  231. fclose( fd );
  232. buf = trim( buf );
  233. } else {
  234. memset( buf, 0, INET6_ADDRSTRLEN );
  235. };
  236. #ifdef DEBUG
  237. LOG( buf );
  238. #endif
  239. };
  240. };
  241. void argusHardwareManager::getGW( CSTR if_name, CSTR buf ){
  242. FILE * fd = popen( "route", "r" );
  243. char tB[1024];
  244. //char expBuf[1024];
  245. memset( buf, 0, INET6_ADDRSTRLEN );
  246. if ( fd ) {
  247. while (!feof(fd)) {
  248. fgets((CSTR)tB, 1024, fd);
  249. trim((CSTR)tB);
  250. if( strlen(tB) ) {
  251. if( strstr( tB, if_name ) != NULL ){
  252. //strncpy( expBuf, tB, 1024 );
  253. CArray *CA = explode( ' ', tB );
  254. if ( CA ) {
  255. //for (int i=0; i< CA->size(); i++ ) {
  256. // char tB[128];
  257. // trim( CA->at(i) );
  258. // sprintf( tB, "GW# %d: %s", i, CA->at(i) );
  259. // LOG( tB );
  260. //};
  261. trim( CA->at(1) );
  262. if( strstr(CA->at(1), "0.0.0.0") == NULL ) {
  263. strncpy( buf, CA->at(1), INET6_ADDRSTRLEN );
  264. };
  265. delete(CA);
  266. };
  267. };
  268. };
  269. };
  270. fclose( fd );
  271. };
  272. if( strlen( buf ) == 0 ) strncpy( buf, "0.0.0.0", INET6_ADDRSTRLEN );
  273. };
  274. void argusHardwareManager::getTTyDesc( CSTR dev_name, CSTR buf ){
  275. FILE * fd = popen( "dmesg | grep tty", "r" );
  276. char tB[1024];
  277. //char expBuf[1024];
  278. memset( buf, 0, DBC_RESOURCE_DESC_SIZE );
  279. if ( fd ) {
  280. while (!feof(fd)) {
  281. fgets((CSTR)tB, 1024, fd);
  282. trim((CSTR)tB);
  283. if( strlen(tB) ) {
  284. if( strstr( tB, dev_name ) != NULL ){
  285. //strncpy( expBuf, tB, 1024 );
  286. CArray *CA = explode( ':', tB );
  287. if ( CA ) {
  288. //for (int i=0; i< CA->size(); i++ ) {
  289. // char tB[128];
  290. // trim( CA->at(i) );
  291. // sprintf( tB, "GW# %d: %s", i, CA->at(i) );
  292. // LOG( tB );
  293. //};
  294. trim( CA->at(1) );
  295. strncpy( buf, CA->at(1), DBC_RESOURCE_DESC_SIZE );
  296. buf[DBC_RESOURCE_DESC_SIZE-1]=(char)0;
  297. delete(CA);
  298. };
  299. };
  300. };
  301. };
  302. fclose( fd );
  303. };
  304. if( strlen( buf ) == 0 ){
  305. strncpy( buf, "--Unknown--", DBC_RESOURCE_DESC_SIZE );
  306. buf[DBC_RESOURCE_DESC_SIZE-1]=(char)0;
  307. };
  308. };
  309. PARSE_RESULT_NET_INTERFACES *argusHardwareManager::parse_neconfig( CSTR if_name ){
  310. PARSE_RESULT_NET_INTERFACES *tVal = NULL;
  311. FILE *fd;
  312. bool cConfig = false;
  313. fd = fopen("/etc/network/interfaces","r");
  314. if ( fd ) {
  315. char tB[1024];
  316. char expBuf[1024];
  317. tVal = (PARSE_RESULT_NET_INTERFACES *) malloc( sizeof(PARSE_RESULT_NET_INTERFACES));
  318. if ( tVal ) {
  319. memset(tVal, 0 , sizeof(PARSE_RESULT_NET_INTERFACES));
  320. while (!feof(fd)) {
  321. fgets((CSTR)tB, 1024, fd);
  322. trim((CSTR)tB);
  323. if( strlen(tB) ) {
  324. strncpy( expBuf, tB, 1024 );
  325. CArray *CA = explode( ' ', expBuf );
  326. if( CA ) {
  327. for( CArray::iterator idx = CA->begin(); idx < CA->end(); idx++ ) {
  328. if((*idx)[0]=='#') break;
  329. if(strstr((*idx), "auto")!= NULL ) if( strstr( tB, if_name )!= NULL ) tVal->enabled = true;
  330. if(strstr((*idx), "allow-hotplug")!= NULL ) if( strstr( tB, if_name )!= NULL ) tVal->hot_plug = true;
  331. if(strstr((*idx), "iface")!= NULL ) if( strstr( tB, if_name )!= NULL ){
  332. if (strstr( tB, "static") != NULL) tVal->if_static = true;
  333. cConfig = true;
  334. } else cConfig = false;
  335. if(strstr((*idx), "address")!= NULL ) if( cConfig ){
  336. idx++;
  337. if(strstr((*idx),"/")!= NULL ){
  338. int8_t sh = strstr((*idx),"/") - (*idx);
  339. strncpy( tVal->address, (*idx), sh );
  340. tVal->address[sh]=(char)0;
  341. int8_t bm = atoi( (*idx) + sh + 1 );
  342. union{
  343. uint32_t mm;
  344. uint8_t nm[4];
  345. } nmu = {0};
  346. for ( sh = 0; sh < bm; sh++ ) nmu.mm |= (1 << sh);
  347. sprintf(tVal->netmask, "%d.%d.%d.%d", nmu.nm[0], nmu.nm[1], nmu.nm[2], nmu.nm[3]);
  348. } else {
  349. strncpy( tVal->address, (*idx), INET6_ADDRSTRLEN );
  350. };
  351. };
  352. if(strstr((*idx), "netmask")!= NULL ) if( cConfig ){
  353. LOG("netMSK");
  354. idx++;
  355. LOG(*idx);
  356. strncpy(tVal->netmask, (*idx), INET6_ADDRSTRLEN);
  357. };
  358. //if(strstr((*idx), "bridge_ports")!= NULL ) cMode = 6;
  359. //if(strstr((*idx), "bridge_stp")!= NULL ) cMode = 7;
  360. //if(strstr((*idx), "bridge_waitport")!= NULL ) cMode = 8;
  361. //if(strstr((*idx), "bridge_fd")!= NULL ) cMode = 9;
  362. };
  363. delete( CA );
  364. };
  365. };
  366. };
  367. };
  368. fclose( fd );
  369. };
  370. #ifdef DEBUG
  371. if ( tVal ) {
  372. if( tVal->enabled ) LOG("NIF Enabled"); else LOG("NIF Disabled");
  373. if (tVal->hot_plug) LOG("NIF Hot Plugable"); else LOG("NIF not Hot Plugable");
  374. if (tVal->if_static) LOG("Address is static"); else LOG("Address is Dynamic");
  375. if(strlen(tVal->address))LOG(tVal->address);
  376. if(strlen(tVal->netmask))LOG(tVal->netmask);
  377. };
  378. #endif
  379. return tVal;
  380. };
  381. PARSE_RESULT_WIFI_SETTINGS *argusHardwareManager::parse_wifi( CSTR if_name ){
  382. PARSE_RESULT_WIFI_SETTINGS *tVal = NULL;
  383. FILE *fd;
  384. bool cConfig = false;
  385. tVal = (PARSE_RESULT_WIFI_SETTINGS *) malloc( sizeof(PARSE_RESULT_WIFI_SETTINGS));
  386. if ( tVal ) {
  387. memset(tVal, 0 , sizeof(PARSE_RESULT_WIFI_SETTINGS));
  388. fd = fopen("/etc/dnsmasq.conf","r");
  389. if ( fd ) {
  390. char tB[1024];
  391. char expBuf[1024];
  392. while (!feof(fd)) {
  393. fgets((CSTR)tB, 1024, fd);
  394. trim((CSTR)tB);
  395. if( strlen(tB) ) {
  396. strncpy( expBuf, tB, 1024 );
  397. CArray *CA = explode( '=', expBuf );
  398. if( CA ) {
  399. if( CA->size() ){
  400. if( CA->at(0)[0] == '#' ) continue;
  401. if( strstr(CA->at(0), "interface")!= NULL ) {
  402. if( strstr( tB, if_name)!= NULL ) {
  403. cConfig = true;
  404. tVal->dhcp_enabled = true;
  405. };
  406. };
  407. if( cConfig ){
  408. if ( strstr(CA->at(0), "dhcp-range")!= NULL ) {
  409. char tT[1024];
  410. strncpy( tT, CA->at(1), 1024 );
  411. CArray *CB = explode(',', tT );
  412. if( CB ){
  413. if( CB->size() > 3 ){
  414. strncpy( tVal->range_start, CB->at(0), INET6_ADDRSTRLEN);
  415. strncpy( tVal->range_stop, CB->at(1), INET6_ADDRSTRLEN);
  416. strncpy( tVal->netmask, CB->at(2), INET6_ADDRSTRLEN);
  417. strncpy( tVal->lease_period, CB->at(3), INET6_ADDRSTRLEN);
  418. };
  419. delete( CB );
  420. };
  421. };
  422. };
  423. };
  424. delete( CA );
  425. };
  426. };
  427. };
  428. fclose( fd );
  429. };
  430. //Check WiFi
  431. if( strstr(if_name,"w")!= NULL ) {
  432. char tName[128];
  433. sprintf( tName, "iwconfig %s", if_name );
  434. fd = popen( tName, "r" );
  435. if ( fd ) {
  436. char tB[1024];
  437. while (!feof(fd)) {
  438. fgets((CSTR)tB, 1024, fd );
  439. trim( tB );
  440. if (strstr(tB, "Mode") != NULL) if (strstr(tB, "Master") != NULL) tVal->access_point = true;
  441. };
  442. fclose( fd );
  443. };
  444. };
  445. if ( tVal->access_point ) {
  446. fd = fopen("/etc/hostapd/hostapd.conf", "r");
  447. if ( fd ) {
  448. char tB[1024];
  449. char expBuf[1024];
  450. while (!feof(fd)) {
  451. fgets((CSTR)tB, 1024, fd);
  452. trim( tB );
  453. if( strlen(tB) ) {
  454. if( tB[0]=='#' ) continue;
  455. strncpy(expBuf, tB, 1024);
  456. CArray *CA = explode( '=', expBuf );
  457. if( CA ) {
  458. if( CA->size() ){
  459. if (strstr(CA->at(0), "ssid") != NULL) if (strstr(CA->at(0), "ignore_broadcast_ssid") == NULL){
  460. strncpy( tVal->SSID, CA->at(1), 64 );
  461. } else tVal->hiden_ssid = (bool) atoi(CA->at(1));
  462. if (strstr(CA->at(0), "wpa_passphrase") != NULL) strncpy( tVal->password, CA->at(1), 64 );
  463. if (strstr(CA->at(0), "country_code") != NULL) strncpy(tVal->country_code, CA->at(1), 3);
  464. if (strstr(CA->at(0), "channel") != NULL) tVal->channel = atoi(CA->at(1));
  465. };
  466. delete( CA );
  467. };
  468. };
  469. };
  470. fclose( fd );
  471. };
  472. } else {
  473. fd = fopen("/etc/wpa_supplicant/wpa_supplicant.conf", "r");
  474. if ( fd ) {
  475. fclose( fd );
  476. };
  477. };
  478. fd = fopen("/etc/rc.local","r");
  479. if ( fd ) {
  480. char tB[1024];
  481. char expBuf[1024];
  482. while (!feof(fd)) {
  483. fgets((CSTR)tB, 1024, fd);
  484. trim((CSTR)tB);
  485. if( strlen(tB) ) {
  486. strncpy( expBuf, tB, 1024 );
  487. CArray *CA = explode( ' ', expBuf );
  488. if( CA ) {
  489. if ( CA->size() ) {
  490. if (CA->at(0)[0] == '#') continue;
  491. if( strstr(CA->at(0), "iptables-restore")!= NULL ) {
  492. char zB[1024];
  493. FILE *afd = fopen( CA->at(2), "r" );
  494. if ( afd ) {
  495. while( !feof( afd ) ){
  496. fgets((CSTR)zB, 1024, afd );
  497. if( strstr( zB, "POSTROUTING" )!= NULL ) if( strstr( zB, "MASQUERADE" )!= NULL ) if( strstr( zB, if_name )== NULL ){
  498. CArray *CB = explode( ' ', zB );
  499. if ( CB ) {
  500. if ( CB->size() > 3 ) {
  501. strncpy(tVal->router_outbond, CB->at(3), INET6_ADDRSTRLEN);
  502. };
  503. delete( CB );
  504. };
  505. tVal->router_enabled = true;
  506. };
  507. };
  508. fclose(afd);
  509. } else LOG("Error Opening Firewall Rules!");
  510. };
  511. };
  512. delete( CA );
  513. };
  514. };
  515. };
  516. fclose(fd);
  517. } else LOG_ERROR("Can not open rc.local!");
  518. };
  519. #ifdef DEBUG
  520. if ( tVal ) {
  521. if (tVal->access_point) LOG("AP Mode"); else LOG("WiFi Mode");
  522. LOG(tVal->SSID);
  523. if (tVal->hiden_ssid) LOG("SSID is hidden");
  524. LOG(tVal->password);
  525. if (tVal->dhcp_enabled){
  526. LOG("DHCP Service");
  527. LOG(tVal->range_start);
  528. LOG(tVal->range_stop);
  529. LOG(tVal->lease_period);
  530. } else LOG("DHCP Disabled");
  531. if (tVal->router_enabled){
  532. LOG("Routing Enabled");
  533. LOG( tVal->router_outbond );
  534. } else LOG("Routing Disabled");
  535. };
  536. #endif
  537. return tVal;
  538. };
  539. void argusHardwareManager::enumerateNIFS( void ){
  540. struct ifaddrs * ifaddr = NULL;
  541. char tBuffer[128];
  542. if( m_pKernel == NULL ) return;
  543. if( m_pDBC == NULL ) return;
  544. if( -1 == getifaddrs(&ifaddr)) {
  545. LOG_ERROR("No NET INTERFACES?!");
  546. } else {
  547. #ifdef DEBUG
  548. LOG("Enumerating Network Interfaces");
  549. #endif
  550. struct ifaddrs * ifa;
  551. for( ifa = ifaddr; ifa; ifa = ifa->ifa_next ){
  552. char addr[INET6_ADDRSTRLEN];
  553. char mask[INET6_ADDRSTRLEN];
  554. char gw[INET6_ADDRSTRLEN];
  555. char bc[INET6_ADDRSTRLEN];
  556. char mac[INET6_ADDRSTRLEN];
  557. char device[INET6_ADDRSTRLEN];
  558. uint32_t nif_id = 0;
  559. uint32_t nif_aux_id = 0;
  560. uint32_t router_id = 0;
  561. ARGUS_NIF_TYPE nif_type = NIF_LOOPBACK;
  562. ARGUS_NETWORK_TYPE net_type = NETWORK_IPV4;
  563. ARGUS_RESOURCE_TYPE res_type = ARGUS_HARDWARE_NIF_LOOPBACK;
  564. ARGUS_NIF_MODE nif_mode = ADDRESS_DISABLED;
  565. #ifdef DEBUG_EXTRA
  566. LOG("NIF ***");
  567. #endif
  568. strncpy( device, ifa->ifa_name, INET6_ADDRSTRLEN );
  569. strncpy( mac,"--no-mac--", INET6_ADDRSTRLEN );
  570. // Filter Loopback if
  571. if( strstr( device, "lo" ) != NULL ){
  572. nif_type = NIF_LOOPBACK;
  573. res_type = ARGUS_HARDWARE_NIF_LOOPBACK;
  574. //Not Implemented
  575. continue;
  576. };
  577. // Filter vpn TUN if
  578. if( strstr( device, "tun" ) != NULL ){
  579. nif_type = NIF_VPN_TUNEL;
  580. res_type = ARGUS_HARDWARE_NIF_TUNEL;
  581. //Not Implemented Yet
  582. continue;
  583. };
  584. if( strstr( device, "eth" ) != NULL ){
  585. LOG(device);
  586. nif_type = NIF_ETHERNET;
  587. res_type = ARGUS_HARDWARE_NIF_ETHERNET;
  588. };
  589. if( strstr( device, "wlan" ) != NULL ){
  590. LOG(device);
  591. nif_type = NIF_WIFI;
  592. res_type = ARGUS_HARDWARE_NIF_WIFI;
  593. };
  594. if( strstr( device, "wwan" ) != NULL ){
  595. LOG(device);
  596. nif_type = NIF_WAN;
  597. res_type = ARGUS_HARDWARE_NIF_WAN;
  598. };
  599. if( ifa->ifa_addr != NULL ){
  600. if (ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) {
  601. LOG("CONT");
  602. continue;
  603. }
  604. } else {
  605. LOG("ifa->ifa_addr is NULL");
  606. continue;
  607. };
  608. LOG("GETMAC");
  609. getMAC( device, mac );
  610. getGW( device, gw );
  611. LOG( mac );
  612. LOG( gw );
  613. PARSE_RESULT_NET_INTERFACES * tt = NULL;
  614. PARSE_RESULT_WIFI_SETTINGS * tw = NULL;
  615. tt = parse_neconfig( device );
  616. tw = argusHardwareManager::parse_wifi( device );
  617. if( tt ) {
  618. if ( tt->enabled ) {
  619. if( tt->if_static ){
  620. nif_mode = ADDRESS_STATIC;
  621. } else {
  622. nif_mode = ADDRESS_DYNAMIC;
  623. };
  624. } else {
  625. nif_mode = ADDRESS_DISABLED;
  626. };
  627. } else {
  628. LOG_ERROR("Error Parsing System NIF Settings!");
  629. continue;
  630. };
  631. if ( tw ){
  632. if( nif_mode == ADDRESS_STATIC ) {
  633. if (tw->dhcp_enabled) {
  634. nif_mode = ADDRESS_STATIC_RAP;
  635. };
  636. };
  637. if ( nif_type == NIF_WIFI ){
  638. nif_id = m_pDBC->addLocalNIF( device, mac, nif_type, nif_mode );
  639. if( nif_id == 0 ) continue;
  640. ARGUS_WIFI_MODE w_mode;
  641. if( !tt->enabled ) {
  642. w_mode = WIFI_DISABLED;
  643. } else {
  644. if (tt->if_static) {
  645. if( tw->access_point ) {
  646. w_mode = WIFI_IS_AP;
  647. uint32_t outbond_id = 0;
  648. if( tw->router_enabled ){
  649. outbond_id = m_pDBC->getNIF_IDbyDevice( tw->router_outbond );
  650. };
  651. if( tw->dhcp_enabled ) {
  652. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period );
  653. } else {
  654. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id );
  655. };
  656. } else {
  657. w_mode = WIFI_CONNECTED_TO_AP;
  658. };
  659. };
  660. };
  661. nif_aux_id = m_pDBC->addLocalWiFi(nif_id, w_mode, tw->SSID, tw->password, ENCRIPTION_WPA_PSK, router_id );
  662. };
  663. if( nif_type == NIF_ETHERNET || nif_type == NIF_ETHERNET_VIRTUAL ){
  664. };
  665. free(tt);
  666. free(tw);
  667. } else {
  668. LOG_ERROR("Error Parsing System Router/AP Settings!");
  669. free(tt);
  670. continue;
  671. };
  672. nif_id = m_pDBC->addLocalNIF( device, mac, nif_type, nif_mode, nif_aux_id );
  673. if( nif_id == 0 ) continue;
  674. LOG("NIF added!");
  675. m_pDBC->addResource( m_nServerNodeID, res_type, device, nif_id );
  676. if(ifa->ifa_addr->sa_family == AF_INET){
  677. net_type = NETWORK_IPV4;
  678. // create IPv4 string
  679. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  680. inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
  681. sprintf( mask, "%d.%d.%d.%d",
  682. (uint8_t)ifa->ifa_netmask->sa_data[2],
  683. (uint8_t)ifa->ifa_netmask->sa_data[3],
  684. (uint8_t)ifa->ifa_netmask->sa_data[4],
  685. (uint8_t)ifa->ifa_netmask->sa_data[5]
  686. );
  687. sprintf( bc, "%d.%d.%d.%d",
  688. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  689. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  690. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  691. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  692. );
  693. } else { // AF_INET6
  694. net_type = NETWORK_IPV6;
  695. // create IPv6 string
  696. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  697. inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
  698. sprintf( mask, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  699. (uint8_t)ifa->ifa_netmask->sa_data[2],
  700. (uint8_t)ifa->ifa_netmask->sa_data[3],
  701. (uint8_t)ifa->ifa_netmask->sa_data[4],
  702. (uint8_t)ifa->ifa_netmask->sa_data[5],
  703. (uint8_t)ifa->ifa_netmask->sa_data[6],
  704. (uint8_t)ifa->ifa_netmask->sa_data[7],
  705. (uint8_t)ifa->ifa_netmask->sa_data[8],
  706. (uint8_t)ifa->ifa_netmask->sa_data[9],
  707. (uint8_t)ifa->ifa_netmask->sa_data[10],
  708. (uint8_t)ifa->ifa_netmask->sa_data[11],
  709. (uint8_t)ifa->ifa_netmask->sa_data[12],
  710. (uint8_t)ifa->ifa_netmask->sa_data[13],
  711. (uint8_t)ifa->ifa_netmask->sa_data[14],
  712. (uint8_t)ifa->ifa_netmask->sa_data[15],
  713. (uint8_t)ifa->ifa_netmask->sa_data[16],
  714. (uint8_t)ifa->ifa_netmask->sa_data[17]
  715. );
  716. if( ifa->ifa_ifu.ifu_broadaddr != NULL ){
  717. sprintf( bc, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  718. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  719. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  720. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  721. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5],
  722. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[6],
  723. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[7],
  724. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[8],
  725. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[9],
  726. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[10],
  727. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[11],
  728. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[12],
  729. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[13],
  730. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[14],
  731. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[15],
  732. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[16],
  733. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[17]
  734. );
  735. } else {
  736. sprintf( bc, "" );
  737. };
  738. };
  739. m_pDBC->addLocalNetwork( nif_id, net_type, addr, mask, gw, bc );
  740. #ifdef DEBUG
  741. sprintf( tBuffer, "Detected Network Interface: %s [%s]", ifa->ifa_name, addr );
  742. LOG(tBuffer);
  743. #endif
  744. };
  745. freeifaddrs( ifaddr );
  746. };
  747. };
  748. void argusHardwareManager::enumerateHardware( void ){
  749. char tBuffer[CTEXT];
  750. DIR * dFD = NULL;
  751. struct dirent * pDR = NULL;
  752. //ToDo: Check Ability to work without Kernel and DBC
  753. if( m_pKernel == NULL ) return;
  754. if( m_pDBC == NULL ) return;
  755. #ifdef DEBUG
  756. LOG("HW Enumeration Started!");
  757. #endif
  758. // Moved to a constructor!!
  759. //m_nServerNodeID = m_pKernel->getServerNodeId();
  760. //m_nServerID = m_pKernel->getServerId();
  761. //m_nSiteID = m_pKernel->getSiteId();
  762. /* Add Network Interfaces */
  763. m_pDBC->cleanResources( m_nServerNodeID, true );
  764. enumerateNIFS();
  765. /* Check HID Devices */
  766. struct hid_device_info * pDevs;
  767. uint8_t DCBCount;
  768. //Enumerate USB devices with the given IDs
  769. pDevs = hid_enumerate( 0x16c0, 0x05df );
  770. if( pDevs!=NULL ){
  771. struct hid_device_info * pCDev = pDevs;
  772. //Count the number of returned devices
  773. for( DCBCount = 0; pCDev!=NULL; DCBCount++ ) pCDev = pCDev->next;
  774. //Allocate a buffer for the relays
  775. if( DCBCount > 0 ){
  776. DC_BOARD tDCB;
  777. //Fill the relay structs
  778. pCDev = pDevs;
  779. for( int idx = 0; idx < DCBCount; idx++ ){
  780. //Save the path to this device
  781. memset( tDCB.path, 0, MAX_PATH_LEN );
  782. strncpy( tDCB.path, pCDev->path, MAX_PATH_LEN );
  783. // The product string is USBRelayx where x is number of relays read to the \0 in case there are more than 9
  784. tDCB.relay_count = atoi((const char *)&pCDev->product_string[8]);
  785. //Open it to get more details
  786. hid_device * handle;
  787. handle = hid_open_path( tDCB.path );
  788. if( handle ){
  789. unsigned char buf[9];
  790. //Get the features of the device
  791. buf[0] = 0x01;
  792. if( hid_get_feature_report( handle, buf, sizeof(buf)) == -1 ){
  793. LOG_ERROR("hid_get_feature_report");
  794. pCDev = pCDev->next;
  795. hid_close( handle );
  796. continue;
  797. };
  798. //Set the serial number (0x0 for termination)
  799. memset( tDCB.serial, 0x0, (HID_DCR_SERIIAL_LENGTH+1) );
  800. memcpy( tDCB.serial, buf, HID_DCR_SERIIAL_LENGTH );
  801. //Byte 7 in the response contains the target_state of the relays
  802. tDCB.state = buf[7];
  803. hid_close( handle );
  804. #ifdef DEBUG
  805. //Output the device enumeration details if verbose is on
  806. LOG("HID DCB Found");
  807. sprintf(tBuffer, " Type: %04hx::%04hx", pCDev->vendor_id, pCDev->product_id);
  808. LOG(tBuffer);
  809. sprintf(tBuffer, " Path: %s", pCDev->path );
  810. LOG(tBuffer);
  811. sprintf(tBuffer, " Serial_number: %ls", pCDev->serial_number );
  812. LOG(tBuffer);
  813. sprintf(tBuffer, " Manufacturer: %ls", pCDev->manufacturer_string);
  814. LOG(tBuffer);
  815. sprintf(tBuffer, " Product: %ls", pCDev->product_string);
  816. LOG(tBuffer);
  817. sprintf(tBuffer, " Release: %hx", pCDev->release_number);
  818. LOG(tBuffer);
  819. sprintf(tBuffer, " Interface: %d", pCDev->interface_number);
  820. LOG(tBuffer);
  821. sprintf(tBuffer, " Number of Relays = %d", tDCB.relay_count);
  822. LOG(tBuffer);
  823. //If debug are on, output individual relay details
  824. for( int k = 0; k < tDCB.relay_count; k++ ){
  825. if( tDCB.state & 1 << k ){
  826. sprintf(tBuffer, " %s_%d=1", tDCB.serial, k + 1);
  827. } else {
  828. sprintf(tBuffer, " %s_%d=0", tDCB.serial, k + 1);
  829. };
  830. LOG(tBuffer);
  831. };
  832. #endif
  833. m_pDBC->addLocalDCB( m_nSiteID, pCDev->path, tDCB.serial, tDCB.relay_count, tDCB.state );
  834. } else {
  835. LOG_ERROR("unable to open HID device");
  836. };
  837. pCDev = pCDev->next;
  838. };
  839. };
  840. hid_free_enumeration( pDevs );
  841. };
  842. /* Add devices from DEV */
  843. dFD = opendir("/dev");
  844. if( dFD != NULL ) {
  845. while( ( pDR = readdir( dFD ))!= NULL ){
  846. if( strcasestr( pDR->d_name, "video" ) == pDR->d_name ) {
  847. createResourceVIDEO( pDR->d_name );
  848. continue;
  849. };
  850. if( strcasestr( pDR->d_name, "ttyS" ) == pDR->d_name ){
  851. createResourceUART( pDR->d_name );
  852. continue;
  853. };
  854. // RaspberyPI Hardware
  855. if( strcasestr( pDR->d_name, "ttyAMA" ) == pDR->d_name ){
  856. createResourceUART( pDR->d_name );
  857. continue;
  858. };
  859. if( strcasestr( pDR->d_name, "ttyUSB" ) == pDR->d_name ){
  860. createResourceUART( pDR->d_name );
  861. continue;
  862. };
  863. };
  864. closedir( dFD );
  865. };
  866. //m_bHardwareEnumerated = true;
  867. };
  868. uint32_t argusHardwareManager::createResourceUART( CSTR devName ){
  869. uint32_t RetVal = 0;
  870. char tBuffer[256];
  871. /* Enumerate BuiltIn Serial */
  872. #ifdef DEBUG_EXTRA
  873. sprintf( tBuffer, "Serial Device: %s", devName );
  874. LOG( tBuffer );
  875. #endif
  876. char dev_name[CTEXT];
  877. int fd = -1;
  878. struct serial_struct ss;
  879. sprintf( dev_name, "/dev/%s", devName );
  880. fd = open( dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK );
  881. if (-1 == fd) {
  882. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  883. LOG_ERROR( tBuffer );
  884. } else {
  885. if ( 0 && ( -1 == xioctl(fd, TIOCGSERIAL, &ss) )){
  886. LOG_ERROR("TIOCGSERIAL");
  887. } else {
  888. #ifdef DEBUG_EXTRA
  889. sprintf( tBuffer, "Serial Port[Port:0x%X IRQ:%d BB:%d Line:%d Type:%d Flags:%X]",
  890. ss.port,
  891. ss.irq,
  892. ss.baud_base,
  893. ss.line,
  894. ss.type,
  895. ss.flags
  896. );
  897. LOG( tBuffer );
  898. #endif
  899. getTTyDesc( devName, tBuffer );
  900. uint32_t tID = m_pDBC->addLocalUART( m_nServerNodeID, 1, 115200, 8, 'N', 1.0f, dev_name );
  901. if ( tID ) {
  902. m_pDBC->addResource( m_nServerNodeID, ARGUS_HARDWARE_UART, tBuffer, tID );
  903. };
  904. };
  905. close(fd);
  906. };
  907. return RetVal;
  908. };
  909. uint32_t argusHardwareManager::createResourceVIDEO( CSTR devName ){
  910. uint32_t RetVal = 0;
  911. char tBuffer[256];
  912. /* Enumerate Video Capture */
  913. #ifdef DEBUG_EXTRA
  914. sprintf( tBuffer, "Video Source: %s", devName );
  915. LOG( tBuffer );
  916. #endif
  917. struct v4l2_capability cap;
  918. //struct v4l2_cropcap cropcap;
  919. //struct v4l2_crop crop;
  920. struct v4l2_format fmt;
  921. char dev_name[CTEXT];
  922. int fd = -1;
  923. struct stat st;
  924. sprintf( dev_name, "/dev/%s", devName );
  925. if (-1 == stat( dev_name, &st )) {
  926. sprintf( tBuffer, "Cannot identify '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  927. LOG_ERROR( tBuffer );
  928. return 0;
  929. };
  930. if (!S_ISCHR(st.st_mode)) {
  931. sprintf( tBuffer, "%s is no devicen", dev_name);
  932. LOG_ERROR( tBuffer );
  933. return 0;
  934. };
  935. fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
  936. if (-1 == fd) {
  937. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  938. LOG_ERROR( tBuffer );
  939. } else {
  940. if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
  941. if (EINVAL == errno) {
  942. sprintf( tBuffer, "%s is no V4L2 device", dev_name );
  943. LOG_ERROR( tBuffer );
  944. } else {
  945. LOG_ERROR( "VIDIOC_QUERYCAP" );
  946. };
  947. close(fd);
  948. return 0;
  949. };
  950. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  951. #ifdef DEBUG_EXTRA
  952. sprintf(tBuffer, "%s is no video capture device", dev_name);
  953. LOG_ERROR( tBuffer );
  954. #endif
  955. close(fd);
  956. return 0;
  957. };
  958. CLEAR(fmt);
  959. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  960. /* Preserve original settings as set by v4l2-ctl for example */
  961. if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt)){
  962. //LOG_ERROR("VIDIOC_G_FMT");
  963. } else {
  964. #ifdef DEBUG_EXTRA
  965. sprintf( tBuffer, "Grabber[%dx%d]",fmt.fmt.pix.width,fmt.fmt.pix.height);
  966. LOG( tBuffer );
  967. #endif
  968. uint64_t mid = m_pDBC->addLocalMediaSource( m_nSiteID, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height );
  969. //m_pDBC->addNodeSocket( m_nServerNodeID, mid, ARGUS_NODE_SOCK_TYPE_ANALOG_VIDEO_GRABBER, ARGUS_NODE_SOCK_MODE_OUT, false, dev_name, tBuffer );
  970. RetVal = m_pDBC->addResource( m_nServerNodeID, ARGUS_HARDWARE_ANALOG_VIDEO_IN, dev_name, mid );
  971. };
  972. close(fd);
  973. };
  974. return RetVal;
  975. };
  976. uint32_t argusHardwareManager::createResourceUDCB( CSTR devName ){
  977. uint32_t RetVal = 0;
  978. char tBuffer[256];
  979. return RetVal;
  980. };
  981. argusHardwareResource *argusHardwareManager::requestResource( ARGUS_HARDWARE_PROVIDERS type, uint32_t id ){
  982. argusHardwareResource *RetVal = NULL;
  983. if( m_vResources.size() ) {
  984. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  985. if( ((*idx)->getProviderType() == type ) && ( (*idx)->getDbId() == id ) ) {
  986. RetVal = (*idx);
  987. break;
  988. };
  989. };
  990. };
  991. return RetVal;
  992. };
  993. DBC_INT_LIST *argusHardwareManager::requestResourcesByType( ARGUS_HARDWARE_PROVIDERS type ){
  994. DBC_INT_LIST *RetVal = NULL;
  995. #ifdef DEBUG_EXTRA
  996. char tBuffer[128];
  997. sprintf( tBuffer, "Requested Resource type: %d", type );
  998. LOG( tBuffer );
  999. #endif
  1000. if( m_vResources.size() ) {
  1001. #ifdef DEBUG_EXTRA
  1002. LOG("Have Some Resources!");
  1003. #endif
  1004. RetVal = new DBC_INT_LIST();
  1005. if( RetVal ) {
  1006. #ifdef DEBUG_EXTRA
  1007. LOG("RetVal Created!");
  1008. #endif
  1009. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  1010. #ifdef DEBUG_EXTRA
  1011. sprintf( tBuffer, "Have Resource type: %d", (*idx)->getHardwareType() );
  1012. LOG( tBuffer );
  1013. #endif
  1014. if( (*idx)->getProviderType() == type ) {
  1015. #ifdef DEBUG_EXTRA
  1016. LOG("Matched!");
  1017. #endif
  1018. RetVal->push_back((*idx)->getDbId());
  1019. };
  1020. };
  1021. } else {
  1022. LOG_ERROR("Cannot Create DBC_INT_LIST");
  1023. };
  1024. };
  1025. return RetVal;
  1026. };
  1027. #undef CLEAR
  1028. #undef LOG
  1029. #undef LOG_ERROR