Skip to content

Commit 5443665

Browse files
authored
Merge pull request #1584 from snakeye/master
ESP8266: Use strtok for parsing incoming job
2 parents 9e608f2 + b54d33b commit 5443665

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

ESP8266_Code/ESP8266_Code.ino

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -518,23 +518,6 @@ void handleSystemEvents(void) {
518518
yield();
519519
}
520520

521-
// https://stackoverflow.com/questions/9072320/split-string-into-string-array
522-
String getValue(String data, char separator, int index)
523-
{
524-
int found = 0;
525-
int strIndex[] = {0, -1};
526-
int max_index = data.length() - 1;
527-
528-
for (int i = 0; i <= max_index && found <= index; i++) {
529-
if (data.charAt(i) == separator || i == max_index) {
530-
found++;
531-
strIndex[0] = strIndex[1] + 1;
532-
strIndex[1] = (i == max_index) ? i + 1 : i;
533-
}
534-
}
535-
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
536-
}
537-
538521
void waitForClientData(void) {
539522
client_buffer = "";
540523

@@ -610,6 +593,33 @@ uint8_t *hexStringToUint8Array(const String &hexString, uint8_t *uint8Array, con
610593
return uint8Array;
611594
}
612595

596+
struct MiningJob
597+
{
598+
String last_block_hash;
599+
String expected_hash_str;
600+
uint8_t expected_hash[20];
601+
unsigned int difficulty;
602+
603+
bool parse(char* job_str)
604+
{
605+
String tokens[3];
606+
char *token = strtok(job_str, ",");
607+
for (int i = 0; token != NULL && i < 3; i++)
608+
{
609+
tokens[i] = token;
610+
token = strtok(NULL, ",");
611+
}
612+
613+
last_block_hash = tokens[0];
614+
expected_hash_str = tokens[1];
615+
hexStringToUint8Array(expected_hash_str, expected_hash, 20);
616+
difficulty = tokens[2].toInt() * 100 + 1;
617+
618+
return true;
619+
}
620+
};
621+
622+
613623
void setup() {
614624
Serial.begin(500000);
615625
Serial.println("\nDuino-Coin " + String(MINER_VER));
@@ -714,21 +724,17 @@ void loop() {
714724
#endif
715725

716726
waitForClientData();
717-
String last_block_hash = getValue(client_buffer, SEP_TOKEN, 0);
718-
String expected_hash_str = getValue(client_buffer, SEP_TOKEN, 1);
719-
difficulty = getValue(client_buffer, SEP_TOKEN, 2).toInt() * 100 + 1;
720-
721-
if (USE_HIGHER_DIFF) system_update_cpu_freq(160);
727+
Serial.println("Received job with size of " + String(client_buffer));
722728

723-
int job_len = last_block_hash.length() + expected_hash_str.length() + String(difficulty).length();
729+
MiningJob job;
730+
job.parse((char*)client_buffer.c_str());
724731

725-
Serial.println("Received job with size of " + String(job_len) + " bytes: " + last_block_hash + " " + expected_hash_str + " " + difficulty);
732+
Serial.println("Parsed job: " + job.last_block_hash + " " + job.expected_hash_str + " " + String(job.difficulty));
726733

727-
uint8_t expected_hash[20];
728-
hexStringToUint8Array(expected_hash_str, expected_hash, 20);
734+
if (USE_HIGHER_DIFF) system_update_cpu_freq(160);
729735

730736
br_sha1_init(&sha1_ctx_base);
731-
br_sha1_update(&sha1_ctx_base, last_block_hash.c_str(), last_block_hash.length());
737+
br_sha1_update(&sha1_ctx_base, job.last_block_hash.c_str(), job.last_block_hash.length());
732738

733739
float start_time = micros();
734740
max_micros_elapsed(start_time, 0);
@@ -743,7 +749,7 @@ void loop() {
743749
br_sha1_update(&sha1_ctx, duco_numeric_result_str.c_str(), duco_numeric_result_str.length());
744750
br_sha1_out(&sha1_ctx, hashArray);
745751

746-
if (memcmp(expected_hash, hashArray, 20) == 0) {
752+
if (memcmp(job.expected_hash, hashArray, 20) == 0) {
747753
// If result is found
748754
if (LED_BLINKING) digitalWrite(LED_BUILTIN, HIGH);
749755
unsigned long elapsed_time = micros() - start_time;

0 commit comments

Comments
 (0)