Go to the documentation of this file.00001
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include "DJI_Memory.h"
00021 #include "DJI_API.h"
00022
00023 using namespace DJI::onboardSDK;
00024
00025 void DJI::onboardSDK::CoreAPI::setupMMU()
00026 {
00027 unsigned int i;
00028 MMU[0].tabIndex = 0;
00029 MMU[0].usageFlag = 1;
00030 MMU[0].pmem = memory;
00031 MMU[0].memSize = 0;
00032 for (i = 1; i < (MMU_TABLE_NUM - 1); i++)
00033 {
00034 MMU[i].tabIndex = i;
00035 MMU[i].usageFlag = 0;
00036 }
00037 MMU[MMU_TABLE_NUM - 1].tabIndex = MMU_TABLE_NUM - 1;
00038 MMU[MMU_TABLE_NUM - 1].usageFlag = 1;
00039 MMU[MMU_TABLE_NUM - 1].pmem = memory + MEMORY_SIZE;
00040 MMU[MMU_TABLE_NUM - 1].memSize = 0;
00041 }
00042
00043 void freeMemory(MMU_Tab *mmu_tab)
00044 {
00045 if (mmu_tab == (MMU_Tab *)0)
00046 return;
00047 if (mmu_tab->tabIndex == 0 || mmu_tab->tabIndex == (MMU_TABLE_NUM - 1))
00048 return;
00049 mmu_tab->usageFlag = 0;
00050 }
00051
00052 MMU_Tab *DJI::onboardSDK::CoreAPI::allocMemory(unsigned short size)
00053 {
00054 unsigned int mem_used = 0;
00055 unsigned char i;
00056 unsigned char j = 0;
00057 unsigned char mmu_tab_used_num = 0;
00058 unsigned char mmu_tab_used_index[MMU_TABLE_NUM];
00059
00060 unsigned int temp32;
00061 unsigned int temp_area[2] = { 0xFFFFFFFF, 0xFFFFFFFF };
00062
00063 unsigned int record_temp32 = 0;
00064 unsigned char magic_flag = 0;
00065
00066 if (size > PRO_PURE_DATA_MAX_SIZE || size > MEMORY_SIZE)
00067 return (MMU_Tab *)0;
00068
00069 for (i = 0; i < MMU_TABLE_NUM; i++)
00070 if (MMU[i].usageFlag == 1)
00071 {
00072 mem_used += MMU[i].memSize;
00073 mmu_tab_used_index[mmu_tab_used_num++] = MMU[i].tabIndex;
00074 }
00075
00076 if (MEMORY_SIZE < (mem_used + size))
00077 return (MMU_Tab *)0;
00078
00079 if (mem_used == 0)
00080 {
00081 MMU[1].pmem = MMU[0].pmem;
00082 MMU[1].memSize = size;
00083 MMU[1].usageFlag = 1;
00084 return &MMU[1];
00085 }
00086
00087 for (i = 0; i < (mmu_tab_used_num - 1); i++)
00088 for (j = 0; j < (mmu_tab_used_num - i - 1); j++)
00089 if (MMU[mmu_tab_used_index[j]].pmem > MMU[mmu_tab_used_index[j + 1]].pmem)
00090 {
00091 mmu_tab_used_index[j + 1] ^= mmu_tab_used_index[j];
00092 mmu_tab_used_index[j] ^= mmu_tab_used_index[j + 1];
00093 mmu_tab_used_index[j + 1] ^= mmu_tab_used_index[j];
00094 }
00095
00096 for (i = 0; i < (mmu_tab_used_num - 1); i++)
00097 {
00098 temp32 = (unsigned int)(MMU[mmu_tab_used_index[i + 1]].pmem -
00099 MMU[mmu_tab_used_index[i]].pmem);
00100
00101 if ((temp32 - MMU[mmu_tab_used_index[i]].memSize) >= size)
00102 {
00103 if (temp_area[1] > (temp32 - MMU[mmu_tab_used_index[i]].memSize))
00104 {
00105 temp_area[0] = MMU[mmu_tab_used_index[i]].tabIndex;
00106 temp_area[1] = temp32 - MMU[mmu_tab_used_index[i]].memSize;
00107 }
00108 }
00109
00110 record_temp32 += temp32 - MMU[mmu_tab_used_index[i]].memSize;
00111 if (record_temp32 >= size && magic_flag == 0)
00112 {
00113 j = i;
00114 magic_flag = 1;
00115 }
00116 }
00117
00118 if (temp_area[0] == 0xFFFFFFFF && temp_area[1] == 0xFFFFFFFF)
00119 {
00120 for (i = 0; i < j; i++)
00121 {
00122 if (MMU[mmu_tab_used_index[i + 1]].pmem >
00123 (MMU[mmu_tab_used_index[i]].pmem + MMU[mmu_tab_used_index[i]].memSize))
00124 {
00125 memmove(MMU[mmu_tab_used_index[i]].pmem + MMU[mmu_tab_used_index[i]].memSize,
00126 MMU[mmu_tab_used_index[i + 1]].pmem,
00127 MMU[mmu_tab_used_index[i + 1]].memSize);
00128 MMU[mmu_tab_used_index[i + 1]].pmem =
00129 MMU[mmu_tab_used_index[i]].pmem + MMU[mmu_tab_used_index[i]].memSize;
00130 }
00131 }
00132
00133 for (i = 1; i < (MMU_TABLE_NUM - 1); i++)
00134 {
00135 if (MMU[i].usageFlag == 0)
00136 {
00137 MMU[i].pmem =
00138 MMU[mmu_tab_used_index[j]].pmem + MMU[mmu_tab_used_index[j]].memSize;
00139
00140 MMU[i].memSize = size;
00141 MMU[i].usageFlag = 1;
00142 return &MMU[i];
00143 }
00144 }
00145 return (MMU_Tab *)0;
00146 }
00147
00148 for (i = 1; i < (MMU_TABLE_NUM - 1); i++)
00149 {
00150 if (MMU[i].usageFlag == 0)
00151 {
00152 MMU[i].pmem = MMU[temp_area[0]].pmem + MMU[temp_area[0]].memSize;
00153
00154 MMU[i].memSize = size;
00155 MMU[i].usageFlag = 1;
00156 return &MMU[i];
00157 }
00158 }
00159
00160 return (MMU_Tab *)0;
00161 }
00162
00163 void DJI::onboardSDK::CoreAPI::setupSession()
00164 {
00165 unsigned int i;
00166 for (i = 0; i < SESSION_TABLE_NUM; i++)
00167 {
00168 CMDSessionTab[i].sessionID = i;
00169 CMDSessionTab[i].usageFlag = 0;
00170 CMDSessionTab[i].mmu = (MMU_Tab *)NULL;
00171 }
00172
00173 for (i = 0; i < (SESSION_TABLE_NUM - 1); i++)
00174 {
00175 ACKSessionTab[i].sessionID = i + 1;
00176 ACKSessionTab[i].sessionStatus = ACK_SESSION_IDLE;
00177 ACKSessionTab[i].mmu = (MMU_Tab *)NULL;
00178 }
00179 }
00180
00187 CMDSession *DJI::onboardSDK::CoreAPI::allocSession(unsigned short session_id,
00188 unsigned short size)
00189 {
00190 unsigned int i;
00191 API_LOG(serialDevice, DEBUG_LOG, "Allocation size %d", size);
00192 MMU_Tab *mmu = NULL;
00193
00194 if (session_id == 0 || session_id == 1)
00195 {
00196 if (this->CMDSessionTab[session_id].usageFlag == 0)
00197 i = session_id;
00198 else
00199 {
00200
00201 API_LOG(serialDevice, ERROR_LOG, "session %d is busy\n", session_id);
00202 return NULL;
00203 }
00204 }
00205 else
00206 {
00207 for (i = 2; i < SESSION_TABLE_NUM; i++)
00208 if (CMDSessionTab[i].usageFlag == 0)
00209 break;
00210 }
00211 if (i < 32 && CMDSessionTab[i].usageFlag == 0)
00212 {
00213 CMDSessionTab[i].usageFlag = 1;
00214 mmu = allocMemory(size);
00215 if (mmu == NULL)
00216 CMDSessionTab[i].usageFlag = 0;
00217 else
00218 {
00219 CMDSessionTab[i].mmu = mmu;
00220 return &CMDSessionTab[i];
00221 }
00222 }
00223 return NULL;
00224 }
00225
00226 void DJI::onboardSDK::CoreAPI::freeSession(CMDSession *session)
00227 {
00228 if (session->usageFlag == 1)
00229 {
00230 API_LOG(serialDevice, DEBUG_LOG, "session id %d\n", session->sessionID);
00231 freeMemory(session->mmu);
00232 session->usageFlag = 0;
00233 }
00234 }
00235
00236 ACKSession *DJI::onboardSDK::CoreAPI::allocACK(unsigned short session_id, unsigned short size)
00237 {
00238 MMU_Tab *mmu = NULL;
00239 if (session_id > 0 && session_id < 32)
00240 {
00241 if (ACKSessionTab[session_id - 1].mmu)
00242 freeACK(&ACKSessionTab[session_id - 1]);
00243 mmu = allocMemory(size);
00244 if (mmu == NULL)
00245 {
00246 API_LOG(serialDevice, ERROR_LOG, "there is not enough memory\n");
00247 return NULL;
00248 }
00249 else
00250 {
00251 ACKSessionTab[session_id - 1].mmu = mmu;
00252 return &ACKSessionTab[session_id - 1];
00253 }
00254 }
00255 API_LOG(serialDevice, ERROR_LOG, "wrong Ack session ID: 0x%X\n", session_id);
00256 return NULL;
00257 }
00258
00259 void DJI::onboardSDK::CoreAPI::freeACK(ACKSession *session) { freeMemory(session->mmu); }