Osamu Koizumi / Mbed OS Hello-TLSSocket-old
Committer:
Osamu Koizumi
Date:
Fri Apr 27 14:03:25 2018 +0900
Revision:
9:38b485904577
Parent:
7:fc43e66fb54a
Child:
12:7d952bcda2c1
Update TLSSocket library. Changed to set root CA certificate by mbed_app.json.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
coisme 0:f96053986356 1 #include "mbed.h"
coisme 0:f96053986356 2 #include "easy-connect.h"
coisme 0:f96053986356 3 #include "TLSSocket.h"
coisme 0:f96053986356 4
Osamu Koizumi 3:cf61a5596cf2 5 #include "mbed-trace/mbed_trace.h"
Osamu Koizumi 3:cf61a5596cf2 6
coisme 0:f96053986356 7 const char* HOST_NAME = "os.mbed.com";
coisme 0:f96053986356 8 const int PORT = 443;
coisme 0:f96053986356 9 const char* HTTPS_PATH = "/";
coisme 0:f96053986356 10
coisme 0:f96053986356 11 int main(int argc, char* argv[]) {
Osamu Koizumi 3:cf61a5596cf2 12 mbed_trace_init();
Osamu Koizumi 3:cf61a5596cf2 13
coisme 0:f96053986356 14 printf("HelloTSLSocket, HTTPS example of TLSSocket\r\n");
Osamu Koizumi 1:65339c530def 15 printf("\r\n");
coisme 0:f96053986356 16
Osamu Koizumi 1:65339c530def 17 // Open a network interface
coisme 0:f96053986356 18 NetworkInterface* network = NULL;
coisme 0:f96053986356 19 network = easy_connect(false); // If true, prints out connection details.
coisme 0:f96053986356 20 if (!network) {
coisme 0:f96053986356 21 printf("Unable to open network interface.\r\n");
coisme 0:f96053986356 22 return -1;
coisme 0:f96053986356 23 }
coisme 0:f96053986356 24
Osamu Koizumi 1:65339c530def 25 // Create a TLS socket
Osamu Koizumi 7:fc43e66fb54a 26 TLSSocket socket = TLSSocket();
Osamu Koizumi 7:fc43e66fb54a 27 if(socket.open(network) != 0) {
coisme 0:f96053986356 28 printf("Unable to open TLS socket.\r\n");
coisme 0:f96053986356 29 return -1;
coisme 0:f96053986356 30 }
Osamu Koizumi 1:65339c530def 31
Osamu Koizumi 1:65339c530def 32 // Connect to the server, including TLS handshake
Osamu Koizumi 9:38b485904577 33 if(socket.connect(HOST_NAME, PORT) != 0) {
coisme 0:f96053986356 34 printf("Failed to connect to the server.");
coisme 0:f96053986356 35 return -1;
coisme 0:f96053986356 36 }
coisme 0:f96053986356 37
Osamu Koizumi 6:75b01b028cd4 38 const size_t buf_size = 1024;
coisme 0:f96053986356 39 char *buf = new char[buf_size];
coisme 0:f96053986356 40
Osamu Koizumi 1:65339c530def 41 // Send HTTP request
Osamu Koizumi 1:65339c530def 42 /* "Connection: close" header is specified to detect end of the body
Osamu Koizumi 1:65339c530def 43 * contents by connection close notification. If this is not specified,
Osamu Koizumi 1:65339c530def 44 * connection is kept, and need to detect end of the content in another
Osamu Koizumi 1:65339c530def 45 * way.
Osamu Koizumi 1:65339c530def 46 */
Osamu Koizumi 1:65339c530def 47 int len = snprintf(buf, buf_size,
Osamu Koizumi 1:65339c530def 48 "GET %s HTTP/1.1\n"
Osamu Koizumi 1:65339c530def 49 "Host: %s\n"
Osamu Koizumi 1:65339c530def 50 "Connection: close\n"
Osamu Koizumi 1:65339c530def 51 "\n", HTTPS_PATH, HOST_NAME);
Osamu Koizumi 1:65339c530def 52 printf("\r\n%s", buf);
coisme 0:f96053986356 53 int rc = 0;
Osamu Koizumi 7:fc43e66fb54a 54 rc = socket.send(buf, len);
coisme 0:f96053986356 55 if(rc < 0) {
coisme 0:f96053986356 56 printf("send error.\r\n");
coisme 0:f96053986356 57 return -1;
coisme 0:f96053986356 58 }
Osamu Koizumi 1:65339c530def 59
Osamu Koizumi 6:75b01b028cd4 60 // Receive response from the server
Osamu Koizumi 7:fc43e66fb54a 61 while((rc = socket.recv(buf, buf_size - 1)) > 0) {
coisme 0:f96053986356 62 buf[rc] = '\0';
coisme 0:f96053986356 63 printf("%s", buf);
coisme 0:f96053986356 64 }
coisme 0:f96053986356 65 if(rc < 0) {
coisme 0:f96053986356 66 printf("\r\n! Read failed. err code = %d\r\n", rc);
coisme 0:f96053986356 67 }
Osamu Koizumi 1:65339c530def 68
Osamu Koizumi 1:65339c530def 69 // Done
Osamu Koizumi 1:65339c530def 70 printf("HelloTSLSocket DONE.\r\n");
coisme 0:f96053986356 71 delete[] buf;
coisme 0:f96053986356 72
Osamu Koizumi 7:fc43e66fb54a 73 socket.close();
coisme 0:f96053986356 74 }