yy1717
2020-11-24 6e0f29b08a040d14576d7053c1206a8439936570
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
//
// 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"
 
#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 bool virtRtkIsValid = false;
static int connectCnt = 0;
 
static void ConnectLater(union sigval sig);
static void ConnectV(void);
static void *VDataListenThread(void *p);
 
void InitVirtualDevice(const char *domain_name, int port)
{
    DEBUG("InitVirtualDevice %s: %d", domain_name, port);
 
    strcpy(VAddr.domain_name, domain_name);
    VAddr.port = port;
 
    ConnectV();
}
 
bool VirtualIsConnected(void)
{
    bool temp;
 
    do {
        temp = virtRtkIsValid;
    } while (temp != virtRtkIsValid);
 
    return temp;
}
 
static void ConnectLater(union sigval sig) {
    AppTimer_delete(ConnectLater);
 
    ConnectV();
}
 
static void ConnectV(void)
{
    pthread_t pid;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//detached
    pthread_create(&pid, &attr, VDataListenThread, &VAddr);
}
 
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) {
        DisconnectTCP(fd);
    }
    virtRtkIsValid = false;
 
    if (connectCnt < 5) {
        AppTimer_add(ConnectLater, D_SEC(3));
    }
 
//    PlayTTS("模拟器断开", NULL);
 
    pthread_exit(NULL);
}