Skip to content

Commit a572798

Browse files
committed
almost
Signed-off-by: faradaym <[email protected]>
1 parent 389b05c commit a572798

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

src/DuckNet.cpp

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ int DuckNet::setupWebServer(bool createCaptivePortal, std::string html) {
171171
switch (err) {
172172
case DUCK_ERR_NONE:
173173
{
174+
loginfo_ln("success sending packer");
174175
request->send(200, "text/html", "OK.");
175176
}
176177
break;
@@ -187,19 +188,21 @@ int DuckNet::setupWebServer(bool createCaptivePortal, std::string html) {
187188
});
188189

189190
webServer.on("/atakHistory", HTTP_GET, [&](AsyncWebServerRequest* request) {
190-
uint8_t* atakBytes = DuckNet::serializeAtakHistoryToBytes(&atakBuffer);
191-
size_t atakSize = sizeof(atakBytes);
192-
// Use shared_ptr for safe cleanup
193-
std::shared_ptr<uint8_t> atakData(atakBytes, [](uint8_t* p) { delete[] p; });
191+
size_t atakSize;
194192

195-
AsyncWebServerResponse* response = request->beginResponse("application/octet-stream", atakSize,???
196-
});
193+
uint8_t* atakBytes = DuckNet::serializeAtakHistoryToBytes(&atakBuffer, &atakSize);
197194

198-
response->addHeader("Content-Disposition", "attachment; filename=\"atakHistory.bin\"");
195+
// std::shared_ptr<uint8_t> atakData(atakBytes, [](uint8_t* p) { delete[] p; });
196+
197+
AsyncWebServerResponse *response = request->beginResponse_P(200, "application/octet-stream", atakBytes, atakSize);
198+
199+
loginfo_ln("sending atak history");
200+
// response->addHeader("Content-Disposition", "attachment; filename=\"atakHistory.bin\"");
199201
request->send(response);
200202
});
201203

202204
webServer.on("/atakChatHistory", HTTP_GET, [&](AsyncWebServerRequest* request){
205+
loginfo_ln("this is what should be called in captive portal");
203206
std::string response = DuckNet::serializeAtakHistoryToJSON(&atakBuffer);
204207
const char* res = response.c_str();
205208
request->send(200, "text/json", res);
@@ -357,6 +360,7 @@ void DuckNet::addToAtakBuffer(CdpPacket message) {
357360
if(atakBuffer.findMuid(message.muid) < 0){
358361
message.timeReceived = millis();
359362
atakBuffer.push(message);
363+
loginfo_ln("pushed new packet to buffer");
360364
}
361365
}
362366

@@ -391,38 +395,28 @@ std::string DuckNet::serializeAtakHistoryToJSON(CircularBuffer* buffer) {
391395

392396
}
393397

394-
uint8_t* DuckNet::serializeAtakHistoryToBytes(CircularBuffer* buffer) {
398+
uint8_t* DuckNet::serializeAtakHistoryToBytes(CircularBuffer* buffer, size_t* totalAtakBytes) {
395399
// int atakBytes = buffer->getCount();
396400
// size_t dataSize = atakBytes * sizeof(CdpPacket);
397-
// outSize = dataSize + sizeof(uint32_t);
398-
399-
uint8_t* result = new uint8_t[dataSize];
400-
401-
// // First 4 bytes = count (little endian)
402-
// result[0] = atakBytes & 0xFF;
403-
// result[1] = (atakBytes >> 8) & 0xFF;
404-
// result[2] = (atakBytes >> 16) & 0xFF;
405-
// result[3] = (atakBytes >> 24) & 0xFF;
401+
// totalAtakBytes = dataSize + sizeof(uint32_t);
402+
std::vector<byte> packetArr;
406403

407404
// Serialize packets
408405
int tail = buffer->getTail();
409-
// for (int i = 0; i < atakBytes; i++) {
410-
// CdpPacket packet = buffer->getMessage(tail);
411-
// memcpy(result + 4 + i * sizeof(CdpPacket), &packet, sizeof(CdpPacket));
412-
// tail = (tail + 1) % buffer->getBufferEnd();
413-
// }
414406

415407
while(tail != buffer->getHead()){
416408
CdpPacket packet = buffer->getMessage(tail);
417-
//get the size of this specific packet
418-
//copy the bytes into result
419-
memcpy(result + 4 + i * sizeof(CdpPacket), &packet, sizeof(CdpPacket));
409+
packetArr.insert(packetArr.end(), packet.muid.begin(), packet.muid.end());
410+
packetArr.insert(packetArr.end(), packet.data.begin(), packet.data.end());
420411
tail++;
421412
if(tail == buffer->getBufferEnd()){
422413
tail = 0;
423414
}
424415
}
425-
416+
*totalAtakBytes = packetArr.size();
417+
uint8_t* result = new uint8_t[*totalAtakBytes];
418+
memcpy(result, packetArr.data(), *totalAtakBytes);
419+
loginfo_ln("complete serialize");
426420
return result;
427421
}
428422

src/include/DuckNet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class DuckNet {
225225
* @brief retrieve all messages from from message circular buffer
226226
* @returns a byte array of messages with only the body
227227
*/
228-
uint8_t* serializeAtakHistoryToBytes(CircularBuffer* buffer);
228+
uint8_t* serializeAtakHistoryToBytes(CircularBuffer* buffer, size_t* totalAtakBytes);
229229

230230
/**
231231
* @brief Set the WiFi network ssid.

src/include/chatPage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const char chat_page[] PROGMEM = R"=====(
6969
var req = new XMLHttpRequest();
7070
req.addEventListener("load", chatHistoryListener);
7171
req.addEventListener("error", errorListener);
72-
req.open("GET", "/atakHistory");
72+
req.open("GET", "/atakChatHistory");
7373
req.send();
7474
}
7575

0 commit comments

Comments
 (0)