houxiao
2017-08-16 a188ad2e23ef190c8a044b17bc467fa3e2ae0ca5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <logger.h>
#include "TeleWrapper.h"
 
extern "C" {
#include "serial.h"
}
 
bool TeleWrapper::start()
{
    LOG_INFO << "TeleWrapper::start" << LOG_ENDL;
 
    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;
 
    running = false;
    pthread_join(tel_thid, NULL);//阻塞线程,等待线程
    pthread_mutex_destroy(&mutex);
 
    serialClose(fd);
}
 
 
 
 
 
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::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);
 
        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)
    {
        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(const std::string phone)
{
 
    serialWriteString(fd, "BA");
    serialWriteString(fd, "AA");//摘机
    sleep(1);
    const char* _phone = phone.c_str();
    while (*_phone)
    {
        sleep(1);
        callNum(*(_phone++));
    }
}
 
void TeleWrapper::hang()
{
    sleep(40);
    serialWriteString(fd, "BA");
}