add mqttclient to the component, and add fire stm32f429 board demo.

This commit is contained in:
jiejieTop
2020-02-20 00:09:00 +08:00
committed by jiejieTop
parent 6731d4692d
commit 8f76e16646
281 changed files with 145740 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
/*
* @Author: jiejie
* @Github: https://github.com/jiejieTop
* @Date: 2020-01-11 02:04:49
* @LastEditTime : 2020-01-11 02:12:16
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
*/
#include "mbedtls/entropy.h"
#include "random.h"
#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
static int mbedtls_get_random(unsigned char *buf, size_t len)
{
int i, j;
unsigned long tmp;
for (i = 0; i < ((len + 3) & ~3) / 4; i++) {
tmp = random_number();
for (j = 0; j < 4; j++) {
if ((i * 4 + j) < len) {
buf[i * 4 + j] = (unsigned char)(tmp >> (j * 8));
} else {
break;
}
}
}
return 0;
}
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen)
{
mbedtls_get_random(output, len);
*olen = len;
return 0;
}
#endif

View File

@@ -0,0 +1,200 @@
/*
* @Author: jiejie
* @Github: https://github.com/jiejieTop
* @Date: 2020-01-08 21:48:09
* @LastEditTime : 2020-01-12 00:23:42
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if !defined(MBEDTLS_NET_C)
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#endif
#include "mbedtls/net_sockets.h"
#include "platform_net_socket.h"
#include "platform_timer.h"
/*
* Initialize a context
*/
void mbedtls_net_init(mbedtls_net_context *ctx)
{
if (!ctx) {
return;
}
ctx->fd = -1;
}
/*
* Initiate a TCP connection with host:port and the given protocol
*/
int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto)
{
int net_proto;
net_proto = (proto == MBEDTLS_NET_PROTO_UDP) ? PLATFORM_NET_PROTO_UDP : PLATFORM_NET_PROTO_TCP;
ctx->fd = platform_net_socket_connect(host, port, net_proto);
if (ctx->fd < 0) {
return ctx->fd;
}
return 0;
}
/*
* Set the socket blocking or non-blocking
*/
int mbedtls_net_set_block( mbedtls_net_context *ctx )
{
return platform_net_socket_set_block(ctx->fd);
}
int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
{
return platform_net_socket_set_nonblock(ctx->fd);
}
/*
* Read at most 'len' characters
*/
int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
int ret;
int fd = ((mbedtls_net_context *)ctx)->fd;
if (fd < 0) {
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
}
ret = platform_net_socket_recv(fd, buf, len, 0);
if (ret == 0) {
return MBEDTLS_ERR_SSL_WANT_READ;
} else if (ret < 0) {
return MBEDTLS_ERR_NET_RECV_FAILED;
}
return ret;
}
/*
* Read at most 'len' characters, blocking for at most 'timeout' ms
*/
int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
{
int ret;
int fd = ((mbedtls_net_context *)ctx)->fd;
if (fd < 0) {
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
}
ret = platform_net_socket_recv_timeout(fd, buf, len, timeout);
if (ret == 0) {
return MBEDTLS_ERR_SSL_TIMEOUT;
} else if (ret < 0) {
return MBEDTLS_ERR_NET_RECV_FAILED;
}
return ret;
}
/*
* Write at most 'len' characters
*/
int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
{
int ret;
int fd = ((mbedtls_net_context *) ctx)->fd;
if (fd < 0) {
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
}
ret = platform_net_socket_write(fd, (unsigned char*)buf, len);
if (ret == 0) {
return MBEDTLS_ERR_SSL_WANT_WRITE;
} else if (ret < 0) {
return MBEDTLS_ERR_NET_SEND_FAILED;
}
return ret;
}
/*
* Gracefully close the connection
*/
void mbedtls_net_free(mbedtls_net_context *ctx)
{
if (ctx->fd < 0) {
return;
}
// shutdown(ctx->fd, 2);
platform_net_socket_close(ctx->fd);
ctx->fd = -1;
}
/*
* Portable usleep helper
*/
void mbedtls_net_usleep( unsigned long usec )
{
platform_timer_usleep(usec);
}
/* dtls */
void mbedtls_dtls_net_init(mbedtls_net_context *ctx)
{
mbedtls_net_init(ctx);
}
int mbedtls_dtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto)
{
return mbedtls_net_connect(ctx, host, port, proto);
}
void mbedtls_dtls_net_usleep(unsigned long usec)
{
mbedtls_net_usleep(usec);
}
int mbedtls_dtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
return mbedtls_net_recv(ctx, buf, len);
}
int mbedtls_dtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
{
return mbedtls_net_recv_timeout(ctx, buf, len, timeout);
}
int mbedtls_dtls_net_send(void *ctx, const unsigned char *buf, size_t len)
{
return mbedtls_net_send(ctx, buf, len);
}
void mbedtls_dtls_net_free(mbedtls_net_context *ctx)
{
mbedtls_net_free(ctx);
}
#endif /* MBEDTLS_NET_C */

View File

@@ -0,0 +1,86 @@
/*
* @Author: jiejie
* @Github: https://github.com/jiejieTop
* @Date: 2020-01-08 19:44:56
* @LastEditTime : 2020-01-13 01:01:39
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "platform_timer.h"
#include "timing_alt.h"
unsigned long mbedtls_timing_hardclock( void )
{
return 1600*1000*1000;
}
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
{
struct mbedtls_timing_hr_time now;
now.timer_ms = platform_timer_now();
if (reset) {
val->timer_ms = now.timer_ms;
}
return (unsigned long)(now.timer_ms - val->timer_ms);
}
/*
* Set delays to watch
*/
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
{
mbedtls_timing_delay_context *ctx;
if (!data) {
return;
}
ctx = (mbedtls_timing_delay_context*)data;
ctx->int_ms = int_ms;
ctx->fin_ms = fin_ms;
if (fin_ms != 0) {
(void)mbedtls_timing_get_timer(&ctx->timer, 1);
}
}
/*
* Get number of delays expired
*/
int mbedtls_timing_get_delay(void *data)
{
unsigned long elapsed_ms;
mbedtls_timing_delay_context *ctx;
if (!data) {
return -1;
}
ctx = (mbedtls_timing_delay_context*)data;
if (ctx->fin_ms == 0) {
return -1;
}
elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
if (elapsed_ms >= ctx->fin_ms) {
return 2;
}
if (elapsed_ms >= ctx->int_ms) {
return 1;
}
return 0;
}

View File

@@ -0,0 +1,92 @@
/*
* @Author: jiejie
* @Github: https://github.com/jiejieTop
* @Date: 2020-01-08 19:41:14
* @LastEditTime: 2020-01-08 19:44:11
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
*/
/**
* \file timing_alt.h
*
* \brief Portable interface to the CPU cycle counter
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#ifndef _TIMING_ALT_H_
#define _TIMING_ALT_H_
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "stdint.h"
/**
* @brief timer structure
*/
struct mbedtls_timing_hr_time {
uint64_t timer_ms;
};
/**
* @brief Context for mbedtls_timing_set/get_delay()
*/
typedef struct {
struct mbedtls_timing_hr_time timer;
uint32_t int_ms;
uint32_t fin_ms;
} mbedtls_timing_delay_context;
/**
* @brief Return the elapsed time in milliseconds
*
* @param val points to a timer structure
* @param reset if set to 1, the timer is restarted
*/
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset);
/**
* @brief Set a pair of delays to watch
* Must point to a valid mbedtls_timing_delay_context struct.
*
* @param data Pointer to timing data
* @param int_ms First (intermediate) delay in milliseconds.
* @param fin_ms Second (final) delay in milliseconds.
* Pass 0 to cancel the current delay.
*/
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms);
/**
* @brief Get the status of delays
* (Memory helper: number of delays passed.)
*
* @param data Pointer to timing data
* Must point to a valid mbedtls_timing_delay_context struct.
*
* @return -1 if cancelled (fin_ms = 0)
* 0 if none of the delays are passed,
* 1 if only the intermediate delay is passed,
* 2 if the final delay is passed.
*/
int mbedtls_timing_get_delay(void *data);
#endif