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.

588 lines
24 KiB

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
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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 <hidapi/hidapi.h>
  8. #define LOG( X ) m_pLog->Log( "HWM", this, X )
  9. #ifdef DEBUG
  10. #define LOG_ERROR( X ) {\
  11. char _tBfrErrMSG2w34[256];\
  12. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  13. m_pLog->Log( "HWM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  14. }
  15. #else
  16. #define LOG_ERROR( X ) m_pLog->Log( "HWM", this, _LOG_ERROR, X );
  17. #endif
  18. argusHardwareManager *argusHardwareManager::_instance = NULL;
  19. /**
  20. * Returns the single instance of the object.
  21. */
  22. argusHardwareManager* argusHardwareManager::getInstance() {
  23. // check if the instance has been created yet
  24. if( 0 == _instance ){
  25. // if not, then create it
  26. _instance = new argusHardwareManager;
  27. };
  28. // return the single instance
  29. return _instance;
  30. };
  31. argusHardwareManager::argusHardwareManager(){
  32. m_pLog = basicLogCollection::getInstance();
  33. m_bHardwareEnumerated = false;
  34. m_nServerNodeID = 0;
  35. m_nServerID = 0;
  36. m_nSiteID = 0;
  37. m_pKernel = argusKernel::getInstance();
  38. if( m_pKernel == NULL ){
  39. LOG_ERROR( "Failure to get Kernel Instance!" );
  40. } else {
  41. m_nServerNodeID = m_pKernel->getServerNodeId();
  42. m_nServerID = m_pKernel->getServerId();
  43. m_nSiteID = m_pKernel->getSiteId();
  44. };
  45. m_pDBC = argusDBC::getInstance();
  46. if( m_pDBC == NULL ) {
  47. LOG_ERROR( "Failure to get DBC Instance!" );
  48. } else {
  49. if( !m_bHardwareEnumerated ){
  50. enumerateHardware();
  51. m_bHardwareEnumerated = true;
  52. };
  53. loadResources();
  54. };
  55. };
  56. argusHardwareManager::~argusHardwareManager(){
  57. };
  58. void argusHardwareManager::unloadResources( void ){
  59. if( m_vResources.size() ) {
  60. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  61. switch( (*idx)->getHardwareType() ) {
  62. case ARGUS_HARDWARE_UART:
  63. case ARGUS_HARDWARE_IP_UART: delete( (argusHardwareResourceSerialProvider*)(*idx) ); break;
  64. case ARGUS_HARDWARE_NETWORK_IPV4:
  65. case ARGUS_HARDWARE_NETWORK_IPV6: delete( (argusHardwareResourceNetwork*)(*idx) ); break;
  66. };
  67. };
  68. m_vResources.clear();
  69. };
  70. };
  71. void argusHardwareManager::loadResources( void ){
  72. if( m_pDBC == NULL ) return;
  73. DBC_INT_LIST *tVal;
  74. tVal = m_pDBC->listResources();//load whole site resources
  75. if( tVal!= NULL ) {
  76. for( DBC_INT_LIST::iterator index = tVal->begin(); index < tVal->end(); index++ ) {
  77. DBC_RESOURCE_DATA *tData = m_pDBC->getResourceData( *index );
  78. if( tData != NULL ) {
  79. argusHardwareResource *tPtr = NULL;
  80. switch( tData->type ){
  81. case ARGUS_HARDWARE_UART:{
  82. argusHardwareResourceSerialProvider *tRes = new argusHardwareResourceSerialProvider( tData->id );
  83. if( tRes != NULL ) {
  84. if( tRes->create() ) {
  85. tPtr = ( argusHardwareResource *) tRes;
  86. } else {
  87. delete( tRes );
  88. };
  89. };
  90. break;
  91. };
  92. case ARGUS_HARDWARE_NETWORK_IPV4:
  93. case ARGUS_HARDWARE_NETWORK_IPV6:{
  94. argusHardwareResourceNetwork *tRes = new argusHardwareResourceNetwork( tData->id );
  95. if( tRes != NULL ) {
  96. if( tRes->create() ) {
  97. tPtr = ( argusHardwareResource *) tRes;
  98. } else {
  99. delete( tRes );
  100. };
  101. };
  102. break;
  103. };
  104. case ARGUS_HARDWARE_IP_UART:{
  105. argusHardwareResourceSerialProvider *tRes = new argusHardwareResourceSerialProvider( tData->id );
  106. if( tRes != NULL ) {
  107. //if( tRes->create() ) {
  108. tRes->create();
  109. tPtr = ( argusHardwareResource *) tRes;
  110. //} else {
  111. // delete( tRes );
  112. //};
  113. };
  114. break;
  115. };
  116. };
  117. if( tPtr!= NULL ) {
  118. #ifdef DEBUG
  119. LOG("ADD RESOURCE!");
  120. #endif
  121. m_vResources.push_back( tPtr );
  122. };
  123. } else {
  124. LOG_ERROR("Can not fetch Resource Data from Database!");
  125. };
  126. };
  127. tVal->clear();
  128. delete( tVal );
  129. };
  130. };
  131. void argusHardwareManager::validate( void ){
  132. #ifdef DEBUG_EXTRA
  133. LOG("Validator");
  134. #endif
  135. if( !m_bHardwareEnumerated ) return;
  136. if( m_vResources.size() ) {
  137. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  138. if( !(*idx)->validate() ) {
  139. // recreate Resource
  140. LOG_ERROR( "HW Resource Invalid!" );
  141. if( !(*idx)->create()){
  142. LOG_ERROR( "HW Resource Creation Failure!" );
  143. } else LOG("Re-Created OK");
  144. }
  145. #ifdef DEBUG_EXTRA
  146. else LOG("Resource OK!")
  147. #endif
  148. ;
  149. };
  150. }
  151. #ifdef DEBUG_EXTRA
  152. else LOG("No Resources!")
  153. #endif
  154. ;
  155. };
  156. int xioctl(int fh, int request, void *arg){
  157. int r;
  158. do {
  159. r = ioctl( fh, request, arg );
  160. } while (-1 == r && EINTR == errno);
  161. return r;
  162. };
  163. #define CLEAR(x) memset(&(x), 0, sizeof(x))
  164. #include <sys/types.h>
  165. #include <ifaddrs.h>
  166. void argusHardwareManager::enumerateHardware( void ){
  167. DIR * dFD;
  168. struct dirent * pDR = NULL;
  169. char tBuffer[CTEXT];
  170. struct ifaddrs * ifaddr = NULL;
  171. if( m_pKernel == NULL ) return;
  172. if( m_pDBC == NULL ) return;
  173. if( m_bHardwareEnumerated ) return;
  174. #ifdef DEBUG
  175. LOG("HW Enumeration Started!");
  176. #endif
  177. // Moved to a constructor!!
  178. //m_nServerNodeID = m_pKernel->getServerNodeId();
  179. //m_nServerID = m_pKernel->getServerId();
  180. //m_nSiteID = m_pKernel->getSiteId();
  181. /* Add Network Interfaces */
  182. m_pDBC->cleanResources( m_nSiteID, m_nServerNodeID, true );
  183. if( -1 == getifaddrs(&ifaddr)) {
  184. LOG_ERROR("No NET INTERFACES?!");
  185. } else {
  186. #ifdef DEBUG_EXTRA
  187. LOG("NETWORK!");
  188. #endif
  189. struct ifaddrs * ifa;
  190. char addr[INET6_ADDRSTRLEN];
  191. char mask[INET6_ADDRSTRLEN];
  192. char gw[INET6_ADDRSTRLEN];
  193. char bc[INET6_ADDRSTRLEN];
  194. for( ifa = ifaddr; ifa; ifa = ifa->ifa_next ){
  195. #ifdef DEBUG_EXTRA
  196. LOG("NIF ***");
  197. LOG( ifa->ifa_name );
  198. #endif
  199. // Filter Loopback if
  200. if( strstr( ifa->ifa_name, "lo" ) != NULL ) continue;
  201. // Filter vpn TUN if
  202. if( strstr( ifa->ifa_name, "tun" ) != NULL ) continue;
  203. if( ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6 ) continue;
  204. if( ifa->ifa_addr->sa_family == AF_INET ){
  205. // create IPv4 string
  206. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  207. inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
  208. sprintf( mask, "%d.%d.%d.%d",
  209. (uint8_t)ifa->ifa_netmask->sa_data[2],
  210. (uint8_t)ifa->ifa_netmask->sa_data[3],
  211. (uint8_t)ifa->ifa_netmask->sa_data[4],
  212. (uint8_t)ifa->ifa_netmask->sa_data[5]
  213. );
  214. sprintf( bc, "%d.%d.%d.%d",
  215. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  216. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  217. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  218. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  219. );
  220. sprintf( gw, "" );
  221. //sprintf( tBuffer, "NET4:%s", ifa->ifa_name );
  222. //m_pDBC->addNodeSocket( m_nServerNodeID, m_nServerID, ARGUS_NODE_SOCK_TYPE_NETWORK_IPV4, ARGUS_NODE_SOCK_MODE_INOUT, true, ifa->ifa_name, addr );
  223. uint32_t tID = m_pDBC->addLocalNetInterface( addr, mask, gw, bc );
  224. if( tID ) {
  225. m_pDBC->addResource( m_nSiteID, m_nServerNodeID, ARGUS_HARDWARE_NETWORK_IPV4, addr, tID );
  226. };
  227. } else { // AF_INET6
  228. // create IPv6 string
  229. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  230. inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
  231. sprintf( mask, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  232. (uint8_t)ifa->ifa_netmask->sa_data[2],
  233. (uint8_t)ifa->ifa_netmask->sa_data[3],
  234. (uint8_t)ifa->ifa_netmask->sa_data[4],
  235. (uint8_t)ifa->ifa_netmask->sa_data[5],
  236. (uint8_t)ifa->ifa_netmask->sa_data[6],
  237. (uint8_t)ifa->ifa_netmask->sa_data[7],
  238. (uint8_t)ifa->ifa_netmask->sa_data[8],
  239. (uint8_t)ifa->ifa_netmask->sa_data[9],
  240. (uint8_t)ifa->ifa_netmask->sa_data[10],
  241. (uint8_t)ifa->ifa_netmask->sa_data[11],
  242. (uint8_t)ifa->ifa_netmask->sa_data[12],
  243. (uint8_t)ifa->ifa_netmask->sa_data[13],
  244. (uint8_t)ifa->ifa_netmask->sa_data[14],
  245. (uint8_t)ifa->ifa_netmask->sa_data[15],
  246. (uint8_t)ifa->ifa_netmask->sa_data[16],
  247. (uint8_t)ifa->ifa_netmask->sa_data[17]
  248. );
  249. if( ifa->ifa_ifu.ifu_broadaddr != NULL ){
  250. sprintf( bc, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  251. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  252. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  253. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  254. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5],
  255. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[6],
  256. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[7],
  257. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[8],
  258. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[9],
  259. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[10],
  260. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[11],
  261. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[12],
  262. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[13],
  263. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[14],
  264. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[15],
  265. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[16],
  266. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[17]
  267. );
  268. } else {
  269. sprintf( bc, "" );
  270. };
  271. sprintf( gw, "" );
  272. //sprintf( tBuffer, "NET6:%s", ifa->ifa_name );
  273. //m_pDBC->addNodeSocket( m_nServerNodeID, m_nServerID, ARGUS_NODE_SOCK_TYPE_NETWORK_IPV6, ARGUS_NODE_SOCK_MODE_INOUT, true, ifa->ifa_name, addr );
  274. uint32_t tID = m_pDBC->addLocalNetInterface( addr, mask, gw, bc );
  275. if( tID ) {
  276. m_pDBC->addResource( m_nSiteID, m_nServerNodeID, ARGUS_HARDWARE_NETWORK_IPV6, addr, tID );
  277. };
  278. };
  279. #ifdef DEBUG
  280. sprintf( tBuffer, "Network Interface: %s [%s]", ifa->ifa_name, addr );
  281. LOG(tBuffer);
  282. #endif
  283. };
  284. freeifaddrs( ifaddr );
  285. };
  286. /* Check HID Devices */
  287. struct hid_device_info * pDevs;
  288. uint8_t DCBCount;
  289. //Enumerate USB devices with the given IDs
  290. pDevs = hid_enumerate( 0x16c0, 0x05df );
  291. if( pDevs!=NULL ){
  292. struct hid_device_info * pCDev=pDevs;
  293. //Count the number of returned devices
  294. for( DCBCount = 0; pCDev!=NULL; DCBCount++ ){
  295. pCDev = pCDev->next;
  296. };
  297. //Allocate a buffer for the relays
  298. if( DCBCount > 0 ){
  299. DC_BOARD tDCB;
  300. //Fill the relay structs
  301. pCDev = pDevs;
  302. for( int idx = 0; idx < DCBCount; idx++ ){
  303. //Save the path to this device
  304. //pDCB[idx].path = malloc(strlen(pCDev->path)+1);
  305. //memcpy( pDCB[idx].path, pCDev->path, strlen(pCDev->path) + 1 );
  306. tDCB.path = pCDev->path;
  307. // The product string is USBRelayx where x is number of relays read to the \0 in case there are more than 9
  308. tDCB.relay_count = atoi((const char *)&pCDev->product_string[8]);
  309. //Open it to get more details
  310. hid_device * handle;
  311. handle = hid_open_path( pCDev->path );
  312. if( handle ){
  313. unsigned char buf[9];
  314. //Get the features of the device
  315. buf[0] = 0x01;
  316. if ( hid_get_feature_report(handle, buf, sizeof(buf)) == -1){
  317. LOG_ERROR("hid_get_feature_report");
  318. pCDev = pCDev->next;
  319. continue;
  320. };
  321. //Set the serial number (0x0 for termination)
  322. memset( tDCB.serial, 0x0, sizeof( tDCB.serial ));
  323. memcpy( tDCB.serial, buf, HID_DCR_SERIIAL_LENGTH );
  324. //Byte 7 in the response contains the target_state of the relays
  325. tDCB.state = buf[7];
  326. hid_close( handle );
  327. //Output the device enumeration details if verbose is on
  328. LOG("HID DCB Found");
  329. sprintf(tBuffer, " Type: %04hx::%04hx", pCDev->vendor_id, pCDev->product_id);
  330. LOG(tBuffer);
  331. sprintf(tBuffer, " Path: %s", pCDev->path );
  332. LOG(tBuffer);
  333. sprintf(tBuffer, " Serial_number: %ls", pCDev->serial_number );
  334. LOG(tBuffer);
  335. sprintf(tBuffer, " Manufacturer: %ls", pCDev->manufacturer_string);
  336. LOG(tBuffer);
  337. sprintf(tBuffer, " Product: %ls", pCDev->product_string);
  338. LOG(tBuffer);
  339. sprintf(tBuffer, " Release: %hx", pCDev->release_number);
  340. LOG(tBuffer);
  341. sprintf(tBuffer, " Interface: %d", pCDev->interface_number);
  342. LOG(tBuffer);
  343. sprintf(tBuffer, " Number of Relays = %d", tDCB.relay_count);
  344. LOG(tBuffer);
  345. #ifdef DEBUG
  346. //If debug are on, output individual relay details
  347. for( int k = 0; k < tDCB.relay_count; k++ ){
  348. if( tDCB.state & 1 << k ){
  349. sprintf(tBuffer, " %s_%d=1", tDCB.serial, k + 1);
  350. } else {
  351. sprintf(tBuffer, " %s_%d=0", tDCB.serial, k + 1);
  352. };
  353. LOG(tBuffer);
  354. };
  355. #endif
  356. m_pDBC->addLocalDCB( m_nSiteID, pCDev->path, tDCB.serial, tDCB.relay_count, tDCB.state );
  357. } else {
  358. LOG_ERROR("unable to open HID device");
  359. };
  360. pCDev = pCDev->next;
  361. };
  362. };
  363. hid_free_enumeration( pDevs );
  364. };
  365. /* Add devices from DEV */
  366. dFD = opendir("/dev");
  367. if( dFD != NULL ) {
  368. while( ( pDR = readdir( dFD ))!= NULL ){
  369. if( strcasestr( pDR->d_name, "video" ) == pDR->d_name ) {
  370. createResourceVIDEO( pDR->d_name );
  371. continue;
  372. };
  373. if( strcasestr( pDR->d_name, "ttyS" ) == pDR->d_name ){
  374. createResourceUART( pDR->d_name );
  375. continue;
  376. };
  377. // RaspberyPI Hardware
  378. if( strcasestr( pDR->d_name, "ttyAMA" ) == pDR->d_name ){
  379. createResourceUART( pDR->d_name );
  380. continue;
  381. };
  382. if( strcasestr( pDR->d_name, "ttyUSB" ) == pDR->d_name ){
  383. createResourceUART( pDR->d_name );
  384. continue;
  385. };
  386. };
  387. closedir( dFD );
  388. };
  389. m_bHardwareEnumerated = true;
  390. };
  391. uint32_t argusHardwareManager::createResourceUART( CSTR devName ){
  392. uint32_t RetVal = 0;
  393. char tBuffer[256];
  394. /* Enumerate BuiltIn Serial */
  395. #ifdef DEBUG
  396. sprintf( tBuffer, "Serial Device: %s", devName );
  397. LOG( tBuffer );
  398. #endif
  399. char dev_name[CTEXT];
  400. int fd = -1;
  401. struct serial_struct ss;
  402. sprintf( dev_name, "/dev/%s", devName );
  403. fd = open( dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK );
  404. if (-1 == fd) {
  405. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  406. LOG_ERROR( tBuffer );
  407. } else {
  408. if (-1 == xioctl(fd, TIOCGSERIAL, &ss)){
  409. LOG_ERROR("TIOCGSERIAL");
  410. } else {
  411. #ifdef DEBUG
  412. sprintf( tBuffer, "Serial Port[Port:0x%X IRQ:%d BB:%d Line:%d Type:%d Flags:%X]",
  413. ss.port,
  414. ss.irq,
  415. ss.baud_base,
  416. ss.line,
  417. ss.type,
  418. ss.flags
  419. );
  420. LOG( tBuffer );
  421. #endif
  422. char comName[16];
  423. int pn = atoi( devName+4 ) + 1;
  424. sprintf( comName,"COM%d", pn );
  425. //uint32_t dbid = m_pDBC->addNodeSocket( m_nServerNodeID, m_nServerID, ARGUS_NODE_SOCK_TYPE_SERIAL_PORT, ARGUS_NODE_SOCK_MODE_INOUT, true, dev_name, comName );
  426. //if( dbid == 0 ) {
  427. // Socket Allredy in DB - was just updated
  428. // dbid = m_pDBC->getNodeSocketID( m_nServerNodeID, m_nServerID, ARGUS_NODE_SOCK_TYPE_SERIAL_PORT, dev_name );
  429. //};
  430. RetVal = m_pDBC->addResource( m_nSiteID, m_nServerNodeID, ARGUS_HARDWARE_UART, dev_name );
  431. if( RetVal ) {
  432. m_pDBC->updateResourceAUX( RetVal, m_pDBC->addLocalUART( RetVal, 1, 115200, 8, 'N', 1.0f, dev_name ) );
  433. };
  434. };
  435. close(fd);
  436. };
  437. return RetVal;
  438. };
  439. uint32_t argusHardwareManager::createResourceVIDEO( CSTR devName ){
  440. uint32_t RetVal = 0;
  441. char tBuffer[256];
  442. /* Enumerate Video Capture */
  443. #ifdef DEBUG
  444. sprintf( tBuffer, "Video Source: %s", devName );
  445. LOG( tBuffer );
  446. #endif
  447. struct v4l2_capability cap;
  448. //struct v4l2_cropcap cropcap;
  449. //struct v4l2_crop crop;
  450. struct v4l2_format fmt;
  451. char dev_name[CTEXT];
  452. int fd = -1;
  453. struct stat st;
  454. sprintf( dev_name, "/dev/%s", devName );
  455. if (-1 == stat( dev_name, &st )) {
  456. sprintf( tBuffer, "Cannot identify '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  457. LOG_ERROR( tBuffer );
  458. return 0;
  459. };
  460. if (!S_ISCHR(st.st_mode)) {
  461. sprintf( tBuffer, "%s is no devicen", dev_name);
  462. LOG_ERROR( tBuffer );
  463. return 0;
  464. };
  465. fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
  466. if (-1 == fd) {
  467. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  468. LOG_ERROR( tBuffer );
  469. } else {
  470. if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
  471. if (EINVAL == errno) {
  472. sprintf( tBuffer, "%s is no V4L2 device", dev_name );
  473. LOG_ERROR( tBuffer );
  474. } else {
  475. LOG_ERROR( "VIDIOC_QUERYCAP" );
  476. };
  477. close(fd);
  478. return 0;
  479. };
  480. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  481. sprintf(tBuffer, "%s is no video capture device", dev_name);
  482. LOG_ERROR( tBuffer );
  483. close(fd);
  484. return 0;
  485. };
  486. CLEAR(fmt);
  487. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  488. /* Preserve original settings as set by v4l2-ctl for example */
  489. if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt)){
  490. //LOG_ERROR("VIDIOC_G_FMT");
  491. } else {
  492. #ifdef DEBUG
  493. sprintf( tBuffer, "Grabber[%dx%d]",fmt.fmt.pix.width,fmt.fmt.pix.height);
  494. LOG( tBuffer );
  495. #endif
  496. uint64_t mid = m_pDBC->addLocalMediaSource( m_nSiteID, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height );
  497. //m_pDBC->addNodeSocket( m_nServerNodeID, mid, ARGUS_NODE_SOCK_TYPE_ANALOG_VIDEO_GRABBER, ARGUS_NODE_SOCK_MODE_OUT, false, dev_name, tBuffer );
  498. RetVal = m_pDBC->addResource( m_nSiteID, m_nServerNodeID, ARGUS_HARDWARE_ANALOG_VIDEO_IN, dev_name, mid );
  499. };
  500. close(fd);
  501. };
  502. return RetVal;
  503. };
  504. uint32_t argusHardwareManager::createResourceUDCB( CSTR devName ){
  505. uint32_t RetVal = 0;
  506. char tBuffer[256];
  507. return RetVal;
  508. };
  509. argusHardwareResource *argusHardwareManager::requestResource( ARGUS_HARDWARE_PROVIDERS type, uint32_t id ){
  510. argusHardwareResource *RetVal = NULL;
  511. if( m_vResources.size() ) {
  512. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  513. if( ((*idx)->getProviderType() == type ) && ( (*idx)->getDbId() == id ) ) {
  514. RetVal = (*idx);
  515. break;
  516. };
  517. };
  518. };
  519. return RetVal;
  520. };
  521. DBC_INT_LIST *argusHardwareManager::requestResourcesByType( ARGUS_HARDWARE_PROVIDERS type ){
  522. DBC_INT_LIST *RetVal = NULL;
  523. #ifdef DEBUG_EXTRA
  524. char tBuffer[128];
  525. sprintf( tBuffer, "Requested Resource type: %d", type );
  526. LOG( tBuffer );
  527. #endif
  528. if( m_vResources.size() ) {
  529. #ifdef DEBUG_EXTRA
  530. LOG("Have Some Resources!");
  531. #endif
  532. RetVal = new DBC_INT_LIST();
  533. if( RetVal ) {
  534. #ifdef DEBUG_EXTRA
  535. LOG("RetVal Created!");
  536. #endif
  537. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  538. #ifdef DEBUG_EXTRA
  539. sprintf( tBuffer, "Have Resource type: %d", (*idx)->getType() );
  540. LOG( tBuffer );
  541. #endif
  542. if( (*idx)->getProviderType() == type ) {
  543. #ifdef DEBUG_EXTRA
  544. LOG("Matched!");
  545. #endif
  546. RetVal->push_back((*idx)->getDbId());
  547. };
  548. };
  549. } else {
  550. LOG_ERROR("Cannot Create DBC_INT_LIST");
  551. };
  552. };
  553. return RetVal;
  554. };
  555. #undef CLEAR
  556. #undef LOG
  557. #undef LOG_ERROR