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.

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