| | |
| | | #include <cstdio> |
| | | #include <cstring> |
| | | #include <malloc.h> |
| | | #include <pthread.h> |
| | | #include <mutex> |
| | | #include <semaphore.h> |
| | | #include "../jni_log.h" |
| | | #include "../defs.h" |
| | |
| | | }largeMessage[DATA_ACCESS_END]; |
| | | |
| | | static uint8_t PhoneNumber[PHONE_NUM_SIZE] = {0}; |
| | | static pthread_mutex_t seq_mutex = PTHREAD_MUTEX_INITIALIZER; |
| | | static pthread_mutex_t tx_queue_mutex = PTHREAD_MUTEX_INITIALIZER; |
| | | static mutex seq_mutex; |
| | | static mutex tx_queue_mutex; |
| | | |
| | | static void ParseTimeout(union sigval sig); |
| | | static void ParseTimeout(apptimer_var_t val); |
| | | static uint32_t GetResendTimeout(uint8_t access, uint8_t curr_resend_cnt, uint32_t base_time); |
| | | static uint16_t GetMessageSeq(uint8_t access); |
| | | static void PacketEntry(uint8_t access, const uint8_t *data, uint16_t length); |
| | |
| | | static void ResendItemTimeout(message_tx_table_t **head, uint32_t tm); |
| | | static uint32_t GetResentTimeoutTxQueue(message_tx_table_t **head); |
| | | |
| | | static void *TxQueueMgrThread(void *p); |
| | | static void TriggerResendTxQueue(union sigval sig); |
| | | static void TxQueueMgrThread(void); |
| | | static void TriggerResendTxQueue(apptimer_var_t val); |
| | | |
| | | /* |
| | | 7E 80 89 00 40 87 00 00 01 37 20 20 55 68 00 76 00 9A 41 11 00 22 00 77 00 00 00 00 00 00 00 00 |
| | |
| | | } |
| | | } |
| | | |
| | | static void ParseTimeout(union sigval sig) { |
| | | AppTimer_delete(ParseTimeout); |
| | | packet_parse_status[sig.sival_int] = PKT_SYNC_HEAD; |
| | | |
| | | DEBUG("ParseTimeout %d", sig.sival_int); |
| | | static void ParseTimeout(apptimer_var_t val) { |
| | | packet_parse_status[val.var1] = PKT_SYNC_HEAD; |
| | | } |
| | | |
| | | static uint32_t GetResendTimeout(uint8_t access, uint8_t curr_resend_cnt, uint32_t base_time) |
| | |
| | | static uint16_t TxSeq[DATA_ACCESS_END] = {0}; |
| | | uint16_t seq; |
| | | |
| | | pthread_mutex_lock(&seq_mutex); |
| | | lock_guard<std::mutex> lock(seq_mutex); |
| | | |
| | | seq = TxSeq[access]; |
| | | TxSeq[access]++; |
| | | pthread_mutex_unlock(&seq_mutex); |
| | | |
| | | return seq; |
| | | } |
| | |
| | | |
| | | DEBUG("AddTxQueue id = 0x%X, seq = %d, len = %d", id, seq, length); |
| | | |
| | | pthread_mutex_lock(&tx_queue_mutex); |
| | | lock_guard<mutex> lock(tx_queue_mutex); |
| | | |
| | | // If the item is exist, skip it |
| | | if (FindTxQueue(head, id, seq) != NULL) { |
| | |
| | | } |
| | | |
| | | ATQ_END: |
| | | pthread_mutex_unlock(&tx_queue_mutex); |
| | | |
| | | uint32_t tim = GetResentTimeoutTxQueue(&message_tx_table_head[DATA_ACCESS_PLATFORM]); |
| | | |
| | |
| | | |
| | | message_tx_table_t *ptr = *head; |
| | | |
| | | pthread_mutex_lock(&tx_queue_mutex); |
| | | lock_guard<mutex> lock(tx_queue_mutex); |
| | | while ( ptr != NULL ) { |
| | | if (ptr->id == id && ptr->seq == seq) { |
| | | // delete |
| | |
| | | ptr = ptr->next; |
| | | } |
| | | } |
| | | pthread_mutex_unlock(&tx_queue_mutex); |
| | | } |
| | | |
| | | static void RemoveTxQueue(message_tx_table_t **head, uint16_t id) |
| | |
| | | |
| | | message_tx_table_t *ptr = *head; |
| | | |
| | | pthread_mutex_lock(&tx_queue_mutex); |
| | | lock_guard<mutex> lock(tx_queue_mutex); |
| | | while ( ptr != NULL ) { |
| | | if (ptr->id == id) { |
| | | // delete |
| | |
| | | ptr = ptr->next; |
| | | } |
| | | } |
| | | pthread_mutex_unlock(&tx_queue_mutex); |
| | | } |
| | | |
| | | static void RemoveAllTxQueue(message_tx_table_t **head) |
| | |
| | | return; |
| | | } |
| | | |
| | | pthread_mutex_lock(&tx_queue_mutex); |
| | | lock_guard<mutex> lock(tx_queue_mutex); |
| | | for (message_tx_table_t *ptr = *head, *next; ptr != NULL; ptr = next) { |
| | | next = ptr->next; |
| | | free(ptr); |
| | | } |
| | | *head = NULL; |
| | | pthread_mutex_unlock(&tx_queue_mutex); |
| | | } |
| | | |
| | | static int SendQueue(message_tx_table_t *item) |
| | |
| | | DEBUG("SendQueue id = 0x%04X, seq = %d, length = %d", item->id, item->seq, item->length); |
| | | if (item != NULL) { |
| | | if (item->access == DATA_ACCESS_MCU) { |
| | | if (WriteSerialPort(GetSerialPort(UART_1), item->data, item->length) != item->length) { |
| | | item->time_out = 100 + AppTimer_GetTickCount(); |
| | | LOGE("发往串口出错了"); |
| | | // return -1; |
| | | } |
| | | |
| | | } else if(item->access == DATA_ACCESS_PLATFORM) { |
| | | if (WritePlatform(item->data, item->length) != item->length) { |
| | | item->time_out = D_SEC(3) + AppTimer_GetTickCount(); |
| | |
| | | // delete |
| | | DEBUG("Delete item %d", ptr->curr_cnt); |
| | | message_tx_table_t *temp = ptr; |
| | | lock_guard<mutex> lock(tx_queue_mutex); |
| | | |
| | | pthread_mutex_lock(&tx_queue_mutex); |
| | | if (ptr == *head) { |
| | | if (ptr->next == NULL) { |
| | | DEBUG("****************** Delete all 2 ******************"); |
| | |
| | | ptr->prev->next = ptr->next; |
| | | ptr = ptr->next; |
| | | } |
| | | pthread_mutex_unlock(&tx_queue_mutex); |
| | | |
| | | free(temp); |
| | | } else { |
| | |
| | | if (head == NULL) |
| | | return resentTime; |
| | | |
| | | pthread_mutex_lock(&tx_queue_mutex); |
| | | lock_guard<mutex> lock(tx_queue_mutex); |
| | | message_tx_table_t *ptr = *head; |
| | | |
| | | while ( ptr != NULL ) { |
| | |
| | | } |
| | | ptr = ptr->next; |
| | | } |
| | | pthread_mutex_unlock(&tx_queue_mutex); |
| | | |
| | | return resentTime; |
| | | } |
| | | |
| | | static sem_t sem_tx_mgr; |
| | | |
| | | static void *TxQueueMgrThread(void *p) { |
| | | static void TxQueueMgrThread(void) { |
| | | while (true) { |
| | | sem_wait(&sem_tx_mgr); |
| | | uint32_t tim = AppTimer_GetTickCount(); |
| | |
| | | AppTimer_add(TriggerResendTxQueue, tim); |
| | | } |
| | | } |
| | | pthread_exit(NULL); |
| | | } |
| | | |
| | | static void TriggerResendTxQueue(union sigval sig) { |
| | | AppTimer_delete(TriggerResendTxQueue); |
| | | static void TriggerResendTxQueue(apptimer_var_t val) { |
| | | sem_post(&sem_tx_mgr); |
| | | } |
| | | |
| | |
| | | PhoneNumber[6] = 0x00; |
| | | PhoneNumber[7] = 0x02;*/ |
| | | |
| | | pthread_mutex_init(&seq_mutex, NULL); |
| | | pthread_mutex_init(&tx_queue_mutex, NULL); |
| | | sem_init(&sem_tx_mgr, 0, 0); |
| | | |
| | | pthread_t pid; |
| | | pthread_attr_t attr; |
| | | pthread_attr_init(&attr); |
| | | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//detached |
| | | pthread_create(&pid, &attr, TxQueueMgrThread, NULL); |
| | | std::thread(TxQueueMgrThread).detach(); |
| | | } |
| | | |
| | | void SetPlatformTxPhoneNum(const char *phone) |