AppManager.cpp
AppManager.cpp
CPP1/* Copyright Statement:2 *3 * (C) 2021-2025 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::handlePat()70 {71 uint32_t tapCount = getTap();7273 if (application)74 {75 application->on_Tap(tapCount);76 application->on_Pat(tapCount);77 }78 }7980 int AppManager::Main()81 {82 std::array<Screen, 3> scr = {Screen(0), Screen(1), Screen(2)};8384 u32_t eventList = EVENT_getList();8586 if (eventList & WASM_EVENT_ON_CLOSE)87 {88 if (application)89 {90 application->on_Close();91 }92 return 1;93 }9495 if (eventList & WASM_EVENT_ON_UART_RX)96 {97 handleNetPacket();98 }99100 if (eventList & WASM_EVENT_ON_CFG_DATA)101 {102 handleExternalData();103 }104105 if (eventList & WASM_EVENT_ON_PHYSICAL_TICK)106 {107 if (application)108 {109 application->on_PhysicsTick(scr);110 }111 }112 if (eventList & WASM_EVENT_ON_TWIST)113 {114 handleTwist();115 }116117 if (eventList & WASM_EVENT_ON_PAT)118 {119 handlePat();120 }121122 if (application)123 {124 application->curTime = getTime();125126 if (prevTime == 0)127 {128 prevTime = application->curTime;129 }130131 application->on_Tick(application->curTime, application->curTime - prevTime);132 application->handleTimers();133134 application->on_Render(scr);135136 prevTime = application->curTime;137 }138139 return 0;140 }141142 void AppManager::SendNetworkMessage(uint32_t type, uint8_t *data, size_t size) const143 {144 if(data==nullptr || size==0)145 {146 LOG_e("Failed to send packet %d, data buffer or data size is null!", type);147 }148149 if (size > m_sMaxRxPacketSize)150 {151 LOG_e("Network packet size exceeds %d b,skipping...", m_sMaxRxPacketSize);152 return;153 }154155 if (type > m_sMaxPacketType)156 {157 LOG_e("Network packet type should be in [0,31] range,skipping...");158 return;159 }160161 if (sendPacket(type, data, size) < 0)162 {163 LOG_e("Failed to send packet %d of size %d", type, size);164 }165 }166167 void AppManager::SendExternalMessage(uint32_t type, uint8_t *data, size_t size)168 {169 if (size > (m_sMaxRxPacketSize - 10))170 {171 LOG_e("External message data size exceeds %d b,skipping...", (m_sMaxRxPacketSize - 10));172 return;173 }174175 if (type > m_sMaxPacketType)176 {177 LOG_e("External message type should be in [0,31] range,skipping...");178 return;179 }180181 if (sendBuffer == nullptr)182 {183 sendBuffer = (uint8_t *)malloc(m_sMaxRxPacketSize);184 if (sendBuffer == nullptr)185 {186 LOG_e("Failed to send external message, can't allocate memory buffer");187 return;188 }189 else190 {191 memcpy(sendBuffer, application->appUUID, 10);192 }193 }194195 memcpy((uint8_t *)(sendBuffer + 10), data, size);196 int32_t r = sendBleData(type, sendBuffer, size + 10);197198 if (r < 0)199 {200 LOG_e("Failed to send external message %d of size %d", type, size);201 }202 }203204 void AppManager::SetApplication(Application *app, const std::string &appIdentifier)205 {206 if (appIdentifier.length() != 10)207 {208 memset(app->appUUID, 0x00, 10);209 LOG_w("Application UUID is not defined!");210 }211 else212 {213 memcpy(app->appUUID, appIdentifier.data(), appIdentifier.length());214 }215216 app->appMgr = this;217 this->application.reset(app);218 }219220 void Application::SendNetworkMessage(uint32_t type, NetworkMessage *msg) const221 {222 int size = 0;223 uint8_t *data = msg->GetData(size);224225 appMgr->SendNetworkMessage(type, data, size);226 }227228 void Application::SendExternalMessage(uint32_t type, uint8_t *data, size_t size) const229 {230 appMgr->SendExternalMessage(type, data, size);231 }232233 void Application::SaveState(void *data, size_t size) const234 {235 if(data==nullptr || size==0)236 {237 LOG_e("Unable to save application state, invalid source buffer or its size!");238 return;239 }240241 if (saveState(data, size) < 0)242 {243 LOG_e("Unable to save application state!");244 }245 }246247 i32_t Application::LoadState(u32_t *id, void *data, size_t size) const248 {249 if(data==nullptr || size==0)250 {251 LOG_e("Unable to load application state, invalid destination buffer or its size!");252 return -1;253 }254255 i32_t result = loadState(id, data, size);256 if (result < 0)257 {258 LOG_e("Unable to load application state!");259 }260 return result;261 }262263 void Application::SaveState(SaveMessage *msg) const264 {265 int size = 0;266 if(msg==nullptr)267 {268 LOG_e("Unable to save application state, invalid source buffer!");269 return;270 }271272 uint8_t *data = msg->GetData(size);273274 SaveState(data, size);275 }276277 i32_t Application::LoadState(u32_t *id, SaveMessage *msg) const278 {279 uint8_t data[GAME_SAVE_SIZE];280 if(msg==nullptr)281 {282 LOG_e("Unable to load application state, invalid destination buffer!");283 return -1;284 }285286 i32_t result = loadState(id, data, GAME_SAVE_SIZE);287 if (result < 0)288 {289 LOG_e("Unable to load application state!");290 }291 msg->SetData(data, GAME_SAVE_SIZE);292 return result;293 }294295 void Application::handleTimers()296 {297 for (size_t i = 0; i < this->timers.size(); i++)298 {299 if (this->timers[i].suspended)300 continue;301302 if (this->curTime - this->timers[i].time >= this->timers[i].delay)303 {304 this->timers[i].time = this->curTime;305 this->on_Timer(this->timers[i].id);306 }307 }308 }309310 bool Application::SetTimer(uint8_t id, uint32_t delay, bool suspended)311 {312 for (Cubios::Timer_t t : this->timers)313 {314 if (t.id == id)315 {316 LOG_e("Timer with ID=%d already set!", id);317 return false;318 }319 }320321 Timer_t newTimer;322 newTimer.id = id;323 newTimer.delay = delay;324 newTimer.time = this->curTime;325 newTimer.suspended = suspended;326327 this->timers.push_back(newTimer);328329 return true;330 }331332 bool Application::StartTimer(uint8_t id)333 {334 for (size_t i = 0; i < this->timers.size(); i++)335 {336 if (this->timers[i].id == id)337 {338 this->timers[i].suspended = false;339 return true;340 }341 }342343 LOG_e("Timer with ID=%d is either already started or not set!", id);344 return false;345 }346 bool Application::StopTimer(uint8_t id)347 {348 for (size_t i = 0; i < this->timers.size(); i++)349 {350 if (this->timers[i].id == id)351 {352 this->timers[i].suspended = true;353 return true;354 }355 }356357 LOG_e("Timer with ID=%d is either already stopped or not set!", id);358 return false;359 }360361 void Application::KillTimer(uint8_t id)362 {363 for (size_t i = 0; i < this->timers.size(); i++)364 {365 if (this->timers[i].id == id)366 {367 this->timers.erase(this->timers.begin() + i);368 return;369 }370 }371 }372373 void Application::ShowFPSCounter(bool b)374 {375 if (b)376 Cubios::GFX_setFpsWindow(1);377 else378 Cubios::GFX_setFpsWindow(0);379 }380381}382383
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.