From e0a8ae3449f90a6d88ad8d597af8fd27be7cfe6c Mon Sep 17 00:00:00 2001
From: qvyuanxin <qvyuanxin@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期四, 13 七月 2017 18:08:08 +0800
Subject: [PATCH] 

---
 VisitFace/RtspNativeCodec/app/src/main/cpp/TeleWrapper.h          |   66 +++++++++++
 VisitFace/RtspNativeCodec/app/src/main/cpp/TeleWrapper.cpp        |  161 ++++++++++++++++++++++++++
 VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNI.cpp |   79 +++----------
 VisitFace/RtspNativeCodec/app/src/main/cpp/CMakeLists.txt         |    1 
 4 files changed, 247 insertions(+), 60 deletions(-)

diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/CMakeLists.txt b/VisitFace/RtspNativeCodec/app/src/main/cpp/CMakeLists.txt
index c9c27b9..d2dd470 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/CMakeLists.txt
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/CMakeLists.txt
@@ -14,6 +14,7 @@
             DebugNetwork.cpp
             CaptureCamera.cpp
             serial.c
+            TeleWrapper.cpp
 
             "D:/workspace/proxy/RtspFace/PipeLine.cpp"
             "D:/workspace/proxy/RtspFace/Logger/src/logger.cc"
diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNI.cpp b/VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNI.cpp
index 797c337..db1440e 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNI.cpp
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/RtspNativeCodecJNI.cpp
@@ -17,12 +17,8 @@
 #include <stdlib.h>
 
 #include "DebugNetwork.h"
+#include "TeleWrapper.h" 
 
-
-extern "C"
-{
-#include "serial.h"
-}
 
 //#include <mediastreamer2/include/mediastreamer2/msjava.h>
 
@@ -34,6 +30,8 @@
 static std::string g_stface_license_str;
 
 CameraWrapper g_CameraWrappers[CAMERA_COUNT];
+
+TeleWrapper g_TeleWrapper;
 
 CaptureCamera g_CaptureCamera;
 
@@ -57,6 +55,7 @@
         g_CameraWrappers[i].pipeLineRender = new PipeLine;
         //PipeLine& pipeLine(*(g_CameraWrappers[i].pipeLineDecoderDetector));
     }
+    g_TeleWrapper.start();
 }
 
 void
@@ -736,55 +735,14 @@
 
 }
 
-int  fd =-1;
-#define PORT  "/dev/ttyS4"
-#define  BAUD 2400
 
 
-static void callNum(  char phone)
-{
-    switch (phone)
-    {
-        case '1':
-            serialWriteString(fd, "DA");//1
-            break;
-        case '2':
-            serialWriteString(fd, "DB");//2
-            break;
-        case '3':
-            serialWriteString(fd, "DC");//3
-            break;
-        case '4':
-            serialWriteString(fd, "DD");//4
-            break;
-        case '5':
-            serialWriteString(fd, "DE");//5
-            break;
-        case '6':
-            serialWriteString(fd, "DF");//6
-            break;
-        case '7':
-            serialWriteString(fd, "DG");//7
-            break;
-        case '8':
-            serialWriteString(fd, "DH");//8
-            break;
-        case '9':
-            serialWriteString(fd, "DI");//9
-            break;
-        case '0':
-            serialWriteString(fd, "DJ");//0
-            break;
-    }
-}
+
 
 bool Java_cn_com_basic_face_util_RtspFaceNative_telCall(JNIEnv *env, jclass clazz, jstring phone)
 {
-    int fd = serialOpen(PORT, BAUD);
-    serialWriteString(fd, "AA");
-    sleep(1);
-
-    std::string _phone;
+    
+    std::string _phone;  
     {
         const char *utfFunc = env->GetStringUTFChars(phone, NULL);
         _phone = utfFunc;
@@ -792,23 +750,24 @@
     }
 
     const char *phoneNum = _phone.c_str();
-
-    while (*phoneNum)
-    {
-        sleep(1);
-        callNum(*(phoneNum++));
-    }
-
-    return false;
+      TeleTask  task;
+      task.command= TeleTask::CALL;
+      task.param = phoneNum;
+      g_TeleWrapper.push(task);
 }
 
 void Java_cn_com_basic_face_util_RtspFaceNative_Hang(JNIEnv *env, jclass clazz)
 {
-
-    serialWriteString(fd, "BA");
-    serialClose(fd);
+     TeleTask  task;
+    task.command =TeleTask::HANGUP;
+    g_TeleWrapper.push(task);
 }
 
+void Java_cn_com_basic_face_util_RtspFaceNative_TelShutdown(JNIEnv *env, jclass clazz)
+{
+   LOG_DEBUG << "@@@ Java_cn_com_basic_face_util_RtspFaceNative_telShutdown" << LOG_ENDL;
+   g_TeleWrapper.stop();
+}
 void Java_cn_com_basic_face_util_RtspFaceNative_setFaceLabel(JNIEnv *env, jclass clazz, jint cameraIdx, jint stTrackId, jstring phoneNumber)
 {
     LOG_DEBUG << "@@@ Java_cn_com_basic_face_util_RtspFaceNative_setFaceLabel" << LOG_ENDL;
diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/TeleWrapper.cpp b/VisitFace/RtspNativeCodec/app/src/main/cpp/TeleWrapper.cpp
new file mode 100644
index 0000000..c0efaf0
--- /dev/null
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/TeleWrapper.cpp
@@ -0,0 +1,161 @@
+#include "TeleWrapper.h"
+
+bool TeleWrapper::start()
+{
+LOG_INFO << "TeleWrapper::start" << LOG_ENDL;
+running = true;
+ pthread_mutex_init(&mutex, NULL); // // 初始化互斥对象
+	int ret = pthread_create(&tel_thid, NULL, TeleWrapper::tel_thd, this);
+	if(ret != 0)
+	{
+		LOGP(ERROR, "pthread_create: %s/n", strerror(ret));
+		running = false;
+		return false;
+	}
+
+	return true;
+ 
+}
+
+void TeleWrapper::stop()
+{
+LOG_INFO << "TeleWrapper::stop" << LOG_ENDL;
+	
+	if (!running)
+		return;
+
+	running = false;
+	pthread_join(tel_thid, NULL);
+	pthread_mutex_destroy(&mutex);  
+}
+
+void TeleWrapper::pushTask(TeleTask task)
+{                                      
+      if(telQueue.empty()|| telQueue.back().command!=task.command) //队列为空 或者 放入任务和前一个任务不一致 时
+           {
+              telQueue.push(task); //放进队列 
+              thread_resume(); //唤醒线程
+           }
+  }
+      
+     void TeleWrapper::popTask()
+{
+           TeleTask curTask = telQueue.front();
+            telQueue.pop();
+            switch(curTask.command)
+            {
+                case TeleTask::CALL: //打电话
+                 callNum(curTask.param);
+                 break:
+                 case TeleTask::HANGUP:
+                 //  挂机
+                 hang();
+                 break;
+            }
+}
+ 
+     
+void TeleWrapper:: *tel_thd(void *arg)  //线程函数
+{
+	LOG_INFO << "TeleWrapper::tel_thd start" << LOG_ENDL;
+	
+		TeleWrapper& teleWrapper = *(TeleWrapper*)arg;
+     while(teleWrapper.running)
+     {
+         if(telQueue.empty())
+       {
+        thread_pause();
+        return ;
+       }
+          if(!pthread_pause)
+          {
+           popTask();
+        }
+     }
+   LOG_INFO << "TeleWrapper::tel_thd stop" << LOG_ENDL;
+}
+
+void TeleWrapper::thread_pause()
+{
+   if(!pthread_pause)
+    {   
+        pthread_mutex_lock(&mutex);
+        pthread_pause = true;
+        LOG_INFO << "thread pause " << LOG_ENDL;
+        pthread_mutex_unlock(&mutex)
+       }
+}
+
+
+void TeleWrapper::thread_resume()
+{
+    if(pthread_pause)
+    {
+      pthread_mutex_lock(&mutex);
+        pthread_pause = false;
+         pthread_cond_broadcast(&cond_pause);  
+        LOG_INFO << "thread wake " << LOG_ENDL;
+        pthread_mutex_unlock(&mutex)
+    }
+}
+
+
+
+void TeleWrapper::callNum(  char phone)
+{
+    switch (phone)
+    {
+        case '1':
+            serialWriteString(fd, "DA");//1
+            break;
+        case '2':
+            serialWriteString(fd, "DB");//2
+            break;
+        case '3':
+            serialWriteString(fd, "DC");//3
+            break;
+        case '4':
+            serialWriteString(fd, "DD");//4
+            break;
+        case '5':
+            serialWriteString(fd, "DE");//5
+            break;
+        case '6':
+            serialWriteString(fd, "DF");//6
+            break;
+        case '7':
+            serialWriteString(fd, "DG");//7
+            break;
+        case '8':
+            serialWriteString(fd, "DH");//8
+            break;
+        case '9':
+            serialWriteString(fd, "DI");//9
+            break;
+        case '0':
+            serialWriteString(fd, "DJ");//0
+            break;
+    }
+}
+
+void TeleWrapper::callNum( std::string phone)
+{
+   fd = serialOpen(PORT, BAUD);
+    serialWriteString(fd, "AA");//摘机
+    sleep(1);
+     while (*phone)
+    {
+        sleep(1);
+        callNum(*(phone++));
+    }
+}
+
+void TeleWrapper::hang()
+{
+      serialWriteString(fd, "BA");
+      serialClose(fd);
+}
+
+
+
+
diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/TeleWrapper.h b/VisitFace/RtspNativeCodec/app/src/main/cpp/TeleWrapper.h
new file mode 100644
index 0000000..c9bc047
--- /dev/null
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/TeleWrapper.h
@@ -0,0 +1,66 @@
+#ifndef __TeleWrapper_H__
+#define __TeleWrapper_H__
+
+#include <queue>
+#include <pthread.h>
+#include <unistd.h>  
+#include <stdlib.h> 
+#include "serial.h"
+ 
+
+#define PORT  "/dev/ttyS4"
+#define  BAUD 2400
+
+
+typedef struct TeleTask
+{
+	enum Tel
+	{
+		CALL,
+		HANGUP
+	};
+	Tel command;
+	std::string param;
+}TeleTask;
+
+class TeleWrapper
+{
+public:
+	TeleWrapper() {}
+	~TeleWrapper() {}
+
+	bool start();
+	void stop();
+	void pushTask(TeleTask task);
+	void popTask();
+	
+	
+  void call(std::string phone);
+  void hang();
+
+	pthread_t tel_thid;
+  bool running;
+private:
+
+  int  fd =-1;
+  
+	std::queue<TeleTask> telQueue;
+	void *tel_thd(void *arg) ;
+ 
+  bool pthread_pause = false;  
+  
+  pthread_cond_t cond_pause = PTHREAD_COND_INITIALIZER;  
+  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+  
+  void thread_pause();
+  void thread_resume();
+  
+  
+
+ void callNum(  char phone);
+
+};
+
+
+
+#endif 

--
Gitblit v1.8.0