Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2020 Nordic Semiconductor ASA
- *
- * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
- */
- #include <string.h>
- #include <zephyr.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <net/socket.h>
- #include <modem/bsdlib.h>
- #include <net/tls_credentials.h>
- #include <modem/lte_lc.h>
- #include <modem/at_cmd.h>
- #include <modem/at_notif.h>
- #include <modem/modem_key_mgmt.h>
- #define HTTPS_PORT 443
- #define ONBOARDING_ENDPOINT "device.connectivity-suite.cloud"
- #define RECV_BUF_SIZE 2048
- #define TLS_SEC_TAG 42
- static char recv_buf[RECV_BUF_SIZE];
- /* Certificate for `google.com` */
- static const char cert[] = {
- #include "../cert/root-CA"
- };
- BUILD_ASSERT(sizeof(cert) < KB(4), "Certificate too large");
- /* Initialize AT communications */
- int at_comms_init(void)
- {
- int err;
- err = at_cmd_init();
- if (err) {
- printk("Failed to initialize AT commands, err %d\n", err);
- return err;
- }
- err = at_notif_init();
- if (err) {
- printk("Failed to initialize AT notifications, err %d\n", err);
- return err;
- }
- return 0;
- }
- /* Provision certificate to modem */
- int cert_provision(void)
- {
- int err;
- bool exists;
- uint8_t unused;
- err = modem_key_mgmt_exists(TLS_SEC_TAG,
- MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
- &exists, &unused);
- if (err) {
- printk("Failed to check for certificates err %d\n", err);
- return err;
- }
- if (exists) {
- /* For the sake of simplicity we delete what is provisioned
- * with our security tag and reprovision our certificate.
- */
- err = modem_key_mgmt_delete(TLS_SEC_TAG,
- MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN);
- if (err) {
- printk("Failed to delete existing certificate, err %d\n",
- err);
- }
- }
- printk("Provisioning certificate\n");
- /* Provision certificate to the modem */
- err = modem_key_mgmt_write(TLS_SEC_TAG,
- MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
- cert, sizeof(cert) - 1);
- if (err) {
- printk("Failed to provision certificate, err %d\n", err);
- return err;
- }
- return 0;
- }
- /* Setup TLS options on a given socket */
- int tls_setup(int fd)
- {
- int err;
- int verify;
- /* Security tag that we have provisioned the certificate with */
- const sec_tag_t tls_sec_tag[] = {
- TLS_SEC_TAG,
- };
- /* Set up TLS peer verification */
- enum {
- NONE = 0,
- OPTIONAL = 1,
- REQUIRED = 2,
- };
- verify = NONE;
- err = setsockopt(fd, SOL_TLS, TLS_PEER_VERIFY, &verify, sizeof(verify));
- if (err) {
- printk("Failed to setup peer verification, err %d\n", errno);
- return err;
- }
- /* Associate the socket with the security tag
- * we have provisioned the certificate with.
- */
- /*err = setsockopt(fd, SOL_TLS, TLS_SEC_TAG_LIST, tls_sec_tag,
- sizeof(tls_sec_tag));
- if (err) {
- printk("Failed to setup TLS sec tag, err %d\n", errno);
- return err;
- }*/
- return 0;
- }
- int blocking_recv(int fd, uint8_t *buf, uint32_t size, uint32_t flags)
- {
- int err;
- do {
- printk("Receive\n\r");
- err = recv(fd, buf, size, flags);
- } while (err < 0 && errno == EAGAIN);
- return err;
- }
- void main(void)
- {
- int err;
- int fd;
- char *p;
- int bytes;
- size_t off;
- struct addrinfo *res;
- struct addrinfo hints = {
- .ai_family = AF_INET,
- .ai_socktype = SOCK_STREAM,
- };
- printk("HTTPS client sample started\n\r");
- err = bsdlib_init();
- if (err) {
- printk("Failed to initialize bsdlib!");
- return;
- }
- /* Initialize AT comms in order to provision the certificate */
- err = at_comms_init();
- if (err) {
- return;
- }
- //err = cert_provision();
- if (err) {
- return;
- }
- printk("Waiting for network.. ");
- err = lte_lc_init_and_connect();
- if (err) {
- printk("Failed to connect to the LTE network, err %d\n", err);
- return;
- }
- printk("OK\n");
- err = getaddrinfo(ONBOARDING_ENDPOINT, NULL, &hints, &res);
- if (err) {
- printk("getaddrinfo() failed, err %d\n", errno);
- return;
- }
- ((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTPS_PORT);
- fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (fd == -1) {
- printk("Failed to open socket!\n");
- goto clean_up;
- }
- char ipv4_addr[NET_IPV4_ADDR_LEN];
- inet_ntop(AF_INET, &((struct sockaddr_in *)res->ai_addr)->sin_port, ipv4_addr,
- sizeof(ipv4_addr));
- printk("Connect to Addr IP: %s \n",ipv4_addr);
- //err = tls_setup(fd);
- if (err) {
- goto clean_up;
- }
- err = connect(fd, res->ai_addr, sizeof(struct sockaddr_in));
- if (err) {
- printk("connect() failed, err: %d\n", errno);
- goto clean_up;
- }
- printk("send()\n");
- off = 0;
- char send_packet[100];
- sprintf(send_packet, "GET /device-api/onboarding HTTP/1.1\r\n"
- "Host: %s\r\n"
- "Accept: text/csv\r\n\r\n", ONBOARDING_ENDPOINT);
- bytes = send(fd, &send_packet, strlen(send_packet), 0);
- printk("Sent %d bytes\n", bytes);
- off = 0;
- k_sleep(K_MSEC(2000));
- int tot_num_bytes = 0;
- do {
- /* TODO: make a proper timeout *
- * Current solution will just hang
- * until remote side closes connection */
- bytes =
- blocking_recv(fd, recv_buf, RECV_BUF_SIZE, 0);
- tot_num_bytes += bytes;
- if (bytes <= 0) {
- break;
- }
- } while (tot_num_bytes > 0);
- printk("Received %d bytes\n", bytes);
- printk("GOT %s", recv_buf);
- printk("Finished, closing socket.\n");
- clean_up:
- freeaddrinfo(res);
- (void)close(fd);
- }
Advertisement
Add Comment
Please, Sign In to add comment