diff --git a/components/stratum/include/stratum_api.h b/components/stratum/include/stratum_api.h index d4bbe47a9..a310b0647 100644 --- a/components/stratum/include/stratum_api.h +++ b/components/stratum/include/stratum_api.h @@ -59,6 +59,7 @@ typedef struct uint32_t version_mask; // result bool response_success; + char * error_str; } StratumApiV1Message; void STRATUM_V1_reset_uid(); diff --git a/components/stratum/stratum_api.c b/components/stratum/stratum_api.c index fed1c63c5..037360374 100644 --- a/components/stratum/stratum_api.c +++ b/components/stratum/stratum_api.c @@ -146,6 +146,7 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json) // parse results cJSON * result_json = cJSON_GetObjectItem(json, "result"); cJSON * error_json = cJSON_GetObjectItem(json, "error"); + cJSON * reject_reason_json = cJSON_GetObjectItem(json, "reject-reason"); //if the result is null, then it's a fail if (result_json == NULL) { @@ -158,6 +159,15 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json) } else { result = STRATUM_RESULT; } + if (cJSON_IsArray(error_json)) { + int len = cJSON_GetArraySize(error_json); + if (len >= 2) { + cJSON * error_msg = cJSON_GetArrayItem(error_json, 1); + if (cJSON_IsString(error_msg)) { + message->error_str = strdup(cJSON_GetStringValue(error_msg)); + } + } + } message->response_success = false; //if the result is a boolean, then parse it @@ -171,6 +181,9 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json) message->response_success = true; } else { message->response_success = false; + if (cJSON_IsString(reject_reason_json)) { + message->error_str = strdup(cJSON_GetStringValue(reject_reason_json)); + } } //if the id is STRATUM_ID_SUBSCRIBE parse it diff --git a/components/stratum/test/test_stratum_json.c b/components/stratum/test/test_stratum_json.c index d15e7becf..1ea16cf26 100644 --- a/components/stratum/test/test_stratum_json.c +++ b/components/stratum/test/test_stratum_json.c @@ -153,4 +153,16 @@ TEST_CASE("Parse stratum result error", "[stratum]") TEST_ASSERT_EQUAL(1, stratum_api_v1_message.message_id); TEST_ASSERT_EQUAL(STRATUM_RESULT, stratum_api_v1_message.method); TEST_ASSERT_FALSE(stratum_api_v1_message.response_success); + TEST_ASSERT_EQUAL("Job not found", stratum_api_v1_message.error_str); +} + +TEST_CASE("Parse stratum result alternative error", "[stratum]") +{ + StratumApiV1Message stratum_api_v1_message = {}; + const char *json_string = "{\"reject-reason\":\"Above target\",\"result\":false,\"error\":null,\"id\":8}"; + STRATUM_V1_parse(&stratum_api_v1_message, json_string); + TEST_ASSERT_EQUAL(1, stratum_api_v1_message.message_id); + TEST_ASSERT_EQUAL(STRATUM_RESULT, stratum_api_v1_message.method); + TEST_ASSERT_FALSE(stratum_api_v1_message.response_success); + TEST_ASSERT_EQUAL("Above target 2", stratum_api_v1_message.error_str); } \ No newline at end of file diff --git a/main/tasks/stratum_task.c b/main/tasks/stratum_task.c index 4e4a2942f..c3f9edc78 100644 --- a/main/tasks/stratum_task.c +++ b/main/tasks/stratum_task.c @@ -302,14 +302,14 @@ void stratum_task(void * pvParameters) ESP_LOGI(TAG, "message result accepted"); SYSTEM_notify_accepted_share(GLOBAL_STATE); } else { - ESP_LOGW(TAG, "message result rejected"); + ESP_LOGW(TAG, "message result rejected: %s", stratum_api_v1_message.error_str ? stratum_api_v1_message.error_str : "unknown"); SYSTEM_notify_rejected_share(GLOBAL_STATE); } } else if (stratum_api_v1_message.method == STRATUM_RESULT_SETUP) { if (stratum_api_v1_message.response_success) { ESP_LOGI(TAG, "setup message accepted"); } else { - ESP_LOGE(TAG, "setup message rejected"); + ESP_LOGE(TAG, "setup message rejected: %s", stratum_api_v1_message.error_str ? stratum_api_v1_message.error_str : "unknown"); } } }