From 2bd86aabc7a0eb9d0f3eaeaef7f4eba63f62bbaa Mon Sep 17 00:00:00 2001
From: lizhanwei <Dana_Lee1016@126.com>
Date: 星期三, 08 一月 2020 14:17:58 +0800
Subject: [PATCH] Merge b

---
 lib/src/main/cpp/rtk_platform/platform.cpp |  149 +++++++++++++++++++++++++++++++++----------------
 1 files changed, 101 insertions(+), 48 deletions(-)

diff --git a/lib/src/main/cpp/rtk_platform/platform.cpp b/lib/src/main/cpp/rtk_platform/platform.cpp
index 30dec58..e0072ab 100644
--- a/lib/src/main/cpp/rtk_platform/platform.cpp
+++ b/lib/src/main/cpp/rtk_platform/platform.cpp
@@ -18,6 +18,7 @@
 #include "../mcu/mcu_if.h"
 #include "../master/comm_if.h"
 #include "../utils/xconvert.h"
+#include "../utils/num.h"
 
 #define PARSE_BUFF_SIZE         4096
 
@@ -54,6 +55,8 @@
 
 struct platformSocket exceptSocket, currSocket;
 static uint32_t eventMask;
+static void *eventData[32];
+static int eventDataLength[32];
 static sem_t sem_status_changed;
 
 static bool requestPlatformSendRtk = false;
@@ -62,11 +65,14 @@
 static pthread_mutex_t platform_tx_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t events_mutex = PTHREAD_MUTEX_INITIALIZER;
 
+static void ReqRtkPlatformConfigTimeout(union sigval sig);
+
 static void ConnectPlatform(const char *domain_name, int port);
 static void ConnectPlatformLater(union sigval sig);
 
 static void *PlatformDataListenThread(void *p);
 static void *StatusListenThread(void *p);
+static void PlatformChangeEntry(uint32_t events, const uint8_t *data, int length);
 
 static void RegisterPlatform(void);
 static void RegisterPlatformTimeout(union sigval sig);
@@ -82,6 +88,8 @@
     pthread_mutex_init(&events_mutex, NULL);
 
     eventMask = 0;
+    memset(eventData, 0, sizeof(eventData));
+    memset(eventDataLength, 0, sizeof(eventDataLength));
     platform_tcp_fd = -1;
 
     memset(&currSocket, 0, sizeof(currSocket));
@@ -98,11 +106,20 @@
     pthread_create(&pid, &attr, StatusListenThread, NULL);
 
     MA_ReqRtkPlatformConfig();
+    AppTimer_add(ReqRtkPlatformConfigTimeout, D_SEC(2));
+}
+
+static void ReqRtkPlatformConfigTimeout(union sigval sig)
+{
+    AppTimer_delete(ReqRtkPlatformConfigTimeout);
+    MA_ReqRtkPlatformConfig();
+    AppTimer_add(ReqRtkPlatformConfigTimeout, D_SEC(2));
 }
 
 void ConfigPlatform(const rtk_platform_cfg_t *p)
 {
     DEBUG("ConfigPlatform");
+    AppTimer_delete(ReqRtkPlatformConfigTimeout);
 
     strcpy(exceptSocket.domain_name, p->domain_name);
     exceptSocket.port = p->port;
@@ -132,16 +149,29 @@
     pthread_mutex_unlock(&platform_tx_mutex);
 }
 
-void PlatformStatusChanged(uint32_t event)
+void PlatformStatusChanged(uint32_t event, const uint8_t *data, int length)
 {
     pthread_mutex_lock(&events_mutex);
     eventMask |= event;
     pthread_mutex_unlock(&events_mutex);
+
+    if (data != NULL && length > 0) {
+        int x = BitCount(event-1);
+
+        if (eventData[x] != NULL) free(eventData[x]);
+        eventData[x] = malloc(length);
+        memcpy(eventData[x], data, length);
+        eventDataLength[x] = length;
+    }
+
     sem_post(&sem_status_changed);
 }
 
 static void ConnectPlatform(const char *domain_name, int port)
 {
+    if (domain_name == NULL || strlen(domain_name) == 0 || port == 0)
+        return;
+
     DEBUG("ConnectPlatform %s: %d", domain_name, port);
     // TODO
     struct platformSocketInfo *ptr = (struct platformSocketInfo *)malloc(sizeof(struct platformSocketInfo));
@@ -175,53 +205,77 @@
         eventMask = 0;
         pthread_mutex_unlock(&events_mutex);
 
-        if (events & PLATFORM_CONNECT_EVT) {
-            char out[64];
-            sprintf(out, "骞冲彴杩炴帴鎴愬姛 %s:%d", currSocket.domain_name, currSocket.port);
-            DisplayText(out);
-            MA_RtkPlatformConnect(1, currSocket.domain_name, currSocket.port);
-
-            platformStatus.connected = 1;
-            if (!platformStatus.registed || platformStatus.platformKeyLength == 0) {
-                RegisterPlatform();
-            } else if (!platformStatus.login) {
-                LoginPlatform();
+        while (events > 0) {
+            int x = BitCount(((events - 1) ^ events) - 1);
+            PlatformChangeEntry(BV(x), (uint8_t *)eventData[x], eventDataLength[x]);
+            if (eventData[x] != NULL) {
+                free(eventData[x]);
+                eventData[x] = NULL;
+                eventDataLength[x] = 0;
             }
+            events &= events - 1;
         }
+    }
+}
 
-        if (events & PLATFORM_DISCONNECT_EVT) {
-            char out[64];
-            sprintf(out, "骞冲彴鏂紑 %s:%d", currSocket.domain_name, currSocket.port);
-            DisplayText(out);
-            MA_RtkPlatformConnect(0, currSocket.domain_name, currSocket.port);
+static void PlatformChangeEntry(uint32_t events, const uint8_t *data, int length)
+{
+    if (events & PLATFORM_CONNECT_EVT) {
+        char out[64];
+        sprintf(out, "骞冲彴杩炴帴鎴愬姛 %s:%d", currSocket.domain_name, currSocket.port);
+        DisplayText(out);
+        MA_RtkPlatformConnect(1, currSocket.domain_name, currSocket.port);
 
-            AppTimer_delete(ConnectPlatformLater);
-            AppTimer_add(ConnectPlatformLater, D_SEC(2));
-
-            platformStatus.login = 0;
-            platformStatus.connected = 0;
-
-            AppTimer_delete(TriggerHeartbeat);
-            AppTimer_delete(RegisterPlatformTimeout);
-            AppTimer_delete(LoginPlatformTimeout);
+        platformStatus.connected = 1;
+        if (!platformStatus.registed || platformStatus.platformKeyLength == 0) {
+            RegisterPlatform();
+        } else if (!platformStatus.login) {
+            LoginPlatform();
         }
-        if (events & PLATFORM_REGISTER_EVT) {
-            DEBUG("PLATFORM_REGISTER_EVT");
+    }
 
-            platformStatus.login = 0;
-            DEBUG("platformStatus.platformKeyLength = %d", platformStatus.platformKeyLength);
+    if (events & PLATFORM_DISCONNECT_EVT) {
+        char out[64];
+        sprintf(out, "骞冲彴鏂紑 %s:%d", currSocket.domain_name, currSocket.port);
+        DisplayText(out);
+        MA_RtkPlatformConnect(0, currSocket.domain_name, currSocket.port);
 
-            if (platformStatus.registed && platformStatus.platformKeyLength > 0) {
-                LoginPlatform();
-            }
+        AppTimer_delete(ConnectPlatformLater);
+        AppTimer_add(ConnectPlatformLater, D_SEC(2));
+
+        platformStatus.login = 0;
+        platformStatus.connected = 0;
+
+        AppTimer_delete(TriggerHeartbeat);
+        AppTimer_delete(RegisterPlatformTimeout);
+        AppTimer_delete(LoginPlatformTimeout);
+    }
+    if (events & PLATFORM_REGISTER_EVT) {
+        DEBUG("PLATFORM_REGISTER_EVT");
+
+        platformStatus.login = 0;
+        if (data[0] == 0) {
+            platformStatus.registed = 1;
+            platformStatus.platformKeyLength = length - 1;
+            memcpy(platformStatus.platformKey, data+1, length-1);
+            LoginPlatform();
+        } else {
+            platformStatus.registed = 0;
         }
-        if (events & PLATFORM_LOGIN_EVT) {
-            DEBUG("PLATFORM_LOGIN_EVT");
+        MA_RtkPlatformRegister(data[0], data + 1, length - 1);
+    }
+    if (events & PLATFORM_LOGIN_EVT) {
+        DEBUG("PLATFORM_LOGIN_EVT");
+
+        if (data[0] == 0) {
             platformStatus.login = 1;
             requestPlatformSendRtk = true;
             AppTimer_delete(TriggerHeartbeat);
             AppTimer_add(TriggerHeartbeat, D_SEC(30));
+        } else {
+            platformStatus.login = 0;
         }
+        MA_RtkPlatformLogin(data[0]);
     }
 }
 
@@ -248,7 +302,7 @@
     pthread_mutex_unlock(&platform_tx_mutex);
 
     if (fd > 0) {
-        PlatformStatusChanged(PLATFORM_CONNECT_EVT);
+        PlatformStatusChanged(PLATFORM_CONNECT_EVT, NULL, 0);
     }
 
     while (fd > 0) {
@@ -282,7 +336,7 @@
     pthread_mutex_unlock(&platform_tx_mutex);
     free(ptr);
 
-    PlatformStatusChanged(PLATFORM_DISCONNECT_EVT);
+    PlatformStatusChanged(PLATFORM_DISCONNECT_EVT, NULL, 0);
     pthread_exit(NULL);
 }
 
@@ -353,27 +407,26 @@
 
 void DeviceRegisterCallback(uint8_t res, const uint8_t *data, int length)
 {
+    uint8_t az[16];
     AppTimer_delete(RegisterPlatformTimeout);
-    if (res != 0) {
 
+    az[0] = res;
+    if (res == 0) {
+        memcpy(az+1, data, length);
+        PlatformStatusChanged(PLATFORM_REGISTER_EVT, az, length + 1);
     } else {
-        TextSpeak("缁堢娉ㄥ唽鎴愬姛");
-        DisplayText("缁堢娉ㄥ唽鎴愬姛");
-        SetPlatformKey(data, length);
-        PlatformStatusChanged(PLATFORM_REGISTER_EVT);
+        PlatformStatusChanged(PLATFORM_REGISTER_EVT, az, 1);
     }
 }
 
 void DeviceLoginCallback(uint8_t res)
 {
+    uint8_t az;
     AppTimer_delete(LoginPlatformTimeout);
-    if (res != 0) {
 
-    } else {
-        TextSpeak("缁堢鐧诲綍鎴愬姛");
-        DisplayText("缁堢鐧诲綍鎴愬姛");
-        PlatformStatusChanged(PLATFORM_LOGIN_EVT);
-    }
+    az = res;
+
+    PlatformStatusChanged(PLATFORM_LOGIN_EVT, &az, 1);
 }
 
 void ReceivedRtk(const uint8_t *data, int length)

--
Gitblit v1.8.0