From 63f7c1fb25fd50d78f7430cb50ae908530529806 Mon Sep 17 00:00:00 2001 From: yy1717 <fctom1215@outlook.com> Date: 星期三, 08 一月 2020 13:08:23 +0800 Subject: [PATCH] 实现rtk平台基础配置 --- lib/src/main/cpp/utils/num.cpp | 11 + lib/src/main/cpp/rtk_platform/platform.h | 2 lib/src/main/cpp/master/comm_if.cpp | 2 lib/src/main/cpp/native-lib.cpp | 222 --------------------------- lib/src/main/cpp/utils/num.h | 1 lib/src/main/cpp/rtk_platform/platform.cpp | 149 ++++++++++++------ lib/src/main/java/com/anyun/exam/lib/RemoteService.java | 42 ----- 7 files changed, 115 insertions(+), 314 deletions(-) diff --git a/lib/src/main/cpp/master/comm_if.cpp b/lib/src/main/cpp/master/comm_if.cpp index 9eed093..6b481b0 100644 --- a/lib/src/main/cpp/master/comm_if.cpp +++ b/lib/src/main/cpp/master/comm_if.cpp @@ -85,7 +85,7 @@ writer.EndObject(); - SendMsgToMainProc(ID_SM_RTK_PLAT_REG, sb.GetString()); + SendMsgToMainProc(ID_SM_RTK_PLAT_LOGIN, sb.GetString()); } void MA_MainProcMsgEntry(int cmd, const char *value) diff --git a/lib/src/main/cpp/native-lib.cpp b/lib/src/main/cpp/native-lib.cpp index 211125f..fad1480 100644 --- a/lib/src/main/cpp/native-lib.cpp +++ b/lib/src/main/cpp/native-lib.cpp @@ -22,51 +22,6 @@ const int RTK_PLATFORM_PORT = 12125; const uint8_t phone[] = {0x20,0x19,0x10,0x15,0x00,0x00,0x00,0x01}; -#define IMEI_LENGTH 15 - -char * GetImei(void) -{ - JNIEnv *env; - static char imei[IMEI_LENGTH + 1] = {0}; - - LOGD("JNI_GetImei"); - - if (strlen(imei) == 0) { - bool ready_in_java_env = false; - - if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) { - // Attach涓荤嚎绋� - if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) { - LOGE("%s: AttachCurrentThread() failed", __FUNCTION__); - return imei; - } - } else { - ready_in_java_env = true; - } - - jclass cls = env->GetObjectClass(sg_obj); - jmethodID fun = env->GetMethodID(cls, "javaGetImei", "()Ljava/lang/String;"); - - jstring ret = (jstring) env->CallObjectMethod(sg_obj, fun); - - if (ret != NULL) { - const char *pret = env->GetStringUTFChars(ret, JNI_FALSE); - strcpy(imei, pret); - } - - env->DeleteLocalRef(cls); - - if (!ready_in_java_env) { - //Detach涓荤嚎绋� - if (sg_jvm->DetachCurrentThread() != JNI_OK) { - LOGE("%s: DetachCurrentThread() failed", __FUNCTION__); - return imei; - } - } - } - return imei; -} - int DESEncrypt(const uint8_t *key, int key_length, const uint8_t *plaintext, int plaintext_length, uint8_t **ciphertext) @@ -116,183 +71,6 @@ } } return ciphertext_length; -} - -void SetPlatformKey(const uint8_t *key, int length) -{ - JNIEnv *env; - bool ready_in_java_env = false; - - LOGD("JNI_SetPlatformKey"); - - if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) { - // Attach涓荤嚎绋� - if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) { - LOGE("%s: AttachCurrentThread() failed", __FUNCTION__); - return; - } - } else { - ready_in_java_env = true; - } - - jbyteArray jni_cmd = env->NewByteArray(length); - env->SetByteArrayRegion(jni_cmd, 0, length, (jbyte *) key); - - jclass cls = env->GetObjectClass(sg_obj); - jmethodID fun = env->GetMethodID(cls, "SetPlatformKey", "([B)V"); - - env->CallVoidMethod(sg_obj, fun, jni_cmd); - - env->DeleteLocalRef(jni_cmd); - env->DeleteLocalRef(cls); - - if (!ready_in_java_env) { - //Detach涓荤嚎绋� - if (sg_jvm->DetachCurrentThread() != JNI_OK) { - LOGE("%s: DetachCurrentThread() failed", __FUNCTION__); - } - } -} - -void DelPlatformKey(void) -{ - JNIEnv *env; - bool ready_in_java_env = false; - - if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) { - // Attach涓荤嚎绋� - if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) { - LOGE("%s: AttachCurrentThread() failed", __FUNCTION__); - return; - } - } else { - ready_in_java_env = true; - } - - jclass cls = env->GetObjectClass(sg_obj); - jmethodID fun = env->GetMethodID(cls, "DeletePlatformKey", "()V"); - - env->CallVoidMethod(sg_obj, fun); - - env->DeleteLocalRef(cls); - - if (!ready_in_java_env) { - //Detach涓荤嚎绋� - if (sg_jvm->DetachCurrentThread() != JNI_OK) { - LOGE("%s: DetachCurrentThread() failed", __FUNCTION__); - } - } -} - -int GetPlatformKey(uint8_t *pkey) -{ - JNIEnv *env; - bool ready_in_java_env = false; - uint8_t key[64] = {0}; - int key_len = 0; - - LOGD("JNI_GetPlatformKey"); - - if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) { - // Attach涓荤嚎绋� - if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) { - LOGE("%s: AttachCurrentThread() failed", __FUNCTION__); - return 0; - } - } else { - ready_in_java_env = true; - } - - jclass cls = env->GetObjectClass(sg_obj); - jmethodID fun = env->GetMethodID(cls, "GetPlatformKey", "()[B"); - - jbyteArray jni_cmd = (jbyteArray) env->CallObjectMethod(sg_obj, fun); - - if (jni_cmd != NULL) { - key_len = env->GetArrayLength(jni_cmd); - if (key_len <= sizeof(key)) { - env->GetByteArrayRegion(jni_cmd, 0, key_len, (jbyte *) key); - memcpy(pkey, key, key_len); - } else { - key_len = 0; - } - } - - env->DeleteLocalRef(cls); - - if (!ready_in_java_env) { - //Detach涓荤嚎绋� - if (sg_jvm->DetachCurrentThread() != JNI_OK) { - LOGE("%s: DetachCurrentThread() failed", __FUNCTION__); - } - } - return key_len; -} - -void SetSharedValue(const char *key, int value) -{ - JNIEnv *env; - bool ready_in_java_env = false; - - LOGD("JNI_SetSharedValue"); - - if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) { - // Attach涓荤嚎绋� - if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) { - LOGE("%s: AttachCurrentThread() failed", __FUNCTION__); - return; - } - } else { - ready_in_java_env = true; - } - - jclass cls = env->GetObjectClass(sg_obj); - jmethodID fun = env->GetMethodID(cls, "SetSharedValue", "(Ljava/lang/String;I)V"); - - env->CallVoidMethod(sg_obj, fun, env->NewStringUTF(key), value); - - env->DeleteLocalRef(cls); - - if (!ready_in_java_env) { - //Detach涓荤嚎绋� - if (sg_jvm->DetachCurrentThread() != JNI_OK) { - LOGE("%s: DetachCurrentThread() failed", __FUNCTION__); - } - } -} - -int GetSharedValue(const char *key) -{ - JNIEnv *env; - bool ready_in_java_env = false; - int value = 0; - - LOGD("JNI_GetSharedValue"); - - if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) { - // Attach涓荤嚎绋� - if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) { - LOGE("%s: AttachCurrentThread() failed", __FUNCTION__); - return value; - } - } else { - ready_in_java_env = true; - } - - jclass cls = env->GetObjectClass(sg_obj); - jmethodID fun = env->GetMethodID(cls, "GetSharedValue", "(Ljava/lang/String;)I"); - - value = env->CallIntMethod(sg_obj, fun, env->NewStringUTF(key)); - - env->DeleteLocalRef(cls); - - if (!ready_in_java_env) { - //Detach涓荤嚎绋� - if (sg_jvm->DetachCurrentThread() != JNI_OK) { - LOGE("%s: DetachCurrentThread() failed", __FUNCTION__); - } - } - return value; } void TextSpeak(const char *text) 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) diff --git a/lib/src/main/cpp/rtk_platform/platform.h b/lib/src/main/cpp/rtk_platform/platform.h index 736f9d2..2175133 100644 --- a/lib/src/main/cpp/rtk_platform/platform.h +++ b/lib/src/main/cpp/rtk_platform/platform.h @@ -29,7 +29,7 @@ void InitPlatform(const uint8_t *phone, const char *domain_name, int port); void ConfigPlatform(const rtk_platform_cfg_t *p); -void PlatformStatusChanged(uint32_t event); +void PlatformStatusChanged(uint32_t event, const uint8_t *data, int length); int WritePlatform(const uint8_t * buf, uint32_t len); void DeviceRegisterCallback(uint8_t res, const uint8_t *data, int length); void DeviceLoginCallback(uint8_t res); diff --git a/lib/src/main/cpp/utils/num.cpp b/lib/src/main/cpp/utils/num.cpp index fe14678..0019ccf 100644 --- a/lib/src/main/cpp/utils/num.cpp +++ b/lib/src/main/cpp/utils/num.cpp @@ -74,3 +74,14 @@ return true; } + +int BitCount(uint32_t n) +{ + n = (n &0x55555555) + ((n >>1) &0x55555555); + n = (n &0x33333333) + ((n >>2) &0x33333333); + n = (n &0x0f0f0f0f) + ((n >>4) &0x0f0f0f0f); + n = (n &0x00ff00ff) + ((n >>8) &0x00ff00ff); + n = (n &0x0000ffff) + ((n >>16) &0x0000ffff); + + return n ; +} diff --git a/lib/src/main/cpp/utils/num.h b/lib/src/main/cpp/utils/num.h index 45e1937..174d46f 100644 --- a/lib/src/main/cpp/utils/num.h +++ b/lib/src/main/cpp/utils/num.h @@ -9,5 +9,6 @@ long str2int(const uint8_t *s, uint16_t length); bool str2float(double *f, const uint8_t *s, uint16_t length); +int BitCount(uint32_t n); #endif //RTKDRIVERTEST_NUM_H diff --git a/lib/src/main/java/com/anyun/exam/lib/RemoteService.java b/lib/src/main/java/com/anyun/exam/lib/RemoteService.java index 05a24c8..2b2893e 100644 --- a/lib/src/main/java/com/anyun/exam/lib/RemoteService.java +++ b/lib/src/main/java/com/anyun/exam/lib/RemoteService.java @@ -25,7 +25,6 @@ */ public class RemoteService extends Service { private static final String TAG = "RemoteService"; - private final static String LOAD_KEY = "RTKBaseStation"; /**鏈嶅姟鏄惁閿�姣佹爣蹇�*/ private AtomicBoolean mIsServiceDestroyed = new AtomicBoolean(false); private RemoteCallbackList<IListenerInterface> mListenerList = new RemoteCallbackList(); @@ -103,10 +102,6 @@ } } - public String javaGetImei() { - return NetUtils.getDeviceId(getApplicationContext()); - } - public String javaDESEncrypt(String plaintext, String key) { try { byte []des = DESUtil.encrypt(plaintext.getBytes("utf-8"), key); @@ -124,43 +119,6 @@ } return null; - } - - public void SetPlatformKey(byte []key) { - String params = Base64.encodeToString(key, Base64.DEFAULT); - - SharedPreferences sharedPreferences = getSharedPreferences(LOAD_KEY, MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putString("platform_key", params); - editor.commit(); - } - - public byte[] GetPlatformKey() { - SharedPreferences sharedPreferences = getSharedPreferences(LOAD_KEY, MODE_PRIVATE); - String params = sharedPreferences.getString("platform_key", ""); - if (params.equals("")) { - return null; - } - return Base64.decode(params, Base64.DEFAULT); - } - - public void DeletePlatformKey() { - SharedPreferences sharedPreferences = getSharedPreferences(LOAD_KEY, MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putString("platform_key", ""); - editor.commit(); - } - - public void SetSharedValue(String key, int value) { - SharedPreferences sharedPreferences = getSharedPreferences(LOAD_KEY, MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putInt(key, value); - editor.commit(); - } - - public int GetSharedValue(String key) { - SharedPreferences sharedPreferences = getSharedPreferences(LOAD_KEY, MODE_PRIVATE); - return sharedPreferences.getInt(key, 0); } public void TextSpeak(String text) { -- Gitblit v1.8.0