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.

462 lines
12 KiB

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