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.

484 lines
13 KiB

7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
7 years ago
​
​
4 years ago
7 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
4 years ago
7 years ago
​
​
​
​
​
​
​
​
4 years ago
7 years ago
​
4 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
  1. #include "protocolPTZ_VISCA.h"
  2. #define LOG( X ) m_pLog->Log( "Proto[VISCA]", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "Proto[VISCA]", this, _LOG_ERROR, X )
  4. protocolPTZ_VISCA::protocolPTZ_VISCA(config_setting_t *Settings): protocolPTZ(), m_mInquiryMutex(){
  5. m_pLog = basicLogCollection::getInstance();
  6. m_pSettings = Settings;
  7. m_bProtocolDeviceID = 1;
  8. m_bLastCommandIssued = CMD_PTZ_NOOP;
  9. memset( m_aSlots, 0 , 10 );
  10. };
  11. protocolPTZ_VISCA::protocolPTZ_VISCA( uint8_t CAM_ID ): protocolPTZ(), m_mInquiryMutex(){
  12. m_pLog = basicLogCollection::getInstance();
  13. m_pSettings = NULL;
  14. m_bProtocolDeviceID = CAM_ID;
  15. m_bLastCommandIssued = CMD_PTZ_NOOP;
  16. memset( m_aSlots, 0 , 10 );
  17. };
  18. protocolPTZ_VISCA::~protocolPTZ_VISCA(){
  19. };
  20. int protocolPTZ_VISCA::packetChecker( unsigned char *Buffer, size_t BufferSize ){
  21. if( ( Buffer[0] & 0xF0 ) == ( 0x80 | m_bProtocolDeviceID << 4 ) || ( Buffer[0] & 0xF0 ) == 0x80 ) {
  22. if( ( Buffer[0] & 0x0F ) == 0x00 || Buffer[0] == 0x88 ) {
  23. if( BufferSize > 1 ) {
  24. if( BufferSize < 16 ) {
  25. for( U8 i=1; i< BufferSize; i++ ){
  26. if( Buffer[i] == 0xFF ) {
  27. #ifdef DEBUG
  28. LOG("PacketChecker: Packet Is Recognized");
  29. #endif
  30. return 0;
  31. };
  32. };
  33. } else {
  34. for( U8 i=1; i< 16; i++ ){
  35. if( Buffer[i] == 0xFF ) {
  36. #ifdef DEBUG
  37. LOG("PacketChecker: Packet Is Recognized");
  38. #endif
  39. return 0;
  40. };
  41. };
  42. };
  43. };
  44. };
  45. };
  46. #ifdef DEBUG
  47. LOG("PacketChecker: Packet Not Recognized");
  48. #endif
  49. return 1;
  50. };
  51. //int protocolPTZ_VISCA::packetExtractor(unsigned char *Buffer, size_t BufferSize)
  52. protocolPackage *protocolPTZ_VISCA::packetExtractor( unsigned char *Buffer, size_t BufferSize ){
  53. U8 i;
  54. protocolPackage *RetVal;
  55. RetVal = (protocolPackage*)malloc(sizeof(protocolPackage));
  56. if( RetVal != NULL ){
  57. RetVal->m_pData = (unsigned char *) malloc(16);
  58. if ( RetVal->m_pData ) {
  59. for (i = 0; i < BufferSize && i < 16; i++) {
  60. RetVal->m_pData[i] = Buffer[i];
  61. if (Buffer[i] == 0xFF) {
  62. LOG("PacketExtractor: Packet Is Extracted");
  63. break;
  64. };
  65. };
  66. RetVal->m_nSize = i + 1;
  67. char tB[64];
  68. sprintf( tB, "PSize: %d", RetVal->m_nSize );
  69. LOG(tB);
  70. } else {
  71. free( RetVal );
  72. RetVal = NULL;
  73. };
  74. };
  75. return RetVal;
  76. };
  77. bool protocolPTZ_VISCA::packetHandler(protocolPackage *Package){
  78. U8 Com_Mode;
  79. U8 Cat_Code;
  80. if ( Package == NULL ) {
  81. LOG("PacketHandler: Packet Is empty!");
  82. return false;
  83. } else {
  84. char tB[64];
  85. sprintf( tB, "PSize: %d", Package->m_nSize );
  86. LOG(tB);
  87. };
  88. LOG("PacketHandler: Packet Is Handling");
  89. Com_Mode = Package->m_pData[1];
  90. Cat_Code = Package->m_pData[2];
  91. if( ( Com_Mode & 0xF0 ) == 0x40 ){//Ack + Slot Assign
  92. U8 SlotID;
  93. SlotID = Com_Mode & 0x0F;
  94. LOG("Command Slot");
  95. m_aSlots[SlotID] = m_bLastCommandIssued;
  96. LOG("Command Slot Assigned");
  97. } else if( ( Com_Mode & 0xF0 ) == 0x50 ) {// Slot Complete
  98. U8 SlotID;
  99. SlotID = Com_Mode & 0x0F;
  100. if( Package->m_nSize == 3 ){
  101. LOG("Command acompilished -> Slot Fried");
  102. } else {
  103. LOG("Inquiry acompilished -> Slot Fried");
  104. switch( m_aSlots[SlotID]){
  105. default:
  106. LOG_ERROR("Unknown Enquiry!");
  107. };
  108. };
  109. m_aSlots[SlotID] = 0;
  110. } else if( (Com_Mode & 0xF0) == 0x60 ){//error
  111. switch (Cat_Code){
  112. case 0x02:
  113. LOG("Syntax Error");
  114. break;
  115. case 0x03:
  116. LOG("Command Buffer Full");
  117. break;
  118. case 0x04:
  119. LOG("Cancel Command");
  120. break;
  121. case 0x05:
  122. LOG("No Command Slots");
  123. break;
  124. case 0x41:
  125. LOG("Not Executable Command");
  126. break;
  127. default:
  128. LOG("Unknown error");
  129. break;
  130. };
  131. };
  132. m_bLastCommandIssued = CMD_PTZ_NOOP;
  133. //LOG("End Handling - > FREE!");
  134. //free( Package->m_pData );
  135. //free( Package );
  136. //LOG("After Free!");
  137. return true;
  138. };
  139. bool protocolPTZ_VISCA::create( void ){
  140. if( !protocolPTZ::create() ) return false;
  141. if( m_oDataTransport == NULL ) return false;
  142. if( m_pSettings != NULL ) {
  143. config_setting_t *pSetting;
  144. int tPort, tSpeed;
  145. m_bStatus = 0;
  146. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_COM_PORT);
  147. if (pSetting == NULL) return false;
  148. tPort = config_setting_get_int(pSetting);
  149. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_COM_SPEED);
  150. if (pSetting == NULL) return false;
  151. tSpeed = config_setting_get_int(pSetting);
  152. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_CAM_ID);
  153. if (pSetting == NULL) return false;
  154. m_bProtocolDeviceID = (U8)config_setting_get_int(pSetting);
  155. /*
  156. char tBuf[16];
  157. #ifdef __linux__
  158. sprintf( tBuf,"/dev/ttyS%d",tPort);
  159. #else
  160. sprintf(tBuf, "com%d", tPort);
  161. #endif
  162. m_oDataTransport = new serialPort(tBuf,tSpeed);
  163. if (m_oDataTransport != NULL){
  164. if (m_oDataTransport->addRecipient(this)){
  165. m_bStatus = 1;
  166. return true;
  167. };
  168. };
  169. */
  170. };
  171. m_bStatus = 1;
  172. return true;
  173. };
  174. bool protocolPTZ_VISCA::initCamera(void){
  175. U8 Buffer[16];
  176. if (m_bStatus == 0) return false;
  177. //if (m_bReplyRequired) if ( m_bLastCommandIssued != CMD_PTZ_NOOP ) return false;
  178. m_bLastCommandIssued = CMD_PTZ_INIT;
  179. // Broadcasting
  180. Buffer[0] = 0x88;
  181. Buffer[1] = 0x30;
  182. Buffer[2] = 0x01;
  183. Buffer[3] = 0xFF;
  184. LOG("Sending INIT");
  185. m_oDataTransport->sendData(Buffer, 4);
  186. SLEEP(100);
  187. return sendIfClear();
  188. };
  189. bool protocolPTZ_VISCA::sendIfClear(void){
  190. U8 Buffer[16];
  191. if (m_bStatus == 0) return false;
  192. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  193. m_bLastCommandIssued = CMD_PTZ_IF_CLEAR;
  194. Buffer[0] = 0x88;
  195. Buffer[1] = 0x01;
  196. Buffer[2] = 0x00;
  197. Buffer[3] = 0x01;
  198. Buffer[4] = 0xFF;
  199. LOG("Sending IF_Clear");
  200. m_oDataTransport->sendData(Buffer, 5);
  201. SLEEP(100);
  202. return setPowerSaveMode(false);
  203. };
  204. bool protocolPTZ_VISCA::setPowerSaveMode( bool nMode ){
  205. U8 Buffer[16];
  206. if (m_bStatus == 0) return false;
  207. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  208. m_bPowerSave_Mode = !nMode;
  209. m_bLastCommandIssued =( m_bPowerSave_Mode )?CMD_PTZ_POWER_ON:CMD_PTZ_POWER_OFF;
  210. Buffer[0]=0x80+m_bProtocolDeviceID;
  211. Buffer[1]=0x01;
  212. Buffer[2]=0x04;
  213. Buffer[3]=0x00;
  214. Buffer[4]=( m_bPowerSave_Mode )?0x02:0x03;
  215. Buffer[5]=0xFF;
  216. LOG("Sending POWER ON/OFF");
  217. return m_oDataTransport->sendData(Buffer, 6);
  218. };
  219. bool protocolPTZ_VISCA::getPowerSaveMode( void ){
  220. U8 Buffer[16];
  221. if (m_bStatus == 0) return false;
  222. //if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  223. Buffer[0]=0x80+m_bProtocolDeviceID;
  224. Buffer[1]=0x09;
  225. Buffer[2]=0x04;
  226. Buffer[3]=0x00;
  227. Buffer[4]=0xFF;
  228. m_mInquiryMutex.lock();
  229. LOG("Ask POWER mode");
  230. m_oDataTransport->sendData( Buffer, 5 );
  231. m_mInquiryMutex.lock();
  232. m_mInquiryMutex.unlock();
  233. LOG("Ask POWER mode -> Done");
  234. return m_bPowerSave_Mode;
  235. };
  236. bool protocolPTZ_VISCA::startZoomWide( void ){
  237. U8 Buffer[16];
  238. if (m_bStatus == 0) return false;
  239. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  240. Buffer[0]=0x80+m_bProtocolDeviceID;
  241. Buffer[1]=0x01;
  242. Buffer[2]=0x04;
  243. Buffer[3]=0x07;
  244. Buffer[4]=0x03;
  245. Buffer[5]=0xFF;
  246. LOG("Start Zoom Wide(standard)");
  247. return m_oDataTransport->sendData(Buffer, 6);
  248. };
  249. bool protocolPTZ_VISCA::startZoomTele( void ){
  250. U8 Buffer[16];
  251. if (m_bStatus == 0) return false;
  252. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  253. Buffer[0]=0x80+m_bProtocolDeviceID;
  254. Buffer[1]=0x01;
  255. Buffer[2]=0x04;
  256. Buffer[3]=0x07;
  257. Buffer[4]=0x02;
  258. Buffer[5]=0xFF;
  259. LOG("Start Zoom Tele(standard)");
  260. return m_oDataTransport->sendData(Buffer, 6);
  261. };
  262. bool protocolPTZ_VISCA::startZoomWide( double nSpeed ){
  263. U8 Buffer[16];
  264. if (m_bStatus == 0) return false;
  265. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  266. Buffer[0]=0x80+m_bProtocolDeviceID;
  267. Buffer[1]=0x01;
  268. Buffer[2]=0x04;
  269. Buffer[3]=0x07;
  270. U8 Param;
  271. Param = ((U8)( 2 + nSpeed * 5 )) & 0x07;
  272. Buffer[4]=0x30 | Param;
  273. Buffer[5]=0xFF;
  274. LOG("Start Zoom Wide(parameter)");
  275. return m_oDataTransport->sendData(Buffer, 6);
  276. };
  277. bool protocolPTZ_VISCA::startZoomTele( double nSpeed ){
  278. U8 Buffer[16];
  279. if (m_bStatus == 0) return false;
  280. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  281. Buffer[0]=0x80+m_bProtocolDeviceID;
  282. Buffer[1]=0x01;
  283. Buffer[2]=0x04;
  284. Buffer[3]=0x07;
  285. U8 Param;
  286. Param = ((U8)( 2 + nSpeed * 5 )) & 0x07;
  287. Buffer[4]=0x20 | Param;
  288. Buffer[5]=0xFF;
  289. LOG("Start Zoom Tele(parameter)");
  290. return m_oDataTransport->sendData(Buffer, 6);
  291. };
  292. bool protocolPTZ_VISCA::stopZoom( void ){
  293. U8 Buffer[16];
  294. if (m_bStatus == 0) return false;
  295. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  296. Buffer[0]=0x80+m_bProtocolDeviceID;
  297. Buffer[1]=0x01;
  298. Buffer[2]=0x04;
  299. Buffer[3]=0x07;
  300. Buffer[4]=0x00;
  301. Buffer[5]=0xFF;
  302. LOG("Stop Zoom");
  303. return m_oDataTransport->sendData(Buffer, 6);
  304. };
  305. bool protocolPTZ_VISCA::setZoom( double nZoom ){
  306. U8 Buffer[16];
  307. U16 Param;
  308. if (m_bStatus == 0) return false;
  309. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  310. Param = ((U16)( 0x03FF * nZoom )) & 0x03FF;
  311. Buffer[0]=0x80+m_bProtocolDeviceID;
  312. Buffer[1]=0x01;
  313. Buffer[2]=0x04;
  314. Buffer[3]=0x47;
  315. //Buffer[4]=0x0F & ( Param >> 12 );
  316. Buffer[4]=0x00;
  317. Buffer[5]=0x03 & ( Param >> 8 );
  318. Buffer[6]=0x0F & ( Param >> 4 );
  319. Buffer[7]=0x0F & Param;
  320. Buffer[8]=0xFF;
  321. LOG("Set Zoom(direct)");
  322. return m_oDataTransport->sendData(Buffer, 9);
  323. };
  324. double protocolPTZ_VISCA::getZoom( void ){
  325. U8 Buffer[16];
  326. if (m_bStatus == 0) return false;
  327. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  328. Buffer[0]=0x80+m_bProtocolDeviceID;
  329. Buffer[1]=0x09;
  330. Buffer[2]=0x04;
  331. Buffer[3]=0x47;
  332. Buffer[4]=0xFF;
  333. m_mInquiryMutex.lock();
  334. LOG("Ask Zoom");
  335. m_oDataTransport->sendData( Buffer, 5 );
  336. m_mInquiryMutex.lock();
  337. m_mInquiryMutex.unlock();
  338. LOG("Ask Zoom -> Done");
  339. return m_dZoom;
  340. };
  341. bool protocolPTZ_VISCA::resetPTZDrive( void ){
  342. U8 Buffer[16];
  343. if (m_bStatus == 0) return false;
  344. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  345. Buffer[0]=0x80+m_bProtocolDeviceID;
  346. Buffer[1]=0x01;
  347. Buffer[2]=0x06;
  348. Buffer[3]=0x05;
  349. Buffer[4]=0xFF;
  350. LOG("Reset PTZ");
  351. return m_oDataTransport->sendData(Buffer, 5);
  352. };
  353. bool protocolPTZ_VISCA::goHomePosition( void ){
  354. U8 Buffer[16];
  355. if (m_bStatus == 0) return false;
  356. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  357. Buffer[0]=0x80+m_bProtocolDeviceID;
  358. Buffer[1]=0x01;
  359. Buffer[2]=0x06;
  360. Buffer[3]=0x04;
  361. Buffer[4]=0xFF;
  362. LOG("Go Home");
  363. return m_oDataTransport->sendData(Buffer, 5);
  364. };
  365. bool protocolPTZ_VISCA::move(uint32_t MoveMatrix){
  366. U8 Buffer[16];
  367. U8 Param1, Param2;
  368. if (m_bStatus == 0) return false;
  369. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  370. Param1 = (U8)(m_dPanSpeed * 24.0) & 0xFF;
  371. Param2 = (U8)(m_dTiltSpeed * 20.0) & 0xFF;
  372. Buffer[0] = 0x80 + m_bProtocolDeviceID;
  373. Buffer[1] = 0x01;
  374. Buffer[2] = 0x06;
  375. Buffer[3] = 0x01;
  376. Buffer[4] = Param1;
  377. Buffer[5] = Param2;
  378. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0){
  379. Buffer[6] = 0x03;
  380. } else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0){
  381. Buffer[6] = 0x01;
  382. } else {
  383. Buffer[6] = 0x02;
  384. };
  385. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0){
  386. Buffer[7] = 0x03;
  387. } else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0){
  388. Buffer[7] = 0x01;
  389. } else {
  390. Buffer[7] = 0x02;
  391. };
  392. Buffer[8] = 0xFF;
  393. LOG("Make move");
  394. return m_oDataTransport->sendData(Buffer, 9);
  395. };
  396. bool protocolPTZ_VISCA::goAbsolute( float dPan, float dTilt, float dZoom){
  397. U8 Buffer[16];
  398. U8 Param1, Param2;
  399. if (m_bStatus == 0) return false;
  400. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  401. Buffer[0] = 0x80 + m_bProtocolDeviceID;
  402. Buffer[1] = 0x01;
  403. Buffer[2] = 0x06;
  404. Buffer[3] = 0x02;
  405. Buffer[4] = 0x18;//Max Pan Speed
  406. Buffer[5] = 0x14;//Max Tilt Speed
  407. uint16_t pPos, tPos;
  408. //-880 .. 880
  409. pPos = (uint16_t)(880.0 * dPan);
  410. Buffer[6] = 0x0F & (pPos >> 12);
  411. Buffer[7] = 0x0F & (pPos >> 8);
  412. Buffer[8] = 0x0F & (pPos >> 4);
  413. Buffer[9] = 0x0F & pPos;
  414. //-300 .. 300
  415. tPos = (uint16_t)(300.0 * dTilt);
  416. Buffer[10] = 0x0F & (tPos >> 12);
  417. Buffer[11] = 0x0F & (tPos >> 8);
  418. Buffer[12] = 0x0F & (tPos >> 4);
  419. Buffer[13] = 0x0F & tPos;
  420. Buffer[14] = 0xFF;
  421. return m_oDataTransport->sendData(Buffer, 15);
  422. };
  423. bool protocolPTZ_VISCA::setAutoFocus(bool nMode){
  424. m_bAutoFocus_Mode = nMode;
  425. U8 Buffer[16];
  426. if (m_bStatus == 0) return false;
  427. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  428. Buffer[0] = 0x80 + m_bProtocolDeviceID;
  429. Buffer[1] = 0x01;
  430. Buffer[2] = 0x04;
  431. Buffer[3] = 0x38;
  432. Buffer[4] = (nMode) ? 0x02 : 0x03;
  433. Buffer[5] = 0xFF;
  434. LOG("Go Left");
  435. return m_oDataTransport->sendData(Buffer, 6);
  436. };
  437. bool protocolPTZ_VISCA::setOSD( bool nMode ){
  438. U8 Buffer[16];
  439. if (m_bStatus == 0) return false;
  440. if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  441. Buffer[0]=0x80+m_bProtocolDeviceID;
  442. Buffer[1]=0x01;
  443. Buffer[2]=0x06;
  444. Buffer[3]=0x06;
  445. Buffer[4]=(nMode)?0x02:0x03;
  446. Buffer[5]=0xFF;
  447. LOG("Set On Screen Display mode");
  448. return m_oDataTransport->sendData(Buffer, 6);
  449. };
  450. #undef LOG
  451. #undef LOG_ERROR