| | |
| | | #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); |
| | | |
| | | 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]); |
| | | } |
| | | } |
| | | |
| | |
| | | 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) |