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.

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