int Pro_Ack_Interface(ProAckParameter *parameter) { unsigned short ret = 0; ACK_Session_Tab * ack_session = (ACK_Session_Tab *)NULL;; if(parameter->length > PRO_PURE_DATA_MAX_SIZE) { printf("%s:%d:ERROR,length=%d is oversize\n",__func__,__LINE__,parameter->length); return -1; } if(parameter->session_id == 0) { ; } else if(parameter->session_id > 0 && parameter->session_id < 32) { Get_Memory_Lock(); ack_session = Request_ACK_Session(parameter->session_id, Pro_Calc_Length(parameter->length,parameter->need_encrypt)); if(ack_session == (ACK_Session_Tab*)NULL) { printf("%s:%d:ERROR,there is not enough memory\n",__func__,__LINE__); Free_Memory_Lock(); return -1; } ret = sdk_encrypt_interface(ack_session->mmu->pmem,parameter->buf, parameter->length,1,parameter->need_encrypt, parameter->session_id,parameter->seq_num); if(ret == 0) { printf("%s:%d:encrypt ERROR\n",__func__,__LINE__); Free_Memory_Lock(); return -1; } Send_Pro_Data(ack_session->mmu->pmem); Free_Memory_Lock(); ack_session->session_status = ACK_SESSION_USING; return 0; } return -1; }
static void Send_Poll(void) { unsigned char i; unsigned int cur_timestamp; static CMD_Session_Tab * cmd_session = Get_CMD_Session_Tab(); for(i = 1 ; i < SESSION_TABLE_NUM ; i ++) { if(cmd_session[i].usage_flag == 1) { cur_timestamp = Get_TimeStamp(); if((cur_timestamp - cmd_session[i].pre_timestamp) > cmd_session[i].ack_timeout) { Get_Memory_Lock(); if(cmd_session[i].retry_send_time > 0 ) { if(cmd_session[i].sent_time >= cmd_session[i].retry_send_time ) { Free_CMD_Session(&cmd_session[i]); } else { Send_Pro_Data(cmd_session[i].mmu->pmem); cmd_session[i].pre_timestamp = cur_timestamp; cmd_session[i].sent_time ++; } } else { Send_Pro_Data(cmd_session[i].mmu->pmem); cmd_session[i].pre_timestamp = cur_timestamp; } Free_Memory_Lock(); } } } }
MMU_Tab * Request_Memory(unsigned short size) { unsigned int mem_used = 0; unsigned char i; unsigned char j; unsigned char mmu_tab_used_num = 0; unsigned char mmu_tab_used_index[MMU_TABLE_NUM]; unsigned int temp32; unsigned int temp_area[2] = {0xFFFFFFFF,0xFFFFFFFF}; unsigned int record_temp32 = 0; unsigned char magic_flag = 0; if(size > PRO_PURE_DATA_MAX_SIZE || size > STATIC_MEMORY_SIZE) { return (MMU_Tab *)0; } for(i = 0 ; i < MMU_TABLE_NUM ; i ++) { if(DJI_MMU_Tab[i].usage_flag == 1) { mem_used += DJI_MMU_Tab[i].mem_size; mmu_tab_used_index[mmu_tab_used_num ++] = DJI_MMU_Tab[i].tab_index; } } if(STATIC_MEMORY_SIZE < (mem_used + size)) { Free_Memory_Lock(); return (MMU_Tab *)0; } if(mem_used == 0) { DJI_MMU_Tab[1].pmem = DJI_MMU_Tab[0].pmem; DJI_MMU_Tab[1].mem_size = size; DJI_MMU_Tab[1].usage_flag = 1; Free_Memory_Lock(); return &DJI_MMU_Tab[1]; } for(i = 0 ; i < (mmu_tab_used_num - 1) ; i ++) { for(j = 0; j < (mmu_tab_used_num - i - 1) ; j ++) { if(DJI_MMU_Tab[mmu_tab_used_index[j]].pmem > DJI_MMU_Tab[mmu_tab_used_index[j + 1]].pmem) { mmu_tab_used_index[j + 1] ^= mmu_tab_used_index[j]; mmu_tab_used_index[j] ^= mmu_tab_used_index[j + 1]; mmu_tab_used_index[j + 1] ^= mmu_tab_used_index[j]; } } } for(i = 0 ; i < (mmu_tab_used_num - 1) ; i ++) { temp32 = (unsigned int)(DJI_MMU_Tab[mmu_tab_used_index[i + 1]].pmem - DJI_MMU_Tab[mmu_tab_used_index[i]].pmem); if((temp32 - DJI_MMU_Tab[mmu_tab_used_index[i]].mem_size) >= size) { if(temp_area[1] > (temp32 - DJI_MMU_Tab[mmu_tab_used_index[i]].mem_size)) { temp_area[0] = DJI_MMU_Tab[mmu_tab_used_index[i]].tab_index; temp_area[1] = temp32 - DJI_MMU_Tab[mmu_tab_used_index[i]].mem_size; } } record_temp32 += temp32 - DJI_MMU_Tab[mmu_tab_used_index[i]].mem_size; if(record_temp32 >= size && magic_flag == 0) { j = i; magic_flag = 1; } } if(temp_area[0] == 0xFFFFFFFF && temp_area[1] == 0xFFFFFFFF) { for(i = 0; i < j; i ++) { if(DJI_MMU_Tab[mmu_tab_used_index[i + 1]].pmem > (DJI_MMU_Tab[mmu_tab_used_index[i]].pmem + DJI_MMU_Tab[mmu_tab_used_index[i]].mem_size)) { memmove(DJI_MMU_Tab[mmu_tab_used_index[i]].pmem + DJI_MMU_Tab[mmu_tab_used_index[i]].mem_size, DJI_MMU_Tab[mmu_tab_used_index[i + 1]].pmem, DJI_MMU_Tab[mmu_tab_used_index[i + 1]].mem_size); DJI_MMU_Tab[mmu_tab_used_index[i + 1]].pmem = DJI_MMU_Tab[mmu_tab_used_index[i]].pmem + DJI_MMU_Tab[mmu_tab_used_index[i]].mem_size; //printf("move tab_index=%d\n", // DJI_MMU_Tab[mmu_tab_used_index[i + 1]].tab_index); } } for(i = 1 ; i < (MMU_TABLE_NUM - 1) ; i ++) { if(DJI_MMU_Tab[i].usage_flag == 0) { DJI_MMU_Tab[i].pmem = DJI_MMU_Tab[mmu_tab_used_index[j]].pmem + DJI_MMU_Tab[mmu_tab_used_index[j]].mem_size; DJI_MMU_Tab[i].mem_size = size; DJI_MMU_Tab[i].usage_flag = 1; Free_Memory_Lock(); return &DJI_MMU_Tab[i]; } } Free_Memory_Lock(); return (MMU_Tab *)0; } for(i = 1 ; i < (MMU_TABLE_NUM - 1) ; i ++) { if(DJI_MMU_Tab[i].usage_flag == 0) { DJI_MMU_Tab[i].pmem = DJI_MMU_Tab[temp_area[0]].pmem + DJI_MMU_Tab[temp_area[0]].mem_size; DJI_MMU_Tab[i].mem_size = size; DJI_MMU_Tab[i].usage_flag = 1; Free_Memory_Lock(); return &DJI_MMU_Tab[i]; } } return (MMU_Tab *)0; }
void Pro_Link_Recv_Hook(ProHeader *header) { ProHeader *p2header; static ACK_Session_Tab * ack_session = Get_ACK_Session_Tab(); static CMD_Session_Tab * cmd_session = Get_CMD_Session_Tab(); //TODO: parse the protocol data stream here if(header->is_ack == 1) { if(header->session_id == 1) { if(cmd_session[1].usage_flag == 1 && cmd_session[1].ack_callback) { cmd_session[1].ack_callback(header); Get_Memory_Lock(); Free_CMD_Session(&cmd_session[1]); Free_Memory_Lock(); } } else if(header->session_id > 1 && header->session_id < 32) { if(cmd_session[header->session_id].usage_flag == 1) { Get_Memory_Lock(); p2header = (ProHeader*)cmd_session[header->session_id].mmu->pmem; if(p2header->session_id == header->session_id && p2header->sequence_number == header->sequence_number) { //printf("%s:Recv Session %d ACK\n",__func__,p2header->session_id); Call_APP_Func = cmd_session[header->session_id].ack_callback; Free_CMD_Session(&cmd_session[header->session_id]); Free_Memory_Lock(); if(Call_APP_Func) { Call_APP_Func(header); } } else { Free_Memory_Lock(); } } } } else { //TODO,is a request package switch(header->session_id) { case 0: Pro_Request_Interface(header); break; case 1: default: if(ack_session[header->session_id - 1].session_status == ACK_SESSION_PROCESS) { printf("%s,This session is waiting for App ack:" "session id=%d,seq_num=%d\n",__func__, header->session_id,header->sequence_number); } else if(ack_session[header->session_id - 1].session_status == ACK_SESSION_IDLE) { if(header->session_id > 1) { ack_session[header->session_id - 1].session_status = ACK_SESSION_PROCESS; } Pro_Request_Interface(header); } else if(ack_session[header->session_id - 1].session_status == ACK_SESSION_USING) { Get_Memory_Lock(); p2header = (ProHeader *)ack_session[header->session_id - 1].mmu->pmem; if(p2header->sequence_number == header->sequence_number) { printf("%s:repeat ACK to remote,session id=%d,seq_num=%d\n", __func__,header->session_id,header->sequence_number); Send_Pro_Data(ack_session[header->session_id - 1].mmu->pmem); Free_Memory_Lock(); } else { printf("%s:same session,but new seq_num pkg,session id=%d," "pre seq_num=%d,""cur seq_num=%d\n",__func__, header->session_id,p2header->sequence_number, header->sequence_number); ack_session[header->session_id - 1].session_status = ACK_SESSION_PROCESS; Free_Memory_Lock(); Pro_Request_Interface(header); } } break; } } }
int Pro_Send_Interface(ProSendParameter *parameter) { unsigned short ret = 0; CMD_Session_Tab * cmd_session = (CMD_Session_Tab *) NULL; static unsigned short global_seq_num = 0; if(parameter->length > PRO_PURE_DATA_MAX_SIZE) { printf("%s:%d:ERROR,length=%d is oversize\n",__func__,__LINE__,parameter->length); return -1; } switch(parameter->session_mode) { case 0: Get_Memory_Lock(); cmd_session = Request_CMD_Session(CMD_SESSION_0,Pro_Calc_Length(parameter->length,parameter->need_encrypt)); if(cmd_session == (CMD_Session_Tab *)NULL) { Free_Memory_Lock(); printf("%s:%d:ERROR,there is not enough memory\n",__func__,__LINE__); return -1; } ret = sdk_encrypt_interface(cmd_session->mmu->pmem,parameter->buf,parameter->length, 0,parameter->need_encrypt,cmd_session->session_id,global_seq_num); if(ret == 0) { printf("%s:%d:encrypt ERROR\n",__func__,__LINE__); Free_CMD_Session(cmd_session); Free_Memory_Lock(); return -1; } Send_Pro_Data(cmd_session->mmu->pmem); global_seq_num ++; Free_CMD_Session(cmd_session); Free_Memory_Lock(); break; case 1: Get_Memory_Lock(); cmd_session = Request_CMD_Session(CMD_SESSION_1,Pro_Calc_Length(parameter->length,parameter->need_encrypt)); if(cmd_session == (CMD_Session_Tab *)NULL) { Free_Memory_Lock(); printf("%s:%d:ERROR,there is not enough memory\n",__func__,__LINE__); return -1; } if(global_seq_num == cmd_session->pre_seq_num) { global_seq_num ++; } ret = sdk_encrypt_interface(cmd_session->mmu->pmem,parameter->buf,parameter->length, 0,parameter->need_encrypt,cmd_session->session_id,global_seq_num); if(ret == 0) { printf("%s:%d:encrypt ERROR\n",__func__,__LINE__); Free_CMD_Session(cmd_session); Free_Memory_Lock(); return -1; } cmd_session->pre_seq_num = global_seq_num ++; cmd_session->ack_callback = parameter->ack_callback; cmd_session->ack_timeout = (parameter->ack_timeout > POLL_TICK) ? parameter->ack_timeout : POLL_TICK; cmd_session->pre_timestamp = Get_TimeStamp(); cmd_session->sent_time = 1; cmd_session->retry_send_time = 1; Send_Pro_Data(cmd_session->mmu->pmem); Free_Memory_Lock(); break; case 2: Get_Memory_Lock(); cmd_session = Request_CMD_Session(CMD_SESSION_AUTO,Pro_Calc_Length(parameter->length,parameter->need_encrypt)); if(cmd_session == (CMD_Session_Tab *)NULL) { Free_Memory_Lock(); printf("%s:%d:ERROR,there is not enough memory\n",__func__,__LINE__); return -1; } if(global_seq_num == cmd_session->pre_seq_num) { global_seq_num ++; } ret = sdk_encrypt_interface(cmd_session->mmu->pmem,parameter->buf,parameter->length, 0,parameter->need_encrypt,cmd_session->session_id,global_seq_num); if(ret == 0) { printf("%s:%d:encrypt ERROR\n",__func__,__LINE__); Free_CMD_Session(cmd_session); Free_Memory_Lock(); return -1; } cmd_session->pre_seq_num = global_seq_num ++; cmd_session->ack_callback = parameter->ack_callback; cmd_session->ack_timeout = (parameter->ack_timeout > POLL_TICK) ? parameter->ack_timeout : POLL_TICK; cmd_session->pre_timestamp = Get_TimeStamp(); cmd_session->sent_time = 1; cmd_session->retry_send_time = parameter->retry_time; Send_Pro_Data(cmd_session->mmu->pmem); Free_Memory_Lock(); break; } return 0; }