AppManager.cpp
AppManager.cpp
CPP1/* Copyright Statement:2 *3 * (C) 2021-2024 Cubios Inc. All rights reserved.4 */56#include "AppManager.h"7#include "Screen.h"89namespace Cubios10{1112 void AppManager::handleNetPacket()13 {14 uint8_t pkt[m_sMaxRxPacketSize];15 uint32_t pktType;16 int32_t rxSize;1718 rxSize = recvPacket(m_sMaxRxPacketSize, &pktType, pkt);1920 while (rxSize > 0)21 {22 if (application)23 {24 application->on_Message(pktType, pkt, rxSize);25 }2627 rxSize = recvPacket(m_sMaxRxPacketSize, &pktType, pkt);28 }29 }3031 void AppManager::handleExternalData()32 {33 uint8_t pkt[m_sMaxRxPacketSize];34 int32_t rxSize;3536 rxSize = recvBleData(pkt, m_sMaxRxPacketSize);3738 while (rxSize > 0)39 {40 if (application)41 {42 if (rxSize >= 10)43 {44 if (pkt[0] == application->appUUID[0] && pkt[1] == application->appUUID[1] && pkt[2] == application->appUUID[2] && pkt[3] == application->appUUID[3] && pkt[4] == application->appUUID[4] &&45 pkt[5] == application->appUUID[5] && pkt[6] == application->appUUID[6] && pkt[7] == application->appUUID[7] && pkt[8] == application->appUUID[8] && pkt[9] == application->appUUID[9])46 {47 application->on_ExternalMessage((uint8_t *)(pkt + 10), rxSize - 10);48 }49 }50 else51 {52 LOG_e("BLE message doesn't contain app UUID, size is %d", rxSize);53 }54 }55 rxSize = recvBleData(pkt, m_sMaxRxPacketSize);56 }57 }5859 void AppManager::handleTwist()60 {61 TOPOLOGY_twistInfo_t data;62 int count = TOPOLOGY_getTwist(&data);63 if (application)64 {65 application->on_Twist(data);66 }67 }6869 void AppManager::handleTap()70 {71 uint32_t tapCount = getTap();7273 if (application)74 {75 application->on_Tap(tapCount);76 }77 }7879 int AppManager::Main()80 {81 std::array<Screen, 3> scr = {Screen(0), Screen(1), Screen(2)};8283 u32_t eventList = EVENT_getList();8485 if (eventList & WASM_EVENT_ON_CLOSE)86 {87 if (application)88 {89 application->on_Close();90 }91 return 1;92 }9394 if (eventList & WASM_EVENT_ON_UART_RX)95 {96 handleNetPacket();97 }9899 if (eventList & WASM_EVENT_ON_CFG_DATA)100 {101 handleExternalData();102 }103104 if (eventList & WASM_EVENT_ON_PHYSICAL_TICK)105 {106 if (application)107 {108 application->on_PhysicsTick(scr);109 }110 }111 if (eventList & WASM_EVENT_ON_TWIST)112 {113 handleTwist();114 }115116 if (eventList & WASM_EVENT_ON_TAP)117 {118 handleTap();119 }120121 if (application)122 {123 application->curTime = getTime();124125 if (prevTime == 0)126 {127 prevTime = application->curTime;128 }129130 application->on_Tick(application->curTime, application->curTime - prevTime);131 application->handleTimers();132133 application->on_Render(scr);134135 prevTime = application->curTime;136 }137138 return 0;139 }140141 void AppManager::SendNetworkMessage(uint32_t type, uint8_t *data, size_t size) const142 {143 if(data==nullptr || size==0)144 {145 LOG_e("Failed to send packet %d, data buffer or data size is null!", type);146 }147148 if (size > m_sMaxRxPacketSize)149 {150 LOG_e("Network packet size exceeds %d b,skipping...", m_sMaxRxPacketSize);151 return;152 }153154 if (type > m_sMaxPacketType)155 {156 LOG_e("Network packet type should be in [0,31] range,skipping...");157 return;158 }159160 if (sendPacket(type, data, size) < 0)161 {162 LOG_e("Failed to send packet %d of size %d", type, size);163 }164 }165166 void AppManager::SendExternalMessage(uint32_t type, uint8_t *data, size_t size)167 {168 if (size > (m_sMaxRxPacketSize - 10))169 {170 LOG_e("External message data size exceeds %d b,skipping...", (m_sMaxRxPacketSize - 10));171 return;172 }173174 if (type > m_sMaxPacketType)175 {176 LOG_e("External message type should be in [0,31] range,skipping...");177 return;178 }179180 if (sendBuffer == nullptr)181 {182 sendBuffer = (uint8_t *)malloc(m_sMaxRxPacketSize);183 if (sendBuffer == nullptr)184 {185 LOG_e("Failed to send external message, can't allocate memory buffer");186 return;187 }188 else189 {190 memcpy(sendBuffer, application->appUUID, 10);191 }192 }193194 memcpy((uint8_t *)(sendBuffer + 10), data, size);195 int32_t r = sendBleData(type, sendBuffer, size + 10);196197 if (r < 0)198 {199 LOG_e("Failed to send external message %d of size %d", type, size);200 }201 }202203 void AppManager::SetApplication(Application *app, const std::string &appIdentifier)204 {205 if (appIdentifier.length() != 10)206 {207 memset(app->appUUID, 0x00, 10);208 LOG_w("Application UUID is not defined!");209 }210 else211 {212 memcpy(app->appUUID, appIdentifier.data(), appIdentifier.length());213 }214215 app->appMgr = this;216 this->application.reset(app);217 }218219 void Application::SendNetworkMessage(uint32_t type, NetworkMessage *msg) const220 {221 int size = 0;222 uint8_t *data = msg->GetData(size);223224 appMgr->SendNetworkMessage(type, data, size);225 }226227 void Application::SendExternalMessage(uint32_t type, uint8_t *data, size_t size) const228 {229 appMgr->SendExternalMessage(type, data, size);230 }231232 void Application::SaveState(void *data, size_t size) const233 {234 if(data==nullptr || size==0)235 {236 LOG_e("Unable to save application state, invalid source buffer or its size!");237 return;238 }239240 if (saveState(data, size) < 0)241 {242 LOG_e("Unable to save application state!");243 }244 }245246 i32_t Application::LoadState(u32_t *id, void *data, size_t size) const247 {248 if(data==nullptr || size==0)249 {250 LOG_e("Unable to load application state, invalid destination buffer or its size!");251 return -1;252 }253254 i32_t result = loadState(id, data, size);255 if (result < 0)256 {257 LOG_e("Unable to load application state!");258 }259 return result;260 }261262 void Application::SaveState(SaveMessage *msg) const263 {264 int size = 0;265 if(msg==nullptr)266 {267 LOG_e("Unable to save application state, invalid source buffer!");268 return;269 }270271 uint8_t *data = msg->GetData(size);272273 SaveState(data, size);274 }275276 i32_t Application::LoadState(u32_t *id, SaveMessage *msg) const277 {278 uint8_t data[GAME_SAVE_SIZE];279 if(msg==nullptr)280 {281 LOG_e("Unable to load application state, invalid destination buffer!");282 return -1;283 }284285 i32_t result = loadState(id, data, GAME_SAVE_SIZE);286 if (result < 0)287 {288 LOG_e("Unable to load application state!");289 }290 msg->SetData(data, GAME_SAVE_SIZE);291 return result;292 }293294 void Application::handleTimers()295 {296 for (size_t i = 0; i < this->timers.size(); i++)297 {298 if (this->timers[i].suspended)299 continue;300301 if (this->curTime - this->timers[i].time >= this->timers[i].delay)302 {303 this->timers[i].time = this->curTime;304 this->on_Timer(this->timers[i].id);305 }306 }307 }308309 bool Application::SetTimer(uint8_t id, uint32_t delay, bool suspended)310 {311 for (Cubios::Timer_t t : this->timers)312 {313 if (t.id == id)314 {315 LOG_e("Timer with ID=%d already set!", id);316 return false;317 }318 }319320 Timer_t newTimer;321 newTimer.id = id;322 newTimer.delay = delay;323 newTimer.time = this->curTime;324 newTimer.suspended = suspended;325326 this->timers.push_back(newTimer);327328 return true;329 }330331 bool Application::StartTimer(uint8_t id)332 {333 for (size_t i = 0; i < this->timers.size(); i++)334 {335 if (this->timers[i].id == id)336 {337 this->timers[i].suspended = false;338 return true;339 }340 }341342 LOG_e("Timer with ID=%d is either already started or not set!", id);343 return false;344 }345 bool Application::StopTimer(uint8_t id)346 {347 for (size_t i = 0; i < this->timers.size(); i++)348 {349 if (this->timers[i].id == id)350 {351 this->timers[i].suspended = true;352 return true;353 }354 }355356 LOG_e("Timer with ID=%d is either already stopped or not set!", id);357 return false;358 }359360 void Application::KillTimer(uint8_t id)361 {362 for (size_t i = 0; i < this->timers.size(); i++)363 {364 if (this->timers[i].id == id)365 {366 this->timers.erase(this->timers.begin() + i);367 return;368 }369 }370 }371372 void Application::ShowFPSCounter(bool b)373 {374 if (b)375 Cubios::GFX_setFpsWindow(1);376 else377 Cubios::GFX_setFpsWindow(0);378 }379380}381382
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.