Skip to content

Commit 474d268

Browse files
committed
token_verify & http2_daemon: split from token_verifier.c
1 parent 3b9e4f3 commit 474d268

File tree

5 files changed

+228
-128
lines changed

5 files changed

+228
-128
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
/*
2-
* token_verifier.h
2+
* http2_daemon.h
33
*
44
* Created on: 2018-02-17 18:51
55
* Author: Jack Chen <[email protected]>
66
*/
77

8-
#ifndef INC_TASKS_TOKEN_VERIFIER_H_
9-
#define INC_TASKS_TOKEN_VERIFIER_H_
8+
#ifndef INC_TASKS_HTTP2_DAEMON_H_
9+
#define INC_TASKS_HTTP2_DAEMON_H_
1010

1111
#include <stdint.h>
1212

1313
// cert0.pem
1414
extern const uint8_t cert0_pem_ptr[] asm("_binary_cert0_pem_start");
1515
extern const uint8_t cert0_pem_end[] asm("_binary_cert0_pem_end");
1616

17-
extern void token_verifier_task(void *pvParameter);
18-
extern void token_verifier_verify_token(char *token);
17+
extern void http2_daemon(void *pvParameter);
1918

20-
#endif /* INC_TASKS_TOKEN_VERIFIER_H_ */
19+
#endif /* INC_TASKS_HTTP2_DAEMON_H_ */

main/inc/tasks/token_verify.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* token_verify.h
3+
*
4+
* Created on: 2018-04-06 15:09
5+
* Author: Jack Chen <[email protected]>
6+
*/
7+
8+
#ifndef INC_TASKS_TOKEN_VERIFY_H_
9+
#define INC_TASKS_TOKEN_VERIFY_H_
10+
11+
#include "tasks/http2_client.h"
12+
13+
extern int token_verify_parse_data(struct http2c_handle *handle, const char *data, size_t len, int flags);
14+
extern int token_verify_prepare_data(struct http2c_handle *handle, char *buf, size_t length, uint32_t *data_flags);
15+
16+
extern void token_verify(char *token);
17+
18+
#endif /* INC_TASKS_TOKEN_VERIFY_H_ */

main/src/tasks/http2_daemon.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* http2_daemon.c
3+
*
4+
* Created on: 2018-02-17 18:51
5+
* Author: Jack Chen <[email protected]>
6+
*/
7+
8+
#include <string.h>
9+
10+
#include "cJSON.h"
11+
#include "esp_log.h"
12+
#include "esp_ota_ops.h"
13+
14+
#include "device/wifi.h"
15+
#include "system/event.h"
16+
#include "system/firmware.h"
17+
#include "tasks/gui_daemon.h"
18+
#include "tasks/nfc_daemon.h"
19+
#include "tasks/led_daemon.h"
20+
#include "tasks/ota_update.h"
21+
#include "tasks/token_verify.h"
22+
#include "tasks/audio_daemon.h"
23+
#include "tasks/http2_daemon.h"
24+
#include "tasks/http2_client.h"
25+
26+
#define TAG "http2"
27+
28+
#if defined(CONFIG_ENABLE_SERVER_CERT_VERIFY)
29+
static const uint8_t *cert_file_ptr[][2] = {
30+
{cert0_pem_ptr, cert0_pem_end} // "DigiCert Global Root CA"
31+
};
32+
static uint8_t cert_file_index = 0;
33+
#endif
34+
35+
void http2_daemon(void *pvParameter)
36+
{
37+
struct http2c_handle hd;
38+
39+
while (1) {
40+
memset(&hd, 0, sizeof(hd));
41+
42+
EventBits_t uxBitsPrev = xEventGroupWaitBits(
43+
daemon_event_group,
44+
HTTP2_DAEMON_TOKEN_READY_BIT | HTTP2_DAEMON_OTA_READY_BIT,
45+
pdFALSE,
46+
pdFALSE,
47+
portMAX_DELAY
48+
);
49+
50+
led_set_mode(4);
51+
gui_show_image(1);
52+
53+
#if defined(CONFIG_ENABLE_SERVER_CERT_VERIFY)
54+
hd.ca_file_ptr = cert_file_ptr[cert_file_index][0];
55+
hd.ca_file_len = cert_file_ptr[cert_file_index][1] - cert_file_ptr[cert_file_index][0];
56+
#endif
57+
58+
if (http2_client_connect(&hd, CONFIG_SERVER_URI) != 0) {
59+
ESP_LOGE(TAG, "failed to connect");
60+
if (uxBitsPrev & HTTP2_DAEMON_TOKEN_READY_BIT) {
61+
gui_show_image(6);
62+
audio_play_file(3);
63+
}
64+
} else {
65+
/* HTTP POST */
66+
if (uxBitsPrev & HTTP2_DAEMON_TOKEN_READY_BIT) {
67+
http2_client_do_post(
68+
&hd,
69+
CONFIG_SERVER_POST_PATH,
70+
token_verify_prepare_data,
71+
token_verify_parse_data
72+
);
73+
xEventGroupClearBits(
74+
daemon_event_group,
75+
HTTP2_DAEMON_TOKEN_FAILED_BIT | HTTP2_DAEMON_TOKEN_FINISH_BIT
76+
);
77+
} else {
78+
http2_client_do_post(
79+
&hd,
80+
CONFIG_SERVER_POST_PATH,
81+
ota_update_prepare_data,
82+
ota_update_parse_data
83+
);
84+
xEventGroupClearBits(
85+
daemon_event_group,
86+
HTTP2_DAEMON_OTA_FAILED_BIT | HTTP2_DAEMON_OTA_FINISH_BIT | HTTP2_DAEMON_OTA_RUN_BIT
87+
);
88+
}
89+
EventBits_t uxBits;
90+
while (1) {
91+
uxBits = xEventGroupGetBits(system_event_group);
92+
if (!(uxBits & WIFI_READY_BIT) || (http2_client_execute(&hd) < 0)) {
93+
ESP_LOGE(TAG, "error in send/receive");
94+
if (uxBitsPrev & HTTP2_DAEMON_TOKEN_READY_BIT) {
95+
gui_show_image(6);
96+
audio_play_file(5);
97+
}
98+
break;
99+
}
100+
vTaskDelay(2 / portTICK_PERIOD_MS);
101+
uxBits = xEventGroupGetBits(daemon_event_group);
102+
if (!(uxBits & HTTP2_DAEMON_TOKEN_READY_BIT) && !(uxBits & HTTP2_DAEMON_OTA_READY_BIT)) {
103+
break;
104+
}
105+
}
106+
}
107+
http2_client_free(&hd);
108+
vTaskDelay(2000 / portTICK_RATE_MS);
109+
110+
led_set_mode(1);
111+
gui_show_image(3);
112+
113+
if (uxBitsPrev & HTTP2_DAEMON_TOKEN_READY_BIT) {
114+
xEventGroupSetBits(daemon_event_group, HTTP2_DAEMON_TOKEN_FINISH_BIT);
115+
xEventGroupClearBits(daemon_event_group, HTTP2_DAEMON_TOKEN_READY_BIT);
116+
} else {
117+
xEventGroupSetBits(daemon_event_group, HTTP2_DAEMON_OTA_FINISH_BIT);
118+
xEventGroupClearBits(daemon_event_group, HTTP2_DAEMON_OTA_READY_BIT);
119+
}
120+
}
121+
}

main/src/tasks/token_verifier.c

Lines changed: 0 additions & 122 deletions
This file was deleted.

main/src/tasks/token_verify.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* token_verify.c
3+
*
4+
* Created on: 2018-04-06 15:09
5+
* Author: Jack Chen <[email protected]>
6+
*/
7+
8+
#include <string.h>
9+
10+
#include "cJSON.h"
11+
#include "esp_log.h"
12+
#include "esp_ota_ops.h"
13+
14+
#include "device/wifi.h"
15+
#include "system/event.h"
16+
#include "system/firmware.h"
17+
#include "tasks/gui_daemon.h"
18+
#include "tasks/nfc_daemon.h"
19+
#include "tasks/led_daemon.h"
20+
#include "tasks/audio_daemon.h"
21+
#include "tasks/http2_daemon.h"
22+
#include "tasks/http2_client.h"
23+
24+
#define TAG "token"
25+
26+
static char *data_ptr = NULL;
27+
28+
int token_verify_parse_data(struct http2c_handle *handle, const char *data, size_t len, int flags)
29+
{
30+
cJSON *root = NULL;
31+
cJSON *status = NULL;
32+
if (len) {
33+
root = cJSON_Parse(data);
34+
if (cJSON_HasObjectItem(root, "status")) {
35+
status = cJSON_GetObjectItemCaseSensitive(root, "status");
36+
if (cJSON_IsTrue(status)) {
37+
ESP_LOGW(TAG, "authentication success");
38+
gui_show_image(2);
39+
audio_play_file(1);
40+
} else {
41+
ESP_LOGE(TAG, "authentication failed");
42+
gui_show_image(7);
43+
audio_play_file(2);
44+
}
45+
} else {
46+
ESP_LOGE(TAG, "invalid response");
47+
gui_show_image(6);
48+
audio_play_file(6);
49+
xEventGroupSetBits(daemon_event_group, HTTP2_DAEMON_TOKEN_FAILED_BIT);
50+
}
51+
cJSON_Delete(root);
52+
}
53+
if (flags == DATA_RECV_RST_STREAM) {
54+
xEventGroupClearBits(daemon_event_group, HTTP2_DAEMON_TOKEN_READY_BIT);
55+
}
56+
return 0;
57+
}
58+
59+
int token_verify_prepare_data(struct http2c_handle *handle, char *buf, size_t length, uint32_t *data_flags)
60+
{
61+
cJSON *root = NULL;
62+
root = cJSON_CreateObject();
63+
cJSON_AddNumberToObject(root, "request", 100);
64+
cJSON_AddStringToObject(root, "token", data_ptr);
65+
cJSON_AddStringToObject(root, "mac", wifi0_mac_str);
66+
cJSON_PrintPreallocated(root, buf, length, 0);
67+
cJSON_Delete(root);
68+
(*data_flags) |= NGHTTP2_DATA_FLAG_EOF;
69+
return strlen(buf);
70+
}
71+
72+
void token_verify(char *token)
73+
{
74+
data_ptr = token;
75+
EventBits_t uxBits = xEventGroupSync(
76+
daemon_event_group,
77+
HTTP2_DAEMON_TOKEN_READY_BIT,
78+
HTTP2_DAEMON_TOKEN_FINISH_BIT,
79+
10000 / portTICK_RATE_MS
80+
);
81+
if ((uxBits & HTTP2_DAEMON_TOKEN_FINISH_BIT) == 0) {
82+
xEventGroupClearBits(daemon_event_group, HTTP2_DAEMON_TOKEN_READY_BIT);
83+
}
84+
}

0 commit comments

Comments
 (0)