Skip to content

Commit

Permalink
refactor pending task
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanndroid committed Jun 4, 2024
1 parent 188ff68 commit d37ec34
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
6 changes: 3 additions & 3 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void coap_put_handler(cJSON *request, cJSON *response) {
}

cJSON *update = cJSON_GetObjectItem(request, "update");
if (update != NULL && !pending_task_get_method()) {
if (update != NULL) {
update_data_t *update_data = malloc(sizeof(update_data_t));
update_data->url = strdup(cJSON_GetObjectItem(update, "url")->valuestring);
update_data->signature = strdup(cJSON_GetObjectItem(update, "signature")->valuestring);
Expand All @@ -312,12 +312,12 @@ void coap_put_handler(cJSON *request, cJSON *response) {
}

cJSON *restart = cJSON_GetObjectItem(request, "restart");
if (restart != NULL && !pending_task_get_method()) {
if (restart != NULL) {
pending_task_set(pending_task_restart, NULL);
}

cJSON *locate = cJSON_GetObjectItem(request, "locate");
if (locate != NULL && !pending_task_get_method()) {
if (locate != NULL) {
pending_task_set(pending_task_locate, NULL);
}
}
10 changes: 4 additions & 6 deletions main/pending_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
static pending_task_t pending_task = {NULL, NULL};

void pending_task_set(pending_task_method method, void *args) {
pending_task.method = method;
pending_task.args = args;
}

pending_task_method pending_task_get_method(void) {
return pending_task.method;
if (!pending_task.method) {
pending_task.method = method;
pending_task.args = args;
}
}

void pending_task_run(void) {
Expand Down
2 changes: 0 additions & 2 deletions main/pending_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ typedef struct {

void pending_task_set(pending_task_method method, void *args);

pending_task_method pending_task_get_method(void);

void pending_task_run(void);

0 comments on commit d37ec34

Please sign in to comment.