Skip to content

Commit f3ce100

Browse files
authored
Merge pull request #22 from toniebox-reverse-engineering/develop
Develop
2 parents e7bfc0d + 6daaa92 commit f3ce100

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

BoxBattery.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ void BoxBattery::_doBatteryTestStep() {
107107
bool batteryLow = isBatteryLow();
108108
bool batteryCritical = isBatteryCritical();
109109

110-
char* output;
111-
asprintf(&output, "%hu;%s;%hu;%hu.%s%hu;%s;%s;",
110+
char output[5+1 +5+1 +5+1 +3+1+5+5+1 +5+1 +5+1 +1];
111+
sprintf(output, "%hu;%s;%hu;%hu.%s%hu;%s;%s;",
112112
timeRunning,
113113
(chargerConnected ? "true" : "false"),
114114
batteryAdcRaw,
@@ -118,7 +118,6 @@ void BoxBattery::_doBatteryTestStep() {
118118
);
119119
Log.info(output);
120120
file.writeString(output);
121-
free(output);
122121

123122
file.writeString("\r\n");
124123
file.close();
@@ -133,7 +132,7 @@ void BoxBattery::startBatteryTest() {
133132
_batteryTestStartMillis = millis();
134133
FileFs file;
135134
if (file.open(_batteryTestFilename, FA_CREATE_ALWAYS | FA_WRITE)) {
136-
char* output;
135+
char output[26+10+10+1];
137136

138137
file.writeString("Timestamp;");
139138
file.writeString("Charging;");
@@ -144,9 +143,8 @@ void BoxBattery::startBatteryTest() {
144143
file.writeString("Comments");
145144
file.writeString("\r\n");
146145
file.writeString("0;;;;;;");
147-
asprintf(&output, "vFactor=%u, vChargerFactor=%u;", _batteryVoltageFactor, _batteryVoltageChargerFactor);
146+
sprintf(output, "vFactor=%u, vChargerFactor=%u;", _batteryVoltageFactor, _batteryVoltageChargerFactor);
148147
file.writeString(output);
149-
free(output);
150148
file.writeString("\r\n");
151149
file.close();
152150

@@ -164,11 +162,10 @@ void BoxBattery::stopBatteryTest() {
164162
_doBatteryTestStep();
165163
FileFs file;
166164
if (file.open(_batteryTestFilename, FA_OPEN_APPEND | FA_WRITE)) {
167-
char* output;
165+
char output[13+5+1];
168166
uint16_t timeRunning = (millis()-_batteryTestStartMillis) / (1000*60);
169-
asprintf(&output, "%hu;;;;;;stopped", timeRunning);
167+
sprintf(output, "%hu;;;;;;stopped", timeRunning);
170168
file.writeString(output);
171-
free(output);
172169
file.writeString("\r\n");
173170
file.close();
174171
} else {

BoxEvents.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ void BoxEvents::handleWiFiEvent(WrapperWiFi::ConnectionState state) {
164164
case WrapperWiFi::ConnectionState::CONNECTED:
165165
Log.info("IP: %s", WiFi.localIP().toString().c_str());
166166
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Blue, 3);
167+
Box.boxWiFi.mDnsAdvertiseSetup();
167168
break;
168169
case WrapperWiFi::ConnectionState::DISCONNECTED:
169170
//Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Cyan, 3);
@@ -277,10 +278,10 @@ void BoxEvents::handleTagEvent(BoxRFID::TAG_EVENT event) {
277278
}
278279

279280
Box.boxTonie.loadTonieByUid(Box.boxRFID.tagUid);
280-
uint8_t* path;
281+
uint8_t path[strlen(Box.boxTonie.RCONTENT_BASE)+16+1];
281282
uint8_t* uid = Box.boxRFID.tagUid;
282-
asprintf(
283-
(char**)&path,
283+
sprintf(
284+
(char*)path,
284285
"%s%02X%02X%02X%02X%02X%02X%02X%02X",
285286
Box.boxTonie.RCONTENT_BASE,
286287
uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6], uid[7]
@@ -301,18 +302,16 @@ void BoxEvents::handleTagEvent(BoxRFID::TAG_EVENT event) {
301302
if (!foundFile) {
302303
Log.info("No file play.");
303304
} else {
304-
uint8_t* filepath;
305-
asprintf(
306-
(char**)&filepath,
305+
uint8_t filepath[256];
306+
sprintf(
307+
(char*)filepath,
307308
"%s/%s",
308309
path,
309310
dir.fileName()
310311
);
311312
Box.boxDAC.playFile((const char*)filepath);
312-
free(filepath);
313313
}
314314
}
315-
free(path);
316315
}
317316
break;
318317
case BoxRFID::TAG_EVENT::TAG_REMOVED:

BoxTonies.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
#include "BoxTonies.h"
22

33
bool BoxTonies::loadTonieByUid(uint8_t uid[8]) {
4-
uint8_t* path;
4+
uint8_t path[strlen(CONTENT_BASE)+16+1+1];
55

6-
asprintf(
7-
(char**)&path,
6+
sprintf(
7+
(char*)path,
88
"%s%02X%02X%02X%02X/%02X%02X%02X%02X",
99
CONTENT_BASE,
1010
uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6], uid[7]
1111
);
1212

1313
memcpy(currentUid, uid, 8);
1414
if (loadTonieByPath(path)) {
15-
free(path);
1615
return true;
1716
}
18-
free(path);
1917
return false;
2018
}
2119

WrapperWiFi.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,38 @@ void WrapperWiFi::apMode() {
9393
WrapperWiFi::ConnectionState WrapperWiFi::getStatus() {
9494
return _state;
9595
}
96+
97+
void WrapperWiFi::mDnsAdvertiseSetup() {
98+
const char* service = "Hackiebox._api._tcp.local"; //max 64 bytes?
99+
const char* text = "Hackiebox";
100+
101+
int16_t result = sl_NetAppMDNSUnRegisterService(0, 0);
102+
103+
if (result != 0)
104+
Log.error("mDNS service unreg 1 failed=%i", result);
105+
106+
//Registering for the mDNS service.
107+
result = sl_NetAppMDNSUnRegisterService(
108+
(const signed char*)service,
109+
(const unsigned char)strlen(service)
110+
);
111+
112+
if (result != 0 && result != SL_NET_APP_DNS_NOT_EXISTED_SERVICE_ERROR)
113+
Log.error("mDNS service unreg 2 failed=%i", result);
114+
115+
result = sl_NetAppMDNSRegisterService(
116+
(const signed char*)service, //Service
117+
(const unsigned char)strlen(service), //ServiceLen
118+
(const signed char*)text, //Text
119+
(const unsigned char)strlen(text), //TextLen
120+
80, //Port
121+
2000, //TTL
122+
1 //Options
123+
);
124+
125+
if(result == 0) {
126+
Log.info("mDNS service %s with %s enabled", service, text);
127+
} else {
128+
Log.error("mDNS service setup failed=%i", result);
129+
}
130+
}

WrapperWiFi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class WrapperWiFi : public EnhancedThread {
3232

3333
WrapperWiFi::ConnectionState getStatus();
3434

35+
void mDnsAdvertiseSetup();
36+
3537
private:
3638
const char* _ssid;
3739
const char* _password;

0 commit comments

Comments
 (0)