Browse Source

Ajout SSL, découpage code

master
Alexandre Poincet 6 months ago
parent
commit
10973a1ca3

+ 1
- 0
include/certs.h View File

@@ -166,6 +166,7 @@ const unsigned char caCert[] PROGMEM = {
166 166
   0x0a, 0x93, 0xbf, 0x12, 0xdb, 0x78, 0x0b, 0x00, 0x02, 0x60, 0xd7, 0x5b,
167 167
   0xa1, 0xa1, 0x64, 0x63
168 168
 };
169
+unsigned int caCertLen = 928;
169 170
 
170 171
 unsigned char private_client_bin_key[] = {
171 172
   0x30, 0x82, 0x04, 0xa4, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,

+ 11
- 0
include/connect_lib.h View File

@@ -0,0 +1,11 @@
1
+#ifndef CONNECT_LIB_H
2
+#define CONNECT_LIB_H
3
+
4
+#define TIMEOUT_WIFI	5000
5
+#define MAX_RECONNECT_TRY 3
6
+
7
+bool connectToWIfi();
8
+bool connectToServer();
9
+bool addSSL();
10
+
11
+#endif

+ 9
- 0
include/door_lib.h View File

@@ -0,0 +1,9 @@
1
+#ifndef DOOR_LIB_H
2
+#define DOOR_LIB_H
3
+
4
+#define DOOR_PIN 5
5
+
6
+void setupDoor();
7
+void openDoor();
8
+
9
+#endif

+ 6
- 0
include/http_cli_lib.h View File

@@ -0,0 +1,6 @@
1
+#ifndef HTTP_CLIENT_H
2
+#define HTTP_CLIENT_H
3
+
4
+bool send_post_request(const char* data, const char* url, char *res);
5
+
6
+#endif

+ 15
- 0
include/mrfc552_lib.h View File

@@ -0,0 +1,15 @@
1
+#ifndef MRFC522_LIB_H
2
+#define MRFC522_LIB_H
3
+
4
+#define RST_PIN         0          // Configurable, see typical pin layout above
5
+#define SS_PIN          2         // Configurable, see typical pin layout above
6
+#define IRQ_PIN			4
7
+
8
+void setupMFRC552();
9
+void clearInt();
10
+void activateRec();
11
+void printCardUID(const char* uid);
12
+int readCardUID();
13
+void IRQ_cardAvailable();
14
+
15
+#endif

+ 82
- 0
include/request_lib.h View File

@@ -0,0 +1,82 @@
1
+#ifndef REQUESTS_LIB_
2
+#define REQUESTS_LIB_
3
+
4
+#include <time.h>
5
+
6
+#define BUFFER_SIZE 256*1024
7
+
8
+#define URL_OPENNING_REQ "/api/request"
9
+#define URL_REQ_POLL "/api/req_status"
10
+#define URL_PUB_DOOR_OPEN "/index.html"
11
+#define URL_PUB_DOOR_CLOSE "/index.html"
12
+#define URL_PUB_REQ_TO "/index.html"
13
+#define URL_PUB_OPEN_TO "/index.html"
14
+
15
+
16
+
17
+int poll_response(const char *, const char *);
18
+
19
+time_t getEpoch();
20
+
21
+
22
+
23
+/*======================================================================
24
+**  Nom          : send_openning_request
25
+**  Description  : construit et émet la requete au format json 
26
+** ---------------------------------------------------------------------
27
+**  retour       : un objet JSON renvoyé par la blockchain contenant entre autre le txid
28
+					NULL sinon
29
+**======================================================================*/
30
+bool send_openning_request(const char*, const char*, char*);
31
+
32
+
33
+
34
+
35
+/*======================================================================
36
+**  Nom          : publish_door_opening
37
+**  Description  : publier dans la blockhain l'ouverture d'une porte en précisant le user_id et txid de la demande ayant entrainée cette ouverture
38
+ainsi qu'un timestamp à l'instant de l'ouverture 
39
+**  Arguments 	 : curl handler, l'id de la porte, le txid de la demande, l'id de l'utilisateur (dans cet ordre)
40
+** ---------------------------------------------------------------------
41
+**  retour       : txid de la publication (à parser), NULL si erreur
42
+**======================================================================*/
43
+bool publish_door_opening(const char*,const char*,const char*, char*);
44
+
45
+
46
+
47
+
48
+/*======================================================================
49
+**  Nom          : publish_door_closing
50
+**  Description  : publier dans la blockhain la fermeture d'une porte en précisant le user_id et txid de la dernière demande qui a entrainée 
51
+l'ouverture effective de cette porte
52
+**  Arguments 	 : curl handler, l'id de la porte, le txid de la demande, l'id de l'utilisateur (dans cet ordre)
53
+** ---------------------------------------------------------------------
54
+**  retour       : txid de la publication (à parser), NULL si erreur
55
+**======================================================================*/
56
+bool publish_door_closing(const char*,const char*,const char*, char*);
57
+
58
+
59
+
60
+
61
+/*======================================================================
62
+**  Nom          : publish_request_timeout
63
+**  Description  : publier dans la blockhain l'expiration du délais imparti pour une réponse du serveur à une demande précise 
64
+**  Arguments 	 : curl handler,l'id de la porte, le txid de la demande (dans cet ordre)
65
+** ---------------------------------------------------------------------
66
+**  retour       : txid de la publication (à parser), NULL si erreur
67
+**======================================================================*/
68
+bool publish_request_timeout(const char*,const char*, char*);
69
+
70
+
71
+
72
+
73
+/*======================================================================
74
+**  Nom          : publish_opening_timeout
75
+**  Description  : publier dans la blockhain l'expiration du délai imparti à l'utilisateur pour ouvrir une porte qui lui avait été débloquée
76
+**  Arguments 	 : curl handler, l'id de la porte, le txid de la demande (dans cet ordre)
77
+** ---------------------------------------------------------------------
78
+**  retour       : txid de la publication (à parser), NULL si erreur
79
+**======================================================================*/
80
+bool publish_opening_timeout(const char*,const char*, char*);
81
+
82
+#endif

+ 14
- 0
include/statemachine_lib.h View File

@@ -0,0 +1,14 @@
1
+#ifndef STATEMACHINE_LIB_H
2
+#define STATEMACHINE_LIB_H
3
+
4
+#define MAX_TRY_REQUEST 3
5
+#define TIMEOUT_DELAY	10000
6
+
7
+#define DOOR_ID 		"E-209"
8
+
9
+enum State{WAITING_CARD, CARD_AVAILABLE, CARD_READ, REQUEST_SENT, REQUEST_TIMEOUT, REQUEST_ACCEPTED, REQUEST_REJECTED};
10
+
11
+void runStateMachine();
12
+void resetStateMachine();
13
+
14
+#endif

+ 2
- 0
platformio.ini View File

@@ -13,3 +13,5 @@ platform = espressif8266
13 13
 board = nodemcuv2
14 14
 framework = arduino
15 15
 board_build.f_cpu = 160000000L
16
+build_flags = -DDEBUG_ESP_PORT=Serial -DDEBUG_ESP_SSL -DDEBUG_ESP_TLS_MEM -DENABLE_MFRC552 -DENABLE_DOOR -DADD_SSL
17
+;-DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI

+ 0
- 276
src/certs.h View File

@@ -1,276 +0,0 @@
1
-#ifndef CERTS_H
2
-#define CERTS_H
3
-
4
-unsigned char certs_client_bin_crt[] = {
5
-  0x30, 0x82, 0x03, 0xb9, 0x30, 0x82, 0x02, 0xa1, 0xa0, 0x03, 0x02, 0x01,
6
-  0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
7
-  0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x63, 0x31, 0x0b, 0x30,
8
-  0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x46, 0x52, 0x31, 0x0d,
9
-  0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x04, 0x43, 0x68, 0x65,
10
-  0x72, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07,
11
-  0x42, 0x6f, 0x75, 0x72, 0x67, 0x65, 0x73, 0x31, 0x11, 0x30, 0x0f, 0x06,
12
-  0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x49, 0x4e, 0x53, 0x41, 0x2d, 0x43,
13
-  0x56, 0x4c, 0x31, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c,
14
-  0x03, 0x32, 0x53, 0x55, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04,
15
-  0x03, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74,
16
-  0x30, 0x1e, 0x17, 0x0d, 0x31, 0x38, 0x31, 0x32, 0x30, 0x36, 0x31, 0x38,
17
-  0x30, 0x36, 0x32, 0x30, 0x5a, 0x17, 0x0d, 0x31, 0x39, 0x31, 0x32, 0x30,
18
-  0x36, 0x31, 0x38, 0x30, 0x36, 0x32, 0x30, 0x5a, 0x30, 0x60, 0x31, 0x0b,
19
-  0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x46, 0x52, 0x31,
20
-  0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x04, 0x43, 0x68,
21
-  0x65, 0x72, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
22
-  0x07, 0x42, 0x6f, 0x75, 0x72, 0x67, 0x65, 0x73, 0x31, 0x11, 0x30, 0x0f,
23
-  0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x49, 0x4e, 0x53, 0x41, 0x2d,
24
-  0x43, 0x56, 0x4c, 0x31, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x0b,
25
-  0x0c, 0x03, 0x32, 0x53, 0x55, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
26
-  0x04, 0x03, 0x0c, 0x06, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x30, 0x82,
27
-  0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
28
-  0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82,
29
-  0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xe8, 0x23, 0xba, 0x25, 0x34,
30
-  0x87, 0x4a, 0x3e, 0x8f, 0x9f, 0x6e, 0x03, 0xea, 0x9e, 0x9f, 0xd0, 0x40,
31
-  0x8d, 0x78, 0x2d, 0x33, 0x66, 0xa2, 0xda, 0xed, 0xfd, 0x92, 0x12, 0x2d,
32
-  0xd5, 0xa0, 0xc2, 0xb5, 0xd9, 0x7f, 0x85, 0x2e, 0xf9, 0x68, 0x73, 0x6d,
33
-  0x91, 0x8f, 0xf9, 0x5d, 0x44, 0x49, 0xa3, 0xa5, 0x5a, 0xc9, 0x55, 0x42,
34
-  0x4f, 0x77, 0x0b, 0xc3, 0x6d, 0x19, 0x5d, 0x6a, 0x22, 0xa1, 0x66, 0xd9,
35
-  0x2d, 0x5d, 0x3f, 0xda, 0xa0, 0xcd, 0xd0, 0xcf, 0xb8, 0x92, 0xb7, 0x43,
36
-  0x75, 0xda, 0x5a, 0x13, 0x59, 0x42, 0x44, 0xdc, 0xb6, 0x4e, 0xaf, 0x28,
37
-  0xeb, 0x5b, 0x05, 0xea, 0x11, 0xda, 0x7a, 0xc0, 0x48, 0x2b, 0xbd, 0x6a,
38
-  0xf0, 0x10, 0xb4, 0xf2, 0x66, 0xa8, 0xe0, 0x2b, 0x57, 0x6b, 0x25, 0x3e,
39
-  0x28, 0xd2, 0x24, 0x66, 0xdd, 0xcf, 0x72, 0xbc, 0x90, 0x4a, 0x16, 0x48,
40
-  0xbb, 0x16, 0x85, 0x4b, 0x76, 0x37, 0x64, 0x53, 0x74, 0x33, 0xea, 0xe1,
41
-  0x5b, 0xa8, 0x29, 0x19, 0x03, 0xc6, 0x62, 0xec, 0x3d, 0xe5, 0x9c, 0xc3,
42
-  0x62, 0x4b, 0xca, 0x5b, 0xdf, 0x34, 0x51, 0xa6, 0xd6, 0x88, 0x51, 0x69,
43
-  0x6c, 0xf8, 0x38, 0xe7, 0xd3, 0x94, 0x6b, 0xf3, 0x92, 0xe7, 0xcf, 0xea,
44
-  0x9d, 0x60, 0x7d, 0x14, 0x58, 0x3f, 0x0d, 0xe9, 0x69, 0xd2, 0x31, 0x66,
45
-  0xec, 0xec, 0xa4, 0x9c, 0x48, 0xc1, 0x07, 0x62, 0x2c, 0xba, 0x7e, 0xea,
46
-  0xb9, 0x09, 0x81, 0x1f, 0xe5, 0x5d, 0xfa, 0xa8, 0x49, 0x29, 0x43, 0xbe,
47
-  0xab, 0xbe, 0xe3, 0x24, 0xe8, 0x13, 0x0d, 0x13, 0x84, 0x06, 0xaf, 0x08,
48
-  0x5a, 0x92, 0x71, 0xe4, 0xca, 0x5b, 0x3f, 0x79, 0x6c, 0x87, 0x4c, 0x2b,
49
-  0xe4, 0x43, 0xb8, 0x2c, 0x3f, 0xb9, 0x39, 0x64, 0x6f, 0xa6, 0x64, 0x41,
50
-  0x06, 0x92, 0x8e, 0x4b, 0xc4, 0xfb, 0xc7, 0x2b, 0x0f, 0xcc, 0xd1, 0x02,
51
-  0x03, 0x01, 0x00, 0x01, 0xa3, 0x7b, 0x30, 0x79, 0x30, 0x09, 0x06, 0x03,
52
-  0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x2c, 0x06, 0x09, 0x60,
53
-  0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01, 0x0d, 0x04, 0x1f, 0x16, 0x1d,
54
-  0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53, 0x4c, 0x20, 0x47, 0x65, 0x6e, 0x65,
55
-  0x72, 0x61, 0x74, 0x65, 0x64, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66,
56
-  0x69, 0x63, 0x61, 0x74, 0x65, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
57
-  0x04, 0x16, 0x04, 0x14, 0x54, 0x51, 0x24, 0xc1, 0x50, 0x47, 0x40, 0xa3,
58
-  0xc4, 0xe0, 0x56, 0x64, 0xb9, 0xa5, 0xe1, 0xb0, 0xfe, 0xa4, 0x73, 0xba,
59
-  0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
60
-  0x14, 0xfd, 0x47, 0x63, 0x07, 0x27, 0x21, 0x49, 0x64, 0xe9, 0x31, 0xfe,
61
-  0x34, 0x78, 0xb9, 0x7d, 0xd4, 0x60, 0xb2, 0x50, 0x2f, 0x30, 0x0d, 0x06,
62
-  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
63
-  0x03, 0x82, 0x01, 0x01, 0x00, 0x4f, 0x25, 0xdf, 0x17, 0xa3, 0xf7, 0xad,
64
-  0x90, 0xd4, 0xec, 0x35, 0x78, 0x64, 0x61, 0xb6, 0xc0, 0x0c, 0x47, 0x61,
65
-  0x6d, 0x42, 0xe0, 0x97, 0x95, 0x87, 0x87, 0xa0, 0xc9, 0x28, 0x22, 0xb6,
66
-  0xa0, 0x00, 0xc1, 0x42, 0xa7, 0x8e, 0xb8, 0x97, 0x49, 0x5b, 0x28, 0x82,
67
-  0xd9, 0x08, 0x25, 0xe8, 0xd7, 0x69, 0x08, 0x88, 0x8d, 0x6d, 0xd6, 0xa8,
68
-  0x78, 0xd6, 0x8d, 0xc1, 0xdf, 0x77, 0xdc, 0x56, 0x8b, 0x4d, 0xe7, 0xc6,
69
-  0x65, 0xa3, 0x86, 0x03, 0x6c, 0xaa, 0x36, 0x86, 0x9d, 0x2f, 0x00, 0x99,
70
-  0x13, 0xc5, 0x15, 0xe1, 0xfb, 0x67, 0xf5, 0xb5, 0x9d, 0xd2, 0x43, 0x88,
71
-  0x63, 0xca, 0x69, 0xc0, 0x98, 0x5b, 0xb9, 0x7e, 0x32, 0x88, 0x03, 0xa5,
72
-  0x29, 0xec, 0x0f, 0x0b, 0x2c, 0x12, 0x45, 0x6c, 0xa3, 0x2e, 0xff, 0xc7,
73
-  0x7d, 0x11, 0x23, 0x99, 0x6f, 0x0d, 0x03, 0xc1, 0x2e, 0x80, 0xac, 0x69,
74
-  0x06, 0x7a, 0xa9, 0xc9, 0xd6, 0xb3, 0xb7, 0x29, 0xe4, 0xbe, 0x33, 0xaf,
75
-  0x1f, 0x5a, 0x03, 0xb1, 0xf2, 0x8d, 0xc8, 0xec, 0xf4, 0xb8, 0x94, 0xc1,
76
-  0x61, 0x3f, 0xb8, 0xaf, 0x8c, 0x00, 0x38, 0xd3, 0x01, 0x2d, 0xfb, 0x8b,
77
-  0xbf, 0xdb, 0x97, 0xff, 0x15, 0xc1, 0xc7, 0x0e, 0x96, 0x7d, 0x5b, 0xbf,
78
-  0x74, 0x60, 0x5e, 0xe5, 0x7f, 0xb1, 0x26, 0x29, 0xd8, 0x4f, 0x5b, 0xfb,
79
-  0x35, 0x96, 0x21, 0x86, 0x1c, 0xe8, 0x15, 0x67, 0x07, 0xd3, 0x1e, 0x29,
80
-  0xca, 0x6c, 0xf8, 0x50, 0x22, 0xe6, 0x3b, 0xa5, 0xd1, 0xb1, 0x55, 0xd3,
81
-  0x03, 0x9c, 0x3f, 0x4d, 0x6b, 0x54, 0x1e, 0x9a, 0x32, 0x25, 0x26, 0x72,
82
-  0x18, 0xb0, 0xb2, 0xff, 0xce, 0xf3, 0xa0, 0x42, 0x8b, 0xf2, 0xb0, 0xbd,
83
-  0xc9, 0x35, 0xde, 0xdc, 0xa4, 0xa4, 0xbf, 0xee, 0xc5, 0x07, 0x4a, 0x3c,
84
-  0xa8, 0x85, 0x9b, 0x65, 0xcb, 0xa5, 0x11, 0xb0, 0x60
85
-};
86
-unsigned int certs_client_bin_crt_len = 957;
87
-
88
-//CA CERT
89
-const unsigned char caCert[] PROGMEM = {
90
-  0x30, 0x82, 0x03, 0x9c, 0x30, 0x82, 0x02, 0x84, 0xa0, 0x03, 0x02, 0x01,
91
-  0x02, 0x02, 0x09, 0x00, 0xad, 0xed, 0x82, 0x10, 0xcc, 0x63, 0x41, 0x82,
92
-  0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
93
-  0x0b, 0x05, 0x00, 0x30, 0x63, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
94
-  0x04, 0x06, 0x13, 0x02, 0x46, 0x52, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03,
95
-  0x55, 0x04, 0x08, 0x0c, 0x04, 0x43, 0x68, 0x65, 0x72, 0x31, 0x10, 0x30,
96
-  0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x75, 0x72,
97
-  0x67, 0x65, 0x73, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a,
98
-  0x0c, 0x08, 0x49, 0x4e, 0x53, 0x41, 0x2d, 0x43, 0x56, 0x4c, 0x31, 0x0c,
99
-  0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x03, 0x32, 0x53, 0x55,
100
-  0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x09, 0x6c,
101
-  0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x1e, 0x17, 0x0d,
102
-  0x31, 0x38, 0x31, 0x32, 0x30, 0x36, 0x31, 0x36, 0x34, 0x38, 0x30, 0x30,
103
-  0x5a, 0x17, 0x0d, 0x32, 0x33, 0x31, 0x32, 0x30, 0x35, 0x31, 0x36, 0x34,
104
-  0x38, 0x30, 0x30, 0x5a, 0x30, 0x63, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
105
-  0x55, 0x04, 0x06, 0x13, 0x02, 0x46, 0x52, 0x31, 0x0d, 0x30, 0x0b, 0x06,
106
-  0x03, 0x55, 0x04, 0x08, 0x0c, 0x04, 0x43, 0x68, 0x65, 0x72, 0x31, 0x10,
107
-  0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x75,
108
-  0x72, 0x67, 0x65, 0x73, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04,
109
-  0x0a, 0x0c, 0x08, 0x49, 0x4e, 0x53, 0x41, 0x2d, 0x43, 0x56, 0x4c, 0x31,
110
-  0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x03, 0x32, 0x53,
111
-  0x55, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x09,
112
-  0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x82, 0x01,
113
-  0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
114
-  0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01,
115
-  0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb7, 0xd4, 0x52, 0xd5, 0x58, 0xac,
116
-  0x0a, 0x1d, 0x71, 0x8f, 0x1a, 0x68, 0xff, 0x0f, 0xfa, 0x2f, 0xa3, 0x0c,
117
-  0x09, 0xca, 0xfb, 0xd5, 0x7e, 0x4c, 0xc0, 0x6a, 0xc6, 0xa0, 0xc0, 0x49,
118
-  0x7d, 0x17, 0xae, 0xba, 0x9e, 0x51, 0x6f, 0x6d, 0xe2, 0x62, 0x03, 0x07,
119
-  0x70, 0xbe, 0x56, 0x53, 0xb9, 0xad, 0xe5, 0x69, 0xa4, 0xd5, 0x91, 0x43,
120
-  0x5e, 0x67, 0x4d, 0x77, 0x26, 0x29, 0x25, 0xbd, 0xaa, 0xbb, 0x31, 0x34,
121
-  0xf3, 0x0f, 0x0a, 0xa6, 0xbf, 0x8e, 0xdb, 0x7e, 0x4e, 0xd5, 0x8d, 0x8c,
122
-  0x60, 0x5d, 0x89, 0xc5, 0x8e, 0x6a, 0xc2, 0xa1, 0xf2, 0x34, 0xb4, 0x83,
123
-  0xae, 0xa1, 0x74, 0x53, 0xe8, 0x34, 0x2d, 0x8e, 0x0e, 0xb8, 0x2d, 0x14,
124
-  0xcb, 0xe9, 0x85, 0xb7, 0x3d, 0xc9, 0x8e, 0xfd, 0x77, 0x27, 0x3a, 0x5a,
125
-  0xa9, 0x5c, 0xd9, 0xbf, 0x93, 0x00, 0x9a, 0x53, 0xd7, 0xff, 0xca, 0x77,
126
-  0x0b, 0x3f, 0xf0, 0x62, 0xd5, 0xa2, 0xe0, 0x3a, 0xfe, 0xb9, 0x8a, 0xb6,
127
-  0x1d, 0x2d, 0x49, 0xcc, 0x3d, 0x2a, 0xad, 0x5c, 0x7b, 0x8d, 0x42, 0xbd,
128
-  0x2c, 0x95, 0x6b, 0x59, 0x58, 0x19, 0xcf, 0x22, 0xb0, 0x9c, 0xca, 0xd4,
129
-  0x54, 0x5b, 0x2d, 0xf1, 0xcf, 0x40, 0x74, 0x3e, 0x0a, 0x08, 0xad, 0x47,
130
-  0xd0, 0x72, 0x5d, 0x4e, 0x57, 0xe9, 0x3b, 0x25, 0x96, 0x43, 0x35, 0xb0,
131
-  0x32, 0x17, 0xc0, 0xf3, 0xbb, 0x5c, 0x15, 0x47, 0xeb, 0xf0, 0xb5, 0x14,
132
-  0x8e, 0x27, 0x0f, 0x00, 0x1a, 0x64, 0x89, 0xed, 0x3e, 0xcf, 0x8b, 0xb3,
133
-  0x80, 0x86, 0xb8, 0x6a, 0x30, 0x23, 0xf8, 0xad, 0x72, 0xfa, 0xde, 0x48,
134
-  0x42, 0xff, 0xb7, 0x54, 0xc5, 0x7a, 0x06, 0x42, 0x38, 0x35, 0x69, 0x3e,
135
-  0xba, 0x0e, 0xdb, 0x5e, 0x0e, 0xa1, 0xbd, 0x11, 0xf9, 0x65, 0x8d, 0x32,
136
-  0xce, 0xb7, 0xb0, 0xe8, 0x5c, 0x4c, 0x9e, 0xff, 0x13, 0x47, 0x02, 0x03,
137
-  0x01, 0x00, 0x01, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x1d, 0x06, 0x03, 0x55,
138
-  0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xfd, 0x47, 0x63, 0x07, 0x27, 0x21,
139
-  0x49, 0x64, 0xe9, 0x31, 0xfe, 0x34, 0x78, 0xb9, 0x7d, 0xd4, 0x60, 0xb2,
140
-  0x50, 0x2f, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30,
141
-  0x16, 0x80, 0x14, 0xfd, 0x47, 0x63, 0x07, 0x27, 0x21, 0x49, 0x64, 0xe9,
142
-  0x31, 0xfe, 0x34, 0x78, 0xb9, 0x7d, 0xd4, 0x60, 0xb2, 0x50, 0x2f, 0x30,
143
-  0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30,
144
-  0x03, 0x01, 0x01, 0xff, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
145
-  0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
146
-  0x6f, 0xdd, 0x8c, 0xd1, 0xfd, 0xef, 0x84, 0x26, 0x40, 0x4b, 0x19, 0xd1,
147
-  0x0e, 0x32, 0xe8, 0x8a, 0x1b, 0xb4, 0xdd, 0x3e, 0x0d, 0x31, 0xf3, 0xec,
148
-  0x20, 0xc8, 0x6c, 0xe7, 0x0f, 0x05, 0x59, 0x0d, 0x03, 0xe3, 0x47, 0xe4,
149
-  0x22, 0x34, 0xb1, 0x95, 0xbc, 0xe1, 0x1f, 0x29, 0x04, 0x02, 0x5b, 0x99,
150
-  0x2c, 0x43, 0xf5, 0x5e, 0xdf, 0x76, 0xc9, 0x13, 0x6e, 0xb2, 0x15, 0xb2,
151
-  0x9f, 0xda, 0x39, 0x3a, 0xbd, 0x8f, 0x8b, 0x7e, 0x21, 0x89, 0x77, 0x15,
152
-  0x1a, 0x31, 0xe5, 0x0a, 0xe4, 0xe2, 0x83, 0xee, 0xf7, 0xc2, 0xbf, 0x0e,
153
-  0x91, 0xad, 0x93, 0x8b, 0xea, 0xb9, 0xdc, 0x04, 0xd6, 0xd8, 0xe6, 0x31,
154
-  0x97, 0x79, 0x26, 0xbf, 0x8f, 0xfd, 0xef, 0xfb, 0xf4, 0x23, 0x70, 0xbb,
155
-  0x47, 0x71, 0xae, 0x1f, 0x36, 0x01, 0x0a, 0xa2, 0xb0, 0xeb, 0x95, 0xf3,
156
-  0x8e, 0x22, 0x83, 0x9e, 0x36, 0xff, 0x96, 0x13, 0x2d, 0x03, 0x9b, 0x46,
157
-  0xe3, 0xc0, 0xae, 0xfc, 0x94, 0xea, 0x22, 0xa1, 0x07, 0xbe, 0xf5, 0x31,
158
-  0xc2, 0x68, 0xef, 0xfb, 0x3f, 0xf4, 0x0d, 0x9d, 0xb6, 0x7a, 0xda, 0x26,
159
-  0x65, 0xc3, 0xb6, 0x19, 0xfa, 0x75, 0x0a, 0xc2, 0xb6, 0x62, 0x4e, 0xb1,
160
-  0x7c, 0x31, 0x6e, 0x09, 0x0f, 0x2c, 0x06, 0x2e, 0xd8, 0x19, 0x21, 0xe3,
161
-  0x00, 0x6a, 0x07, 0x24, 0xc8, 0x55, 0xa8, 0x07, 0xa0, 0xec, 0xf5, 0x28,
162
-  0xcc, 0x8c, 0x0d, 0x96, 0x35, 0xbf, 0x9d, 0x11, 0xd8, 0x92, 0x99, 0xf7,
163
-  0xf2, 0xe6, 0xb0, 0x84, 0x30, 0xa3, 0x1b, 0x63, 0x02, 0xde, 0x71, 0x3b,
164
-  0x6c, 0x21, 0x43, 0x1b, 0x2c, 0xa0, 0xc7, 0x67, 0xe1, 0x7c, 0x0b, 0x0a,
165
-  0x4d, 0x05, 0x08, 0xba, 0xdf, 0x16, 0xaf, 0x7d, 0xd1, 0x0f, 0xf8, 0x33,
166
-  0x0a, 0x93, 0xbf, 0x12, 0xdb, 0x78, 0x0b, 0x00, 0x02, 0x60, 0xd7, 0x5b,
167
-  0xa1, 0xa1, 0x64, 0x63
168
-};
169
-
170
-const unsigned int caCertLen = 928;
171
-
172
-unsigned char private_client_bin_key[] = {
173
-  0x30, 0x82, 0x04, 0xa4, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
174
-  0xe8, 0x23, 0xba, 0x25, 0x34, 0x87, 0x4a, 0x3e, 0x8f, 0x9f, 0x6e, 0x03,
175
-  0xea, 0x9e, 0x9f, 0xd0, 0x40, 0x8d, 0x78, 0x2d, 0x33, 0x66, 0xa2, 0xda,
176
-  0xed, 0xfd, 0x92, 0x12, 0x2d, 0xd5, 0xa0, 0xc2, 0xb5, 0xd9, 0x7f, 0x85,
177
-  0x2e, 0xf9, 0x68, 0x73, 0x6d, 0x91, 0x8f, 0xf9, 0x5d, 0x44, 0x49, 0xa3,
178
-  0xa5, 0x5a, 0xc9, 0x55, 0x42, 0x4f, 0x77, 0x0b, 0xc3, 0x6d, 0x19, 0x5d,
179
-  0x6a, 0x22, 0xa1, 0x66, 0xd9, 0x2d, 0x5d, 0x3f, 0xda, 0xa0, 0xcd, 0xd0,
180
-  0xcf, 0xb8, 0x92, 0xb7, 0x43, 0x75, 0xda, 0x5a, 0x13, 0x59, 0x42, 0x44,
181
-  0xdc, 0xb6, 0x4e, 0xaf, 0x28, 0xeb, 0x5b, 0x05, 0xea, 0x11, 0xda, 0x7a,
182
-  0xc0, 0x48, 0x2b, 0xbd, 0x6a, 0xf0, 0x10, 0xb4, 0xf2, 0x66, 0xa8, 0xe0,
183
-  0x2b, 0x57, 0x6b, 0x25, 0x3e, 0x28, 0xd2, 0x24, 0x66, 0xdd, 0xcf, 0x72,
184
-  0xbc, 0x90, 0x4a, 0x16, 0x48, 0xbb, 0x16, 0x85, 0x4b, 0x76, 0x37, 0x64,
185
-  0x53, 0x74, 0x33, 0xea, 0xe1, 0x5b, 0xa8, 0x29, 0x19, 0x03, 0xc6, 0x62,
186
-  0xec, 0x3d, 0xe5, 0x9c, 0xc3, 0x62, 0x4b, 0xca, 0x5b, 0xdf, 0x34, 0x51,
187
-  0xa6, 0xd6, 0x88, 0x51, 0x69, 0x6c, 0xf8, 0x38, 0xe7, 0xd3, 0x94, 0x6b,
188
-  0xf3, 0x92, 0xe7, 0xcf, 0xea, 0x9d, 0x60, 0x7d, 0x14, 0x58, 0x3f, 0x0d,
189
-  0xe9, 0x69, 0xd2, 0x31, 0x66, 0xec, 0xec, 0xa4, 0x9c, 0x48, 0xc1, 0x07,
190
-  0x62, 0x2c, 0xba, 0x7e, 0xea, 0xb9, 0x09, 0x81, 0x1f, 0xe5, 0x5d, 0xfa,
191
-  0xa8, 0x49, 0x29, 0x43, 0xbe, 0xab, 0xbe, 0xe3, 0x24, 0xe8, 0x13, 0x0d,
192
-  0x13, 0x84, 0x06, 0xaf, 0x08, 0x5a, 0x92, 0x71, 0xe4, 0xca, 0x5b, 0x3f,
193
-  0x79, 0x6c, 0x87, 0x4c, 0x2b, 0xe4, 0x43, 0xb8, 0x2c, 0x3f, 0xb9, 0x39,
194
-  0x64, 0x6f, 0xa6, 0x64, 0x41, 0x06, 0x92, 0x8e, 0x4b, 0xc4, 0xfb, 0xc7,
195
-  0x2b, 0x0f, 0xcc, 0xd1, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
196
-  0x01, 0x00, 0xdd, 0xf9, 0x43, 0xc6, 0xcd, 0xca, 0xcc, 0x40, 0xb4, 0x3f,
197
-  0x24, 0x08, 0xd4, 0xb1, 0x67, 0x69, 0xd0, 0x40, 0x36, 0x79, 0x2a, 0x5c,
198
-  0x6b, 0x3f, 0x63, 0x31, 0x04, 0x1b, 0x23, 0xfe, 0xd1, 0xa1, 0x3e, 0x18,
199
-  0x42, 0xea, 0xcc, 0x77, 0x2a, 0xdc, 0xad, 0x19, 0xf2, 0x8c, 0xe5, 0x15,
200
-  0x26, 0x41, 0x38, 0x1d, 0x6e, 0xc5, 0x7b, 0x09, 0xd7, 0x70, 0xc5, 0x27,
201
-  0x2e, 0x88, 0xf7, 0xb3, 0x33, 0x98, 0xcf, 0xf4, 0xa3, 0xe5, 0x2a, 0x1e,
202
-  0x77, 0x8f, 0xc3, 0x00, 0xe0, 0x32, 0x23, 0x6c, 0xaf, 0x25, 0x50, 0xb9,
203
-  0x1a, 0x38, 0x14, 0xc0, 0xb2, 0xce, 0xa5, 0x17, 0x20, 0x4b, 0xb0, 0x7d,
204
-  0xef, 0xe7, 0x4e, 0xb3, 0xf4, 0x37, 0x44, 0xe9, 0x57, 0x60, 0x5f, 0x25,
205
-  0xb1, 0x37, 0xe1, 0xd9, 0xce, 0x97, 0xa7, 0x2f, 0x8b, 0x79, 0xc9, 0x6f,
206
-  0x32, 0x2d, 0xa9, 0x87, 0x63, 0x80, 0x99, 0x7d, 0x64, 0x52, 0xb1, 0x16,
207
-  0x5a, 0x00, 0x1c, 0x4f, 0x8c, 0xf0, 0x54, 0x46, 0x84, 0x7d, 0x46, 0xa5,
208
-  0x48, 0xc3, 0xeb, 0x99, 0x2d, 0x7b, 0x5f, 0xcc, 0x00, 0x43, 0x06, 0x97,
209
-  0x68, 0x14, 0xdf, 0xaa, 0x9a, 0xc2, 0xb1, 0xfb, 0x32, 0xc1, 0x1a, 0x20,
210
-  0x27, 0xa7, 0xe1, 0x36, 0xfd, 0xd8, 0x2c, 0xf1, 0xe2, 0xf8, 0x30, 0x8d,
211
-  0x7f, 0x16, 0xb5, 0x10, 0xc5, 0x60, 0x8b, 0xae, 0x94, 0x6f, 0x9e, 0xbe,
212
-  0x09, 0x54, 0x99, 0x16, 0xa2, 0x38, 0x3d, 0xef, 0xab, 0x32, 0x19, 0xa1,
213
-  0x96, 0xda, 0xcb, 0x2c, 0xc4, 0x20, 0x0f, 0xff, 0xc6, 0xfa, 0x4d, 0xfd,
214
-  0x91, 0x4e, 0x1f, 0xd1, 0x76, 0x79, 0x7a, 0x3e, 0x2e, 0x0e, 0x8d, 0x2e,
215
-  0xce, 0xe1, 0x47, 0x7b, 0x41, 0xba, 0x0f, 0x91, 0x09, 0x67, 0x0d, 0xb2,
216
-  0x87, 0xbc, 0xd9, 0x44, 0xa2, 0xa5, 0x83, 0xb6, 0x50, 0x45, 0x85, 0x8d,
217
-  0x01, 0xc0, 0xfc, 0x74, 0x48, 0x3d, 0x02, 0x81, 0x81, 0x00, 0xf4, 0x32,
218
-  0x6b, 0xd6, 0x5d, 0x17, 0xe2, 0x96, 0x1d, 0x04, 0xfe, 0x39, 0xc4, 0xee,
219
-  0x49, 0xbf, 0x01, 0xc2, 0x01, 0x80, 0x92, 0x84, 0x7e, 0x3a, 0x56, 0xa0,
220
-  0xfc, 0xf4, 0xa7, 0x4d, 0xbd, 0x42, 0x35, 0x06, 0xa2, 0x7b, 0xfc, 0x29,
221
-  0x4c, 0xf2, 0xe6, 0x24, 0x34, 0xd0, 0x7f, 0x1d, 0xad, 0x0a, 0x45, 0xc2,
222
-  0x1d, 0x33, 0x53, 0xee, 0x2f, 0x4e, 0x24, 0xaa, 0x1a, 0xe0, 0x7b, 0xbb,
223
-  0x53, 0x7e, 0x1f, 0xbd, 0xf0, 0xfc, 0xb0, 0x76, 0x95, 0x83, 0x03, 0x94,
224
-  0x44, 0xc0, 0x76, 0x96, 0x85, 0xa1, 0xc5, 0x40, 0xbe, 0x23, 0xf4, 0x3c,
225
-  0xac, 0xe0, 0xb6, 0x95, 0x3e, 0x56, 0xe4, 0x5c, 0x92, 0x09, 0xda, 0xd4,
226
-  0x1d, 0x7c, 0x25, 0x42, 0xa4, 0xfa, 0xff, 0x95, 0x3f, 0x63, 0x18, 0x3b,
227
-  0xab, 0x5c, 0x01, 0xdd, 0x27, 0x1b, 0xc5, 0x5a, 0x8e, 0x0e, 0xbb, 0x1a,
228
-  0x4e, 0x87, 0x7b, 0xc6, 0x15, 0x43, 0x02, 0x81, 0x81, 0x00, 0xf3, 0x5c,
229
-  0x1d, 0x00, 0x46, 0x24, 0xc9, 0xc2, 0x89, 0x50, 0x53, 0x20, 0xaf, 0x13,
230
-  0xb0, 0x11, 0x0c, 0x29, 0x36, 0x73, 0x6f, 0x55, 0x80, 0xfe, 0xe8, 0x18,
231
-  0x1c, 0xa7, 0x62, 0x6f, 0xb6, 0x8f, 0x28, 0xa4, 0xaf, 0xc7, 0xb9, 0xe2,
232
-  0x1d, 0x72, 0x03, 0xaf, 0x99, 0x44, 0xe9, 0x82, 0xdc, 0xf9, 0x19, 0x11,
233
-  0x26, 0xe5, 0xe9, 0x46, 0x35, 0xb1, 0x2f, 0x01, 0xeb, 0x4f, 0x30, 0xdd,
234
-  0x97, 0xe1, 0x2b, 0x37, 0x5e, 0x1c, 0xc1, 0xec, 0xf4, 0xb0, 0xa8, 0x7a,
235
-  0x31, 0x48, 0x66, 0xe5, 0x44, 0xbc, 0xa5, 0x32, 0x4a, 0x2f, 0x1f, 0x1e,
236
-  0xda, 0xca, 0x9d, 0x57, 0x02, 0x1c, 0x9a, 0x05, 0x15, 0x7a, 0x76, 0x2f,
237
-  0xc2, 0x4d, 0x9f, 0xad, 0xa4, 0xbd, 0xc6, 0x1b, 0x8c, 0x8f, 0xb6, 0x04,
238
-  0x01, 0xe7, 0xfc, 0x5e, 0xa0, 0x09, 0x67, 0x61, 0xcc, 0xc1, 0x00, 0x2e,
239
-  0xea, 0xf9, 0x73, 0xfd, 0xea, 0x5b, 0x02, 0x81, 0x80, 0x05, 0x8d, 0x47,
240
-  0xea, 0x5d, 0xbf, 0x77, 0x45, 0x56, 0x6f, 0x90, 0xa0, 0x1b, 0x9c, 0x17,
241
-  0x4d, 0x22, 0x6b, 0x18, 0xc1, 0xa5, 0x93, 0x21, 0x93, 0x8d, 0xf8, 0xa7,
242
-  0x07, 0xa0, 0xee, 0x21, 0x89, 0x69, 0xcf, 0x36, 0xce, 0x7e, 0xd3, 0x29,
243
-  0x8e, 0xb4, 0x30, 0xca, 0x8a, 0x9a, 0xf9, 0x3f, 0xbf, 0xe6, 0x0a, 0x1d,
244
-  0xc3, 0xd6, 0xfa, 0xa9, 0x54, 0xab, 0x7c, 0xba, 0x93, 0xb3, 0xfc, 0x74,
245
-  0x4f, 0x7f, 0xc5, 0x3f, 0xbf, 0x5e, 0xfb, 0xb3, 0x36, 0x81, 0x54, 0x53,
246
-  0xa3, 0xd4, 0x8e, 0x63, 0x87, 0x64, 0x06, 0xae, 0xcb, 0xfe, 0x77, 0x8a,
247
-  0xae, 0xa9, 0x38, 0x07, 0x6c, 0xce, 0x50, 0x54, 0x79, 0xa8, 0xe1, 0xfd,
248
-  0xb1, 0x9e, 0xdf, 0x3f, 0x04, 0xfd, 0xde, 0x94, 0xcc, 0x59, 0xf6, 0xb1,
249
-  0x54, 0xac, 0x2e, 0x2c, 0xff, 0x72, 0xa3, 0x36, 0x6b, 0x9b, 0x51, 0x63,
250
-  0xcb, 0xac, 0xc2, 0xba, 0x25, 0x02, 0x81, 0x80, 0x09, 0xce, 0x8f, 0x78,
251
-  0xa0, 0x5b, 0x01, 0x2a, 0x6e, 0xc0, 0x26, 0xe9, 0x6d, 0x44, 0xbc, 0xd6,
252
-  0xa3, 0x07, 0xd2, 0x22, 0xea, 0xbc, 0xa6, 0x5b, 0x1f, 0x5d, 0x41, 0x62,
253
-  0x59, 0x6c, 0x29, 0x0e, 0x31, 0xd4, 0x19, 0x25, 0x52, 0x4f, 0x53, 0x62,
254
-  0x71, 0xd5, 0x94, 0xb6, 0x9b, 0x36, 0x63, 0xd3, 0x41, 0xf8, 0x57, 0xf9,
255
-  0xf1, 0xe1, 0x9b, 0x6e, 0x4d, 0x77, 0xa5, 0x54, 0xe6, 0xa2, 0x37, 0x69,
256
-  0xa2, 0x85, 0x33, 0xac, 0xb2, 0xff, 0xea, 0x88, 0x67, 0xf1, 0xac, 0xa4,
257
-  0xbf, 0x48, 0xc3, 0xf1, 0x5a, 0x32, 0xe1, 0x9c, 0x6e, 0x83, 0x9e, 0xcb,
258
-  0x85, 0x75, 0xa9, 0x27, 0x1f, 0xe9, 0xce, 0x88, 0xc5, 0xd4, 0x7c, 0xdf,
259
-  0x05, 0xed, 0x48, 0x73, 0x41, 0x55, 0x80, 0xdf, 0x95, 0xb5, 0x29, 0xa2,
260
-  0x80, 0xa0, 0x0b, 0x90, 0xf5, 0x72, 0x92, 0x7e, 0xc0, 0x94, 0x23, 0x71,
261
-  0xf4, 0x03, 0xaa, 0x7b, 0x02, 0x81, 0x81, 0x00, 0xd9, 0x31, 0xb1, 0xbd,
262
-  0x33, 0x0c, 0x83, 0x16, 0x2a, 0x2d, 0xbe, 0xde, 0x26, 0x15, 0x94, 0xe0,
263
-  0x5d, 0x1e, 0x3d, 0x3b, 0xb7, 0x6f, 0x1b, 0x89, 0x14, 0x3d, 0x75, 0x19,
264
-  0x25, 0x08, 0xd4, 0xfa, 0x6b, 0x12, 0x37, 0x49, 0xca, 0x4a, 0x21, 0xd8,
265
-  0x83, 0x86, 0xf6, 0x29, 0x64, 0x48, 0xd5, 0xce, 0x5e, 0x5f, 0xf4, 0x2f,
266
-  0x07, 0x5a, 0x26, 0xc7, 0x8c, 0x1a, 0x8c, 0x6e, 0xcd, 0x30, 0x4a, 0x9a,
267
-  0xd5, 0xb6, 0x4d, 0x06, 0x1d, 0xc1, 0xbe, 0xb4, 0x63, 0xf8, 0x95, 0xbf,
268
-  0xb9, 0xc4, 0xd2, 0x69, 0xc1, 0x80, 0x48, 0xb8, 0x26, 0xa5, 0x1f, 0x5d,
269
-  0x3b, 0x85, 0xd5, 0xda, 0xd3, 0x01, 0x80, 0xbb, 0xa3, 0xce, 0xa4, 0x20,
270
-  0xaf, 0x41, 0x04, 0xf2, 0x6c, 0xfc, 0x8a, 0xc2, 0x02, 0x3e, 0x9a, 0xc3,
271
-  0xdd, 0xf6, 0x86, 0x95, 0xbf, 0xa5, 0xdf, 0x2e, 0xc1, 0xe8, 0xec, 0x2f,
272
-  0xd3, 0xef, 0x70, 0x7b
273
-};
274
-unsigned int private_client_bin_key_len = 1192;
275
-
276
-#endif

+ 123
- 0
src/connect_lib.cpp View File

@@ -0,0 +1,123 @@
1
+#include <ESP8266WiFi.h>
2
+#include <time.h>
3
+#include "connect_lib.h"
4
+#include "certs.h"
5
+
6
+#ifdef ADD_SSL
7
+#include <WiFiClientSecure.h>
8
+extern WiFiClientSecure client;
9
+extern BearSSL::Session session;
10
+#else
11
+extern WiFiClient client;
12
+#endif	
13
+
14
+// const char* ssid = "Honor9";
15
+// const char* password = "test123456";
16
+
17
+// const char* host = "192.168.43.203";
18
+// const int httpsPort = 443;
19
+
20
+const char* ssid = "Livebox-9656";
21
+const char* password = "zovCVnFwAqxTjzTN6D";
22
+
23
+const char* host = "192.168.1.21";
24
+const int httpsPort = 4000;
25
+
26
+// const char* ssid = "wifi-2su";
27
+// const char* password = "cle wifi 2su";
28
+
29
+// const char* host = "172.30.5.104";
30
+// const int httpsPort = 4000;
31
+
32
+
33
+bool connectToWIfi(){
34
+  Serial.println();
35
+  Serial.print("connecting to ");
36
+  Serial.println(ssid);
37
+  WiFi.mode(WIFI_STA);
38
+  WiFi.begin(ssid, password);
39
+  long start=millis();
40
+  while (WiFi.status() != WL_CONNECTED) {
41
+  	if(millis()-start > TIMEOUT_WIFI) return false;
42
+    delay(500);
43
+    Serial.print(".");
44
+  }
45
+  Serial.println("");
46
+  Serial.println("WiFi connected");
47
+  Serial.println("IP address: ");
48
+  Serial.println(WiFi.localIP());
49
+  return true;
50
+}
51
+
52
+
53
+bool connectToServer(){
54
+  // Connect to remote server
55
+  Serial.print("connecting to ");
56
+  Serial.println(host);
57
+
58
+  unsigned long t=millis();
59
+  #ifdef ADD_SSL
60
+  client.setSession(&session);
61
+  #endif
62
+  if (!client.connect(host, httpsPort)) {
63
+    Serial.println("connection failed");
64
+    return false;
65
+  }
66
+
67
+  #ifdef ADD_SSL
68
+  // Verify validity of server's certificate
69
+  if (client.verifyCertChain(host)) {
70
+    Serial.println("Server certificate verified");
71
+  } else {
72
+    Serial.println("ERROR: certificate verification failed!");
73
+    return false;
74
+  }
75
+  #endif
76
+  Serial.printf("connect time = %d ms\n",millis()-t);
77
+  return true;
78
+}
79
+
80
+
81
+bool addSSL(){
82
+  Serial.print("Setting time using SNTP");
83
+  configTime(0, 0, "pool.ntp.org", "time.nist.gov");
84
+  time_t now = time(nullptr);
85
+  while (now < 8 * 3600 * 2) {
86
+    delay(500);
87
+    Serial.print(".");
88
+    now = time(nullptr);
89
+  }
90
+  Serial.println("");
91
+  struct tm timeinfo;
92
+  gmtime_r(&now, &timeinfo);
93
+  Serial.print("Current time: ");
94
+  Serial.print(asctime(&timeinfo));
95
+
96
+  #ifdef ADD_SSL
97
+  bool res0 = client.setCertificate(certs_client_bin_crt, certs_client_bin_crt_len);
98
+  if (!res0) {
99
+    Serial.println("Failed to load client certificate!");
100
+    return false;
101
+    // while (true) {
102
+    //   yield();
103
+    // }
104
+  }
105
+  bool res1 = client.setPrivateKey(private_client_bin_key, private_client_bin_key_len);
106
+  if (!res1) {
107
+    Serial.println("Failed to load client private key!");
108
+    return false;
109
+    // while (true) {
110
+    //   yield();
111
+    // }
112
+  }
113
+  bool res = client.setCACert_P(caCert, caCertLen);
114
+  if (!res) {
115
+    Serial.println("Failed to load root CA certificate!");
116
+    return false;
117
+    // while (true) {
118
+    //   yield();
119
+    // }
120
+  }
121
+  #endif
122
+  return true;
123
+}

+ 14
- 0
src/door_lib.cpp View File

@@ -0,0 +1,14 @@
1
+#include <Arduino.h>
2
+#include "door_lib.h"
3
+
4
+void setupDoor(){
5
+	pinMode(DOOR_PIN, OUTPUT);
6
+}
7
+
8
+
9
+void openDoor(){
10
+	// Open door for X seconds and write result blockchain
11
+	digitalWrite(DOOR_PIN, HIGH);
12
+	delay(2000);
13
+	digitalWrite(DOOR_PIN, LOW);
14
+}

+ 76
- 0
src/http_cli_lib.cpp View File

@@ -0,0 +1,76 @@
1
+#include <Arduino.h>
2
+#include <ESP8266WiFi.h>
3
+#include "http_cli_lib.h"
4
+#include "connect_lib.h"
5
+
6
+extern WiFiClient client;
7
+extern const char* host;
8
+const int httpsPort=4000;
9
+
10
+bool send_post_request(const char* data, const char* url, char *res){
11
+	if (!client.connected()) {
12
+	    Serial.println("Error : client not connected to server !");
13
+      bool connected=false;
14
+      for(int i=0; i<MAX_RECONNECT_TRY && !connected; i++){
15
+        connected=connectToServer();
16
+      }
17
+      if(!connected){
18
+        Serial.println("Error : could not reconnect to server !");
19
+        return false;
20
+      }
21
+      else{
22
+        Serial.println("Reconnected !");
23
+      }
24
+	}
25
+  unsigned long t=millis();
26
+  String content = String("POST ") +url + " HTTP/1.1\r\n" +
27
+                "Host: " + host +":"+httpsPort +"\r\n" +
28
+                "User-Agent: BuildFailureDetectorESP8266\r\n" +
29
+                "content-type: application/json\r\n" +
30
+                "json: true\r\n" +
31
+                "content-length: "+strlen(data)+"\r\n" +
32
+                // "content-length: "+53+"\r\n" +
33
+               // "Authorization: Basic dXNlcjphemVydHkK==\r\n" +
34
+                "Connection: keep-alive\r\n\r\n"+
35
+                data+"\r\n";
36
+                // "{\"user\":\"rom\",\"door\":\"E209\",\"timestamp\":\"1548355295\"}\r\n";
37
+	   client.print(content);
38
+     Serial.printf("REQ = %s\n",content.c_str());
39
+
40
+   Serial.println("POST request sent");
41
+
42
+  //delay(200);
43
+  while (client.connected()) {
44
+   if (client.available()) { 
45
+    String line = client.readStringUntil('\n');
46
+    //Serial.printf("header line = %s\n",line.c_str());
47
+    if (line == "\r") {
48
+      Serial.println("headers received");
49
+      break;
50
+    }
51
+  }
52
+}
53
+  String content2;
54
+  // if (client.available()) Serial.println("AVAILABLE !");
55
+  // else Serial.println("NOT AVAILABLE !");
56
+
57
+  while (client.available()) {
58
+    // char c = client.read();
59
+    // // Serial.printf("c = %c , 0x",c);
60
+    // // Serial.println(c,HEX);
61
+    // content2 +=c;
62
+    content2+= client.readStringUntil('\n');
63
+  }
64
+
65
+   int delta=millis()-t;
66
+   Serial.printf("T = %d ms\n",delta);
67
+   Serial.println("reply was:");
68
+   Serial.println("==========");
69
+   Serial.println(content2);
70
+   Serial.println("==========");
71
+   Serial.println();
72
+
73
+   strcpy(res,content2.c_str());
74
+
75
+   return true;
76
+}

+ 36
- 294
src/main.cpp View File

@@ -1,317 +1,53 @@
1 1
 #include <Arduino.h>
2
-#include <time.h>
3 2
 #include <ESP8266WiFi.h>
4
-#include <WiFiClientSecure.h>
5 3
 #include <SPI.h>
6 4
 #include <MFRC522.h>
7
-#include "certs.h"
8
-
9
-#define RST_PIN         0          // Configurable, see typical pin layout above
10
-#define SS_PIN          2         // Configurable, see typical pin layout above
11
-#define IRQ_PIN			4
5
+#include "statemachine_lib.h"
6
+#include "connect_lib.h"
7
+#ifdef ADD_SSL
8
+#include <WiFiClientSecure.h>
9
+#endif
12 10
 
13
-#define DOOR_PIN		5
11
+#ifdef ENABLE_MFRC552
12
+#include "mrfc552_lib.h"
13
+#endif
14
+#ifdef ENABLE_DOOR
15
+#include "door_lib.h"
16
+#endif
14 17
 
15
-#define MAX_TRY_REQUEST 3
16
-#define TIMEOUT_DELAY	5000
17
-#define TIMEOUT_WIFI	5000
18 18
 
19
+#ifdef ENABLE_MFRC552
19 20
 MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create mfrc522 instance
20 21
 MFRC522::MIFARE_Key key;
22
+#endif
21 23
 
22
-unsigned long start_timer=0;
23
-
24
-enum state{WAITING_CARD, CARD_AVAILABLE, CARD_READ, REQUEST_SENT, REQUEST_TIMEOUT, REQUEST_ACCEPTED, REQUEST_REJECTED} current_state, last_state;
25
-volatile bool card_available = false;
26
-volatile bool response_available = false;
27
-
28
-const char* ssid = "Honor9";
29
-const char* password = "test123456";
30
-
31
-const char* host = "192.168.43.203";
32
-const int httpsPort = 443;
33
-
24
+#ifdef ADD_SSL
34 25
 WiFiClientSecure client;
26
+BearSSL::Session session;
27
+#else
28
+WiFiClient client;
29
+#endif	
35 30
 
36
-/*
37
- * The function to clear the pending interrupt bits after interrupt serving routine
38
- */
39
-void clearInt() {
40
-  mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x7F);
41
-}
42
-
43
-/*
44
- * The function sending to the MFRC522 the needed commands to activate the reception
45
- */
46
-void activateRec() {
47
-  mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg, mfrc522.PICC_CMD_REQA);
48
-  mfrc522.PCD_WriteRegister(mfrc522.CommandReg, mfrc522.PCD_Transceive);
49
-  mfrc522.PCD_WriteRegister(mfrc522.BitFramingReg, 0x87);
50
-}
51
-
52
-void resetStateMachine(){
53
-	clearInt();
54
-    mfrc522.PICC_HaltA();
55
-    card_available = false;
56
-    current_state=WAITING_CARD;
57
-    Serial.println("\n---- RESET ST_MACHINE ----");
58
-}
59
-
60
-
61
-void IRQ_cardAvailable(){
62
-	card_available=true;
63
-}
64
-
65
-void openDoor(){
66
-	// Open door for X seconds and write result blockchain
67
-	digitalWrite(DOOR_PIN, HIGH);
68
-	delay(2000);
69
-	digitalWrite(DOOR_PIN, LOW);
70
-}
71
-
72
-void printCardUID(){
73
-	Serial.print(F("	Card UID:"));
74
-	for (byte i = 0; i < mfrc522.uid.size; i++) {
75
-		Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
76
-	    Serial.print(mfrc522.uid.uidByte[i], HEX);
77
-	}
78
-	Serial.println(); 
79
-}
80
-
81
-void readCardUID(){
82
-	mfrc522.PICC_ReadCardSerial();
83
-  	if(mfrc522.uid.size > 0){
84
-  		printCardUID();
85
-  		current_state=CARD_READ;
86
-  	}
87
-  	else{
88
-  		Serial.println("	Error : cannot read card UID");
89
-  		resetStateMachine();
90
-  	}
91
-}
92
-
93
-void processResponse(){
94
-	bool request_response=false;
95
-	if(request_response){
96
-		current_state=REQUEST_ACCEPTED;
97
-	}
98
-	else{
99
-		current_state=REQUEST_REJECTED;
100
-	}
101
-}
102
-
103
-bool sendPostRequest(char *data, int data_len){
104
-	char req[1024];
105
-	String req_str;
106
-	req_str =req_str + "POST index.html"                   
107
-        +" HTTP/1.1\r\n" 
108
-        +"Host: "+host+"\r\n" 
109
-        +"Connection: close\r\n"
110
-        +"Accept: */*\r\n" 
111
-        +"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" 
112
-        +"Content-Type: application/json\r\n"
113
-        +"Content-Length: "+data_len+"\r\n"
114
-        +"\r\n"
115
-        +data+"\r\n";
116
-    req_str.toCharArray(req,1024);
117
-	if(client.print(req)){
118
-		Serial.println("Request : ");
119
-		Serial.println(req);
120
-	}
121
-	else{
122
-		Serial.println("		Failed to send request to server !");
123
-		return false;
124
-	}
125
-	return true;
126
-}
127
-
128
-void sendRequest(){
129
-	byte nb_try=0;
130
-	bool request_sent=false;
131
-	Serial.println("	Sending request ...");
132
-	while(nb_try < MAX_TRY_REQUEST && !request_sent){
133
-		
134
-		//request_sent=true;
135
-		// SEND request to API (connect wifi here ? , server, +.)
136
-		// if no connection to wifi or server current_state=WAITING_CARD
137
-		request_sent=sendPostRequest("{test:1}",8);
138
-
139
-		if(request_sent){
140
-			// start timer timeout
141
-			current_state=REQUEST_SENT;
142
-			start_timer=millis();
143
-			Serial.println("	Request sent !");
144
-			Serial.println("	Waiting for reponse ...");
145
-		}
146
-		nb_try++;
147
-	}
148
-	if(nb_try == MAX_TRY_REQUEST && !request_sent){
149
-		Serial.println("	Error : number of sending attempts > MAX_TRY_REQUEST");
150
-		resetStateMachine();
151
-	}
152
-
153
-}
154
-
155
-void runStateMachine(){
156
-	last_state=REQUEST_REJECTED;
157
-	Serial.println("\n*********** Start state machine ***********");
158
-	while(1){
159
-		if(last_state != current_state){
160
-			Serial.print("\nCurrent state : ");Serial.println(current_state);
161
-			last_state=current_state;
162
-		}
163
-		switch(current_state){
164
-			case WAITING_CARD:
165
-				if(card_available){
166
-					current_state=CARD_AVAILABLE;
167
-				}
168
-				else{
169
-					//delay(500);
170
-				}
171
-				break;
172
-
173
-			case CARD_AVAILABLE:
174
-				readCardUID();
175
-				break;
176
-
177
-			case CARD_READ:
178
-				//printCardUID();
179
-				sendRequest();
180
-				break;
181
-
182
-			case REQUEST_SENT:
183
-				if(millis() - start_timer > TIMEOUT_DELAY){
184
-					Serial.println("	Error : response timeout");
185
-					resetStateMachine();
186
-				}
187
-				else if(response_available){
188
-					processResponse();
189
-				}
190
-				break;
191
-
192
-			case REQUEST_REJECTED:
193
-				resetStateMachine();
194
-				break;
195
-
196
-			case REQUEST_ACCEPTED:
197
-				openDoor();
198
-				break;
199 31
 
200
-			default:
201
-				resetStateMachine();
202
-				break;
203
-		}
204
-		if(!card_available) activateRec();
205
-		delay(10);
206
-	}
207
-}
208
-
209
-bool connectToWIfi(){
210
-	Serial.println();
211
-  Serial.print("connecting to ");
212
-  Serial.println(ssid);
213
-  WiFi.mode(WIFI_STA);
214
-  WiFi.begin(ssid, password);
215
-  long start=millis();
216
-  while (WiFi.status() != WL_CONNECTED) {
217
-  	if(millis()-start > TIMEOUT_WIFI) return false;
218
-    delay(500);
219
-    Serial.print(".");
220
-  }
221
-  Serial.println("");
222
-  Serial.println("WiFi connected");
223
-  Serial.println("IP address: ");
224
-  Serial.println(WiFi.localIP());
225
-  return true;
226
-}
227
-
228
-bool addSSL(){
229
-  Serial.print("Setting time using SNTP");
230
-  configTime(8 * 3600, 0, "pool.ntp.org", "time.nist.gov");
231
-  time_t now = time(nullptr);
232
-  while (now < 8 * 3600 * 2) {
233
-    delay(500);
234
-    Serial.print(".");
235
-    now = time(nullptr);
236
-  }
237
-  Serial.println("");
238
-  struct tm timeinfo;
239
-  gmtime_r(&now, &timeinfo);
240
-  Serial.print("Current time: ");
241
-  Serial.print(asctime(&timeinfo));
242
-
243
-
244
-  bool res0 = client.setCertificate(certs_client_bin_crt, certs_client_bin_crt_len);
245
-  if (!res0) {
246
-    Serial.println("Failed to load client certificate!");
247
-    return false;
248
-    // while (true) {
249
-    //   yield();
250
-    // }
251
-  }
252
-  bool res1 = client.setPrivateKey(private_client_bin_key, private_client_bin_key_len);
253
-  if (!res1) {
254
-    Serial.println("Failed to load client private key!");
255
-    return false;
256
-    // while (true) {
257
-    //   yield();
258
-    // }
259
-  }
260
-  bool res = client.setCACert_P(caCert, caCertLen);
261
-  if (!res) {
262
-    Serial.println("Failed to load root CA certificate!");
263
-    return false;
264
-    // while (true) {
265
-    //   yield();
266
-    // }
267
-  }
268
-  return true;
269
-}
270
-
271
-bool connectToServer(){
272
-  // Connect to remote server
273
-  Serial.print("connecting to ");
274
-  Serial.println(host);
275
-  if (!client.connect(host, httpsPort)) {
276
-    Serial.println("connection failed");
277
-    return false;
278
-  }
279
-
280
-  // Verify validity of server's certificate
281
-
282
-  if (client.verifyCertChain(host)) {
283
-  //if (client.verify(fingerprint, host)){
284
-    Serial.println("Server certificate verified");
285
-  } else {
286
-    Serial.println("ERROR: certificate verification failed!");
287
-    return false;
288
-  }
289
-  return true;
290
-}
32
+//  - add a check function for wifi connect et client connected
33
+//    and call it before any request
34
+//  - add function that ping server every 15 min to keep alive connection and ssl session
35
+//  - add function reconnect with new crypto to server every day
36
+//  - add function that resync ntp every hour
291 37
 
292 38
 
293 39
 void setup(){
294 40
 
295
-	Serial.begin(9600);		// Initialize serial communications with the PC
41
+	Serial.begin(115200);		// Initialize serial communications with the PC
296 42
 	while (!Serial);		// Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
297 43
 
298
-	pinMode(DOOR_PIN, OUTPUT);
299
-	/* setup the IRQ pin*/
300
-	pinMode(IRQ_PIN, INPUT_PULLUP);
301
-
302
-	SPI.begin();			// Init SPI bus
303
-	mfrc522.PCD_Init();		// Init MFRC522
304
-	 /* read and printout the MFRC522 version (valid values 0x91 & 0x92)*/
305
-	Serial.print(F("Ver: 0x"));
306
-	byte readReg = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
307
-	Serial.println(readReg, HEX);
308
-	mfrc522.PCD_WriteRegister(mfrc522.ComIEnReg, 0xA0); // Enable interrupt
309
-
310
-	/*Activate the interrupt*/
311
-	attachInterrupt(digitalPinToInterrupt(IRQ_PIN), IRQ_cardAvailable, FALLING);
44
+	#ifdef ENABLE_DOOR
45
+	setupDoor();
46
+	#endif
312 47
 
313
-	mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
314
-	Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks+."));
48
+	#ifdef ENABLE_MFRC552
49
+	setupMFRC552();
50
+	#endif
315 51
 
316 52
 // proceed with connecting of client to the host
317 53
 	bool wifi_connected = false;
@@ -320,12 +56,14 @@ void setup(){
320 56
 		if(!wifi_connected) delay(60000);
321 57
 	}
322 58
 
59
+	//#ifdef ADD_SSL
323 60
 	// Add SSL layer
324 61
 	bool ssl_added=false;
325 62
 	while(!ssl_added){
326 63
 		ssl_added=addSSL();
327 64
 		if(!ssl_added) delay(500);
328 65
 	}
66
+	//#endif
329 67
 
330 68
 	Serial.println("Connected to WiFi and SSL ready");
331 69
 
@@ -340,7 +78,11 @@ void setup(){
340 78
 
341 79
 
342 80
 	resetStateMachine();
81
+	#ifdef ENABLE_MFRC552
343 82
 	activateRec();
83
+	#else
84
+	current_state=CARD_READ;
85
+	#endif
344 86
 }
345 87
 
346 88
 

+ 72
- 0
src/mrfc552_lib.cpp View File

@@ -0,0 +1,72 @@
1
+#include <Arduino.h>
2
+#include <MFRC522.h>
3
+#include "mrfc552_lib.h"
4
+
5
+
6
+extern bool card_available;
7
+extern String currentCardUID;
8
+extern MFRC522 mfrc522;
9
+
10
+
11
+void setupMFRC552(){
12
+	/* setup the IRQ pin*/
13
+	pinMode(IRQ_PIN, INPUT_PULLUP);
14
+
15
+	SPI.begin();			// Init SPI bus
16
+	mfrc522.PCD_Init();		// Init MFRC522
17
+	 /* read and printout the MFRC522 version (valid values 0x91 & 0x92)*/
18
+	Serial.print(F("Ver: 0x"));
19
+	byte readReg = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
20
+	Serial.println(readReg, HEX);
21
+	mfrc522.PCD_WriteRegister(mfrc522.ComIEnReg, 0xA0); // Enable interrupt
22
+
23
+	/*Activate the interrupt*/
24
+	attachInterrupt(digitalPinToInterrupt(IRQ_PIN), IRQ_cardAvailable, FALLING);
25
+
26
+	mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
27
+	Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks+."));
28
+}
29
+
30
+
31
+/*
32
+ * The function to clear the pending interrupt bits after interrupt serving routine
33
+ */
34
+void clearInt() {
35
+  mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x7F);
36
+  mfrc522.PICC_HaltA();
37
+}
38
+
39
+/*
40
+ * The function sending to the MFRC522 the needed commands to activate the reception
41
+ */
42
+void activateRec() {
43
+  mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg, mfrc522.PICC_CMD_REQA);
44
+  mfrc522.PCD_WriteRegister(mfrc522.CommandReg, mfrc522.PCD_Transceive);
45
+  mfrc522.PCD_WriteRegister(mfrc522.BitFramingReg, 0x87);
46
+}
47
+
48
+void printCardUID(const char* uid){
49
+	Serial.print(F("	Card UID:"));
50
+	Serial.println(uid); 
51
+}
52
+
53
+int readCardUID(){
54
+	mfrc522.PICC_ReadCardSerial();
55
+  	if(mfrc522.uid.size > 0){
56
+  		currentCardUID="";
57
+  		for (byte i = 0; i < mfrc522.uid.size; i++) {
58
+  			currentCardUID += String(mfrc522.uid.uidByte[i],HEX);
59
+		}
60
+  		printCardUID(currentCardUID.c_str());
61
+  		
62
+  	}
63
+  	else{
64
+  		Serial.println("	Error : cannot read card UID");
65
+  		return 1;
66
+  	}
67
+  	return 0;
68
+}
69
+
70
+void IRQ_cardAvailable(){
71
+	card_available=true;
72
+}

+ 149
- 0
src/request_lib.cpp View File

@@ -0,0 +1,149 @@
1
+#include <Arduino.h>
2
+#include <stdlib.h>
3
+#include <string.h>
4
+#include <ArduinoJson.h>
5
+#include "request_lib.h"
6
+#include "http_cli_lib.h"
7
+
8
+extern String currentTimestamp;
9
+
10
+time_t getEpoch(){
11
+	time_t tnow = time(nullptr);
12
+	return tnow;
13
+}
14
+
15
+
16
+
17
+/***********************************************\
18
+			FONCTIONS PRINCIPALES
19
+\***********************************************/
20
+
21
+// ************************
22
+bool send_openning_request(const char *user_id, const char *door_id, char *res) 
23
+{	
24
+	currentTimestamp=String(getEpoch());
25
+	StaticJsonBuffer<500> jsonBuffer;
26
+	JsonObject& json = jsonBuffer.createObject();
27
+	json["user"]=user_id;
28
+	json["door"]=door_id;
29
+	json["timestamp"]=currentTimestamp;
30
+
31
+	char json_str[json.measureLength()+1];
32
+	json.printTo(json_str,json.measureLength()+1);
33
+
34
+	// integrer la string api_request_string dans le champ POST d'une requete
35
+	return send_post_request(json_str, URL_OPENNING_REQ, res);
36
+	// return la réponse du noeud blockchain
37
+}
38
+
39
+
40
+/*
41
+return value :
42
+- 3 --> no response available
43
+- 1 --> request accepted
44
+- 2 --> request rejected
45
+- 0 --> error sending request
46
+*/
47
+int poll_response(const char *target_txid, const char *timestamp){
48
+	StaticJsonBuffer<500> jsonBuffer;
49
+	JsonObject& json = jsonBuffer.createObject();
50
+	json["txid"]=target_txid;
51
+	json["timestamp"]=timestamp;
52
+
53
+	char json_str[json.measureLength()+1];
54
+	json.printTo(json_str,json.measureLength()+1);
55
+
56
+	// integrer la string api_request_string dans le champ POST d'une requete
57
+	char res[256];
58
+	if(send_post_request(json_str, URL_REQ_POLL, res)){
59
+		// parse res
60
+		StaticJsonBuffer<200> jsonBuffer;
61
+		JsonObject& root = jsonBuffer.parseObject(res);
62
+		if (!root.success()) {
63
+		    Serial.println("parseObject() failed");
64
+		    return 0;
65
+		}
66
+		String status=root["doorStatus"];
67
+		return status.toInt();
68
+	}
69
+
70
+	return 0;
71
+	// return la réponse du noeud blockchain
72
+}
73
+
74
+
75
+// TO Modify
76
+
77
+bool publish_door_opening(const char *door_id,const char *request_txid,const char *user_id, char *res) 
78
+{
79
+	StaticJsonBuffer<500> jsonBuffer;
80
+	JsonObject& json = jsonBuffer.createObject();
81
+	json["user"]=user_id;
82
+	json["door"]=door_id;
83
+	json["request_txid"]=request_txid;
84
+	json["timestamp"]=String(getEpoch());
85
+
86
+	char json_str[json.measureLength()+1];
87
+	json.printTo(json_str,json.measureLength()+1);
88
+
89
+	// integrer la string api_request_string dans le champ POST d'une requete
90
+	return send_post_request(json_str, URL_PUB_DOOR_OPEN, res);
91
+	// return la réponse du noeud blockchain
92
+	
93
+}
94
+
95
+
96
+bool publish_door_closing(const char *door_id,const char *request_txid,const char *user_id, char *res) 
97
+{
98
+	StaticJsonBuffer<500> jsonBuffer;
99
+	JsonObject& json = jsonBuffer.createObject();
100
+	json["user"]=user_id;
101
+	json["door"]=door_id;
102
+	json["request_txid"]=request_txid;
103
+	json["timestamp"]=String(getEpoch());
104
+
105
+	char json_str[json.measureLength()+1];
106
+	json.printTo(json_str,json.measureLength()+1);
107
+
108
+	// integrer la string api_request_string dans le champ POST d'une requete
109
+	return send_post_request(json_str, URL_PUB_DOOR_CLOSE, res);
110
+	// return la réponse du noeud blockchain
111
+}
112
+
113
+
114
+
115
+bool publish_request_timeout(const char *door_id,const char *request_txid, char *res) 
116
+{
117
+	StaticJsonBuffer<500> jsonBuffer;
118
+	JsonObject& json = jsonBuffer.createObject();
119
+	json["door"]=door_id;
120
+	json["request_txid"]=request_txid;
121
+	json["timestamp"]=String(getEpoch());
122
+
123
+	char json_str[json.measureLength()+1];
124
+	json.printTo(json_str,json.measureLength()+1);
125
+
126
+	// integrer la string api_request_string dans le champ POST d'une requete
127
+	//Serial.printf("Sending request timeout\n");
128
+	return send_post_request(json_str, URL_PUB_REQ_TO, res);
129
+	// return la réponse du noeud blockchain
130
+}
131
+
132
+
133
+
134
+bool publish_opening_timeout(const char *door_id,const char *request_txid, char *res) 
135
+{
136
+	StaticJsonBuffer<500> jsonBuffer;
137
+	JsonObject& json = jsonBuffer.createObject();
138
+	json["door"]=door_id;
139
+	json["request_txid"]=request_txid;
140
+	json["timestamp"]=String(getEpoch());
141
+
142
+	char json_str[json.measureLength()+1];
143
+	json.printTo(json_str,json.measureLength()+1);
144
+
145
+	// integrer la string api_request_string dans le champ POST d'une requete
146
+	Serial.printf("Sending opening timeout req\n");
147
+	return send_post_request(json_str, URL_PUB_OPEN_TO, res);
148
+	// return la réponse du noeud blockchain
149
+}

+ 143
- 0
src/statemachine_lib.cpp View File

@@ -0,0 +1,143 @@
1
+#include <Arduino.h>
2
+#include <ESP8266WiFi.h>
3
+
4
+#include "statemachine_lib.h"
5
+#include "request_lib.h"
6
+#include "connect_lib.h"
7
+#ifdef ENABLE_DOOR
8
+#include "door_lib.h"
9
+#endif
10
+#ifdef ENABLE_MFRC552
11
+#include "mrfc552_lib.h"
12
+#endif
13
+
14
+#ifdef ADD_SSL
15
+#include <WiFiClientSecure.h>
16
+extern WiFiClientSecure client;
17
+#else
18
+extern WiFiClient client;
19
+#endif
20
+
21
+#ifdef ENABLE_MFRC552
22
+volatile bool card_available = false;
23
+#else
24
+volatile bool card_available = true;
25
+#endif
26
+volatile bool response_available = false;
27
+
28
+unsigned long start_timer=0;
29
+enum State current_state, last_state;
30
+String currentTxid;
31
+String currentCardUID;
32
+String currentTimestamp;
33
+
34
+void runStateMachine(){
35
+	last_state=REQUEST_REJECTED;
36
+	Serial.println("\n*********** Start state machine ***********");
37
+	while(1){
38
+		if(last_state != current_state){
39
+			Serial.print("\nCurrent state : ");Serial.println(current_state);
40
+			last_state=current_state;
41
+		}
42
+		
43
+
44
+		switch(current_state){
45
+			case WAITING_CARD:
46
+				if(card_available){
47
+					current_state=CARD_AVAILABLE;
48
+				}
49
+				else{
50
+					while (!client.connected()) {
51
+						connectToServer();
52
+					}
53
+				}
54
+				break;
55
+
56
+			case CARD_AVAILABLE:
57
+				#ifdef ENABLE_MFRC552
58
+				if(readCardUID()){
59
+					resetStateMachine();
60
+				}
61
+				else{
62
+					current_state=CARD_READ;
63
+				}
64
+				#endif
65
+				break;
66
+
67
+			case CARD_READ:
68
+				char res_txid[200];
69
+				start_timer=millis();
70
+				#ifdef ENABLE_MFRC552
71
+				if(send_openning_request(currentCardUID.c_str(), DOOR_ID, res_txid)){
72
+					Serial.print(" ---- TXID RES = ");Serial.println(res_txid);
73
+					currentTxid = String(res_txid);
74
+					current_state=REQUEST_SENT;
75
+				}
76
+				#else
77
+				if(send_openning_request("test-UID", DOOR_ID, res_txid)){
78
+					currentTxid = String(res_txid);
79
+					current_state=REQUEST_SENT;
80
+				}
81
+				#endif
82
+				else{
83
+					resetStateMachine();
84
+				}
85
+				//while(1);
86
+				break;
87
+
88
+			case REQUEST_SENT:
89
+				if(millis() - start_timer > TIMEOUT_DELAY){
90
+					char res[200];
91
+					publish_request_timeout(DOOR_ID, currentTxid.c_str(), res);
92
+					Serial.println("	Error : response timeout");
93
+					resetStateMachine();
94
+					break;
95
+				}
96
+				int res;
97
+				res=poll_response(currentTxid.c_str(), currentTimestamp.c_str());
98
+				if(res==2){
99
+					current_state=REQUEST_REJECTED;
100
+				}
101
+				// else if(res==3){
102
+				// 	current_state=REQUEST_ACCEPTED;
103
+				// }
104
+				else if(res==1){
105
+					current_state=REQUEST_ACCEPTED;
106
+				}
107
+				else if(!res){
108
+					resetStateMachine();
109
+				}
110
+				//delay(200);
111
+				//while(1);
112
+				break;
113
+
114
+			case REQUEST_REJECTED:
115
+				resetStateMachine();
116
+				break;
117
+
118
+			case REQUEST_ACCEPTED:
119
+				#ifdef ENABLE_DOOR
120
+				openDoor();
121
+				#endif
122
+				resetStateMachine();
123
+				break;
124
+
125
+			default:
126
+				resetStateMachine();
127
+				break;
128
+		}
129
+		#ifdef ENABLE_MFRC552
130
+		if(!card_available) activateRec();
131
+		#endif
132
+		delay(10);
133
+	}
134
+}
135
+
136
+void resetStateMachine(){
137
+	#ifdef ENABLE_MFRC552
138
+	clearInt();
139
+    #endif
140
+    card_available = false;
141
+    current_state=WAITING_CARD;
142
+    Serial.println("\n---- RESET ST_MACHINE ----");
143
+}

Loading…
Cancel
Save