| | |
| | | |
| | | 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) |
| | |
| | | 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) |
| | |
| | | } |
| | | } |
| | | 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) |
| | |
| | | #include "../mcu/mcu_if.h" |
| | | #include "../master/comm_if.h" |
| | | #include "../utils/xconvert.h" |
| | | #include "../utils/num.h" |
| | | |
| | | #define PARSE_BUFF_SIZE 4096 |
| | | |
| | |
| | | |
| | | 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; |
| | |
| | | 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); |
| | |
| | | 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)); |
| | |
| | | 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; |
| | |
| | | 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)); |
| | |
| | | eventMask = 0; |
| | | pthread_mutex_unlock(&events_mutex); |
| | | |
| | | 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; |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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); |
| | |
| | | DEBUG("PLATFORM_REGISTER_EVT"); |
| | | |
| | | platformStatus.login = 0; |
| | | DEBUG("platformStatus.platformKeyLength = %d", platformStatus.platformKeyLength); |
| | | |
| | | if (platformStatus.registed && platformStatus.platformKeyLength > 0) { |
| | | if (data[0] == 0) { |
| | | platformStatus.registed = 1; |
| | | platformStatus.platformKeyLength = length - 1; |
| | | memcpy(platformStatus.platformKey, data+1, length-1); |
| | | LoginPlatform(); |
| | | } else { |
| | | platformStatus.registed = 0; |
| | | } |
| | | 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]); |
| | | } |
| | | } |
| | | |
| | |
| | | pthread_mutex_unlock(&platform_tx_mutex); |
| | | |
| | | if (fd > 0) { |
| | | PlatformStatusChanged(PLATFORM_CONNECT_EVT); |
| | | PlatformStatusChanged(PLATFORM_CONNECT_EVT, NULL, 0); |
| | | } |
| | | |
| | | while (fd > 0) { |
| | |
| | | pthread_mutex_unlock(&platform_tx_mutex); |
| | | free(ptr); |
| | | |
| | | PlatformStatusChanged(PLATFORM_DISCONNECT_EVT); |
| | | PlatformStatusChanged(PLATFORM_DISCONNECT_EVT, NULL, 0); |
| | | pthread_exit(NULL); |
| | | } |
| | | |
| | |
| | | |
| | | 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) |
| | |
| | | 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); |
| | |
| | | |
| | | 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 ; |
| | | } |
| | |
| | | |
| | | 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 |
| | |
| | | */ |
| | | 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(); |
| | |
| | | } |
| | | } |
| | | |
| | | public String javaGetImei() { |
| | | return NetUtils.getDeviceId(getApplicationContext()); |
| | | } |
| | | |
| | | public String javaDESEncrypt(String plaintext, String key) { |
| | | try { |
| | | byte []des = DESUtil.encrypt(plaintext.getBytes("utf-8"), key); |
| | |
| | | |
| | | } |
| | | 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) { |