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.

320 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "kernelModuleBosch.h"
  2. // #include "mojoKernel.h"
  3. #define LOG(X) kernelModule::m_pLog->Log("MOD_BVMS", this, X)
  4. #ifdef DEBUG
  5. #define LOG_ERROR(X) \
  6. { \
  7. char _tBfrErrMSG2w34[256]; \
  8. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  9. kernelModule::m_pLog->Log("MOD_BVMS", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  10. }
  11. #else
  12. #define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_BVMS", this, _LOG_ERROR, X);
  13. #endif
  14. const CSTR bmvs_zones[] = { "C2", "C3", "D1", "D2", "E1", "E2", "E3", "F1", "F2", "G1", "G2", "G3", "H1", "H2", "C1", "B2", "B1", "A3", "A2", "A1", "" };
  15. const uint8_t bmvs_zones_count = 20;
  16. kernelModuleBosch::kernelModuleBosch(uint32_t ModuleID) :
  17. kernelModule(ModuleID, KERNEL_MODULE_BOSCH_REST){
  18. m_pCURL = NULL;
  19. m_pHttpAccessManager = NULL;
  20. m_pEventHeap = basicEventHeap::getInstance();
  21. }
  22. kernelModuleBosch::~kernelModuleBosch(){
  23. m_bEnabled = false;
  24. if ( m_pCURL ){
  25. curl_easy_cleanup(m_pCURL);
  26. m_pCURL = NULL;
  27. };
  28. }
  29. bool kernelModuleBosch::startService(){
  30. m_pHttpAccessManager = curlRequestsManager::getInstance();
  31. if( !m_bEnabled ) return false;
  32. if ( m_pCURL ) return true;
  33. if ( reloadSettings() ) return true;
  34. return false;
  35. }
  36. bool kernelModuleBosch::stopService(bool force){
  37. m_bEnabled = false;
  38. return true;
  39. }
  40. bool kernelModuleBosch::reloadSettings(){
  41. if (m_pDBC == NULL) return false;
  42. if (! m_bEnabled) return false;
  43. REST_SETTINGS * tData = m_pDBC->getBoschData( m_nDBAUXID );
  44. if ( tData != NULL ){
  45. memcpy( &m_sRestSettings, tData, sizeof(REST_SETTINGS));
  46. free( tData );
  47. return true;
  48. };
  49. return false;
  50. }
  51. bool kernelModuleBosch::_send_request(CSTR url, CSTR post_string ){
  52. #ifdef CURL_MANAGER
  53. bool RetVal = false;
  54. if (m_pHttpAccessManager){
  55. m_mResponseProtector.lock();
  56. curlRequest * tRC;
  57. tRC = new curlRequest( url, (CSTR) "Content-Type: application/json; charset=utf-8;");
  58. if (tRC != NULL){
  59. #ifdef DEBUG_BVMS_TRAFFIC
  60. FILE * tf;
  61. tf = fopen("Traffic_bvms.txt", "a+");
  62. fprintf(tf, "--------------------\r\n");
  63. if ( post_string != NULL ) {
  64. fprintf( tf, "REQ: %s (%d)\n", url, strlen(post_string));
  65. fprintf( tf, ">>>>> %s\n", post_string );
  66. } else fprintf(tf, "REQ: %s\n", url );
  67. fprintf(tf,"Creds: [%s]:[%s]\n", m_sRestSettings.access_user, m_sRestSettings.access_pass );
  68. fprintf(tf, "--------------------\r\n");
  69. fclose(tf);
  70. //bool mustReturnData = true; // In Debug Allways Read Return
  71. #endif
  72. tRC->setCredentials( m_sRestSettings.access_user, m_sRestSettings.access_pass, m_sRestSettings.auth_mode );
  73. if( post_string != NULL ){
  74. tRC->setPOST(post_string, strlen(post_string));
  75. };
  76. RetVal = m_pHttpAccessManager->accomplish( tRC );
  77. } else {
  78. LOG_ERROR("Cannot Create curlRequest!");
  79. };
  80. m_mResponseProtector.unlock();
  81. };
  82. return RetVal;
  83. #else
  84. char tBuffer[CTEXT];
  85. if (m_pCURL == NULL)
  86. {
  87. if (m_pLastResponse == NULL)
  88. {
  89. m_pLastResponse = (CSTR) malloc(DEFAULT_BUFFER_SIZE);
  90. };
  91. m_pCURL = curl_easy_init();
  92. if (m_pProxyHost != NULL && m_nProxyPort != 0)
  93. {
  94. curl_easy_setopt(m_pCURL, CURLOPT_PROXY, m_pProxyHost);
  95. curl_easy_setopt(m_pCURL, CURLOPT_PROXYPORT, m_nProxyPort);
  96. if (m_pProxyUsername != NULL && m_pProxyPassword != NULL)
  97. {
  98. sprintf(tBuffer, "%s:%s", m_pProxyUsername, m_pProxyPassword);
  99. curl_easy_setopt(m_pCURL, CURLOPT_PROXYUSERPWD, tBuffer);
  100. };
  101. };
  102. curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT, 1);
  103. curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT, 2);
  104. curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYPEER, 0);
  105. curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYHOST, 0);
  106. curl_easy_setopt(m_pCURL, CURLOPT_VERBOSE, 0);
  107. curl_easy_setopt(m_pCURL, CURLOPT_POST, 1);
  108. // curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
  109. // curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
  110. // curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
  111. curl_easy_setopt(m_pCURL, CURLOPT_TCP_FASTOPEN, 1);
  112. curl_easy_setopt(m_pCURL, CURLOPT_TCP_NODELAY, 1);
  113. // curl_easy_setopt( m_pCURL, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  114. curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, pOnvif::_writer);
  115. curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this);
  116. };
  117. if (m_pCURL != NULL)
  118. {
  119. curl_easy_setopt(m_pCURL, CURLOPT_URL, url);
  120. struct curl_slist * sList = NULL;
  121. // Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"
  122. // Connection: Close
  123. sList = curl_slist_append(sList, "Content-Type: text/xml; charset=utf-8");
  124. sprintf(tBuffer, "Content-Length: %d", strlen(post_string));
  125. sList = curl_slist_append(sList, tBuffer);
  126. curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList);
  127. curl_easy_setopt(m_pCURL, CURLOPT_POSTFIELDS, post_string);
  128. sprintf(tBuffer, "%s:%s", m_pUsername, m_pPassword);
  129. curl_easy_setopt(m_pCURL, CURLOPT_USERPWD, tBuffer);
  130. switch (m_bAuthMode)
  131. {
  132. case 0: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); break;
  133. case 1: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); break;
  134. case 2: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST_IE); break;
  135. case 3: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); break;
  136. case 4: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE); break;
  137. case 5: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); break;
  138. case 6: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM_WB); break;
  139. case 7: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANY); break;
  140. case 8: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE); break;
  141. case 9:
  142. curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ONLY);
  143. break;
  144. // case 10: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_AWS_SIGV4 ); break;
  145. };
  146. #ifdef DEBUG
  147. char tErrorBuffer[CURL_ERROR_SIZE];
  148. curl_easy_setopt(m_pCURL, CURLOPT_ERRORBUFFER, tErrorBuffer);
  149. #endif
  150. m_pLastIndex = 0;
  151. if (curl_easy_perform(m_pCURL))
  152. {
  153. LOG_ERROR("CURL Failure!");
  154. #ifdef DEBUG
  155. LOG_ERROR(tErrorBuffer);
  156. #endif
  157. }
  158. else
  159. {
  160. m_pLastResponse[m_pLastIndex] = (char) 0;
  161. return true;
  162. };
  163. m_pLastIndex = 0;
  164. };
  165. return false;
  166. #endif
  167. }
  168. bool kernelModuleBosch::buildTargetEventMsg( CSTR Buffer, float az, float range, float lat, float lon, float alt, uint64_t extID, CSTR src_Name ){
  169. if ( Buffer == NULL ) return false;
  170. char tTS[64];
  171. time_t LocalTime;
  172. time(&LocalTime);
  173. tm gmtt;
  174. tm * gmt_time = &gmtt;
  175. if( gmtime_r( &LocalTime, &gmtt ) == NULL) return false;
  176. CSTR pp = strstr( src_Name, "_" );
  177. uint8_t zid = bmvs_zones_count;
  178. if( pp!= NULL ) zid = atoi( pp+1 )-1;
  179. if ( zid > bmvs_zones_count ) zid = bmvs_zones_count;
  180. #ifdef ZORRONET_PACKETS
  181. sprintf(tTS, "%04d-%02d-%02dT%02d:%02d:%02dZ", 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec);
  182. sprintf( Buffer, "{\"azimuth\": %0.2f,\"distance\": %0.2f,\"name\": \"RADAR1_DFSL\",\"position\": {\"alt\": %0.2f,\"lat\": %0.8f,\"lon\": %0.8f},\"target_coordinates\": {\"alt\": %0.2f,\"lat\": %0.8f,\"lon\": %0.8f},\"timestamp\": \"%s\"}",
  183. az, range,
  184. alt, lat, lon,
  185. alt, lat, lon,
  186. tTS
  187. );
  188. #else
  189. sprintf(tTS, "%04d-%02d-%02d %02d:%02d:%02d", 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec);
  190. sprintf( Buffer, "{\"externalAlarmId\":%d,\"data\":[%0.2f,%0.1f,%0.12f,%0.12f,%0.3f,10,\"%s\",\"%s\",\"%s\",\"Detection Event\"]}",
  191. m_sRestSettings.detection_src,
  192. az, range,
  193. lat, lon, alt,
  194. src_Name,
  195. bmvs_zones[zid],
  196. tTS
  197. );
  198. #endif
  199. return true;
  200. }
  201. bool kernelModuleBosch::buildCameraEventMsg( CSTR Buffer, CSTR CamName, uint8_t Status ){
  202. if ( Buffer == NULL ) return false;
  203. char tTS[64];
  204. time_t LocalTime;
  205. time(&LocalTime);
  206. tm gmtt;
  207. tm * gmt_time = &gmtt;
  208. if( gmtime_r( &LocalTime, &gmtt ) == NULL) return false;
  209. sprintf( tTS, "%04d-%02d-%02d %02d:%02d:%02d", 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec );
  210. sprintf( Buffer, "{\"externalAlarmId\":%d,\"data\":[\"%s\",%d,\"%s\",\"Camera Event\"]}",
  211. m_sRestSettings.camera_src,
  212. CamName,
  213. Status,
  214. tTS
  215. );
  216. return true;
  217. }
  218. bool kernelModuleBosch::dispatchEvent(EVENT_CONTAINER * Evt){
  219. if( Evt == NULL ) return false;
  220. if( !m_bEnabled ) return true;
  221. char tBuffer[4096];
  222. char tURL[256];
  223. #ifdef DEBUG_EXTRA
  224. sprintf(tBuffer, "Event Class %d Type %d ", Evt->m_eClass, Evt->m_eType);
  225. LOG(tBuffer);
  226. #endif
  227. switch (Evt->m_eType){
  228. case EVT_ROI_ACTIVE:
  229. case EVT_TARGET_2D_BLACK:
  230. case EVT_TARGET_2D_WHITE:{
  231. if ( m_sRestSettings.detection_enabled ) {
  232. TARGET_DATA * tTARGET = (TARGET_DATA *) Evt->m_pEvtData;
  233. if (tTARGET != NULL){
  234. CSTR tN = m_pDBC->getNodeName( Evt->m_nSourceNodeID );
  235. if ( tN ) {
  236. if ( buildTargetEventMsg(
  237. tBuffer,
  238. tTARGET->local_target.fi,
  239. tTARGET->local_target.r,
  240. tTARGET->global_target.lat,
  241. tTARGET->global_target.lon,
  242. tTARGET->global_target.alt,
  243. Evt->m_nSourceNodeID,
  244. tN
  245. ) ) {
  246. if ( m_sRestSettings.use_ssl ) {
  247. sprintf(tURL, "https://%s:%d%s", m_sRestSettings.server_ip, m_sRestSettings.port, m_sRestSettings.rest_api);
  248. } else {
  249. sprintf(tURL, "http://%s:%d%s", m_sRestSettings.server_ip, m_sRestSettings.port, m_sRestSettings.rest_api);
  250. };
  251. _send_request( tURL ,tBuffer );
  252. };
  253. free( tN );
  254. };
  255. };
  256. };
  257. break;
  258. };
  259. case EVT_CAMERA_MOVE_TO:{
  260. // Evt->m_nSourceNodeID
  261. //LOG("CAM MOVE!");
  262. break;
  263. };
  264. case EVT_CAMERA_ON_POSITION:{
  265. if ( m_sRestSettings.camera_enabled ){
  266. DBC_SNAPSHOT_DATA * tData = (DBC_SNAPSHOT_DATA *) Evt->m_pEvtData;
  267. if( tData ){
  268. CSTR tN = m_pDBC->getNodeName( Evt->m_nSourceNodeID );
  269. if ( tN ) {
  270. if ( buildCameraEventMsg( tBuffer, tN, 1 ) ) {
  271. if ( m_sRestSettings.use_ssl ) {
  272. sprintf(tURL, "https://%s:%d%s", m_sRestSettings.server_ip, m_sRestSettings.port, m_sRestSettings.rest_api);
  273. } else {
  274. sprintf(tURL, "http://%s:%d%s", m_sRestSettings.server_ip, m_sRestSettings.port, m_sRestSettings.rest_api);
  275. };
  276. _send_request( tURL ,tBuffer );
  277. };
  278. free( tN );
  279. };
  280. };
  281. };
  282. break;
  283. };
  284. default: return true;
  285. };
  286. return true; // Never return false -> to propogate event to next dispatcher.
  287. }
  288. bool kernelModuleBosch::validate(){
  289. //check settings
  290. REST_SETTINGS * tData = m_pDBC->getBoschData( m_nDBAUXID );
  291. if( tData ){
  292. if( memcmp( &m_sRestSettings, tData, sizeof(REST_SETTINGS) ) != 0 ){
  293. memcpy( &m_sRestSettings, tData, sizeof(REST_SETTINGS) );
  294. };
  295. free( tData );
  296. return true;
  297. };
  298. return false;
  299. }
  300. #undef LOG
  301. #undef LOG_ERROR