diff --git a/components/stratum/include/stratum_api.h b/components/stratum/include/stratum_api.h index b1b68d5dd..7a9479137 100644 --- a/components/stratum/include/stratum_api.h +++ b/components/stratum/include/stratum_api.h @@ -17,9 +17,13 @@ typedef enum MINING_SET_DIFFICULTY, MINING_SET_VERSION_MASK, STRATUM_RESULT, - STRATUM_RESULT_VERSION_MASK + STRATUM_RESULT_VERSION_MASK, + STRATUM_RESULT_CONFIGURE } stratum_method; +static const int STRATUM_ID_SUBSCRIBE = 1; +static const int STRATUM_ID_CONFIGURE = 2; + typedef struct { char *job_id; @@ -37,6 +41,8 @@ typedef struct typedef struct { + char * extranonce_str; + int extranonce_2_len; int16_t message_id; // Indicates the type of request the message represents. @@ -57,7 +63,7 @@ void STRATUM_V1_initialize_buffer(); char *STRATUM_V1_receive_jsonrpc_line(int sockfd); -int STRATUM_V1_subscribe(int socket, char ** extranonce, int * extranonce2_len, char * model); +int STRATUM_V1_subscribe(int socket, char * model); void STRATUM_V1_parse(StratumApiV1Message *message, const char *stratum_json); diff --git a/components/stratum/stratum_api.c b/components/stratum/stratum_api.c index d00d78d37..a3037ff78 100644 --- a/components/stratum/stratum_api.c +++ b/components/stratum/stratum_api.c @@ -23,6 +23,7 @@ static size_t json_rpc_buffer_size = 0; static int send_uid = 1; static void debug_stratum_tx(const char *); +int _parse_stratum_subscribe_result_message(const char * result_json_str, char ** extranonce, int * extranonce2_len); void STRATUM_V1_initialize_buffer() { @@ -116,6 +117,8 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json) cJSON * method_json = cJSON_GetObjectItem(json, "method"); stratum_method result = STRATUM_UNKNOWN; + + //if there is a method, then use that to decide what to do if (method_json != NULL && cJSON_IsString(method_json)) { if (strcmp("mining.notify", method_json->valuestring) == 0) { result = MINING_NOTIFY; @@ -126,30 +129,48 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json) } else { ESP_LOGI(TAG, "unhandled method in stratum message: %s", stratum_json); } + + //if there is no method, then it is a result } else { // parse results cJSON * result_json = cJSON_GetObjectItem(json, "result"); cJSON * error_json = cJSON_GetObjectItem(json, "error"); + + //if the result is null, then it's a fail if (result_json == NULL) { message->response_success = false; - } else { + + //if it's an error, then it's a fail + } else if (!cJSON_IsNull(error_json)) { + result = STRATUM_RESULT; + message->response_success = false; + + //if the result is a boolean, then parse it + } else if (cJSON_IsBool(result_json)) { + result = STRATUM_RESULT; + if (cJSON_IsTrue(result_json)) { + message->response_success = true; + } else { + message->response_success = false; + } + + //if the id is STRATUM_ID_SUBSCRIBE parse it + } else if (parsed_id == STRATUM_ID_SUBSCRIBE) { + result = STRATUM_RESULT_CONFIGURE; + _parse_stratum_subscribe_result_message(result_json, message->extranonce_str, message->extranonce_2_len); + + //if the id is STRATUM_ID_CONFIGURE parse it + } else if (parsed_id == STRATUM_ID_CONFIGURE) { cJSON * mask = cJSON_GetObjectItem(result_json, "version-rolling.mask"); if (mask != NULL) { result = STRATUM_RESULT_VERSION_MASK; message->version_mask = strtoul(mask->valuestring, NULL, 16); - } else if (cJSON_IsBool(result_json)) { - result = STRATUM_RESULT; - if (cJSON_IsTrue(result_json)) { - message->response_success = true; - } else { - message->response_success = false; - } - } else if (!cJSON_IsNull(error_json)) { - result = STRATUM_RESULT; - message->response_success = false; } else { ESP_LOGI(TAG, "unhandled result in stratum message: %s", stratum_json); } + + } else { + ESP_LOGI(TAG, "unhandled result in stratum message: %s", stratum_json); } } @@ -244,20 +265,20 @@ int _parse_stratum_subscribe_result_message(const char * result_json_str, char * return 0; } -int STRATUM_V1_subscribe(int socket, char ** extranonce, int * extranonce2_len, char * model) +int STRATUM_V1_subscribe(int socket, char * model) { // Subscribe char subscribe_msg[BUFFER_SIZE]; sprintf(subscribe_msg, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": [\"bitaxe/%s\"]}\n", send_uid++, model); debug_stratum_tx(subscribe_msg); write(socket, subscribe_msg, strlen(subscribe_msg)); - char * line; - line = STRATUM_V1_receive_jsonrpc_line(socket); - ESP_LOGI(TAG, "Received result %s", line); + // char * line; + // line = STRATUM_V1_receive_jsonrpc_line(socket); + // ESP_LOGI(TAG, "Received result %s", line); - _parse_stratum_subscribe_result_message(line, extranonce, extranonce2_len); + // _parse_stratum_subscribe_result_message(line, extranonce, extranonce2_len); - free(line); + // free(line); return 1; } @@ -333,7 +354,7 @@ void STRATUM_V1_configure_version_rolling(int socket, uint32_t * version_mask) *version_mask = strtoul(mask->valuestring, NULL, 16); ESP_LOGI(TAG, "Set version mask: %08lx", *version_mask); } - }else{ + } else { printf("configure_version result null\n"); } diff --git a/main/global_state.h b/main/global_state.h index 5d3b195bc..5d718e245 100644 --- a/main/global_state.h +++ b/main/global_state.h @@ -16,7 +16,7 @@ typedef struct { - uint8_t (*init_fn)(u_int64_t); + uint8_t (*init_fn)(uint64_t); task_result * (*receive_result_fn)(void * GLOBAL_STATE); int (*set_max_baud_fn)(void); void (*set_difficulty_mask_fn)(int); diff --git a/main/tasks/stratum_task.c b/main/tasks/stratum_task.c index b294934d2..266cd66df 100644 --- a/main/tasks/stratum_task.c +++ b/main/tasks/stratum_task.c @@ -101,21 +101,25 @@ void stratum_task(void * pvParameters) continue; } - // mining.subscribe - STRATUM_V1_subscribe(GLOBAL_STATE->sock, &GLOBAL_STATE->extranonce_str, &GLOBAL_STATE->extranonce_2_len, - GLOBAL_STATE->asic_model); + ///// Start Stratum Action + // mining.subscribe - ID: 1 + // STRATUM_V1_subscribe(GLOBAL_STATE->sock, &GLOBAL_STATE->extranonce_str, &GLOBAL_STATE->extranonce_2_len, GLOBAL_STATE->asic_model); + STRATUM_V1_subscribe(GLOBAL_STATE->sock, GLOBAL_STATE->asic_model); - // mining.configure + + // mining.configure - ID: 2 STRATUM_V1_configure_version_rolling(GLOBAL_STATE->sock, &GLOBAL_STATE->version_mask); // This should come before the final step of authenticate so the first job is sent with the proper difficulty set - //mining.suggest_difficulty + //mining.suggest_difficulty - ID: 3 STRATUM_V1_suggest_difficulty(GLOBAL_STATE->sock, STRATUM_DIFFICULTY); char * username = nvs_config_get_string(NVS_CONFIG_STRATUM_USER, STRATUM_USER); char * password = nvs_config_get_string(NVS_CONFIG_STRATUM_PASS, STRATUM_PW); + + //mining.authorize - ID: 4 STRATUM_V1_authenticate(GLOBAL_STATE->sock, username, password); free(password); free(username); @@ -164,6 +168,10 @@ void stratum_task(void * pvParameters) // 1fffe000 ESP_LOGI(TAG, "Set version mask: %08lx", stratum_api_v1_message.version_mask); GLOBAL_STATE->version_mask = stratum_api_v1_message.version_mask; + } else if (stratum_api_v1_message.method == STRATUM_RESULT_CONFIGURE) { + //copy the extranonce_str and extranonce_2_len from the stratum_api_v1_message to GLOBAL_STATE + strcpy(GLOBAL_STATE->extranonce_str, stratum_api_v1_message.extranonce_str); + GLOBAL_STATE->extranonce_2_len = stratum_api_v1_message.extranonce_2_len; } else if (stratum_api_v1_message.method == STRATUM_RESULT) { if (stratum_api_v1_message.response_success) { ESP_LOGI(TAG, "message result accepted");