From 87156fa3adfa2e3232a6f6e612584aa8a4ebaea1 Mon Sep 17 00:00:00 2001
From: yy1717 <fctom1215@outlook.com>
Date: 星期二, 19 一月 2021 18:40:25 +0800
Subject: [PATCH] 添加模拟灯光

---
 lib/src/main/cpp/rtk_module/virtual_rtk.cpp |  134 +++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 124 insertions(+), 10 deletions(-)

diff --git a/lib/src/main/cpp/rtk_module/virtual_rtk.cpp b/lib/src/main/cpp/rtk_module/virtual_rtk.cpp
index b0e62e5..04e7a08 100644
--- a/lib/src/main/cpp/rtk_module/virtual_rtk.cpp
+++ b/lib/src/main/cpp/rtk_module/virtual_rtk.cpp
@@ -12,6 +12,7 @@
 #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)
 
@@ -22,12 +23,18 @@
     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);
-static void ConnectV(void);
-static void *VDataListenThread(void *p);
 
 void InitVirtualDevice(const char *domain_name, int port)
 {
@@ -36,7 +43,23 @@
     strcpy(VAddr.domain_name, domain_name);
     VAddr.port = port;
 
-    ConnectV();
+    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)
@@ -50,21 +73,111 @@
     return temp;
 }
 
+bool Virtual2IsConnected(void)
+{
+    bool temp;
+
+    do {
+        temp = virtRtkIsValid2;
+    } while (temp != virtRtkIsValid2);
+
+    return temp;
+}
+
 static void ConnectLater(union sigval sig) {
     AppTimer_delete(ConnectLater);
 
-    ConnectV();
+    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 ConnectV(void)
+static void TcpEventCallback(int stat, void *p, void *context)
 {
-    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);
+    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;
 
@@ -139,3 +252,4 @@
 
     pthread_exit(NULL);
 }
+*/
\ No newline at end of file

--
Gitblit v1.8.0