yy1717
2021-01-19 87156fa3adfa2e3232a6f6e612584aa8a4ebaea1
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// Created by YY on 2020/1/20.
//
 
#include <pthread.h>
#include <cstdlib>
#include <cstring>
#include "virtual_rtk.h"
#include "../jni_log.h"
#include "../common/net.h"
#include "../native-lib.h"
#include "../common/apptimer.h"
#include "../defs.h"
#include "parse_gps.h"
#include "../mcu/mcu_if.h"
 
#define DEBUG(fmt, args...)     LOGD("<virtual_device> <%s>: " fmt, __func__, ##args)
 
#define PARSE_BUFF_SIZE         4096
 
struct vSocket {
    char domain_name[32];
    int port;
} VAddr;
 
static CTcpPort *ctp = NULL, *ctp2 = NULL;
static bool virtRtkIsValid = false;
static int connectCnt = 0;
static void TcpEventCallback(int stat, void *p, void *context);
static void TcpDataCallback(void *buffer, int length, void *p, void *context);
 
static bool virtRtkIsValid2 = false;
static int connectCnt2 = 0;
static void TcpEventCallback2(int stat, void *p, void *context);
static void TcpDataCallback2(void *buffer, int length, void *p, void *context);
 
static void ConnectLater(union sigval sig);
 
void InitVirtualDevice(const char *domain_name, int port)
{
    DEBUG("InitVirtualDevice %s: %d", domain_name, port);
 
    strcpy(VAddr.domain_name, domain_name);
    VAddr.port = port;
 
    if (ctp == NULL) {
        ctp = new CTcpPort();
 
        ctp->set_event_callback(TcpEventCallback, NULL);
        ctp->set_data_callback(TcpDataCallback, NULL);
 
        ctp->OpenTcpPort(domain_name, port);
    }
 
    if (ctp2 == NULL) {
        ctp2 = new CTcpPort();
 
        ctp2->set_event_callback(TcpEventCallback2, NULL);
        ctp2->set_data_callback(TcpDataCallback2, NULL);
 
        ctp2->OpenTcpPort(domain_name, port + 1);
    }
}
 
bool VirtualIsConnected(void)
{
    bool temp;
 
    do {
        temp = virtRtkIsValid;
    } while (temp != virtRtkIsValid);
 
    return temp;
}
 
bool Virtual2IsConnected(void)
{
    bool temp;
 
    do {
        temp = virtRtkIsValid2;
    } while (temp != virtRtkIsValid2);
 
    return temp;
}
 
static void ConnectLater(union sigval sig) {
    AppTimer_delete(ConnectLater);
 
    if (sig.sival_int == 1) {
        if (ctp != NULL) {
            ctp->OpenTcpPort(VAddr.domain_name, VAddr.port);
        }
    } else {
        if (ctp2 != NULL) {
            ctp2->OpenTcpPort(VAddr.domain_name, VAddr.port + 1);
        }
    }
}
 
static void TcpEventCallback(int stat, void *p, void *context)
{
    CTcpPort *pTcpPort = (CTcpPort *)p;
 
    if (stat == 0) {
        DEBUG("虚拟平台连接成功 %s: %d", pTcpPort->m_sIp.c_str(), pTcpPort->m_nPort);
        virtRtkIsValid = true;
        connectCnt = 0;
        PlayTTS("模拟器连接", NULL);
    } else {
        DEBUG("虚拟平台连接失败");
        virtRtkIsValid = false;
        connectCnt++;
 
        if (connectCnt < 3) {
            AppTimer_add(ConnectLater, D_SEC(3), 1);
        }
        PlayTTS("模拟器断开", NULL);
    }
}
 
static void TcpDataCallback(void *buffer, int length, void *p, void *context)
{
    static uint8_t RxBuf[PARSE_BUFF_SIZE];
    static int RxBufLen = 0;
 
    if (length > 0) {
        memcpy(RxBuf + RxBufLen, buffer, length);
        RxBufLen += length;
 
        const uint8_t *ptr = parseGPS(RxBuf, RxBuf + RxBufLen);
 
        if(ptr != RxBuf) {
            memcpy(RxBuf, ptr, RxBufLen - (ptr - RxBuf));
            RxBufLen -= ptr - RxBuf;
        } else if(RxBufLen == PARSE_BUFF_SIZE) {        //填满了,且没有一个\r,都抛弃
            DEBUG("Parse GPS error");
            RxBufLen = 0;
        }
    }
}
 
static void TcpEventCallback2(int stat, void *p, void *context)
{
    CTcpPort *pTcpPort = (CTcpPort *)p;
 
    if (stat == 0) {
        DEBUG("灯光虚拟平台连接成功 %s: %d", pTcpPort->m_sIp.c_str(), pTcpPort->m_nPort);
        virtRtkIsValid2 = true;
        connectCnt2 = 0;
        PlayTTS("灯光模拟器连接", NULL);
    } else {
        DEBUG("灯光虚拟平台连接失败");
        virtRtkIsValid2 = false;
        connectCnt2++;
 
        if (connectCnt2 < 3) {
            AppTimer_add(ConnectLater, D_SEC(3), 2);
        }
        PlayTTS("灯光模拟器断开", NULL);
    }
}
 
static void TcpDataCallback2(void *buffer, int length, void *p, void *context)
{
    static uint8_t RxBuf[PARSE_BUFF_SIZE];
    static int RxBufLen = 0;
 
    if (length > 0) {
        memcpy(RxBuf + RxBufLen, buffer, length);
        RxBufLen += length;
 
        if (RxBufLen > 0) {
            ParseMcu(RxBuf, RxBufLen);
            RxBufLen = 0;
        }
    }
}
 
/*
static void *VDataListenThread(void *p) {
    struct vSocket *vs = (struct vSocket *)p;
 
    uint8_t RxBuf[PARSE_BUFF_SIZE];
 
    int fds_ret;
    struct timeval tv;
    fd_set rdfds;
    fd_set exfds;
 
    int fd = -1;
    int RxBufLen = 0;
 
    connectCnt++;
 
    fd = ConnectTCP(vs->domain_name, vs->port);
 
    if (fd > 0) {
        DEBUG("虚拟平台连接成功");
        virtRtkIsValid = true;
        connectCnt = 0;
        PlayTTS("模拟器连接", NULL);
    } else {
        DEBUG("虚拟平台连接失败");
    }
 
    while (fd > 0) {
        tv.tv_sec = 5;
        tv.tv_usec = 0;
        FD_ZERO(&rdfds); //clean
        FD_SET(fd, &rdfds); //set
 
        fds_ret = select(fd + 1, &rdfds, NULL, NULL, &tv);
 
        if (fds_ret < 0) {
            break;
        } else if(fds_ret == 0) {
            //Occur failure(such as line disconnect)
        } else if(FD_ISSET(fd, &rdfds)) {
            int lx = ReadTCP(fd, RxBuf + RxBufLen, sizeof(RxBuf) - RxBufLen);
 
            if (lx < 0) {
                break;
            } else if (lx > 0) {
                RxBufLen += lx;
 
                const uint8_t *ptr = parseGPS(RxBuf, RxBuf + RxBufLen);
 
                if(ptr != RxBuf) {
                    memcpy(RxBuf, ptr, RxBufLen - (ptr - RxBuf));
                    RxBufLen -= ptr - RxBuf;
                } else if(RxBufLen == PARSE_BUFF_SIZE) {        //填满了,且没有一个\r,都抛弃
                    DEBUG("Parse GPS error");
                    RxBufLen = 0;
                }
            }
        }
    }
 
    if (fd > 0) {
        DEBUG("虚拟平台断开");
        DisconnectTCP(fd);
        PlayTTS("模拟器断开", NULL);
    }
    virtRtkIsValid = false;
 
    if (connectCnt < 5) {
        AppTimer_add(ConnectLater, D_SEC(3));
    }
 
    DEBUG("虚拟平台线程退出");
 
    pthread_exit(NULL);
}
*/