| | |
| | | #include <logger.h>
|
| | | #include "TeleWrapper.h"
|
| | |
|
| | | extern "C" {
|
| | | #include "serial.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;
|
| | | }
|
| | | LOG_INFO << "TeleWrapper::start" << LOG_ENDL;
|
| | |
|
| | | return true;
|
| | | |
| | | fd = serialOpen(PORT, BAUD);
|
| | |
|
| | | running = true;
|
| | | pthread_mutex_init(&mutex, NULL); // 初始化互斥对象,动态加锁
|
| | | cond = PTHREAD_COND_INITIALIZER;
|
| | | 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;
|
| | | LOG_INFO << "TeleWrapper::stop" << LOG_ENDL;
|
| | |
|
| | | running = false;
|
| | | pthread_join(tel_thid, NULL);
|
| | | pthread_mutex_destroy(&mutex); |
| | | }
|
| | | if (!running)
|
| | | return;
|
| | |
|
| | | 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;
|
| | | }
|
| | | running = false;
|
| | | pthread_join(tel_thid, NULL);//阻塞线程,等待线程
|
| | | pthread_mutex_destroy(&mutex);
|
| | |
|
| | | void TeleWrapper::thread_pause()
|
| | | {
|
| | | if(!pthread_pause)
|
| | | { |
| | | pthread_mutex_lock(&mutex);
|
| | | pthread_pause = true;
|
| | | LOG_INFO << "thread pause " << LOG_ENDL;
|
| | | pthread_mutex_unlock(&mutex)
|
| | | }
|
| | | serialClose(fd);
|
| | | }
|
| | |
|
| | |
|
| | | void TeleWrapper::thread_resume()
|
| | |
|
| | |
|
| | |
|
| | | void TeleWrapper::popTask()
|
| | | {
|
| | | if(pthread_pause)
|
| | |
|
| | | TeleTask curTask = telQueue.front();
|
| | | telQueue.pop();
|
| | | switch(curTask.command)
|
| | | {
|
| | | pthread_mutex_lock(&mutex);
|
| | | pthread_pause = false;
|
| | | pthread_cond_broadcast(&cond_pause); |
| | | LOG_INFO << "thread wake " << LOG_ENDL;
|
| | | pthread_mutex_unlock(&mutex)
|
| | | case TeleTask::CALL: //打电话
|
| | | callNum(curTask.param);
|
| | | break;
|
| | | case TeleTask::HANGUP:
|
| | | // 挂机
|
| | | hang();
|
| | | break;
|
| | | }
|
| | | }
|
| | | void TeleWrapper::pushTask(TeleTask task)
|
| | | {
|
| | | telQueue.push(task);
|
| | | pthread_mutex_unlock(&mutex);
|
| | | //pthread_cond_signal(&cond);
|
| | | //pthread_mutex_lock(&mutex);
|
| | | //thread_resume(); //唤醒线程
|
| | | }
|
| | |
|
| | | void * TeleWrapper::tel_thd(void *arg) //线程函数
|
| | | {
|
| | | LOG_INFO << "TeleWrapper::tel_thd start" << LOG_ENDL;
|
| | |
|
| | | TeleWrapper& teleWrapper = *(TeleWrapper*)arg;
|
| | | while(teleWrapper.running)
|
| | | {
|
| | | if(teleWrapper.telQueue.empty())
|
| | | pthread_mutex_lock(&teleWrapper.mutex);
|
| | |
|
| | | void TeleWrapper::callNum( char phone)
|
| | | if(teleWrapper.telQueue.empty())
|
| | | continue;
|
| | |
|
| | | teleWrapper.popTask();
|
| | | }
|
| | |
|
| | | pthread_mutex_unlock(&teleWrapper.mutex);
|
| | | LOG_INFO << "TeleWrapper::tel_thd stop" << LOG_ENDL;
|
| | | }
|
| | |
|
| | | void TeleWrapper::callNum(char phone)
|
| | | {
|
| | | switch (phone)
|
| | | {
|
| | |
| | | }
|
| | | }
|
| | |
|
| | | void TeleWrapper::callNum( std::string phone)
|
| | | void TeleWrapper::callNum(const std::string phone)
|
| | | {
|
| | | fd = serialOpen(PORT, BAUD);
|
| | | serialWriteString(fd, "AA");//摘机
|
| | |
|
| | | serialWriteString(fd, "BA");
|
| | | serialWriteString(fd, "AA");//摘机
|
| | | sleep(1);
|
| | | while (*phone)
|
| | | const char* _phone = phone.c_str();
|
| | | while (*_phone)
|
| | | {
|
| | | sleep(1);
|
| | | callNum(*(phone++));
|
| | | callNum(*(_phone++));
|
| | | }
|
| | | }
|
| | |
|
| | | void TeleWrapper::hang()
|
| | | {
|
| | | serialWriteString(fd, "BA");
|
| | | serialClose(fd);
|
| | | sleep(40);
|
| | | serialWriteString(fd, "BA");
|
| | | }
|
| | |
|
| | |
|