#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);
|
}
|
|
|
|
|