├── LICENSE ├── README.md ├── SConscript ├── common ├── CMakeLists.txt ├── log │ ├── CMakeLists.txt │ ├── arch │ │ ├── CMakeLists.txt │ │ └── rt-thread │ │ │ └── arch.c │ ├── build.sh │ ├── fifo.c │ ├── fifo.h │ ├── format.c │ ├── format.h │ ├── salof.c │ ├── salof.h │ ├── salof_config.h │ ├── salof_defconfig.h │ └── test │ │ ├── CMakeLists.txt │ │ └── test.c ├── mqtt_error.h ├── mqtt_list.c ├── mqtt_list.h ├── mqtt_log.h ├── random.c └── random.h ├── docs ├── mqtt-aliyun.md ├── mqtt-baidu.md ├── mqtt-communication.md ├── mqtt-config.md ├── mqtt-design.md ├── mqtt-introduction.md ├── mqtt-onenet.md └── mqtt-tool.md ├── mqtt ├── CMakeLists.txt ├── MQTTConnect.h ├── MQTTConnectClient.c ├── MQTTConnectServer.c ├── MQTTDeserializePublish.c ├── MQTTFormat.c ├── MQTTFormat.h ├── MQTTPacket.c ├── MQTTPacket.h ├── MQTTPublish.h ├── MQTTSerializePublish.c ├── MQTTSubscribe.h ├── MQTTSubscribeClient.c ├── MQTTSubscribeServer.c ├── MQTTUnsubscribe.h ├── MQTTUnsubscribeClient.c ├── MQTTUnsubscribeServer.c └── StackTrace.h ├── mqttclient ├── CMakeLists.txt ├── mqtt_defconfig.h ├── mqttclient.c └── mqttclient.h ├── network ├── CMakeLists.txt ├── mbedtls │ ├── CMakeLists.txt │ ├── build.sh │ ├── configs │ │ ├── config-ccm-psk-tls1_2.h │ │ ├── config-mini-tls1_1.h │ │ ├── config-no-entropy.h │ │ ├── config-suite-b.h │ │ └── config-thread.h │ ├── include │ │ ├── CMakeLists.txt │ │ └── mbedtls │ │ │ ├── aes.h │ │ │ ├── aesni.h │ │ │ ├── arc4.h │ │ │ ├── aria.h │ │ │ ├── asn1.h │ │ │ ├── asn1write.h │ │ │ ├── base64.h │ │ │ ├── bignum.h │ │ │ ├── blowfish.h │ │ │ ├── bn_mul.h │ │ │ ├── camellia.h │ │ │ ├── ccm.h │ │ │ ├── certs.h │ │ │ ├── chacha20.h │ │ │ ├── chachapoly.h │ │ │ ├── check_config.h │ │ │ ├── cipher.h │ │ │ ├── cipher_internal.h │ │ │ ├── cmac.h │ │ │ ├── compat-1.3.h │ │ │ ├── config.h │ │ │ ├── ctr_drbg.h │ │ │ ├── debug.h │ │ │ ├── des.h │ │ │ ├── dhm.h │ │ │ ├── ecdh.h │ │ │ ├── ecdsa.h │ │ │ ├── ecjpake.h │ │ │ ├── ecp.h │ │ │ ├── ecp_internal.h │ │ │ ├── entropy.h │ │ │ ├── entropy_poll.h │ │ │ ├── error.h │ │ │ ├── gcm.h │ │ │ ├── havege.h │ │ │ ├── hkdf.h │ │ │ ├── hmac_drbg.h │ │ │ ├── md.h │ │ │ ├── md2.h │ │ │ ├── md4.h │ │ │ ├── md5.h │ │ │ ├── md_internal.h │ │ │ ├── memory_buffer_alloc.h │ │ │ ├── net.h │ │ │ ├── net_sockets.h │ │ │ ├── nist_kw.h │ │ │ ├── oid.h │ │ │ ├── padlock.h │ │ │ ├── pem.h │ │ │ ├── pk.h │ │ │ ├── pk_internal.h │ │ │ ├── pkcs11.h │ │ │ ├── pkcs12.h │ │ │ ├── pkcs5.h │ │ │ ├── platform.h │ │ │ ├── platform_time.h │ │ │ ├── platform_util.h │ │ │ ├── poly1305.h │ │ │ ├── ripemd160.h │ │ │ ├── rsa.h │ │ │ ├── rsa_internal.h │ │ │ ├── sha1.h │ │ │ ├── sha256.h │ │ │ ├── sha512.h │ │ │ ├── ssl.h │ │ │ ├── ssl_cache.h │ │ │ ├── ssl_ciphersuites.h │ │ │ ├── ssl_cookie.h │ │ │ ├── ssl_internal.h │ │ │ ├── ssl_ticket.h │ │ │ ├── threading.h │ │ │ ├── timing.h │ │ │ ├── version.h │ │ │ ├── x509.h │ │ │ ├── x509_crl.h │ │ │ ├── x509_crt.h │ │ │ ├── x509_csr.h │ │ │ └── xtea.h │ ├── library │ │ ├── .gitignore │ │ ├── CMakeLists.txt │ │ ├── Makefile │ │ ├── aes.c │ │ ├── aesni.c │ │ ├── arc4.c │ │ ├── aria.c │ │ ├── asn1parse.c │ │ ├── asn1write.c │ │ ├── base64.c │ │ ├── bignum.c │ │ ├── blowfish.c │ │ ├── camellia.c │ │ ├── ccm.c │ │ ├── certs.c │ │ ├── chacha20.c │ │ ├── chachapoly.c │ │ ├── cipher.c │ │ ├── cipher_wrap.c │ │ ├── cmac.c │ │ ├── ctr_drbg.c │ │ ├── debug.c │ │ ├── des.c │ │ ├── dhm.c │ │ ├── ecdh.c │ │ ├── ecdsa.c │ │ ├── ecjpake.c │ │ ├── ecp.c │ │ ├── ecp_curves.c │ │ ├── entropy.c │ │ ├── entropy_poll.c │ │ ├── error.c │ │ ├── gcm.c │ │ ├── havege.c │ │ ├── hkdf.c │ │ ├── hmac_drbg.c │ │ ├── md.c │ │ ├── md2.c │ │ ├── md4.c │ │ ├── md5.c │ │ ├── md_wrap.c │ │ ├── memory_buffer_alloc.c │ │ ├── net_sockets.c │ │ ├── nist_kw.c │ │ ├── oid.c │ │ ├── padlock.c │ │ ├── pem.c │ │ ├── pk.c │ │ ├── pk_wrap.c │ │ ├── pkcs11.c │ │ ├── pkcs12.c │ │ ├── pkcs5.c │ │ ├── pkparse.c │ │ ├── pkwrite.c │ │ ├── platform.c │ │ ├── platform_util.c │ │ ├── poly1305.c │ │ ├── ripemd160.c │ │ ├── rsa.c │ │ ├── rsa_internal.c │ │ ├── sha1.c │ │ ├── sha256.c │ │ ├── sha512.c │ │ ├── ssl_cache.c │ │ ├── ssl_ciphersuites.c │ │ ├── ssl_cli.c │ │ ├── ssl_cookie.c │ │ ├── ssl_srv.c │ │ ├── ssl_ticket.c │ │ ├── ssl_tls.c │ │ ├── threading.c │ │ ├── timing.c │ │ ├── version.c │ │ ├── version_features.c │ │ ├── x509.c │ │ ├── x509_create.c │ │ ├── x509_crl.c │ │ ├── x509_crt.c │ │ ├── x509_csr.c │ │ ├── x509write_crt.c │ │ ├── x509write_csr.c │ │ └── xtea.c │ └── wrapper │ │ ├── CMakeLists.txt │ │ ├── entropy_hardware_alt.c │ │ ├── net_sockets_alt.c │ │ ├── timing_alt.c │ │ └── timing_alt.h ├── nettype_tcp.c ├── nettype_tcp.h ├── nettype_tls.c ├── nettype_tls.h ├── network.c └── network.h ├── platform └── RT-Thread │ ├── platform_memory.c │ ├── platform_memory.h │ ├── platform_mutex.c │ ├── platform_mutex.h │ ├── platform_net_socket.c │ ├── platform_net_socket.h │ ├── platform_thread.c │ ├── platform_thread.h │ ├── platform_timer.c │ └── platform_timer.h ├── png ├── mqtt-tool.png └── mqttclient.png └── test ├── test.c └── test_ca.c /SConscript: -------------------------------------------------------------------------------- 1 | Import('RTT_ROOT') 2 | from building import * 3 | 4 | # get current directory 5 | cwd = GetCurrentDir() 6 | 7 | # The set of source files associated with this SConscript file. 8 | src = Glob('common/*.c') 9 | src += Glob('mqtt/*.c') 10 | src += Glob('mqttclient/*.c') 11 | src += Glob('network/*.c') 12 | src += Glob('platform/RT-Thread/*.c') 13 | 14 | path = [cwd + '/common'] 15 | path += [cwd + '/mqtt'] 16 | path += [cwd + '/mqttclient'] 17 | path += [cwd + '/network'] 18 | path += [cwd + '/platform/RT-Thread'] 19 | 20 | if GetDepend(['KAWAII_MQTT_NETWORK_TYPE_TLS']): 21 | src += Glob('network/mbedtls/library/*.c') 22 | src += Glob('network/mbedtls/wrapper/*.c') 23 | path += [cwd + '/network/mbedtls/wrapper'] 24 | path += [cwd + '/network/mbedtls/include'] 25 | path += [cwd + '/network/mbedtls/include/mbedtls'] 26 | 27 | if GetDepend(['KAWAII_MQTT_LOG_IS_SALOF']): 28 | src += Glob('common/log/*.c') 29 | src += Glob('common/log/arch/rt-thread/*.c') 30 | path += [cwd + '/common/log'] 31 | 32 | if GetDepend(['PKG_USING_KAWAII_MQTT_TEST']): 33 | src += Glob('test/*.c') 34 | 35 | group = DefineGroup('kawaii_mqtt',src , depend = ['PKG_USING_KAWAII_MQTT'], CPPPATH = path) 36 | 37 | Return('group') 38 | -------------------------------------------------------------------------------- /common/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(SUBDIRS "log") 2 | 3 | aux_source_directory(. DIR_SRCS) 4 | 5 | string(REGEX REPLACE ".*/(.*)" "\\1" LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 6 | 7 | if (DIR_SRCS) 8 | foreach(libname ${LIBNAMES}) 9 | if (${LIB_NAME} STREQUAL ${libname}) 10 | add_library(${libname} ${CMAKE_LIB_TYPE} ${DIR_SRCS}) 11 | endif() 12 | endforeach() 13 | 14 | else() 15 | message(WARNING "not find is src file!") 16 | endif() 17 | 18 | 19 | foreach(subdir ${SUBDIRS}) 20 | add_subdirectory(${subdir}) 21 | endforeach() 22 | -------------------------------------------------------------------------------- /common/log/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 2.8) 2 | 3 | set(INCDIRS ${CMAKE_CURRENT_SOURCE_DIR}) 4 | set(SUBDIRS "arch") 5 | set(LIBNAMES "arch" "salof") 6 | set(OUTDIRS "build") 7 | 8 | aux_source_directory(. LOG_DIR_SRCS) 9 | 10 | add_library("salof" ${CMAKE_LIB_TYPE} ${LOG_DIR_SRCS}) 11 | target_link_libraries("salof" "arch") 12 | 13 | foreach(incdir ${INCDIRS}) 14 | include_directories(${incdir}) 15 | endforeach() 16 | 17 | foreach(subdir ${SUBDIRS}) 18 | add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${subdir}) 19 | endforeach() 20 | 21 | link_directories(${LIBRARY_OUTPUT_PATH}) 22 | -------------------------------------------------------------------------------- /common/log/arch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(SUBDIRS "linux") 2 | 3 | foreach(subdir ${SUBDIRS}) 4 | add_subdirectory(${subdir}) 5 | endforeach() 6 | 7 | 8 | -------------------------------------------------------------------------------- /common/log/arch/rt-thread/arch.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-26 19:11:34 5 | * @LastEditTime : 2019-12-28 01:51:38 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include "salof_defconfig.h" 9 | 10 | #ifdef SALOF_USING_LOG 11 | 12 | void *salof_alloc(unsigned int size) 13 | { 14 | return rt_malloc(size); 15 | } 16 | 17 | 18 | void salof_free(void *mem) 19 | { 20 | rt_free(mem); 21 | } 22 | 23 | salof_tcb salof_task_create(const char *name, 24 | void (*task_entry)(void *param), 25 | void * const param, 26 | unsigned int stack_size, 27 | unsigned int priority, 28 | unsigned int tick) 29 | { 30 | salof_tcb task; 31 | 32 | task = rt_thread_create((const char *)name, 33 | task_entry, 34 | param, 35 | stack_size, 36 | priority, 37 | tick); 38 | rt_thread_startup(task); 39 | return task; 40 | } 41 | 42 | salof_mutex salof_mutex_create(void) 43 | { 44 | return rt_mutex_create("salof_mutex", RT_IPC_FLAG_PRIO); 45 | } 46 | 47 | void salof_mutex_delete(salof_mutex mutex) 48 | { 49 | rt_mutex_delete(mutex); 50 | } 51 | 52 | 53 | int salof_mutex_pend(salof_mutex mutex, unsigned int timeout) 54 | { 55 | 56 | if(rt_mutex_take((salof_mutex)mutex, timeout) != RT_EOK) 57 | return -1; 58 | return 0; 59 | } 60 | 61 | int salof_mutex_post(salof_mutex mutex) 62 | { 63 | if(rt_mutex_release((salof_mutex)mutex) != RT_EOK) 64 | return -1; 65 | return 0; 66 | } 67 | 68 | salof_sem salof_sem_create(void) 69 | { 70 | return rt_sem_create("salof_sem", 0, RT_IPC_FLAG_PRIO); 71 | } 72 | 73 | void salof_sem_delete(salof_sem sem) 74 | { 75 | rt_sem_delete((salof_sem)sem); 76 | } 77 | 78 | 79 | int salof_sem_pend(salof_sem sem, unsigned int timeout) 80 | { 81 | 82 | if(rt_sem_take((salof_sem)sem, timeout) != RT_EOK) 83 | return -1; 84 | return 0; 85 | } 86 | 87 | int salof_sem_post(salof_sem sem) 88 | { 89 | if(rt_sem_release((salof_sem)sem) != RT_EOK) 90 | return -1; 91 | return 0; 92 | } 93 | 94 | 95 | unsigned int salof_get_tick(void) 96 | { 97 | return rt_tick_get(); 98 | } 99 | 100 | char *salof_get_task_name(void) 101 | { 102 | return NULL; 103 | } 104 | 105 | static rt_device_t new_device = RT_NULL; 106 | 107 | int send_buff(char *buf, int len) 108 | { 109 | /* find new console device */ 110 | if (new_device == RT_NULL) 111 | { 112 | new_device = rt_device_find(RT_CONSOLE_DEVICE_NAME); 113 | // rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM); 114 | } 115 | #ifdef RT_USING_DEVICE 116 | if (new_device == RT_NULL) 117 | { 118 | rt_hw_console_output(buf); 119 | } 120 | else 121 | { 122 | rt_uint16_t old_flag = new_device->open_flag; 123 | 124 | new_device->open_flag |= RT_DEVICE_FLAG_STREAM; 125 | rt_device_write(new_device, 0, buf, rt_strlen(buf)); 126 | new_device->open_flag = old_flag; 127 | } 128 | #else 129 | rt_hw_console_output(buf); 130 | #endif 131 | return len; 132 | } 133 | 134 | #endif 135 | 136 | -------------------------------------------------------------------------------- /common/log/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | mkdir -p build 4 | cd build 5 | cmake .. 6 | make 7 | -------------------------------------------------------------------------------- /common/log/fifo.c: -------------------------------------------------------------------------------- 1 | #include "fifo.h" 2 | #include 3 | 4 | #ifdef SALOF_USING_LOG 5 | 6 | static unsigned int _flbs(unsigned int x) /* find last bit set*/ 7 | { 8 | unsigned int r = 32; 9 | 10 | if (!x) 11 | return 0; 12 | if (!(x & 0xffff0000u)) { 13 | x <<= 16; 14 | r -= 16; 15 | } 16 | if (!(x & 0xff000000u)) { 17 | x <<= 8; 18 | r -= 8; 19 | } 20 | if (!(x & 0xf0000000u)) { 21 | x <<= 4; 22 | r -= 4; 23 | } 24 | if (!(x & 0xc0000000u)) { 25 | x <<= 2; 26 | r -= 2; 27 | } 28 | if (!(x & 0x80000000u)) { 29 | x <<= 1; 30 | r -= 1; 31 | } 32 | return r; 33 | } 34 | 35 | static unsigned int _salof_fifo_align(unsigned int x) 36 | { 37 | return (1 << (_flbs(x-1)-1)); //memory down alignment 38 | } 39 | 40 | salof_fifo_t salof_fifo_create(unsigned int size) 41 | { 42 | salof_fifo_t fifo; 43 | 44 | if (0 == size) 45 | return NULL; 46 | 47 | if (size & (size - 1)) 48 | size = _salof_fifo_align(size); 49 | 50 | fifo = (salof_fifo_t)salof_alloc((sizeof(struct salof_fifo) + size)); 51 | 52 | if (NULL != fifo) { 53 | fifo->buff = (unsigned char *)fifo + sizeof(struct salof_fifo); 54 | 55 | fifo->mutex = salof_mutex_create(); 56 | fifo->sem = salof_sem_create(); 57 | if ((NULL == fifo->mutex) || (NULL == fifo->sem)) { 58 | salof_free(fifo); 59 | return NULL; 60 | } 61 | 62 | fifo->size = size; 63 | fifo->in = 0; 64 | fifo->out = 0; 65 | 66 | return fifo; 67 | } 68 | 69 | return NULL; 70 | } 71 | 72 | unsigned int salof_fifo_write(salof_fifo_t fifo, void *buff, unsigned int len, unsigned int timeout) 73 | { 74 | int err, l; 75 | 76 | if((!fifo) || (!buff) || (!len)) 77 | return 0; 78 | 79 | err = salof_mutex_pend(fifo->mutex, timeout); 80 | if(err == -1) 81 | return 0; 82 | 83 | len = FIFO_MIN(len, (fifo->size - fifo->in + fifo->out)); 84 | 85 | l = FIFO_MIN(len, (fifo->size - (fifo->in & (fifo->size -1)))); 86 | memcpy(((unsigned char *)fifo->buff + (fifo->in & (fifo->size -1))), buff, l); 87 | memcpy(fifo->buff, (unsigned char *)buff + l, len - l); 88 | 89 | fifo->in += len; 90 | 91 | salof_mutex_post(fifo->mutex); 92 | salof_sem_post(fifo->sem); 93 | 94 | return len; 95 | } 96 | 97 | unsigned int salof_fifo_read(salof_fifo_t fifo, void *buff, unsigned int len, unsigned int timeout) 98 | { 99 | int l; 100 | 101 | salof_sem_pend(fifo->sem, timeout); 102 | 103 | if((!fifo) || (!buff) || (!len)) 104 | return 0; 105 | 106 | len = FIFO_MIN(len, fifo->in - fifo->out); 107 | 108 | l = FIFO_MIN(len, (fifo->size - (fifo->out & (fifo->size -1)))); 109 | memcpy(buff, ((unsigned char *)fifo->buff + (fifo->out & (fifo->size -1))), l); 110 | memcpy((unsigned char *)buff + l, fifo->buff, len - l); 111 | 112 | fifo->out += len; 113 | 114 | return len; 115 | } 116 | 117 | unsigned int salof_fifo_read_able(salof_fifo_t fifo) 118 | { 119 | if(NULL == fifo) 120 | return 0; 121 | 122 | else if(fifo->in == fifo->out) 123 | return 0; 124 | 125 | else if(fifo->in > fifo->out) 126 | return (fifo->in - fifo->out); 127 | 128 | return (fifo->size - (fifo->out - fifo->in)); 129 | } 130 | 131 | unsigned int salof_fifo_write_able(salof_fifo_t fifo) 132 | { 133 | return (fifo->size - salof_fifo_read_able(fifo)); 134 | } 135 | 136 | #endif 137 | -------------------------------------------------------------------------------- /common/log/fifo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-25 23:54:38 5 | * @LastEditTime: 2020-06-17 15:10:03 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _FIFO_H_ 9 | #define _FIFO_H_ 10 | 11 | #include "salof_defconfig.h" 12 | 13 | #ifdef SALOF_USING_LOG 14 | 15 | #define FIFO_READ 0 16 | #define FIFO_WRITE 1 17 | 18 | #define FIFO_MAX(a,b) (((a) > (b)) ? (a) : (b)) 19 | #define FIFO_MIN(a,b) (((a) < (b)) ? (a) : (b)) 20 | 21 | struct salof_fifo { 22 | unsigned int size; /* fifo size */ 23 | unsigned int in; /* data input pointer (in % size) */ 24 | unsigned int out; /* data output pointer (out % size) */ 25 | salof_mutex mutex; /* mutex */ 26 | salof_sem sem; /* sem */ 27 | void *buff; /* data area */ 28 | }; 29 | typedef struct salof_fifo * salof_fifo_t; 30 | 31 | salof_fifo_t salof_fifo_create(unsigned int size); 32 | unsigned int salof_fifo_write(salof_fifo_t fifo, void *buff, unsigned int len, unsigned int timeout); 33 | unsigned int salof_fifo_read(salof_fifo_t fifo, void *buff, unsigned int len, unsigned int timeout); 34 | unsigned int salof_fifo_read_able(salof_fifo_t fifo); 35 | unsigned int salof_fifo_write_able(salof_fifo_t fifo); 36 | 37 | #endif 38 | 39 | #endif // !_FIFO_H_ 40 | 41 | -------------------------------------------------------------------------------- /common/log/format.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-25 23:54:38 5 | * @LastEditTime: 2020-06-17 15:22:42 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _FORMAT_H_ 9 | #define _FORMAT_H_ 10 | 11 | #include 12 | 13 | #define FORMAT_BUF_LEN 12 14 | 15 | /* Format states */ 16 | #define S_DEFAULT 0 17 | #define S_FLAGS 1 18 | #define S_WIDTH 2 19 | #define S_PRECIS 3 20 | #define S_LENGTH 4 21 | #define S_CONV 5 22 | 23 | /* Lenght flags */ 24 | #define L_CHAR 1 25 | #define L_SHORT 2 26 | #define L_LONG 3 27 | #define L_LLONG 4 28 | #define L_DOUBLE 5 29 | 30 | #define F_ALTERNATE 0001 // put 0x infront 16, 0 on octals, b on binary 31 | #define F_ZEROPAD 0002 // value should be zero padded 32 | #define F_LEFT 0004 // left justified if set, otherwise right justified 33 | #define F_SPACE 0010 // place a space before positive number 34 | #define F_PLUS 0020 // show +/- on signed numbers, default only for - 35 | #define F_SIGNED 0040 // is an unsigned number? 36 | #define F_SMALL 0100 // use lowercase for hex? 37 | 38 | #define is_digit(c) (c >= '0' && c <= '9') 39 | 40 | int salof_format_nstr(char *buf, unsigned int size, const char *fmt, va_list ap); 41 | 42 | 43 | #endif // !_FORMAT_H_ 44 | 45 | -------------------------------------------------------------------------------- /common/log/salof.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-27 23:10:36 5 | * @LastEditTime: 2020-06-17 15:22:56 6 | * @Description: the code belongs to jiejie, please keep the author insalof_formation and source code according to the license. 7 | */ 8 | /** synchronous asynchronous log output framework */ 9 | 10 | #include "salof.h" 11 | 12 | #ifdef SALOF_USING_LOG 13 | 14 | #ifndef SALOF_BUFF_SIZE 15 | #define SALOF_BUFF_SIZE (1024U) 16 | #endif 17 | #ifndef SALOF_FIFO_SIZE 18 | #define SALOF_FIFO_SIZE (2048U) 19 | #endif 20 | 21 | static int salof_out(char *buf, int len); 22 | 23 | #ifdef SALOF_USING_SALOF 24 | #include 25 | static salof_fifo_t _salof_fifo = NULL; 26 | static int _len; 27 | static char _out_buff[SALOF_BUFF_SIZE]; 28 | 29 | #if !SALOF_USING_IDLE_HOOK 30 | static salof_tcb _salof_task; 31 | void salof_task(void *parm); 32 | #else 33 | 34 | #if !defined(salof_handler) 35 | #error "salof_handler need to be defined as your hook function" 36 | #endif 37 | 38 | #endif 39 | #endif 40 | 41 | static char _salof_format_buff[SALOF_BUFF_SIZE]; 42 | 43 | int salof_init(void) 44 | { 45 | #ifdef SALOF_USING_SALOF 46 | _salof_fifo = salof_fifo_create(SALOF_FIFO_SIZE); 47 | if(_salof_fifo == NULL) 48 | return -1; 49 | 50 | #if !SALOF_USING_IDLE_HOOK 51 | _salof_task = salof_task_create("salof_task", salof_task, NULL, SALOF_TASK_STACK_SIZE, SALOF_TASK_PRIO, SALOF_TASK_TICK); 52 | if(_salof_task == NULL) 53 | return -1; 54 | #endif 55 | #endif 56 | return 0; 57 | } 58 | 59 | 60 | void salof(const char *fmt, ...) 61 | { 62 | va_list args; 63 | int len; 64 | va_start(args, fmt); 65 | 66 | len = salof_format_nstr(_salof_format_buff, SALOF_BUFF_SIZE - 1, fmt, args); 67 | 68 | if(len > SALOF_BUFF_SIZE) 69 | len = SALOF_BUFF_SIZE - 1; 70 | 71 | #ifdef SALOF_USING_SALOF 72 | salof_fifo_write(_salof_fifo, _salof_format_buff, len, 100); 73 | #else 74 | salof_out(_salof_format_buff, len); 75 | #endif 76 | 77 | va_end(args); 78 | } 79 | 80 | static int salof_out(char *buf, int len) 81 | { 82 | return send_buff(buf, len); 83 | } 84 | 85 | #ifdef SALOF_USING_SALOF 86 | void salof_handler( void ) 87 | { 88 | _len = salof_fifo_read(_salof_fifo, _out_buff, sizeof(_out_buff), 0); 89 | if(_len > 0) { 90 | salof_out((char *)_out_buff, _len); 91 | memset(_out_buff, 0, _len); 92 | } 93 | } 94 | #endif 95 | 96 | #if !SALOF_USING_IDLE_HOOK 97 | void salof_task(void *parm) 98 | { 99 | (void)parm; 100 | while(1) 101 | { 102 | #ifdef SALOF_USING_SALOF 103 | salof_handler(); 104 | #endif 105 | } 106 | } 107 | #endif 108 | 109 | #endif 110 | 111 | -------------------------------------------------------------------------------- /common/log/salof.h: -------------------------------------------------------------------------------- 1 | #ifndef _SALOF_H_ 2 | #define _SALOF_H_ 3 | 4 | #include "salof_defconfig.h" 5 | #include "format.h" 6 | #include "fifo.h" 7 | #include 8 | 9 | int salof_init(void); 10 | void salof(const char *fmt, ...); 11 | 12 | /** font color */ 13 | #define SALOF_FC_BLACK 30 14 | #define SALOF_FC_RED 31 15 | #define SALOF_FC_GREEN 32 16 | #define SALOF_FC_YELLOW 33 17 | #define SALOF_FC_BLUE 34 18 | #define SALOF_FC_PURPLE 35 19 | #define SALOF_FC_DARK 36 20 | #define SALOF_FC_WHITE 37 21 | 22 | #ifdef SALOF_USING_LOG 23 | 24 | #ifdef SALOF_USING_SALOF 25 | #define SALOF_PRINT_LOG salof 26 | #else 27 | 28 | #if ((!SALOF_USING_SALOF)&&(!SALOF_PRINT_LOG)) 29 | #define SALOF_PRINT_LOG printf 30 | #endif 31 | 32 | #ifndef SALOF_PRINT_LOG 33 | #error "If the SALOF_USING_LOG macro definition is turned on, you must define SALOF_PRINT_LOG as the LOG output, such as #definePRINT_LOG printf" 34 | #endif 35 | 36 | #endif 37 | 38 | #ifdef SALOF_LOG_COLOR 39 | #define SALOF_LOG_START(l, c) SALOF_PRINT_LOG("\033\n["#c"m["#l"] >> ") 40 | #define SALOF_LOG_END SALOF_PRINT_LOG("\033[0m") 41 | #else 42 | #define SALOF_LOG_START(l, c) SALOF_PRINT_LOG("\n["#l"] >> ") 43 | #define SALOF_LOG_END 44 | #endif 45 | 46 | #if ((defined SALOF_LOG_TS) && (defined SALOF_LOG_TAR)) 47 | #define SALOF_LOG_T SALOF_PRINT_LOG("[TS: %d] [TAR: %s] ",salof_get_tick(), salof_get_task_name()) 48 | #elif (defined SALOF_LOG_TS) 49 | #define SALOF_LOG_T SALOF_PRINT_LOG("[TS: %d] ", salof_get_tick()) 50 | #elif (defined SALOF_LOG_TAR) 51 | #define SALOF_LOG_T SALOF_PRINT_LOG("[TAR: %s] ", salof_get_task_name()) 52 | #else 53 | #define SALOF_LOG_T 54 | #endif 55 | 56 | 57 | #define SALOF_LOG_LINE(l, c, fmt, ...) \ 58 | do { \ 59 | SALOF_LOG_START(l, c); \ 60 | SALOF_LOG_T; \ 61 | SALOF_PRINT_LOG(fmt, ##__VA_ARGS__); \ 62 | SALOF_LOG_END; \ 63 | } while (0) 64 | 65 | #define SALOF_BASE_LEVEL (0) 66 | #define SALOF_ERR_LEVEL (SALOF_BASE_LEVEL + 1) 67 | #define SALOF_WARN_LEVEL (SALOF_ERR_LEVEL + 1) 68 | #define SALOF_INFO_LEVEL (SALOF_WARN_LEVEL + 1) 69 | #define SALOF_DEBUG_LEVEL (SALOF_INFO_LEVEL + 1) 70 | 71 | #ifndef SALOF_LOG_LEVEL 72 | #define SALOF_LOG_LEVEL SALOF_DEBUG_LEVEL 73 | #endif 74 | 75 | #if SALOF_LOG_LEVEL < SALOF_DEBUG_LEVEL 76 | #define SALOF_LOG_DEBUG(fmt, ...) 77 | #else 78 | #define SALOF_LOG_DEBUG(fmt, ...) SALOF_LOG_LINE(D, 0, fmt, ##__VA_ARGS__) 79 | #endif 80 | 81 | #if SALOF_LOG_LEVEL < SALOF_INFO_LEVEL 82 | #define SALOF_LOG_INFO(fmt, ...) 83 | #else 84 | #define SALOF_LOG_INFO(fmt, ...) SALOF_LOG_LINE(I, SALOF_FC_GREEN, fmt, ##__VA_ARGS__) 85 | #endif 86 | 87 | #if SALOF_LOG_LEVEL < SALOF_WARN_LEVEL 88 | #define SALOF_LOG_WARN(fmt, ...) 89 | #else 90 | #define SALOF_LOG_WARN(fmt, ...) SALOF_LOG_LINE(W, SALOF_FC_YELLOW, fmt, ##__VA_ARGS__) 91 | #endif 92 | 93 | #if SALOF_LOG_LEVEL < SALOF_ERR_LEVEL 94 | #define SALOF_LOG_ERR(fmt, ...) 95 | #else 96 | #define SALOF_LOG_ERR(fmt, ...) SALOF_LOG_LINE(E, SALOF_FC_RED, fmt, ##__VA_ARGS__) 97 | #endif 98 | 99 | #if SALOF_LOG_LEVEL < SALOF_BASE_LEVEL 100 | #define SALOF_LOG(fmt, ...) 101 | #else 102 | #define SALOF_LOG(fmt, ...) SALOF_PRINT_LOG(fmt, ##__VA_ARGS__) 103 | #endif 104 | #else 105 | #define SALOF_LOG_DEBUG(fmt, ...) 106 | #define SALOF_LOG_INFO(fmt, ...) 107 | #define SALOF_LOG_WARN(fmt, ...) 108 | #define SALOF_LOG_ERR(fmt, ...) 109 | #define SALOF_LOG_LOG(fmt, ...) 110 | #endif 111 | 112 | #endif // !_SALOF_H_ 113 | 114 | -------------------------------------------------------------------------------- /common/log/salof_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-02-25 06:01:24 5 | * @LastEditTime: 2020-02-25 09:28:09 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | 9 | #ifndef _SALOF_CONFIG_H_ 10 | #define _SALOF_CONFIG_H_ 11 | 12 | #include "rtconfig.h" 13 | 14 | #endif /* _SALOF_CONFIG_H_ */ 15 | -------------------------------------------------------------------------------- /common/log/salof_defconfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-25 23:56:34 5 | * @LastEditTime: 2020-03-02 01:32:32 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _SALOF_DEFCONFIG_H_ 9 | #define _SALOF_DEFCONFIG_H_ 10 | 11 | #include "salof_config.h" 12 | 13 | #ifdef SALOF_USING_LOG 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #define salof_mutex rt_mutex_t 20 | #define salof_sem rt_sem_t 21 | #define salof_tcb rt_thread_t 22 | #define SALOF_TASK_PRIO (RT_THREAD_PRIORITY_MAX - 1) 23 | 24 | 25 | void *salof_alloc(unsigned int size); 26 | void salof_free(void *mem); 27 | salof_tcb salof_task_create(const char *name, 28 | void (*task_entry)(void *param), 29 | void * const param, 30 | unsigned int stack_size, 31 | unsigned int priority, 32 | unsigned int tick); 33 | salof_mutex salof_mutex_create(void); 34 | void salof_mutex_delete(salof_mutex mutex); 35 | int salof_mutex_pend(salof_mutex mutex, unsigned int timeout); 36 | int salof_mutex_post(salof_mutex mutex); 37 | salof_sem salof_sem_create(void); 38 | void salof_sem_delete(salof_sem sem); 39 | int salof_sem_pend(salof_sem sem, unsigned int timeout); 40 | int salof_sem_post(salof_sem sem); 41 | unsigned int salof_get_tick(void); 42 | char *salof_get_task_name(void); 43 | extern int send_buff(char *buf, int len); 44 | 45 | #endif 46 | 47 | #endif // !_SALOF_DEFCONFIG_H_ 48 | 49 | -------------------------------------------------------------------------------- /common/log/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_ROOT_PATH}/${OUTDIRS}/bin/") 2 | 3 | aux_source_directory(. CURRENT_DIR_SRCS) 4 | 5 | add_executable(${TARGETS} ${CURRENT_DIR_SRCS}) 6 | 7 | foreach(findlib ${LIBNAMES}) 8 | target_link_libraries(${TARGETS} ${findlib}) 9 | endforeach() 10 | 11 | find_package("Threads") 12 | target_link_libraries(${TARGETS} ${CMAKE_THREAD_LIBS_INIT}) 13 | 14 | -------------------------------------------------------------------------------- /common/log/test/test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-26 20:00:29 5 | * @LastEditTime : 2019-12-26 20:56:50 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include "salof.h" 9 | #include 10 | #include 11 | 12 | int main() 13 | { 14 | salof_init(); 15 | 16 | LOG_INFO("asfafasf"); 17 | sleep(1); 18 | LOG_ERR("asfafasf"); 19 | LOG_WARN("asfafasf"); 20 | LOG_DEBUG("asfafasf\n"); 21 | 22 | sleep(1); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /common/mqtt_error.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 00:42:16 5 | * @LastEditTime: 2020-04-27 23:35:05 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _MQTT_ERROR_H_ 9 | #define _MQTT_ERROR_H_ 10 | 11 | typedef enum mqtt_error { 12 | KAWAII_MQTT_SSL_CERT_ERROR = -0x001C, /* cetr parse failed */ 13 | KAWAII_MQTT_SOCKET_FAILED_ERROR = -0x001B, /* socket fd failed */ 14 | KAWAII_MQTT_SOCKET_UNKNOWN_HOST_ERROR = -0x001A, /* socket unknown host ip or domain */ 15 | KAWAII_MQTT_SET_PUBLISH_DUP_FAILED_ERROR = -0x0019, /* mqtt publish packet set udp bit failed */ 16 | KAWAII_MQTT_CLEAN_SESSION_ERROR = -0x0018, /* mqtt clean session error */ 17 | KAWAII_MQTT_ACK_NODE_IS_EXIST_ERROR = -0x0017, /* mqtt ack list is exist ack node */ 18 | KAWAII_MQTT_ACK_HANDLER_NUM_TOO_MUCH_ERROR = -0x0016, /* mqtt ack handler number is too much */ 19 | KAWAII_MQTT_RESUBSCRIBE_ERROR = -0x0015, /* mqtt resubscribe error */ 20 | KAWAII_MQTT_SUBSCRIBE_ERROR = -0x0014, /* mqtt subscribe error */ 21 | KAWAII_MQTT_SEND_PACKET_ERROR = -0x0013, /* mqtt send a packet */ 22 | KAWAII_MQTT_SERIALIZE_PUBLISH_ACK_PACKET_ERROR = -0x0012, /* mqtt serialize publish ack packet error */ 23 | KAWAII_MQTT_PUBLISH_PACKET_ERROR = -0x0011, /* mqtt publish packet error */ 24 | KAWAII_MQTT_RECONNECT_TIMEOUT_ERROR = -0x0010, /* mqtt try reconnect, but timeout */ 25 | KAWAII_MQTT_SUBSCRIBE_NOT_ACK_ERROR = -0x000F, /* mqtt subscribe, but not ack */ 26 | KAWAII_MQTT_NOT_CONNECT_ERROR = -0x000E, /* mqtt not connect */ 27 | KAWAII_MQTT_SUBSCRIBE_ACK_PACKET_ERROR = -0x000D, /* mqtt subscribe, but ack packet error */ 28 | KAWAII_MQTT_UNSUBSCRIBE_ACK_PACKET_ERROR = -0x000C, /* mqtt unsubscribe, but ack packet error */ 29 | KAWAII_MQTT_PUBLISH_ACK_PACKET_ERROR = -0x000B, /* mqtt pubilsh ack packet error */ 30 | KAWAII_MQTT_PUBLISH_ACK_TYPE_ERROR = -0x000A, /* mqtt pubilsh ack type error */ 31 | KAWAII_MQTT_PUBREC_PACKET_ERROR = -0x0009, /* mqtt pubrec packet error */ 32 | KAWAII_MQTT_BUFFER_TOO_SHORT_ERROR = -0x0008, /* mqtt buffer too short */ 33 | KAWAII_MQTT_NOTHING_TO_READ_ERROR = -0x0007, /* mqtt nothing to read */ 34 | KAWAII_MQTT_SUBSCRIBE_QOS_ERROR = -0x0006, /* mqtt subsrcibe qos error */ 35 | KAWAII_MQTT_BUFFER_OVERFLOW_ERROR = -0x0005, /* mqtt buffer overflow */ 36 | KAWAII_MQTT_CONNECT_FAILED_ERROR = -0x0004, /* mqtt connect failed */ 37 | KAWAII_MQTT_MEM_NOT_ENOUGH_ERROR = -0x0003, /* mqtt memory not enough */ 38 | KAWAII_MQTT_NULL_VALUE_ERROR = -0x0002, /* mqtt value is null */ 39 | KAWAII_MQTT_FAILED_ERROR = -0x0001, /* failed */ 40 | KAWAII_MQTT_SUCCESS_ERROR = 0x0000 /* success */ 41 | } mqtt_error_t; 42 | 43 | #define RETURN_ERROR(x) { return x; } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /common/mqtt_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-11 22:46:33 5 | * @LastEditTime: 2020-04-27 23:28:12 6 | * @Description: the following code references TencentOS tiny, please keep the author information and source code according to the license. 7 | */ 8 | 9 | # include "mqtt_list.h" 10 | 11 | static void _mqtt_list_add(mqtt_list_t *node, mqtt_list_t *prev, mqtt_list_t *next) 12 | { 13 | next->prev = node; 14 | node->next = next; 15 | node->prev = prev; 16 | prev->next = node; 17 | } 18 | 19 | static void _mqtt_list_del(mqtt_list_t *prev, mqtt_list_t *next) 20 | { 21 | next->prev = prev; 22 | prev->next = next; 23 | } 24 | 25 | static void _mqtt_list_del_entry(mqtt_list_t *entry) 26 | { 27 | _mqtt_list_del(entry->prev, entry->next); 28 | } 29 | 30 | void mqtt_list_init(mqtt_list_t *list) 31 | { 32 | list->next = list; 33 | list->prev = list; 34 | } 35 | 36 | void mqtt_list_add(mqtt_list_t *node, mqtt_list_t *list) 37 | { 38 | _mqtt_list_add(node, list, list->next); 39 | } 40 | 41 | void mqtt_list_add_tail(mqtt_list_t *node, mqtt_list_t *list) 42 | { 43 | _mqtt_list_add(node, list->prev, list); 44 | } 45 | 46 | void mqtt_list_del(mqtt_list_t *entry) 47 | { 48 | _mqtt_list_del(entry->prev, entry->next); 49 | } 50 | 51 | void mqtt_list_del_init(mqtt_list_t *entry) 52 | { 53 | _mqtt_list_del_entry(entry); 54 | mqtt_list_init(entry); 55 | } 56 | 57 | void mqtt_list_move(mqtt_list_t *node, mqtt_list_t *list) 58 | { 59 | _mqtt_list_del_entry(node); 60 | mqtt_list_add(node, list); 61 | } 62 | 63 | void mqtt_list_move_tail(mqtt_list_t *node, mqtt_list_t *list) 64 | { 65 | _mqtt_list_del_entry(node); 66 | mqtt_list_add_tail(node, list); 67 | } 68 | 69 | int mqtt_list_is_empty(mqtt_list_t *list) 70 | { 71 | return list->next == list; 72 | } 73 | -------------------------------------------------------------------------------- /common/mqtt_list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-11 22:47:55 5 | * @LastEditTime: 2020-04-27 23:25:37 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _MQTT_LIST_H_ 9 | #define _MQTT_LIST_H_ 10 | 11 | typedef struct mqtt_list_node { 12 | struct mqtt_list_node *next; 13 | struct mqtt_list_node *prev; 14 | } mqtt_list_t; 15 | 16 | #define OFFSET_OF_FIELD(type, field) \ 17 | ((size_t)&(((type *)0)->field)) 18 | 19 | #define CONTAINER_OF_FIELD(ptr, type, field) \ 20 | ((type *)((unsigned char *)(ptr) - OFFSET_OF_FIELD(type, field))) 21 | 22 | #define LIST_NODE(node) \ 23 | { &(node), &(node) } 24 | 25 | #define LIST_DEFINE(list) \ 26 | mqtt_list_t list = { &(list), &(list) } 27 | 28 | #define LIST_ENTRY(list, type, field) \ 29 | CONTAINER_OF_FIELD(list, type, field) 30 | 31 | #define LIST_FIRST_ENTRY(list, type, field) \ 32 | LIST_ENTRY((list)->next, type, field) 33 | 34 | #define LIST_FIRST_ENTRY_OR_NULL(list, type, field) \ 35 | (mqtt_list_is_empty(list) ? NULL : LIST_FIRST_ENTRY(list, type, field)) 36 | 37 | #define LIST_FOR_EACH(curr, list) \ 38 | for (curr = (list)->next; curr != (list); curr = curr->next) 39 | 40 | #define LIST_FOR_EACH_PREV(curr, list) \ 41 | for (curr = (list)->prev; curr != (list); curr = curr->prev) 42 | 43 | #define LIST_FOR_EACH_SAFE(curr, next, list) \ 44 | for (curr = (list)->next, next = curr->next; curr != (list); \ 45 | curr = next, next = curr->next) 46 | 47 | #define LIST_FOR_EACH_PREV_SAFE(curr, next, list) \ 48 | for (curr = (list)->prev, next = curr->prev; \ 49 | curr != (list); \ 50 | curr = next, next = curr->prev) 51 | 52 | void mqtt_list_init(mqtt_list_t *list); 53 | void mqtt_list_add(mqtt_list_t *node, mqtt_list_t *list); 54 | void mqtt_list_add_tail(mqtt_list_t *node, mqtt_list_t *list); 55 | void mqtt_list_del(mqtt_list_t *entry); 56 | void mqtt_list_del_init(mqtt_list_t *entry); 57 | void mqtt_list_move(mqtt_list_t *node, mqtt_list_t *list); 58 | void mqtt_list_move_tail(mqtt_list_t *node, mqtt_list_t *list); 59 | int mqtt_list_is_empty(mqtt_list_t *list); 60 | 61 | #endif /* _LIST_H_ */ 62 | 63 | -------------------------------------------------------------------------------- /common/mqtt_log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-27 03:25:58 5 | * @LastEditTime: 2020-06-17 15:26:54 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _MQTT_LOG_H_ 9 | #define _MQTT_LOG_H_ 10 | 11 | #include "mqtt_defconfig.h" 12 | 13 | #define KAWAII_MQTT_LOG_BASE_LEVEL (0) 14 | #define KAWAII_MQTT_LOG_ERR_LEVEL (KAWAII_MQTT_LOG_BASE_LEVEL + 1) 15 | #define KAWAII_MQTT_LOG_WARN_LEVEL (KAWAII_MQTT_LOG_ERR_LEVEL + 1) 16 | #define KAWAII_MQTT_LOG_INFO_LEVEL (KAWAII_MQTT_LOG_WARN_LEVEL + 1) 17 | #define KAWAII_MQTT_LOG_DEBUG_LEVEL (KAWAII_MQTT_LOG_INFO_LEVEL + 1) 18 | 19 | #ifdef KAWAII_MQTT_LOG_IS_SALOF 20 | #include "salof.h" 21 | 22 | #define KAWAII_MQTT_LOG_D(fmt, ...) SALOF_LOG_DEBUG(fmt, ##__VA_ARGS__) 23 | #define KAWAII_MQTT_LOG_I(fmt, ...) SALOF_LOG_INFO(fmt, ##__VA_ARGS__) 24 | #define KAWAII_MQTT_LOG_W(fmt, ...) SALOF_LOG_WARN(fmt, ##__VA_ARGS__) 25 | #define KAWAII_MQTT_LOG_E(fmt, ...) SALOF_LOG_ERR(fmt, ##__VA_ARGS__) 26 | #define mqtt_log_init salof_init 27 | #else 28 | #include 29 | 30 | #if KAWAII_MQTT_LOG_LEVEL < KAWAII_MQTT_LOG_DEBUG_LEVEL 31 | #define KAWAII_MQTT_LOG_D(fmt, ...) 32 | #else 33 | #define KAWAII_MQTT_LOG_D(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");} 34 | #endif 35 | 36 | #if KAWAII_MQTT_LOG_LEVEL < KAWAII_MQTT_LOG_INFO_LEVEL 37 | #define KAWAII_MQTT_LOG_I(fmt, ...) 38 | #else 39 | #define KAWAII_MQTT_LOG_I(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");} 40 | #endif 41 | 42 | #if KAWAII_MQTT_LOG_LEVEL < KAWAII_MQTT_LOG_WARN_LEVEL 43 | #define KAWAII_MQTT_LOG_W(fmt, ...) 44 | #else 45 | #define KAWAII_MQTT_LOG_W(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");} 46 | #endif 47 | 48 | #if KAWAII_MQTT_LOG_LEVEL < KAWAII_MQTT_LOG_ERR_LEVEL 49 | #define KAWAII_MQTT_LOG_E(fmt, ...) 50 | #else 51 | #define KAWAII_MQTT_LOG_E(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");} 52 | #endif 53 | 54 | #if KAWAII_MQTT_LOG_LEVEL < KAWAII_MQTT_LOG_BASE_LEVEL 55 | #define KAWAII_MQTT_LOG(fmt, ...) 56 | #else 57 | #define KAWAII_MQTT_LOG(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");} 58 | #endif 59 | 60 | #define mqtt_log_init() 61 | #endif 62 | 63 | #endif /* _LOG_H_ */ 64 | -------------------------------------------------------------------------------- /common/random.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-01-09 19:25:05 5 | * @LastEditTime: 2020-06-16 14:50:33 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include 9 | #include "random.h" 10 | #include "platform_timer.h" 11 | #include "platform_memory.h" 12 | 13 | static unsigned int last_seed = 1; 14 | 15 | static int do_random(unsigned int seed) 16 | { 17 | srand(seed); 18 | return rand(); 19 | } 20 | 21 | int random_number(void) 22 | { 23 | unsigned int seed = (unsigned int) platform_timer_now(); 24 | last_seed += (seed >> ((seed ^ last_seed) % 3)); 25 | return do_random(last_seed ^ seed); 26 | } 27 | 28 | // random number range interval [min, max) 29 | int random_number_range(unsigned int min, unsigned int max) 30 | { 31 | return (random_number() % (max - min)) + min; 32 | } 33 | 34 | char *random_string(unsigned int len) 35 | { 36 | unsigned int i, flag, seed, random; 37 | 38 | char *str = platform_memory_alloc((size_t)(len + 1)); 39 | if (NULL == str) 40 | return NULL; 41 | 42 | seed = (unsigned int) random_number(); 43 | seed += (unsigned int) ((size_t)str ^ seed); 44 | 45 | random = (unsigned int)do_random(seed); 46 | 47 | for (i = 0; i < len; i++) { 48 | random = do_random(seed ^ random); 49 | flag = (unsigned int)random % 3; 50 | switch (flag) { 51 | case 0: 52 | str[i] = 'A' + do_random(random ^ (i & flag)) % 26; 53 | break; 54 | case 1: 55 | str[i] = 'a' + do_random(random ^ (i & flag)) % 26; 56 | break; 57 | case 2: 58 | str[i] = '0' + do_random(random ^ (i & flag)) % 10; 59 | break; 60 | default: 61 | str[i] = 'x'; 62 | break; 63 | } 64 | random += ((0xb433e5c6 ^ random) << (i & flag)); 65 | } 66 | 67 | str[len] = '\0'; 68 | return str; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /common/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-01-09 19:59:19 5 | * @LastEditTime : 2020-01-10 08:52:45 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | 9 | #ifndef _RANDOM_H_ 10 | #define _RANDOM_H_ 11 | 12 | #define RANDOM_MAX 0x7FFFFFFF 13 | 14 | int random_number(void); 15 | int random_number_range(unsigned int min, unsigned int max); 16 | char *random_string(unsigned int len); 17 | 18 | #endif /* _RANDOM_H_ */ 19 | -------------------------------------------------------------------------------- /docs/mqtt-communication.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # 图文并茂学习MQTT协议通信过程 4 | 5 | # MQTT连接服务器 6 | 7 | 客户端到服务器的网络连接建立后,客户端发送给服务器的第一个报文必须是CONNECT报文 8 | 9 | 在一个网络连接上,客户端只能发送一次CONNECT报文,如果出现第二个CONNECT报文,按照协议标准,服务器会将第二个CONNECT报文当作协议违规处理并断开客户端的连接。 10 | 11 | 对于正常的连接请求,服务器必须产生应答报文,如果无法建立会话,服务器应该在应答报文中报告对应的错误代码。 12 | 13 | ![mqtt007](http://qiniu.jiejie01.top/mqtt007.png) 14 | 15 | # MQTT订阅主题 16 | 17 | 客户端向服务器发送SUBSCRIBE报文用于创建一个或多个订阅。 18 | 19 | 在服务器中,会记录这个客户关注的一个或者多个主题,当服务器收到这些主题的PUBLISH报文的时候,将分发应用消息到与之匹配的客户端中。 20 | 21 | SUBSCRIBE报文支持通配符,也为每个订阅指定了最大的QoS等级,服务器根据这些信息分发应用消息给客户端。 22 | 23 | SUBSCRIBE报文拥有固定报头、可变报头、有效载荷。 24 | 25 | 当服务器收到客户端发送的一个SUBSCRIBE报文时,必须向客户端发送一个SUBACK报文响应,同时SUBACK报文必须和等待确认的SUBSCRIBE报文有相同的报文标识符。 26 | 27 | 28 | 如果服务器收到一个SUBSCRIBE报文,报文的主题过滤器与一个现存订阅的主题过滤器相同,那么必须使用新的订阅彻底替换现存的订阅。新订阅的主题过滤器和之前订阅的相同,但是它的最大QoS值可以不同。与这个主题过滤器匹配的任何现存的保留消息必须被重发,但是发布流程不能中断。 29 | 30 | ![mqtt008](http://qiniu.jiejie01.top/mqtt008.png) 31 | 32 | SUBSCRIBE报文的有效载荷包含了一个主题过滤器列表,它们表示客户端想要订阅的主题,SUBSCRIBE报文有效载荷中的主题过滤器列表必须是UTF-8字符串。 33 | 34 | 服务器应该支持包含通配符的主题过滤器。如果服务器选择不支持包含通配符的主题过滤器,必须拒绝任何包含通配符过滤器的订阅请求。 35 | 36 | 每一个过滤器后面跟着一个字节,这个字节被叫做服务质量要求(Requested QoS)。它给出了服务器向客户端发送应用消息所允许的最大QoS等级。 37 | 38 | # MQTT发布消息 39 | 40 | PUBLISH控制报文是指从客户端向服务器或者服务器向客户端发送一个应用消息。其实从服务器分发的报文给订阅者,也是属于PUBLISH控制报文。 41 | 42 | ## 服务质量等级 QoS 43 | 44 | QoS的值表示应用消息分发的服务质量等级保证,在不同的服务质量等级中,PUBLISH控制报文的处理方式也是不同的,而且PUBLISH报文的接收者(可以是服务器,也可以是客户端)必须按照根据PUBLISH报文中的QoS等级发送对应的应答报文。 45 | 46 | PUBLISH报文固定报头的bit2-bit1位表示服务质量等级: 47 | 48 | | QoS值 | Bit 2 | Bit 1 | 描述 | 49 | | -- | -- | --| -- | 50 | | 0 | 0 | 0 | 最多分发一次 | 51 | | 1 | 0 | 1 | 至少分发一次 | 52 | | 2 | 1 | 0 | 只分发一次 | 53 | | - | 1 | 1 | 保留位 | 54 | 55 | 56 | MQTT按照这里定义的服务质量 (QoS) 等级分发应用消息。服务器分发应用消息给多个客户端(订阅者)时,每个客户端独立处理。从发布者发布消息到接受者,分发的消息服务质量可能是不同的,这取决于订阅者订阅主题时指定的服务质量等级。而对于发布者而言,发布消息时就指定了服务质量等级。 57 | 58 | ## QoS0的PUBLISH控制报文 59 | 60 | 消息的分发依赖于底层网络的能力。服务器不会发送响应,发布者也不会重试,它在发出这个消息的时候就立马将消息丢弃,这个消息可能送达一次也可能根本没送达。 61 | 62 | 发布者必须发送QoS等于0,DUP等于0的PUBLISH报文。 63 | 64 | 在服务器接受PUBLISH报文时要将消息分发给订阅该主题(消息)的订阅者。 65 | 66 | ![mqtt009](http://qiniu.jiejie01.top/mqtt009.png) 67 | 68 | 69 | ## QoS1的PUBLISH控制报文 70 | 71 | 服务质量确保消息至少送达一次,甚至可能被多次处理。QoS1的PUBLISH报文的可变报头中包含一个报文标识符,需要PUBACK报文确认。 72 | 73 | 发布者在每次发送新的应用消息都必须分配一个未使用的报文标识符,在发布消息的同时将消息存储起来,等待服务器的应答,直到从接收者那收到对应的PUBACK报文。发送的PUBLISH报文必须包含报文标识符且QoS等于1,DUP等于0。 74 | 75 | 一旦发布者收到来自服务器的PUBACK报文后,这个报文标识符就可以重复使用。 76 | 77 | 接收者响应的PUBACK报文必须包含一个报文标识符,这个标识符来自接收到的PUBLISH报文。在发送了PUBACK报文之后,接收者必须将任何包含相同报文标识符的入站PUBLISH报文当作一个新的消息,并忽略它的DUP标志的值。 78 | 79 | ![mqtt010](http://qiniu.jiejie01.top/mqtt010.png) 80 | 81 | 82 | ## QoS2的PUBLISH控制报文 83 | 84 | 这是最高等级的服务质量,必须保证有且只有处理一次消息,消息丢失和重复都是不可接受的。使用这个服务质量等级会有额外的开销。 85 | 86 | QoS2的消息可变报头中有报文标识符。 87 | 88 | QoS2的PUBLISH报文的接收者使用一个两步确认过程来确认收到。 89 | 90 | 发送者必须给要发送的新应用消息分配一个未使用的报文标识符。发送的PUBLISH报文必须包含报文标识符且报文的QoS等于2,,DUP等于0。 91 | 92 | 在消息发出去后,需要将这个消息存储起来,而且必须将这个PUBLISH报文看作是未确认的,直到从接收者那收到对应的PUBREC报文。 93 | 94 | 当发布者收到的PUBREC报文后必须发送一个PUBREL报文。PUBREL报文必须包含与原始PUBLISH报文相同的报文标识符。 95 | 96 | 而且发布者还必须必须将这个PUBREL报文看作是未确认的,直到从接收者那收到对应的PUBCOMP报文。一旦发送了对应的PUBREL报文就不能重发这个PUBLISH报文。 97 | 98 | 所以就如下图所示,在发布消息的时候,立马存储消息,在收到PUBREC报文后必须将存储的消息丢弃掉,然后存储报文标识符,与此同时还要将PUBREL报文发送出去,最后在收到PUBCOMP报文后,才丢弃存储的报文标识符。 99 | 100 | ![mqtt011](http://qiniu.jiejie01.top/mqtt011.png) 101 | 102 | 103 | 当然啦,对应分发消息也是比较复杂的,它一般有两种处理方案,每一种方案都要确保消息有且只有处理一次。 104 | 105 | 接收者(此处指服务器)响应的PUBREC报文必须包含报文标识符,这个标识符来自接收到的PUBLISH报文。 106 | 107 | 发送PUBREC报文后,在收到对应的PUBREL报文之前,接收者可以将消息分发给订阅者,但是必须要存储报文标识符(方案1)。 108 | 109 | 当然,它在这种情况下,也可以存储消息,直到收到PUBREL报文才将消息分发到订阅者(方案2)。 110 | 111 | 而当它收到PUBREL报文后,它必须发送PUBCOMP报文响应发布者,该报文必须包含与PUBREL报文相同的标识符。 112 | 113 | 与此同时,它可以丢弃存储的报文标识符(方案1),而不必再分发应用消息给订阅者。 114 | 115 | 如果此前没有分发应用消息给订阅者(方案2),那么此时需要分发应用消息给订阅者,然后丢弃消息。 116 | 117 | 在接收者发送PUBCOMP报文之后,接收者必须将包含相同报文标识符的任何后续PUBLISH报文当作一个新的发布。 118 | 119 | 120 | # 取消订阅 121 | 122 | 客户端发送UNSUBSCRIBE报文给服务器,用于取消订阅主题。 123 | 124 | UNSUBSCRIBE报文固定报头的第3,2,1,0位是保留位且必须分别设置为0,0,1,0。否则服务器必须认为任何其它的值都是不合法的并关闭网络连。具体的描述可以看协议文档。 125 | 126 | UNSUBSCRIBE报文的有效载荷包含客户端想要取消订阅的主题过滤器列表。UNSUBSCRIBE报文中的主题过滤器必须是连续打包的UTF-8编码字符串。 127 | 128 | UNSUBSCRIBE报文的有效载荷必须至少包含一个主题过滤器列表,而且这个主题过滤器是已经被客户端订阅的,否则的话没有订阅也就没有取消订阅一说了。如果一个UNSUBSCRIBE报文没有有效载荷是违反协议的标准的,服务器也不会去处理它。 129 | 130 | 131 | 而对于服务器删除了一个订阅,那么它将不会再分发该主题的消息到这个客户端中。而且它必须完成分发任何已经开始往客户端发送的QoS1和QoS2的消息,以保证消息的服务质量。 132 | 133 | 然后服务器必须发送UNSUBACK报文来响应客户端的UNSUBSCRIBE请求。UNSUBACK报文必须包含和UNSUBSCRIBE报文相同的报文标识符。即使没有删除任何主题订阅(客户端取消订阅的主题未被订阅),服务器也必须发送一个UNSUBACK响应。 134 | 135 | ![mqtt012](http://qiniu.jiejie01.top/mqtt012.png) 136 | 137 | 138 | # 断开连接 139 | 140 | DISCONNECT报文是客户端发给服务端的最后一个控制报文。表示客户端正常断开连接。 141 | 142 | DISCONNECT报文的固定报头保留位必须全为0。 143 | 144 | 客户端发送DISCONNECT报文之后必须关闭网络连接,不能通过那个网络连接再发送任何控制报文。 145 | 146 | 服务端在收到DISCONNECT报文时必须丢弃任何与当前连接关联的未发布的遗嘱消息。而且当客户端没有关闭网络连接的时候服务器应该主动去关闭网络连接。 147 | 148 | ![mqtt013](http://qiniu.jiejie01.top/mqtt013.png) 149 | 150 | 151 | **上一篇**:[MQTT协议简介](./mqtt-introduction.md) 152 | 153 | **下一篇**:[mqttclient代码生成工具](./mqtt-tool.md) 154 | -------------------------------------------------------------------------------- /docs/mqtt-tool.md: -------------------------------------------------------------------------------- 1 | 2 | # mqttclient代码生产工具介绍 3 | 4 | mqttclient代码生产工具主要是用于配置MQTT的参数,并且生成相应的代码,因为是可视化的配置,极易使用。 5 | 6 | 地址:[https://jiejietop.gitee.io/mqtt/index.html](https://jiejietop.gitee.io/mqtt/index.html) 7 | 8 | ![在线代码生成工具](http://qiniu.jiejie01.top/mqtt-tool.png) 9 | 10 | 与此同时改工具页面还包含了mqttclient的API接口介绍及示例,通过它们就能知道这个工具应该配置了什么内容。 11 | 12 | ![mqtt-tool-api](http://qiniu.jiejie01.top/mqtt-tool-api.png) 13 | 14 | # 连接参数配置 15 | 16 | 首先连接参数代表着MQTT客户端应该如何连接到服务器,建立MQTT会话应该是如何配置,这些就是连接参数 17 | 18 | - 与服务器相关的参数有: 19 | 20 | - 服务器地址。 21 | 22 | - 服务器端口号。 23 | 24 | - 服务器CA证书(如果有TLS加密的话)。 25 | 26 | - 建立MQTT连接需要的参数有: 27 | 28 | - MQTT协议版本。 29 | 30 | - 用户名。 31 | 32 | - 密码。 33 | 34 | - 客户端ID。 35 | 36 | - 配置是否清除会话,默认清除会话。 37 | 38 | - 心跳时间间隔,默认是50秒。 39 | 40 | - 是否使用遗嘱。 41 | 42 | - 是否保留遗嘱消息。 43 | 44 | - 设置遗嘱主题。 45 | 46 | - 设置遗嘱消息的服务质量等级。 47 | 48 | - 设置遗嘱消息的内容。 49 | 50 | - 与与客户端资源相关的配置: 51 | 52 | - 设置命令的超时,它主要是用于socket读写超时,默认是5000毫秒。 53 | 54 | - 读缓冲区大小,默认是1024。 55 | 56 | - 写缓冲区大小,默认是1024。 57 | 58 | ![mqtt-tool-connect](http://qiniu.jiejie01.top/mqtt-tool-connect.png) 59 | 60 | # 订阅主题相关的代码配置 61 | 62 | 此配置用于配置MQTT客户端订阅的主题信息,指定订阅主题名字,服务质量等级以及当收到来自这个主题消息时候的回调处理函数(可以为NULL),mqttclient代码生成工具支持动态添加多个主题,满足绝大部分的日常需求。 63 | 64 | ![mqtt-tool-sub](http://qiniu.jiejie01.top/mqtt-tool-sub.png) 65 | 66 | # 发布消息相关的代码配置 67 | 68 | mqttclient代码生成工具支持动态添向多个主题发布消息,满足绝大部分的日常需求。只需指定要发布消息的主题名字、服务质量等级、以及发布的消息内容即可。 69 | 70 | ![mqtt-tool-pub](http://qiniu.jiejie01.top/mqtt-tool-pub.png) 71 | 72 | # 生成代码 73 | 74 | 在配置完成后,点击下方的生成代码按钮,即可生成对应的配置代码。当然,你也可以点击导入模板,使用模板默认的配置来生成代码。 75 | 76 | ![mqtt-tool-code](http://qiniu.jiejie01.top/mqtt-tool-code.png) 77 | 78 | 直接将生成的代码复制到你的main.c文件即可编译运行。 79 | 80 | 81 | **上一篇**:[MQTT通信过程](./mqtt-communication.md) 82 | 83 | **下一篇**:[mqttclient配置及裁剪工具](./mqtt-config.md) 84 | -------------------------------------------------------------------------------- /mqtt/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | aux_source_directory(. DIR_SRCS) 2 | 3 | string(REGEX REPLACE ".*/(.*)" "\\1" LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 4 | 5 | if (DIR_SRCS) 6 | foreach(libname ${LIBNAMES}) 7 | if (${LIB_NAME} STREQUAL ${libname}) 8 | add_library(${libname} ${CMAKE_LIB_TYPE} ${DIR_SRCS}) 9 | endif() 10 | endforeach() 11 | 12 | else() 13 | message(WARNING "not find is src file!") 14 | endif() 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /mqtt/MQTTDeserializePublish.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "StackTrace.h" 18 | #include "MQTTPacket.h" 19 | #include 20 | 21 | #define min(a, b) ((a < b) ? 1 : 0) 22 | 23 | /** 24 | * Deserializes the supplied (wire) buffer into publish data 25 | * @param dup returned integer - the MQTT dup flag 26 | * @param qos returned integer - the MQTT QoS value 27 | * @param retained returned integer - the MQTT retained flag 28 | * @param packetid returned integer - the MQTT packet identifier 29 | * @param topicName returned MQTTString - the MQTT topic in the publish 30 | * @param payload returned byte buffer - the MQTT publish payload 31 | * @param payloadlen returned integer - the length of the MQTT payload 32 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 33 | * @param buflen the length in bytes of the data in the supplied buffer 34 | * @return error code. 1 is success 35 | */ 36 | int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 37 | unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) 38 | { 39 | MQTTHeader header = {0}; 40 | unsigned char* curdata = buf; 41 | unsigned char* enddata = NULL; 42 | int rc = 0; 43 | int mylen = 0; 44 | 45 | FUNC_ENTRY; 46 | header.byte = readChar(&curdata); 47 | if (header.bits.type != PUBLISH) 48 | goto exit; 49 | *dup = header.bits.dup; 50 | *qos = header.bits.qos; 51 | *retained = header.bits.retain; 52 | 53 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 54 | enddata = curdata + mylen; 55 | 56 | if (!readMQTTLenString(topicName, &curdata, enddata) || 57 | enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */ 58 | goto exit; 59 | 60 | if (*qos > 0) 61 | *packetid = readInt(&curdata); 62 | 63 | *payloadlen = enddata - curdata; 64 | *payload = curdata; 65 | rc = 1; 66 | exit: 67 | FUNC_EXIT_RC(rc); 68 | return rc; 69 | } 70 | 71 | 72 | 73 | /** 74 | * Deserializes the supplied (wire) buffer into an ack 75 | * @param packettype returned integer - the MQTT packet type 76 | * @param dup returned integer - the MQTT dup flag 77 | * @param packetid returned integer - the MQTT packet identifier 78 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 79 | * @param buflen the length in bytes of the data in the supplied buffer 80 | * @return error code. 1 is success, 0 is failure 81 | */ 82 | int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen) 83 | { 84 | MQTTHeader header = {0}; 85 | unsigned char* curdata = buf; 86 | unsigned char* enddata = NULL; 87 | int rc = 0; 88 | int mylen; 89 | 90 | FUNC_ENTRY; 91 | header.byte = readChar(&curdata); 92 | *dup = header.bits.dup; 93 | *packettype = header.bits.type; 94 | 95 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 96 | enddata = curdata + mylen; 97 | 98 | if (enddata - curdata < 2) 99 | goto exit; 100 | *packetid = readInt(&curdata); 101 | 102 | rc = 1; 103 | exit: 104 | FUNC_EXIT_RC(rc); 105 | return rc; 106 | } 107 | 108 | -------------------------------------------------------------------------------- /mqtt/MQTTFormat.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #if !defined(MQTTFORMAT_H) 18 | #define MQTTFORMAT_H 19 | 20 | #include "StackTrace.h" 21 | #include "MQTTPacket.h" 22 | 23 | const char* MQTTPacket_getName(unsigned short packetid); 24 | int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data); 25 | int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent); 26 | int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained, 27 | unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen); 28 | int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid); 29 | int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count, 30 | MQTTString topicFilters[], int requestedQoSs[]); 31 | int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs); 32 | int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, 33 | int count, MQTTString topicFilters[]); 34 | char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen); 35 | char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /mqtt/MQTTPacket.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTPACKET_H_ 19 | #define MQTTPACKET_H_ 20 | 21 | #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ 22 | extern "C" { 23 | #endif 24 | 25 | #if defined(WIN32_DLL) || defined(WIN64_DLL) 26 | #define DLLImport __declspec(dllimport) 27 | #define DLLExport __declspec(dllexport) 28 | #elif defined(LINUX_SO) 29 | #define DLLImport extern 30 | #define DLLExport __attribute__ ((visibility ("default"))) 31 | #else 32 | #define DLLImport 33 | #define DLLExport 34 | #endif 35 | 36 | enum errors 37 | { 38 | MQTTPACKET_BUFFER_TOO_SHORT = -2, 39 | MQTTPACKET_READ_ERROR = -1, 40 | MQTTPACKET_READ_COMPLETE 41 | }; 42 | 43 | enum msgTypes 44 | { 45 | CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, 46 | PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, 47 | PINGREQ, PINGRESP, DISCONNECT 48 | }; 49 | 50 | /** 51 | * Bitfields for the MQTT header byte. 52 | */ 53 | typedef union 54 | { 55 | unsigned char byte; /**< the whole byte */ 56 | #if defined(REVERSED) 57 | struct 58 | { 59 | unsigned int type : 4; /**< message type nibble */ 60 | unsigned int dup : 1; /**< DUP flag bit */ 61 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 62 | unsigned int retain : 1; /**< retained flag bit */ 63 | } bits; 64 | #else 65 | struct 66 | { 67 | unsigned int retain : 1; /**< retained flag bit */ 68 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 69 | unsigned int dup : 1; /**< DUP flag bit */ 70 | unsigned int type : 4; /**< message type nibble */ 71 | } bits; 72 | #endif 73 | } MQTTHeader; 74 | 75 | typedef struct 76 | { 77 | int len; 78 | char* data; 79 | } MQTTLenString; 80 | 81 | typedef struct 82 | { 83 | char* cstring; 84 | MQTTLenString lenstring; 85 | } MQTTString; 86 | 87 | #define MQTTString_initializer {NULL, {0, NULL}} 88 | 89 | int MQTTstrlen(MQTTString mqttstring); 90 | 91 | #include "MQTTConnect.h" 92 | #include "MQTTPublish.h" 93 | #include "MQTTSubscribe.h" 94 | #include "MQTTUnsubscribe.h" 95 | #include "MQTTFormat.h" 96 | 97 | DLLExport int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid); 98 | DLLExport int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen); 99 | 100 | int MQTTPacket_len(int rem_len); 101 | DLLExport int MQTTPacket_equals(MQTTString* a, char* b); 102 | 103 | DLLExport int MQTTPacket_encode(unsigned char* buf, int length); 104 | int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value); 105 | int MQTTPacket_decodeBuf(unsigned char* buf, int* value); 106 | 107 | int readInt(unsigned char** pptr); 108 | char readChar(unsigned char** pptr); 109 | void writeChar(unsigned char** pptr, char c); 110 | void writeInt(unsigned char** pptr, int anInt); 111 | int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); 112 | void writeCString(unsigned char** pptr, const char* string); 113 | void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); 114 | 115 | DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); 116 | 117 | typedef struct { 118 | int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ 119 | void *sck; /* pointer to whatever the system may use to identify the transport */ 120 | int multiplier; 121 | int rem_len; 122 | int len; 123 | char state; 124 | }MQTTTransport; 125 | 126 | int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp); 127 | 128 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ 129 | } 130 | #endif 131 | 132 | 133 | #endif /* MQTTPACKET_H_ */ 134 | -------------------------------------------------------------------------------- /mqtt/MQTTPublish.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTPUBLISH_H_ 19 | #define MQTTPUBLISH_H_ 20 | 21 | #if !defined(DLLImport) 22 | #define DLLImport 23 | #endif 24 | #if !defined(DLLExport) 25 | #define DLLExport 26 | #endif 27 | 28 | DLLExport int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, 29 | MQTTString topicName, unsigned char* payload, int payloadlen); 30 | 31 | DLLExport int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 32 | unsigned char** payload, int* payloadlen, unsigned char* buf, int len); 33 | 34 | DLLExport int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid); 35 | DLLExport int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid); 36 | DLLExport int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid); 37 | 38 | #endif /* MQTTPUBLISH_H_ */ 39 | -------------------------------------------------------------------------------- /mqtt/MQTTSubscribe.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-09 20:15:32 5 | * @LastEditTime: 2019-12-20 20:37:31 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | /******************************************************************************* 9 | * Copyright (c) 2014 IBM Corp. 10 | * 11 | * All rights reserved. This program and the accompanying materials 12 | * are made available under the terms of the Eclipse Public License v1.0 13 | * and Eclipse Distribution License v1.0 which accompany this distribution. 14 | * 15 | * The Eclipse Public License is available at 16 | * http://www.eclipse.org/legal/epl-v10.html 17 | * and the Eclipse Distribution License is available at 18 | * http://www.eclipse.org/org/documents/edl-v10.php. 19 | * 20 | * Contributors: 21 | * Ian Craggs - initial API and implementation and/or initial documentation 22 | * Xiang Rong - 442039 Add makefile to Embedded C client 23 | *******************************************************************************/ 24 | 25 | #ifndef MQTTSUBSCRIBE_H_ 26 | #define MQTTSUBSCRIBE_H_ 27 | 28 | #if !defined(DLLImport) 29 | #define DLLImport 30 | #endif 31 | #if !defined(DLLExport) 32 | #define DLLExport 33 | #endif 34 | 35 | DLLExport int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 36 | int count, MQTTString topicFilters[], int requestedQoSs[]); 37 | 38 | DLLExport int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, 39 | int maxcount, int* count, MQTTString topicFilters[], int requestedQoSs[], unsigned char* buf, int len); 40 | 41 | DLLExport int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs); 42 | 43 | DLLExport int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int len); 44 | 45 | 46 | #endif /* MQTTSUBSCRIBE_H_ */ 47 | -------------------------------------------------------------------------------- /mqtt/MQTTSubscribeClient.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters 24 | * @param count the number of topic filter strings in topicFilters 25 | * @param topicFilters the array of topic filter strings to be used in the publish 26 | * @return the length of buffer needed to contain the serialized version of the packet 27 | */ 28 | int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) 29 | { 30 | int i; 31 | int len = 2; /* packetid */ 32 | 33 | for (i = 0; i < count; ++i) 34 | len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */ 35 | return len; 36 | } 37 | 38 | 39 | /** 40 | * Serializes the supplied subscribe data into the supplied buffer, ready for sending 41 | * @param buf the buffer into which the packet will be serialized 42 | * @param buflen the length in bytes of the supplied bufferr 43 | * @param dup integer - the MQTT dup flag 44 | * @param packetid integer - the MQTT packet identifier 45 | * @param count - number of members in the topicFilters and reqQos arrays 46 | * @param topicFilters - array of topic filter names 47 | * @param requestedQoSs - array of requested QoS 48 | * @return the length of the serialized data. <= 0 indicates error 49 | */ 50 | int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count, 51 | MQTTString topicFilters[], int requestedQoSs[]) 52 | { 53 | unsigned char *ptr = buf; 54 | MQTTHeader header = {0}; 55 | int rem_len = 0; 56 | int rc = 0; 57 | int i = 0; 58 | 59 | FUNC_ENTRY; 60 | if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) 61 | { 62 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 63 | goto exit; 64 | } 65 | 66 | header.byte = 0; 67 | header.bits.type = SUBSCRIBE; 68 | header.bits.dup = dup; 69 | header.bits.qos = 1; 70 | writeChar(&ptr, header.byte); /* write header */ 71 | 72 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 73 | 74 | writeInt(&ptr, packetid); 75 | 76 | for (i = 0; i < count; ++i) 77 | { 78 | writeMQTTString(&ptr, topicFilters[i]); 79 | writeChar(&ptr, requestedQoSs[i]); 80 | } 81 | 82 | rc = ptr - buf; 83 | exit: 84 | FUNC_EXIT_RC(rc); 85 | return rc; 86 | } 87 | 88 | 89 | 90 | /** 91 | * Deserializes the supplied (wire) buffer into suback data 92 | * @param packetid returned integer - the MQTT packet identifier 93 | * @param maxcount - the maximum number of members allowed in the grantedQoSs array 94 | * @param count returned integer - number of members in the grantedQoSs array 95 | * @param grantedQoSs returned array of integers - the granted qualities of service 96 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 97 | * @param buflen the length in bytes of the data in the supplied buffer 98 | * @return error code. 1 is success, 0 is failure 99 | */ 100 | int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen) 101 | { 102 | MQTTHeader header = {0}; 103 | unsigned char* curdata = buf; 104 | unsigned char* enddata = NULL; 105 | int rc = 0; 106 | int mylen; 107 | 108 | FUNC_ENTRY; 109 | header.byte = readChar(&curdata); 110 | if (header.bits.type != SUBACK) 111 | goto exit; 112 | 113 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 114 | enddata = curdata + mylen; 115 | if (enddata - curdata < 2) 116 | goto exit; 117 | 118 | *packetid = readInt(&curdata); 119 | 120 | *count = 0; 121 | while (curdata < enddata) 122 | { 123 | if (*count > maxcount) 124 | { 125 | rc = -1; 126 | goto exit; 127 | } 128 | grantedQoSs[(*count)++] = readChar(&curdata); 129 | } 130 | 131 | rc = 1; 132 | exit: 133 | FUNC_EXIT_RC(rc); 134 | return rc; 135 | } 136 | 137 | 138 | -------------------------------------------------------------------------------- /mqtt/MQTTSubscribeServer.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | 23 | /** 24 | * Deserializes the supplied (wire) buffer into subscribe data 25 | * @param dup integer returned - the MQTT dup flag 26 | * @param packetid integer returned - the MQTT packet identifier 27 | * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays 28 | * @param count - number of members in the topicFilters and requestedQoSs arrays 29 | * @param topicFilters - array of topic filter names 30 | * @param requestedQoSs - array of requested QoS 31 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 32 | * @param buflen the length in bytes of the data in the supplied buffer 33 | * @return the length of the serialized data. <= 0 indicates error 34 | */ 35 | int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], 36 | int requestedQoSs[], unsigned char* buf, int buflen) 37 | { 38 | MQTTHeader header = {0}; 39 | unsigned char* curdata = buf; 40 | unsigned char* enddata = NULL; 41 | int rc = -1; 42 | int mylen = 0; 43 | 44 | FUNC_ENTRY; 45 | header.byte = readChar(&curdata); 46 | if (header.bits.type != SUBSCRIBE) 47 | goto exit; 48 | *dup = header.bits.dup; 49 | 50 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 51 | enddata = curdata + mylen; 52 | 53 | *packetid = readInt(&curdata); 54 | 55 | *count = 0; 56 | while (curdata < enddata) 57 | { 58 | if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata)) 59 | goto exit; 60 | if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */ 61 | goto exit; 62 | requestedQoSs[*count] = readChar(&curdata); 63 | (*count)++; 64 | } 65 | 66 | rc = 1; 67 | exit: 68 | FUNC_EXIT_RC(rc); 69 | return rc; 70 | } 71 | 72 | 73 | /** 74 | * Serializes the supplied suback data into the supplied buffer, ready for sending 75 | * @param buf the buffer into which the packet will be serialized 76 | * @param buflen the length in bytes of the supplied buffer 77 | * @param packetid integer - the MQTT packet identifier 78 | * @param count - number of members in the grantedQoSs array 79 | * @param grantedQoSs - array of granted QoS 80 | * @return the length of the serialized data. <= 0 indicates error 81 | */ 82 | int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs) 83 | { 84 | MQTTHeader header = {0}; 85 | int rc = -1; 86 | unsigned char *ptr = buf; 87 | int i; 88 | 89 | FUNC_ENTRY; 90 | if (buflen < 2 + count) 91 | { 92 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 93 | goto exit; 94 | } 95 | header.byte = 0; 96 | header.bits.type = SUBACK; 97 | writeChar(&ptr, header.byte); /* write header */ 98 | 99 | ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */ 100 | 101 | writeInt(&ptr, packetid); 102 | 103 | for (i = 0; i < count; ++i) 104 | writeChar(&ptr, grantedQoSs[i]); 105 | 106 | rc = ptr - buf; 107 | exit: 108 | FUNC_EXIT_RC(rc); 109 | return rc; 110 | } 111 | 112 | 113 | -------------------------------------------------------------------------------- /mqtt/MQTTUnsubscribe.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTUNSUBSCRIBE_H_ 19 | #define MQTTUNSUBSCRIBE_H_ 20 | 21 | #if !defined(DLLImport) 22 | #define DLLImport 23 | #endif 24 | #if !defined(DLLExport) 25 | #define DLLExport 26 | #endif 27 | 28 | DLLExport int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 29 | int count, MQTTString topicFilters[]); 30 | 31 | DLLExport int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int max_count, int* count, MQTTString topicFilters[], 32 | unsigned char* buf, int len); 33 | 34 | DLLExport int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid); 35 | 36 | DLLExport int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int len); 37 | 38 | #endif /* MQTTUNSUBSCRIBE_H_ */ 39 | -------------------------------------------------------------------------------- /mqtt/MQTTUnsubscribeClient.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters 24 | * @param count the number of topic filter strings in topicFilters 25 | * @param topicFilters the array of topic filter strings to be used in the publish 26 | * @return the length of buffer needed to contain the serialized version of the packet 27 | */ 28 | int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[]) 29 | { 30 | int i; 31 | int len = 2; /* packetid */ 32 | 33 | for (i = 0; i < count; ++i) 34 | len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/ 35 | return len; 36 | } 37 | 38 | 39 | /** 40 | * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending 41 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 42 | * @param buflen the length in bytes of the data in the supplied buffer 43 | * @param dup integer - the MQTT dup flag 44 | * @param packetid integer - the MQTT packet identifier 45 | * @param count - number of members in the topicFilters array 46 | * @param topicFilters - array of topic filter names 47 | * @return the length of the serialized data. <= 0 indicates error 48 | */ 49 | int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 50 | int count, MQTTString topicFilters[]) 51 | { 52 | unsigned char *ptr = buf; 53 | MQTTHeader header = {0}; 54 | int rem_len = 0; 55 | int rc = -1; 56 | int i = 0; 57 | 58 | FUNC_ENTRY; 59 | if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen) 60 | { 61 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 62 | goto exit; 63 | } 64 | 65 | header.byte = 0; 66 | header.bits.type = UNSUBSCRIBE; 67 | header.bits.dup = dup; 68 | header.bits.qos = 1; 69 | writeChar(&ptr, header.byte); /* write header */ 70 | 71 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 72 | 73 | writeInt(&ptr, packetid); 74 | 75 | for (i = 0; i < count; ++i) 76 | writeMQTTString(&ptr, topicFilters[i]); 77 | 78 | rc = ptr - buf; 79 | exit: 80 | FUNC_EXIT_RC(rc); 81 | return rc; 82 | } 83 | 84 | 85 | /** 86 | * Deserializes the supplied (wire) buffer into unsuback data 87 | * @param packetid returned integer - the MQTT packet identifier 88 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 89 | * @param buflen the length in bytes of the data in the supplied buffer 90 | * @return error code. 1 is success, 0 is failure 91 | */ 92 | int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen) 93 | { 94 | unsigned char type = 0; 95 | unsigned char dup = 0; 96 | int rc = 0; 97 | 98 | FUNC_ENTRY; 99 | rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen); 100 | if (type == UNSUBACK) 101 | rc = 1; 102 | FUNC_EXIT_RC(rc); 103 | return rc; 104 | } 105 | 106 | 107 | -------------------------------------------------------------------------------- /mqtt/MQTTUnsubscribeServer.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | 23 | /** 24 | * Deserializes the supplied (wire) buffer into unsubscribe data 25 | * @param dup integer returned - the MQTT dup flag 26 | * @param packetid integer returned - the MQTT packet identifier 27 | * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays 28 | * @param count - number of members in the topicFilters and requestedQoSs arrays 29 | * @param topicFilters - array of topic filter names 30 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 31 | * @param buflen the length in bytes of the data in the supplied buffer 32 | * @return the length of the serialized data. <= 0 indicates error 33 | */ 34 | int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], 35 | unsigned char* buf, int len) 36 | { 37 | MQTTHeader header = {0}; 38 | unsigned char* curdata = buf; 39 | unsigned char* enddata = NULL; 40 | int rc = 0; 41 | int mylen = 0; 42 | 43 | FUNC_ENTRY; 44 | header.byte = readChar(&curdata); 45 | if (header.bits.type != UNSUBSCRIBE) 46 | goto exit; 47 | *dup = header.bits.dup; 48 | 49 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 50 | enddata = curdata + mylen; 51 | 52 | *packetid = readInt(&curdata); 53 | 54 | *count = 0; 55 | while (curdata < enddata) 56 | { 57 | if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata)) 58 | goto exit; 59 | (*count)++; 60 | } 61 | 62 | rc = 1; 63 | exit: 64 | FUNC_EXIT_RC(rc); 65 | return rc; 66 | } 67 | 68 | 69 | /** 70 | * Serializes the supplied unsuback data into the supplied buffer, ready for sending 71 | * @param buf the buffer into which the packet will be serialized 72 | * @param buflen the length in bytes of the supplied buffer 73 | * @param packetid integer - the MQTT packet identifier 74 | * @return the length of the serialized data. <= 0 indicates error 75 | */ 76 | int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid) 77 | { 78 | MQTTHeader header = {0}; 79 | int rc = 0; 80 | unsigned char *ptr = buf; 81 | 82 | FUNC_ENTRY; 83 | if (buflen < 2) 84 | { 85 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 86 | goto exit; 87 | } 88 | header.byte = 0; 89 | header.bits.type = UNSUBACK; 90 | writeChar(&ptr, header.byte); /* write header */ 91 | 92 | ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */ 93 | 94 | writeInt(&ptr, packetid); 95 | 96 | rc = ptr - buf; 97 | exit: 98 | FUNC_EXIT_RC(rc); 99 | return rc; 100 | } 101 | 102 | 103 | -------------------------------------------------------------------------------- /mqtt/StackTrace.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - fix for bug #434081 16 | *******************************************************************************/ 17 | 18 | #ifndef STACKTRACE_H_ 19 | #define STACKTRACE_H_ 20 | 21 | #include 22 | #define NOSTACKTRACE 1 23 | 24 | #if defined(NOSTACKTRACE) 25 | #define FUNC_ENTRY 26 | #define FUNC_ENTRY_NOLOG 27 | #define FUNC_ENTRY_MED 28 | #define FUNC_ENTRY_MAX 29 | #define FUNC_EXIT 30 | #define FUNC_EXIT_NOLOG 31 | #define FUNC_EXIT_MED 32 | #define FUNC_EXIT_MAX 33 | #define FUNC_EXIT_RC(x) 34 | #define FUNC_EXIT_MED_RC(x) 35 | #define FUNC_EXIT_MAX_RC(x) 36 | 37 | #else 38 | 39 | #if defined(WIN32) 40 | #define inline __inline 41 | #define FUNC_ENTRY StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MINIMUM) 42 | #define FUNC_ENTRY_NOLOG StackTrace_entry(__FUNCTION__, __LINE__, -1) 43 | #define FUNC_ENTRY_MED StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MEDIUM) 44 | #define FUNC_ENTRY_MAX StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MAXIMUM) 45 | #define FUNC_EXIT StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MINIMUM) 46 | #define FUNC_EXIT_NOLOG StackTrace_exit(__FUNCTION__, __LINE__, -1) 47 | #define FUNC_EXIT_MED StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MEDIUM) 48 | #define FUNC_EXIT_MAX StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MAXIMUM) 49 | #define FUNC_EXIT_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MINIMUM) 50 | #define FUNC_EXIT_MED_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MEDIUM) 51 | #define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MAXIMUM) 52 | #else 53 | #define FUNC_ENTRY StackTrace_entry(__func__, __LINE__, TRACE_MINIMUM) 54 | #define FUNC_ENTRY_NOLOG StackTrace_entry(__func__, __LINE__, -1) 55 | #define FUNC_ENTRY_MED StackTrace_entry(__func__, __LINE__, TRACE_MEDIUM) 56 | #define FUNC_ENTRY_MAX StackTrace_entry(__func__, __LINE__, TRACE_MAXIMUM) 57 | #define FUNC_EXIT StackTrace_exit(__func__, __LINE__, NULL, TRACE_MINIMUM) 58 | #define FUNC_EXIT_NOLOG StackTrace_exit(__func__, __LINE__, NULL, -1) 59 | #define FUNC_EXIT_MED StackTrace_exit(__func__, __LINE__, NULL, TRACE_MEDIUM) 60 | #define FUNC_EXIT_MAX StackTrace_exit(__func__, __LINE__, NULL, TRACE_MAXIMUM) 61 | #define FUNC_EXIT_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MINIMUM) 62 | #define FUNC_EXIT_MED_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MEDIUM) 63 | #define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MAXIMUM) 64 | 65 | void StackTrace_entry(const char* name, int line, int trace); 66 | void StackTrace_exit(const char* name, int line, void* return_value, int trace); 67 | 68 | void StackTrace_printStack(FILE* dest); 69 | char* StackTrace_get(unsigned long); 70 | 71 | #endif 72 | 73 | #endif 74 | 75 | 76 | 77 | 78 | #endif /* STACKTRACE_H_ */ 79 | -------------------------------------------------------------------------------- /mqttclient/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | aux_source_directory(. DIR_SRCS) 2 | 3 | string(REGEX REPLACE ".*/(.*)" "\\1" LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 4 | 5 | if (DIR_SRCS) 6 | foreach(libname ${LIBNAMES}) 7 | if (${LIB_NAME} STREQUAL ${libname}) 8 | add_library(${libname} ${CMAKE_LIB_TYPE} ${DIR_SRCS}) 9 | target_link_libraries(${libname} "mqtt" "platform" "network" "common") 10 | endif() 11 | endforeach() 12 | 13 | else() 14 | message(WARNING "not find is src file!") 15 | endif() 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /mqttclient/mqtt_defconfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-02-25 03:36:09 5 | * @LastEditTime: 2020-05-20 16:24:20 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | 9 | #ifndef _DEFCONFIG_H_ 10 | #define _DEFCONFIG_H_ 11 | 12 | #include "rtconfig.h" 13 | 14 | #ifndef KAWAII_MQTT_LOG_LEVEL 15 | #define KAWAII_MQTT_LOG_LEVEL KAWAII_MQTT_LOG_DEBUG_LEVEL //KAWAII_MQTT_LOG_WARN_LEVEL KAWAII_MQTT_LOG_DEBUG_LEVEL 16 | #endif // !KAWAII_MQTT_LOG_LEVEL 17 | 18 | #ifndef KAWAII_MQTT_MAX_PACKET_ID 19 | #define KAWAII_MQTT_MAX_PACKET_ID (0xFFFF - 1) 20 | #endif // !KAWAII_MQTT_MAX_PACKET_ID 21 | 22 | #ifndef KAWAII_MQTT_TOPIC_LEN_MAX 23 | #define KAWAII_MQTT_TOPIC_LEN_MAX 64 24 | #endif // !KAWAII_MQTT_TOPIC_LEN_MAX 25 | 26 | #ifndef KAWAII_MQTT_ACK_HANDLER_NUM_MAX 27 | #define KAWAII_MQTT_ACK_HANDLER_NUM_MAX 64 28 | #endif // !KAWAII_MQTT_ACK_HANDLER_NUM_MAX 29 | 30 | #ifndef KAWAII_MQTT_DEFAULT_BUF_SIZE 31 | #define KAWAII_MQTT_DEFAULT_BUF_SIZE 1024 32 | #endif // !KAWAII_MQTT_DEFAULT_BUF_SIZE 33 | 34 | #ifndef KAWAII_MQTT_DEFAULT_CMD_TIMEOUT 35 | #define KAWAII_MQTT_DEFAULT_CMD_TIMEOUT 4000 36 | #endif // !KAWAII_MQTT_DEFAULT_CMD_TIMEOUT 37 | 38 | #ifndef KAWAII_MQTT_MAX_CMD_TIMEOUT 39 | #define KAWAII_MQTT_MAX_CMD_TIMEOUT 20000 40 | #endif // !KAWAII_MQTT_MAX_CMD_TIMEOUT 41 | 42 | #ifndef KAWAII_MQTT_MIN_CMD_TIMEOUT 43 | #define KAWAII_MQTT_MIN_CMD_TIMEOUT 1000 44 | #endif // !KAWAII_MQTT_MIN_CMD_TIMEOUT 45 | 46 | #ifndef KAWAII_MQTT_KEEP_ALIVE_INTERVAL 47 | #define KAWAII_MQTT_KEEP_ALIVE_INTERVAL 100 // unit: second 48 | #endif // !KAWAII_MQTT_KEEP_ALIVE_INTERVAL 49 | 50 | #ifndef KAWAII_MQTT_VERSION 51 | #define KAWAII_MQTT_VERSION 4 // 4 is mqtt 3.1.1 52 | #endif // !KAWAII_MQTT_VERSION 53 | 54 | #ifndef KAWAII_MQTT_RECONNECT_DEFAULT_DURATION 55 | #define KAWAII_MQTT_RECONNECT_DEFAULT_DURATION 1000 56 | #endif // !KAWAII_MQTT_RECONNECT_DEFAULT_DURATION 57 | 58 | #ifndef KAWAII_MQTT_THREAD_STACK_SIZE 59 | #define KAWAII_MQTT_THREAD_STACK_SIZE 4096 60 | #endif // !KAWAII_MQTT_THREAD_STACK_SIZE 61 | 62 | #ifndef KAWAII_MQTT_THREAD_PRIO 63 | #define KAWAII_MQTT_THREAD_PRIO 5 64 | #endif // !KAWAII_MQTT_THREAD_PRIO 65 | 66 | #ifndef KAWAII_MQTT_THREAD_TICK 67 | #define KAWAII_MQTT_THREAD_TICK 50 68 | #endif // !KAWAII_MQTT_THREAD_TICK 69 | 70 | 71 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 72 | 73 | #ifndef KAWAII_MQTT_TLS_HANDSHAKE_TIMEOUT 74 | #define KAWAII_MQTT_TLS_HANDSHAKE_TIMEOUT (5 * 1000) 75 | #endif // !KAWAII_MQTT_TLS_HANDSHAKE_TIMEOUT 76 | 77 | #include "mbedtls/config.h" 78 | #include "mbedtls/ssl.h" 79 | #include "mbedtls/entropy.h" 80 | #include "mbedtls/net_sockets.h" 81 | #include "mbedtls/ctr_drbg.h" 82 | #include "mbedtls/error.h" 83 | #include "mbedtls/debug.h" 84 | #endif /* KAWAII_MQTT_NETWORK_TYPE_TLS */ 85 | 86 | #endif /* _DEFCONFIG_H_ */ 87 | -------------------------------------------------------------------------------- /network/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(SUBDIRS "mbedtls") 2 | 3 | aux_source_directory(. DIR_SRCS) 4 | 5 | string(REGEX REPLACE ".*/(.*)" "\\1" LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 6 | 7 | if (DIR_SRCS) 8 | foreach(libname ${LIBNAMES}) 9 | if (${LIB_NAME} STREQUAL ${libname}) 10 | add_library(${libname} ${CMAKE_LIB_TYPE} ${DIR_SRCS}) 11 | target_link_libraries(${libname} "platform" "mbedtls") 12 | endif() 13 | endforeach() 14 | 15 | else() 16 | message(WARNING "not find is src file!") 17 | endif() 18 | 19 | foreach(subdir ${SUBDIRS}) 20 | add_subdirectory(${subdir}) 21 | endforeach() 22 | 23 | -------------------------------------------------------------------------------- /network/mbedtls/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | set(MBEDTLS_SUBDIRS "library" "include" "wrapper") 4 | set(MBEDTLS_INCDIRS "include" "include/mbedtls" "configs" "wrapper") 5 | 6 | foreach(incdir ${INCDIRS}) 7 | include_directories(${MBEDTLS_INCDIRS}) 8 | endforeach() 9 | 10 | foreach(subdir ${MBEDTLS_SUBDIRS}) 11 | add_subdirectory(${subdir}) 12 | endforeach() 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /network/mbedtls/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | mkdir -p build build/bin build/lib 4 | cd build 5 | cmake .. 6 | make 7 | -------------------------------------------------------------------------------- /network/mbedtls/configs/config-ccm-psk-tls1_2.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config-ccm-psk-tls1_2.h 3 | * 4 | * \brief Minimal configuration for TLS 1.2 with PSK and AES-CCM ciphersuites 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | /* 25 | * Minimal configuration for TLS 1.2 with PSK and AES-CCM ciphersuites 26 | * Distinguishing features: 27 | * - no bignum, no PK, no X509 28 | * - fully modern and secure (provided the pre-shared keys have high entropy) 29 | * - very low record overhead with CCM-8 30 | * - optimized for low RAM usage 31 | * 32 | * See README.txt for usage instructions. 33 | */ 34 | #ifndef MBEDTLS_CONFIG_H 35 | #define MBEDTLS_CONFIG_H 36 | 37 | /* System support */ 38 | //#define MBEDTLS_HAVE_TIME /* Optionally used in Hello messages */ 39 | /* Other MBEDTLS_HAVE_XXX flags irrelevant for this configuration */ 40 | 41 | /* mbed TLS feature support */ 42 | #define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED 43 | #define MBEDTLS_SSL_PROTO_TLS1_2 44 | 45 | /* mbed TLS modules */ 46 | #define MBEDTLS_AES_C 47 | #define MBEDTLS_CCM_C 48 | #define MBEDTLS_CIPHER_C 49 | #define MBEDTLS_CTR_DRBG_C 50 | #define MBEDTLS_ENTROPY_C 51 | #define MBEDTLS_MD_C 52 | #define MBEDTLS_NET_C 53 | #define MBEDTLS_SHA256_C 54 | #define MBEDTLS_SSL_CLI_C 55 | #define MBEDTLS_SSL_SRV_C 56 | #define MBEDTLS_SSL_TLS_C 57 | 58 | /* Save RAM at the expense of ROM */ 59 | #define MBEDTLS_AES_ROM_TABLES 60 | 61 | /* Save some RAM by adjusting to your exact needs */ 62 | #define MBEDTLS_PSK_MAX_LEN 16 /* 128-bits keys are generally enough */ 63 | 64 | /* 65 | * You should adjust this to the exact number of sources you're using: default 66 | * is the "platform_entropy_poll" source, but you may want to add other ones 67 | * Minimum is 2 for the entropy test suite. 68 | */ 69 | #define MBEDTLS_ENTROPY_MAX_SOURCES 2 70 | 71 | /* 72 | * Use only CCM_8 ciphersuites, and 73 | * save ROM and a few bytes of RAM by specifying our own ciphersuite list 74 | */ 75 | #define MBEDTLS_SSL_CIPHERSUITES \ 76 | MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8, \ 77 | MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8 78 | 79 | /* 80 | * Save RAM at the expense of interoperability: do this only if you control 81 | * both ends of the connection! (See comments in "mbedtls/ssl.h".) 82 | * The optimal size here depends on the typical size of records. 83 | */ 84 | #define MBEDTLS_SSL_MAX_CONTENT_LEN 1024 85 | 86 | #include "mbedtls/check_config.h" 87 | 88 | #endif /* MBEDTLS_CONFIG_H */ 89 | -------------------------------------------------------------------------------- /network/mbedtls/configs/config-mini-tls1_1.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config-mini-tls1_1.h 3 | * 4 | * \brief Minimal configuration for TLS 1.1 (RFC 4346) 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | /* 25 | * Minimal configuration for TLS 1.1 (RFC 4346), implementing only the 26 | * required ciphersuite: MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA 27 | * 28 | * See README.txt for usage instructions. 29 | */ 30 | 31 | #ifndef MBEDTLS_CONFIG_H 32 | #define MBEDTLS_CONFIG_H 33 | 34 | /* System support */ 35 | #define MBEDTLS_HAVE_ASM 36 | #define MBEDTLS_HAVE_TIME 37 | 38 | /* mbed TLS feature support */ 39 | #define MBEDTLS_CIPHER_MODE_CBC 40 | #define MBEDTLS_PKCS1_V15 41 | #define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED 42 | #define MBEDTLS_SSL_PROTO_TLS1_1 43 | 44 | /* mbed TLS modules */ 45 | #define MBEDTLS_AES_C 46 | #define MBEDTLS_ASN1_PARSE_C 47 | #define MBEDTLS_ASN1_WRITE_C 48 | #define MBEDTLS_BIGNUM_C 49 | #define MBEDTLS_CIPHER_C 50 | #define MBEDTLS_CTR_DRBG_C 51 | #define MBEDTLS_DES_C 52 | #define MBEDTLS_ENTROPY_C 53 | #define MBEDTLS_MD_C 54 | #define MBEDTLS_MD5_C 55 | #define MBEDTLS_NET_C 56 | #define MBEDTLS_OID_C 57 | #define MBEDTLS_PK_C 58 | #define MBEDTLS_PK_PARSE_C 59 | #define MBEDTLS_RSA_C 60 | #define MBEDTLS_SHA1_C 61 | #define MBEDTLS_SHA256_C 62 | #define MBEDTLS_SSL_CLI_C 63 | #define MBEDTLS_SSL_SRV_C 64 | #define MBEDTLS_SSL_TLS_C 65 | #define MBEDTLS_X509_CRT_PARSE_C 66 | #define MBEDTLS_X509_USE_C 67 | 68 | /* For test certificates */ 69 | #define MBEDTLS_BASE64_C 70 | #define MBEDTLS_CERTS_C 71 | #define MBEDTLS_PEM_PARSE_C 72 | 73 | /* For testing with compat.sh */ 74 | #define MBEDTLS_FS_IO 75 | 76 | #include "mbedtls/check_config.h" 77 | 78 | #endif /* MBEDTLS_CONFIG_H */ 79 | -------------------------------------------------------------------------------- /network/mbedtls/configs/config-no-entropy.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config-no-entropy.h 3 | * 4 | * \brief Minimal configuration of features that do not require an entropy source 5 | */ 6 | /* 7 | * Copyright (C) 2016, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | /* 25 | * Minimal configuration of features that do not require an entropy source 26 | * Distinguishing reatures: 27 | * - no entropy module 28 | * - no TLS protocol implementation available due to absence of an entropy 29 | * source 30 | * 31 | * See README.txt for usage instructions. 32 | */ 33 | 34 | #ifndef MBEDTLS_CONFIG_H 35 | #define MBEDTLS_CONFIG_H 36 | 37 | /* System support */ 38 | #define MBEDTLS_HAVE_ASM 39 | #define MBEDTLS_HAVE_TIME 40 | 41 | /* mbed TLS feature support */ 42 | #define MBEDTLS_CIPHER_MODE_CBC 43 | #define MBEDTLS_CIPHER_PADDING_PKCS7 44 | #define MBEDTLS_REMOVE_ARC4_CIPHERSUITES 45 | #define MBEDTLS_ECP_DP_SECP256R1_ENABLED 46 | #define MBEDTLS_ECP_DP_SECP384R1_ENABLED 47 | #define MBEDTLS_ECP_DP_CURVE25519_ENABLED 48 | #define MBEDTLS_ECP_NIST_OPTIM 49 | #define MBEDTLS_ECDSA_DETERMINISTIC 50 | #define MBEDTLS_PK_RSA_ALT_SUPPORT 51 | #define MBEDTLS_PKCS1_V15 52 | #define MBEDTLS_PKCS1_V21 53 | #define MBEDTLS_SELF_TEST 54 | #define MBEDTLS_VERSION_FEATURES 55 | #define MBEDTLS_X509_CHECK_KEY_USAGE 56 | #define MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE 57 | 58 | /* mbed TLS modules */ 59 | #define MBEDTLS_AES_C 60 | #define MBEDTLS_ASN1_PARSE_C 61 | #define MBEDTLS_ASN1_WRITE_C 62 | #define MBEDTLS_BASE64_C 63 | #define MBEDTLS_BIGNUM_C 64 | #define MBEDTLS_CCM_C 65 | #define MBEDTLS_CIPHER_C 66 | #define MBEDTLS_ECDSA_C 67 | #define MBEDTLS_ECP_C 68 | #define MBEDTLS_ERROR_C 69 | #define MBEDTLS_GCM_C 70 | #define MBEDTLS_HMAC_DRBG_C 71 | #define MBEDTLS_MD_C 72 | #define MBEDTLS_OID_C 73 | #define MBEDTLS_PEM_PARSE_C 74 | #define MBEDTLS_PK_C 75 | #define MBEDTLS_PK_PARSE_C 76 | #define MBEDTLS_PK_WRITE_C 77 | #define MBEDTLS_PLATFORM_C 78 | #define MBEDTLS_RSA_C 79 | #define MBEDTLS_SHA256_C 80 | #define MBEDTLS_SHA512_C 81 | #define MBEDTLS_VERSION_C 82 | #define MBEDTLS_X509_USE_C 83 | #define MBEDTLS_X509_CRT_PARSE_C 84 | #define MBEDTLS_X509_CRL_PARSE_C 85 | //#define MBEDTLS_CMAC_C 86 | 87 | /* Miscellaneous options */ 88 | #define MBEDTLS_AES_ROM_TABLES 89 | 90 | #include "check_config.h" 91 | 92 | #endif /* MBEDTLS_CONFIG_H */ 93 | -------------------------------------------------------------------------------- /network/mbedtls/configs/config-suite-b.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config-suite-b.h 3 | * 4 | * \brief Minimal configuration for TLS NSA Suite B Profile (RFC 6460) 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | /* 25 | * Minimal configuration for TLS NSA Suite B Profile (RFC 6460) 26 | * 27 | * Distinguishing features: 28 | * - no RSA or classic DH, fully based on ECC 29 | * - optimized for low RAM usage 30 | * 31 | * Possible improvements: 32 | * - if 128-bit security is enough, disable secp384r1 and SHA-512 33 | * - use embedded certs in DER format and disable PEM_PARSE_C and BASE64_C 34 | * 35 | * See README.txt for usage instructions. 36 | */ 37 | 38 | #ifndef MBEDTLS_CONFIG_H 39 | #define MBEDTLS_CONFIG_H 40 | 41 | /* System support */ 42 | #define MBEDTLS_HAVE_ASM 43 | #define MBEDTLS_HAVE_TIME 44 | 45 | /* mbed TLS feature support */ 46 | #define MBEDTLS_ECP_DP_SECP256R1_ENABLED 47 | #define MBEDTLS_ECP_DP_SECP384R1_ENABLED 48 | #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED 49 | #define MBEDTLS_SSL_PROTO_TLS1_2 50 | 51 | /* mbed TLS modules */ 52 | #define MBEDTLS_AES_C 53 | #define MBEDTLS_ASN1_PARSE_C 54 | #define MBEDTLS_ASN1_WRITE_C 55 | #define MBEDTLS_BIGNUM_C 56 | #define MBEDTLS_CIPHER_C 57 | #define MBEDTLS_CTR_DRBG_C 58 | #define MBEDTLS_ECDH_C 59 | #define MBEDTLS_ECDSA_C 60 | #define MBEDTLS_ECP_C 61 | #define MBEDTLS_ENTROPY_C 62 | #define MBEDTLS_GCM_C 63 | #define MBEDTLS_MD_C 64 | #define MBEDTLS_NET_C 65 | #define MBEDTLS_OID_C 66 | #define MBEDTLS_PK_C 67 | #define MBEDTLS_PK_PARSE_C 68 | #define MBEDTLS_SHA256_C 69 | #define MBEDTLS_SHA512_C 70 | #define MBEDTLS_SSL_CLI_C 71 | #define MBEDTLS_SSL_SRV_C 72 | #define MBEDTLS_SSL_TLS_C 73 | #define MBEDTLS_X509_CRT_PARSE_C 74 | #define MBEDTLS_X509_USE_C 75 | 76 | /* For test certificates */ 77 | #define MBEDTLS_BASE64_C 78 | #define MBEDTLS_CERTS_C 79 | #define MBEDTLS_PEM_PARSE_C 80 | 81 | /* Save RAM at the expense of ROM */ 82 | #define MBEDTLS_AES_ROM_TABLES 83 | 84 | /* Save RAM by adjusting to our exact needs */ 85 | #define MBEDTLS_ECP_MAX_BITS 384 86 | #define MBEDTLS_MPI_MAX_SIZE 48 // 384 bits is 48 bytes 87 | 88 | /* Save RAM at the expense of speed, see ecp.h */ 89 | #define MBEDTLS_ECP_WINDOW_SIZE 2 90 | #define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 91 | 92 | /* Significant speed benefit at the expense of some ROM */ 93 | #define MBEDTLS_ECP_NIST_OPTIM 94 | 95 | /* 96 | * You should adjust this to the exact number of sources you're using: default 97 | * is the "mbedtls_platform_entropy_poll" source, but you may want to add other ones. 98 | * Minimum is 2 for the entropy test suite. 99 | */ 100 | #define MBEDTLS_ENTROPY_MAX_SOURCES 2 101 | 102 | /* Save ROM and a few bytes of RAM by specifying our own ciphersuite list */ 103 | #define MBEDTLS_SSL_CIPHERSUITES \ 104 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, \ 105 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 106 | 107 | /* 108 | * Save RAM at the expense of interoperability: do this only if you control 109 | * both ends of the connection! (See coments in "mbedtls/ssl.h".) 110 | * The minimum size here depends on the certificate chain used as well as the 111 | * typical size of records. 112 | */ 113 | #define MBEDTLS_SSL_MAX_CONTENT_LEN 1024 114 | 115 | #include "mbedtls/check_config.h" 116 | 117 | #endif /* MBEDTLS_CONFIG_H */ 118 | -------------------------------------------------------------------------------- /network/mbedtls/configs/config-thread.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config-thread.h 3 | * 4 | * \brief Minimal configuration for using TLS as part of Thread 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | 25 | /* 26 | * Minimal configuration for using TLS a part of Thread 27 | * http://threadgroup.org/ 28 | * 29 | * Distinguishing features: 30 | * - no RSA or classic DH, fully based on ECC 31 | * - no X.509 32 | * - support for experimental EC J-PAKE key exchange 33 | * 34 | * See README.txt for usage instructions. 35 | */ 36 | 37 | #ifndef MBEDTLS_CONFIG_H 38 | #define MBEDTLS_CONFIG_H 39 | 40 | /* System support */ 41 | #define MBEDTLS_HAVE_ASM 42 | 43 | /* mbed TLS feature support */ 44 | #define MBEDTLS_AES_ROM_TABLES 45 | #define MBEDTLS_ECP_DP_SECP256R1_ENABLED 46 | #define MBEDTLS_ECP_NIST_OPTIM 47 | #define MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED 48 | #define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH 49 | #define MBEDTLS_SSL_PROTO_TLS1_2 50 | #define MBEDTLS_SSL_PROTO_DTLS 51 | #define MBEDTLS_SSL_DTLS_ANTI_REPLAY 52 | #define MBEDTLS_SSL_DTLS_HELLO_VERIFY 53 | #define MBEDTLS_SSL_EXPORT_KEYS 54 | 55 | /* mbed TLS modules */ 56 | #define MBEDTLS_AES_C 57 | #define MBEDTLS_ASN1_PARSE_C 58 | #define MBEDTLS_ASN1_WRITE_C 59 | #define MBEDTLS_BIGNUM_C 60 | #define MBEDTLS_CCM_C 61 | #define MBEDTLS_CIPHER_C 62 | #define MBEDTLS_CTR_DRBG_C 63 | #define MBEDTLS_CMAC_C 64 | #define MBEDTLS_ECJPAKE_C 65 | #define MBEDTLS_ECP_C 66 | #define MBEDTLS_ENTROPY_C 67 | #define MBEDTLS_HMAC_DRBG_C 68 | #define MBEDTLS_MD_C 69 | #define MBEDTLS_OID_C 70 | #define MBEDTLS_PK_C 71 | #define MBEDTLS_PK_PARSE_C 72 | #define MBEDTLS_SHA256_C 73 | #define MBEDTLS_SSL_COOKIE_C 74 | #define MBEDTLS_SSL_CLI_C 75 | #define MBEDTLS_SSL_SRV_C 76 | #define MBEDTLS_SSL_TLS_C 77 | 78 | /* For tests using ssl-opt.sh */ 79 | #define MBEDTLS_NET_C 80 | #define MBEDTLS_TIMING_C 81 | 82 | /* Save RAM at the expense of ROM */ 83 | #define MBEDTLS_AES_ROM_TABLES 84 | 85 | /* Save RAM by adjusting to our exact needs */ 86 | #define MBEDTLS_ECP_MAX_BITS 256 87 | #define MBEDTLS_MPI_MAX_SIZE 32 // 256 bits is 32 bytes 88 | 89 | /* Save ROM and a few bytes of RAM by specifying our own ciphersuite list */ 90 | #define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8 91 | 92 | #include "mbedtls/check_config.h" 93 | 94 | #endif /* MBEDTLS_CONFIG_H */ 95 | -------------------------------------------------------------------------------- /network/mbedtls/include/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | option(INSTALL_MBEDTLS_HEADERS "Install mbed TLS headers." ON) 2 | 3 | if(INSTALL_MBEDTLS_HEADERS) 4 | 5 | file(GLOB headers "mbedtls/*.h") 6 | 7 | install(FILES ${headers} 8 | DESTINATION include/mbedtls 9 | PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) 10 | 11 | endif(INSTALL_MBEDTLS_HEADERS) 12 | 13 | # Make config.h available in an out-of-source build. ssl-opt.sh requires it. 14 | if (ENABLE_TESTING AND NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) 15 | link_to_source(mbedtls) 16 | endif() 17 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/aesni.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file aesni.h 3 | * 4 | * \brief AES-NI for hardware AES acceleration on some Intel processors 5 | * 6 | * \warning These functions are only for internal use by other library 7 | * functions; you must not call them directly. 8 | */ 9 | /* 10 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 11 | * SPDX-License-Identifier: Apache-2.0 12 | * 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 14 | * not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at 16 | * 17 | * http://www.apache.org/licenses/LICENSE-2.0 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | * This file is part of mbed TLS (https://tls.mbed.org) 26 | */ 27 | #ifndef MBEDTLS_AESNI_H 28 | #define MBEDTLS_AESNI_H 29 | 30 | #if !defined(MBEDTLS_CONFIG_FILE) 31 | #include "config.h" 32 | #else 33 | #include MBEDTLS_CONFIG_FILE 34 | #endif 35 | 36 | #include "aes.h" 37 | 38 | #define MBEDTLS_AESNI_AES 0x02000000u 39 | #define MBEDTLS_AESNI_CLMUL 0x00000002u 40 | 41 | #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \ 42 | ( defined(__amd64__) || defined(__x86_64__) ) && \ 43 | ! defined(MBEDTLS_HAVE_X86_64) 44 | #define MBEDTLS_HAVE_X86_64 45 | #endif 46 | 47 | #if defined(MBEDTLS_HAVE_X86_64) 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | /** 54 | * \brief Internal function to detect the AES-NI feature in CPUs. 55 | * 56 | * \note This function is only for internal use by other library 57 | * functions; you must not call it directly. 58 | * 59 | * \param what The feature to detect 60 | * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL) 61 | * 62 | * \return 1 if CPU has support for the feature, 0 otherwise 63 | */ 64 | int mbedtls_aesni_has_support( unsigned int what ); 65 | 66 | /** 67 | * \brief Internal AES-NI AES-ECB block encryption and decryption 68 | * 69 | * \note This function is only for internal use by other library 70 | * functions; you must not call it directly. 71 | * 72 | * \param ctx AES context 73 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT 74 | * \param input 16-byte input block 75 | * \param output 16-byte output block 76 | * 77 | * \return 0 on success (cannot fail) 78 | */ 79 | int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx, 80 | int mode, 81 | const unsigned char input[16], 82 | unsigned char output[16] ); 83 | 84 | /** 85 | * \brief Internal GCM multiplication: c = a * b in GF(2^128) 86 | * 87 | * \note This function is only for internal use by other library 88 | * functions; you must not call it directly. 89 | * 90 | * \param c Result 91 | * \param a First operand 92 | * \param b Second operand 93 | * 94 | * \note Both operands and result are bit strings interpreted as 95 | * elements of GF(2^128) as per the GCM spec. 96 | */ 97 | void mbedtls_aesni_gcm_mult( unsigned char c[16], 98 | const unsigned char a[16], 99 | const unsigned char b[16] ); 100 | 101 | /** 102 | * \brief Internal round key inversion. This function computes 103 | * decryption round keys from the encryption round keys. 104 | * 105 | * \note This function is only for internal use by other library 106 | * functions; you must not call it directly. 107 | * 108 | * \param invkey Round keys for the equivalent inverse cipher 109 | * \param fwdkey Original round keys (for encryption) 110 | * \param nr Number of rounds (that is, number of round keys minus one) 111 | */ 112 | void mbedtls_aesni_inverse_key( unsigned char *invkey, 113 | const unsigned char *fwdkey, 114 | int nr ); 115 | 116 | /** 117 | * \brief Internal key expansion for encryption 118 | * 119 | * \note This function is only for internal use by other library 120 | * functions; you must not call it directly. 121 | * 122 | * \param rk Destination buffer where the round keys are written 123 | * \param key Encryption key 124 | * \param bits Key size in bits (must be 128, 192 or 256) 125 | * 126 | * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH 127 | */ 128 | int mbedtls_aesni_setkey_enc( unsigned char *rk, 129 | const unsigned char *key, 130 | size_t bits ); 131 | 132 | #ifdef __cplusplus 133 | } 134 | #endif 135 | 136 | #endif /* MBEDTLS_HAVE_X86_64 */ 137 | 138 | #endif /* MBEDTLS_AESNI_H */ 139 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/arc4.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file arc4.h 3 | * 4 | * \brief The ARCFOUR stream cipher 5 | * 6 | * \warning ARC4 is considered a weak cipher and its use constitutes a 7 | * security risk. We recommend considering stronger ciphers instead. 8 | */ 9 | /* 10 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 11 | * SPDX-License-Identifier: Apache-2.0 12 | * 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 14 | * not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at 16 | * 17 | * http://www.apache.org/licenses/LICENSE-2.0 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | * This file is part of mbed TLS (https://tls.mbed.org) 26 | * 27 | */ 28 | #ifndef MBEDTLS_ARC4_H 29 | #define MBEDTLS_ARC4_H 30 | 31 | #if !defined(MBEDTLS_CONFIG_FILE) 32 | #include "config.h" 33 | #else 34 | #include MBEDTLS_CONFIG_FILE 35 | #endif 36 | 37 | #include 38 | 39 | /* MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED is deprecated and should not be used. */ 40 | #define MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED -0x0019 /**< ARC4 hardware accelerator failed. */ 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | #if !defined(MBEDTLS_ARC4_ALT) 47 | // Regular implementation 48 | // 49 | 50 | /** 51 | * \brief ARC4 context structure 52 | * 53 | * \warning ARC4 is considered a weak cipher and its use constitutes a 54 | * security risk. We recommend considering stronger ciphers instead. 55 | * 56 | */ 57 | typedef struct mbedtls_arc4_context 58 | { 59 | int x; /*!< permutation index */ 60 | int y; /*!< permutation index */ 61 | unsigned char m[256]; /*!< permutation table */ 62 | } 63 | mbedtls_arc4_context; 64 | 65 | #else /* MBEDTLS_ARC4_ALT */ 66 | #include "arc4_alt.h" 67 | #endif /* MBEDTLS_ARC4_ALT */ 68 | 69 | /** 70 | * \brief Initialize ARC4 context 71 | * 72 | * \param ctx ARC4 context to be initialized 73 | * 74 | * \warning ARC4 is considered a weak cipher and its use constitutes a 75 | * security risk. We recommend considering stronger ciphers 76 | * instead. 77 | * 78 | */ 79 | void mbedtls_arc4_init( mbedtls_arc4_context *ctx ); 80 | 81 | /** 82 | * \brief Clear ARC4 context 83 | * 84 | * \param ctx ARC4 context to be cleared 85 | * 86 | * \warning ARC4 is considered a weak cipher and its use constitutes a 87 | * security risk. We recommend considering stronger ciphers 88 | * instead. 89 | * 90 | */ 91 | void mbedtls_arc4_free( mbedtls_arc4_context *ctx ); 92 | 93 | /** 94 | * \brief ARC4 key schedule 95 | * 96 | * \param ctx ARC4 context to be setup 97 | * \param key the secret key 98 | * \param keylen length of the key, in bytes 99 | * 100 | * \warning ARC4 is considered a weak cipher and its use constitutes a 101 | * security risk. We recommend considering stronger ciphers 102 | * instead. 103 | * 104 | */ 105 | void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key, 106 | unsigned int keylen ); 107 | 108 | /** 109 | * \brief ARC4 cipher function 110 | * 111 | * \param ctx ARC4 context 112 | * \param length length of the input data 113 | * \param input buffer holding the input data 114 | * \param output buffer for the output data 115 | * 116 | * \return 0 if successful 117 | * 118 | * \warning ARC4 is considered a weak cipher and its use constitutes a 119 | * security risk. We recommend considering stronger ciphers 120 | * instead. 121 | * 122 | */ 123 | int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input, 124 | unsigned char *output ); 125 | 126 | #if defined(MBEDTLS_SELF_TEST) 127 | 128 | /** 129 | * \brief Checkup routine 130 | * 131 | * \return 0 if successful, or 1 if the test failed 132 | * 133 | * \warning ARC4 is considered a weak cipher and its use constitutes a 134 | * security risk. We recommend considering stronger ciphers 135 | * instead. 136 | * 137 | */ 138 | int mbedtls_arc4_self_test( int verbose ); 139 | 140 | #endif /* MBEDTLS_SELF_TEST */ 141 | 142 | #ifdef __cplusplus 143 | } 144 | #endif 145 | 146 | #endif /* arc4.h */ 147 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/base64.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file base64.h 3 | * 4 | * \brief RFC 1521 base64 encoding/decoding 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_BASE64_H 25 | #define MBEDTLS_BASE64_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | 35 | #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */ 36 | #define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */ 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * \brief Encode a buffer into base64 format 44 | * 45 | * \param dst destination buffer 46 | * \param dlen size of the destination buffer 47 | * \param olen number of bytes written 48 | * \param src source buffer 49 | * \param slen amount of data to be encoded 50 | * 51 | * \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL. 52 | * *olen is always updated to reflect the amount 53 | * of data that has (or would have) been written. 54 | * If that length cannot be represented, then no data is 55 | * written to the buffer and *olen is set to the maximum 56 | * length representable as a size_t. 57 | * 58 | * \note Call this function with dlen = 0 to obtain the 59 | * required buffer size in *olen 60 | */ 61 | int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen, 62 | const unsigned char *src, size_t slen ); 63 | 64 | /** 65 | * \brief Decode a base64-formatted buffer 66 | * 67 | * \param dst destination buffer (can be NULL for checking size) 68 | * \param dlen size of the destination buffer 69 | * \param olen number of bytes written 70 | * \param src source buffer 71 | * \param slen amount of data to be decoded 72 | * 73 | * \return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or 74 | * MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is 75 | * not correct. *olen is always updated to reflect the amount 76 | * of data that has (or would have) been written. 77 | * 78 | * \note Call this function with *dst = NULL or dlen = 0 to obtain 79 | * the required buffer size in *olen 80 | */ 81 | int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, 82 | const unsigned char *src, size_t slen ); 83 | 84 | #if defined(MBEDTLS_SELF_TEST) 85 | /** 86 | * \brief Checkup routine 87 | * 88 | * \return 0 if successful, or 1 if the test failed 89 | */ 90 | int mbedtls_base64_self_test( int verbose ); 91 | 92 | #endif /* MBEDTLS_SELF_TEST */ 93 | 94 | #ifdef __cplusplus 95 | } 96 | #endif 97 | 98 | #endif /* base64.h */ 99 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/cipher_internal.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file cipher_internal.h 3 | * 4 | * \brief Cipher wrappers. 5 | * 6 | * \author Adriaan de Jong 7 | */ 8 | /* 9 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * http://www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | * 24 | * This file is part of mbed TLS (https://tls.mbed.org) 25 | */ 26 | #ifndef MBEDTLS_CIPHER_WRAP_H 27 | #define MBEDTLS_CIPHER_WRAP_H 28 | 29 | #if !defined(MBEDTLS_CONFIG_FILE) 30 | #include "config.h" 31 | #else 32 | #include MBEDTLS_CONFIG_FILE 33 | #endif 34 | 35 | #include "cipher.h" 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /** 42 | * Base cipher information. The non-mode specific functions and values. 43 | */ 44 | struct mbedtls_cipher_base_t 45 | { 46 | /** Base Cipher type (e.g. MBEDTLS_CIPHER_ID_AES) */ 47 | mbedtls_cipher_id_t cipher; 48 | 49 | /** Encrypt using ECB */ 50 | int (*ecb_func)( void *ctx, mbedtls_operation_t mode, 51 | const unsigned char *input, unsigned char *output ); 52 | 53 | #if defined(MBEDTLS_CIPHER_MODE_CBC) 54 | /** Encrypt using CBC */ 55 | int (*cbc_func)( void *ctx, mbedtls_operation_t mode, size_t length, 56 | unsigned char *iv, const unsigned char *input, 57 | unsigned char *output ); 58 | #endif 59 | 60 | #if defined(MBEDTLS_CIPHER_MODE_CFB) 61 | /** Encrypt using CFB (Full length) */ 62 | int (*cfb_func)( void *ctx, mbedtls_operation_t mode, size_t length, size_t *iv_off, 63 | unsigned char *iv, const unsigned char *input, 64 | unsigned char *output ); 65 | #endif 66 | 67 | #if defined(MBEDTLS_CIPHER_MODE_OFB) 68 | /** Encrypt using OFB (Full length) */ 69 | int (*ofb_func)( void *ctx, size_t length, size_t *iv_off, 70 | unsigned char *iv, 71 | const unsigned char *input, 72 | unsigned char *output ); 73 | #endif 74 | 75 | #if defined(MBEDTLS_CIPHER_MODE_CTR) 76 | /** Encrypt using CTR */ 77 | int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, 78 | unsigned char *nonce_counter, unsigned char *stream_block, 79 | const unsigned char *input, unsigned char *output ); 80 | #endif 81 | 82 | #if defined(MBEDTLS_CIPHER_MODE_XTS) 83 | /** Encrypt or decrypt using XTS. */ 84 | int (*xts_func)( void *ctx, mbedtls_operation_t mode, size_t length, 85 | const unsigned char data_unit[16], 86 | const unsigned char *input, unsigned char *output ); 87 | #endif 88 | 89 | #if defined(MBEDTLS_CIPHER_MODE_STREAM) 90 | /** Encrypt using STREAM */ 91 | int (*stream_func)( void *ctx, size_t length, 92 | const unsigned char *input, unsigned char *output ); 93 | #endif 94 | 95 | /** Set key for encryption purposes */ 96 | int (*setkey_enc_func)( void *ctx, const unsigned char *key, 97 | unsigned int key_bitlen ); 98 | 99 | /** Set key for decryption purposes */ 100 | int (*setkey_dec_func)( void *ctx, const unsigned char *key, 101 | unsigned int key_bitlen); 102 | 103 | /** Allocate a new context */ 104 | void * (*ctx_alloc_func)( void ); 105 | 106 | /** Free the given context */ 107 | void (*ctx_free_func)( void *ctx ); 108 | 109 | }; 110 | 111 | typedef struct 112 | { 113 | mbedtls_cipher_type_t type; 114 | const mbedtls_cipher_info_t *info; 115 | } mbedtls_cipher_definition_t; 116 | 117 | extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[]; 118 | 119 | extern int mbedtls_cipher_supported[]; 120 | 121 | #ifdef __cplusplus 122 | } 123 | #endif 124 | 125 | #endif /* MBEDTLS_CIPHER_WRAP_H */ 126 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/entropy_poll.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file entropy_poll.h 3 | * 4 | * \brief Platform-specific and custom entropy polling functions 5 | */ 6 | /* 7 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_ENTROPY_POLL_H 25 | #define MBEDTLS_ENTROPY_POLL_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | /* 40 | * Default thresholds for built-in sources, in bytes 41 | */ 42 | #define MBEDTLS_ENTROPY_MIN_PLATFORM 32 /**< Minimum for platform source */ 43 | #define MBEDTLS_ENTROPY_MIN_HAVEGE 32 /**< Minimum for HAVEGE */ 44 | #define MBEDTLS_ENTROPY_MIN_HARDCLOCK 4 /**< Minimum for mbedtls_timing_hardclock() */ 45 | #if !defined(MBEDTLS_ENTROPY_MIN_HARDWARE) 46 | #define MBEDTLS_ENTROPY_MIN_HARDWARE 32 /**< Minimum for the hardware source */ 47 | #endif 48 | 49 | /** 50 | * \brief Entropy poll callback that provides 0 entropy. 51 | */ 52 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) 53 | int mbedtls_null_entropy_poll( void *data, 54 | unsigned char *output, size_t len, size_t *olen ); 55 | #endif 56 | 57 | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) 58 | /** 59 | * \brief Platform-specific entropy poll callback 60 | */ 61 | int mbedtls_platform_entropy_poll( void *data, 62 | unsigned char *output, size_t len, size_t *olen ); 63 | #endif 64 | 65 | #if defined(MBEDTLS_HAVEGE_C) 66 | /** 67 | * \brief HAVEGE based entropy poll callback 68 | * 69 | * Requires an HAVEGE state as its data pointer. 70 | */ 71 | int mbedtls_havege_poll( void *data, 72 | unsigned char *output, size_t len, size_t *olen ); 73 | #endif 74 | 75 | #if defined(MBEDTLS_TIMING_C) 76 | /** 77 | * \brief mbedtls_timing_hardclock-based entropy poll callback 78 | */ 79 | int mbedtls_hardclock_poll( void *data, 80 | unsigned char *output, size_t len, size_t *olen ); 81 | #endif 82 | 83 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) 84 | /** 85 | * \brief Entropy poll callback for a hardware source 86 | * 87 | * \warning This is not provided by mbed TLS! 88 | * See \c MBEDTLS_ENTROPY_HARDWARE_ALT in config.h. 89 | * 90 | * \note This must accept NULL as its first argument. 91 | */ 92 | int mbedtls_hardware_poll( void *data, 93 | unsigned char *output, size_t len, size_t *olen ); 94 | #endif 95 | 96 | #if defined(MBEDTLS_ENTROPY_NV_SEED) 97 | /** 98 | * \brief Entropy poll callback for a non-volatile seed file 99 | * 100 | * \note This must accept NULL as its first argument. 101 | */ 102 | int mbedtls_nv_seed_poll( void *data, 103 | unsigned char *output, size_t len, size_t *olen ); 104 | #endif 105 | 106 | #ifdef __cplusplus 107 | } 108 | #endif 109 | 110 | #endif /* entropy_poll.h */ 111 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/error.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file error.h 3 | * 4 | * \brief Error to string translation 5 | */ 6 | /* 7 | * Copyright (C) 2006-2018, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_ERROR_H 25 | #define MBEDTLS_ERROR_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | 35 | /** 36 | * Error code layout. 37 | * 38 | * Currently we try to keep all error codes within the negative space of 16 39 | * bits signed integers to support all platforms (-0x0001 - -0x7FFF). In 40 | * addition we'd like to give two layers of information on the error if 41 | * possible. 42 | * 43 | * For that purpose the error codes are segmented in the following manner: 44 | * 45 | * 16 bit error code bit-segmentation 46 | * 47 | * 1 bit - Unused (sign bit) 48 | * 3 bits - High level module ID 49 | * 5 bits - Module-dependent error code 50 | * 7 bits - Low level module errors 51 | * 52 | * For historical reasons, low-level error codes are divided in even and odd, 53 | * even codes were assigned first, and -1 is reserved for other errors. 54 | * 55 | * Low-level module errors (0x0002-0x007E, 0x0003-0x007F) 56 | * 57 | * Module Nr Codes assigned 58 | * MPI 7 0x0002-0x0010 59 | * GCM 3 0x0012-0x0014 0x0013-0x0013 60 | * BLOWFISH 3 0x0016-0x0018 0x0017-0x0017 61 | * THREADING 3 0x001A-0x001E 62 | * AES 5 0x0020-0x0022 0x0021-0x0025 63 | * CAMELLIA 3 0x0024-0x0026 0x0027-0x0027 64 | * XTEA 2 0x0028-0x0028 0x0029-0x0029 65 | * BASE64 2 0x002A-0x002C 66 | * OID 1 0x002E-0x002E 0x000B-0x000B 67 | * PADLOCK 1 0x0030-0x0030 68 | * DES 2 0x0032-0x0032 0x0033-0x0033 69 | * CTR_DBRG 4 0x0034-0x003A 70 | * ENTROPY 3 0x003C-0x0040 0x003D-0x003F 71 | * NET 13 0x0042-0x0052 0x0043-0x0049 72 | * ARIA 4 0x0058-0x005E 73 | * ASN1 7 0x0060-0x006C 74 | * CMAC 1 0x007A-0x007A 75 | * PBKDF2 1 0x007C-0x007C 76 | * HMAC_DRBG 4 0x0003-0x0009 77 | * CCM 3 0x000D-0x0011 78 | * ARC4 1 0x0019-0x0019 79 | * MD2 1 0x002B-0x002B 80 | * MD4 1 0x002D-0x002D 81 | * MD5 1 0x002F-0x002F 82 | * RIPEMD160 1 0x0031-0x0031 83 | * SHA1 1 0x0035-0x0035 0x0073-0x0073 84 | * SHA256 1 0x0037-0x0037 0x0074-0x0074 85 | * SHA512 1 0x0039-0x0039 0x0075-0x0075 86 | * CHACHA20 3 0x0051-0x0055 87 | * POLY1305 3 0x0057-0x005B 88 | * CHACHAPOLY 2 0x0054-0x0056 89 | * PLATFORM 1 0x0070-0x0072 90 | * 91 | * High-level module nr (3 bits - 0x0...-0x7...) 92 | * Name ID Nr of Errors 93 | * PEM 1 9 94 | * PKCS#12 1 4 (Started from top) 95 | * X509 2 20 96 | * PKCS5 2 4 (Started from top) 97 | * DHM 3 11 98 | * PK 3 15 (Started from top) 99 | * RSA 4 11 100 | * ECP 4 10 (Started from top) 101 | * MD 5 5 102 | * HKDF 5 1 (Started from top) 103 | * CIPHER 6 8 104 | * SSL 6 23 (Started from top) 105 | * SSL 7 32 106 | * 107 | * Module dependent error code (5 bits 0x.00.-0x.F8.) 108 | */ 109 | 110 | #ifdef __cplusplus 111 | extern "C" { 112 | #endif 113 | 114 | /** 115 | * \brief Translate a mbed TLS error code into a string representation, 116 | * Result is truncated if necessary and always includes a terminating 117 | * null byte. 118 | * 119 | * \param errnum error code 120 | * \param buffer buffer to place representation in 121 | * \param buflen length of the buffer 122 | */ 123 | void mbedtls_strerror( int errnum, char *buffer, size_t buflen ); 124 | 125 | #ifdef __cplusplus 126 | } 127 | #endif 128 | 129 | #endif /* error.h */ 130 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/havege.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file havege.h 3 | * 4 | * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_HAVEGE_H 25 | #define MBEDTLS_HAVEGE_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | 35 | #define MBEDTLS_HAVEGE_COLLECT_SIZE 1024 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /** 42 | * \brief HAVEGE state structure 43 | */ 44 | typedef struct mbedtls_havege_state 45 | { 46 | int PT1, PT2, offset[2]; 47 | int pool[MBEDTLS_HAVEGE_COLLECT_SIZE]; 48 | int WALK[8192]; 49 | } 50 | mbedtls_havege_state; 51 | 52 | /** 53 | * \brief HAVEGE initialization 54 | * 55 | * \param hs HAVEGE state to be initialized 56 | */ 57 | void mbedtls_havege_init( mbedtls_havege_state *hs ); 58 | 59 | /** 60 | * \brief Clear HAVEGE state 61 | * 62 | * \param hs HAVEGE state to be cleared 63 | */ 64 | void mbedtls_havege_free( mbedtls_havege_state *hs ); 65 | 66 | /** 67 | * \brief HAVEGE rand function 68 | * 69 | * \param p_rng A HAVEGE state 70 | * \param output Buffer to fill 71 | * \param len Length of buffer 72 | * 73 | * \return 0 74 | */ 75 | int mbedtls_havege_random( void *p_rng, unsigned char *output, size_t len ); 76 | 77 | #ifdef __cplusplus 78 | } 79 | #endif 80 | 81 | #endif /* havege.h */ 82 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/md_internal.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file md_internal.h 3 | * 4 | * \brief Message digest wrappers. 5 | * 6 | * \warning This in an internal header. Do not include directly. 7 | * 8 | * \author Adriaan de Jong 9 | */ 10 | /* 11 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 12 | * SPDX-License-Identifier: Apache-2.0 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 15 | * not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at 17 | * 18 | * http://www.apache.org/licenses/LICENSE-2.0 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | * This file is part of mbed TLS (https://tls.mbed.org) 27 | */ 28 | #ifndef MBEDTLS_MD_WRAP_H 29 | #define MBEDTLS_MD_WRAP_H 30 | 31 | #if !defined(MBEDTLS_CONFIG_FILE) 32 | #include "config.h" 33 | #else 34 | #include MBEDTLS_CONFIG_FILE 35 | #endif 36 | 37 | #include "md.h" 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /** 44 | * Message digest information. 45 | * Allows message digest functions to be called in a generic way. 46 | */ 47 | struct mbedtls_md_info_t 48 | { 49 | /** Digest identifier */ 50 | mbedtls_md_type_t type; 51 | 52 | /** Name of the message digest */ 53 | const char * name; 54 | 55 | /** Output length of the digest function in bytes */ 56 | int size; 57 | 58 | /** Block length of the digest function in bytes */ 59 | int block_size; 60 | 61 | /** Digest initialisation function */ 62 | int (*starts_func)( void *ctx ); 63 | 64 | /** Digest update function */ 65 | int (*update_func)( void *ctx, const unsigned char *input, size_t ilen ); 66 | 67 | /** Digest finalisation function */ 68 | int (*finish_func)( void *ctx, unsigned char *output ); 69 | 70 | /** Generic digest function */ 71 | int (*digest_func)( const unsigned char *input, size_t ilen, 72 | unsigned char *output ); 73 | 74 | /** Allocate a new context */ 75 | void * (*ctx_alloc_func)( void ); 76 | 77 | /** Free the given context */ 78 | void (*ctx_free_func)( void *ctx ); 79 | 80 | /** Clone state from a context */ 81 | void (*clone_func)( void *dst, const void *src ); 82 | 83 | /** Internal use only */ 84 | int (*process_func)( void *ctx, const unsigned char *input ); 85 | }; 86 | 87 | #if defined(MBEDTLS_MD2_C) 88 | extern const mbedtls_md_info_t mbedtls_md2_info; 89 | #endif 90 | #if defined(MBEDTLS_MD4_C) 91 | extern const mbedtls_md_info_t mbedtls_md4_info; 92 | #endif 93 | #if defined(MBEDTLS_MD5_C) 94 | extern const mbedtls_md_info_t mbedtls_md5_info; 95 | #endif 96 | #if defined(MBEDTLS_RIPEMD160_C) 97 | extern const mbedtls_md_info_t mbedtls_ripemd160_info; 98 | #endif 99 | #if defined(MBEDTLS_SHA1_C) 100 | extern const mbedtls_md_info_t mbedtls_sha1_info; 101 | #endif 102 | #if defined(MBEDTLS_SHA256_C) 103 | extern const mbedtls_md_info_t mbedtls_sha224_info; 104 | extern const mbedtls_md_info_t mbedtls_sha256_info; 105 | #endif 106 | #if defined(MBEDTLS_SHA512_C) 107 | extern const mbedtls_md_info_t mbedtls_sha384_info; 108 | extern const mbedtls_md_info_t mbedtls_sha512_info; 109 | #endif 110 | 111 | #ifdef __cplusplus 112 | } 113 | #endif 114 | 115 | #endif /* MBEDTLS_MD_WRAP_H */ 116 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/net.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file net.h 3 | * 4 | * \brief Deprecated header file that includes net_sockets.h 5 | * 6 | * \deprecated Superseded by mbedtls/net_sockets.h 7 | */ 8 | /* 9 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * http://www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | * 24 | * This file is part of mbed TLS (https://tls.mbed.org) 25 | */ 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) 33 | #include "net_sockets.h" 34 | #if defined(MBEDTLS_DEPRECATED_WARNING) 35 | #warning "Deprecated header file: Superseded by mbedtls/net_sockets.h" 36 | #endif /* MBEDTLS_DEPRECATED_WARNING */ 37 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ 38 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/padlock.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file padlock.h 3 | * 4 | * \brief VIA PadLock ACE for HW encryption/decryption supported by some 5 | * processors 6 | * 7 | * \warning These functions are only for internal use by other library 8 | * functions; you must not call them directly. 9 | */ 10 | /* 11 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 12 | * SPDX-License-Identifier: Apache-2.0 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 15 | * not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at 17 | * 18 | * http://www.apache.org/licenses/LICENSE-2.0 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | * This file is part of mbed TLS (https://tls.mbed.org) 27 | */ 28 | #ifndef MBEDTLS_PADLOCK_H 29 | #define MBEDTLS_PADLOCK_H 30 | 31 | #if !defined(MBEDTLS_CONFIG_FILE) 32 | #include "config.h" 33 | #else 34 | #include MBEDTLS_CONFIG_FILE 35 | #endif 36 | 37 | #include "aes.h" 38 | 39 | #define MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED -0x0030 /**< Input data should be aligned. */ 40 | 41 | #if defined(__has_feature) 42 | #if __has_feature(address_sanitizer) 43 | #define MBEDTLS_HAVE_ASAN 44 | #endif 45 | #endif 46 | 47 | /* Some versions of ASan result in errors about not enough registers */ 48 | #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) && \ 49 | !defined(MBEDTLS_HAVE_ASAN) 50 | 51 | #ifndef MBEDTLS_HAVE_X86 52 | #define MBEDTLS_HAVE_X86 53 | #endif 54 | 55 | #include 56 | 57 | #define MBEDTLS_PADLOCK_RNG 0x000C 58 | #define MBEDTLS_PADLOCK_ACE 0x00C0 59 | #define MBEDTLS_PADLOCK_PHE 0x0C00 60 | #define MBEDTLS_PADLOCK_PMM 0x3000 61 | 62 | #define MBEDTLS_PADLOCK_ALIGN16(x) (uint32_t *) (16 + ((int32_t) (x) & ~15)) 63 | 64 | #ifdef __cplusplus 65 | extern "C" { 66 | #endif 67 | 68 | /** 69 | * \brief Internal PadLock detection routine 70 | * 71 | * \note This function is only for internal use by other library 72 | * functions; you must not call it directly. 73 | * 74 | * \param feature The feature to detect 75 | * 76 | * \return 1 if CPU has support for the feature, 0 otherwise 77 | */ 78 | int mbedtls_padlock_has_support( int feature ); 79 | 80 | /** 81 | * \brief Internal PadLock AES-ECB block en(de)cryption 82 | * 83 | * \note This function is only for internal use by other library 84 | * functions; you must not call it directly. 85 | * 86 | * \param ctx AES context 87 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT 88 | * \param input 16-byte input block 89 | * \param output 16-byte output block 90 | * 91 | * \return 0 if success, 1 if operation failed 92 | */ 93 | int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx, 94 | int mode, 95 | const unsigned char input[16], 96 | unsigned char output[16] ); 97 | 98 | /** 99 | * \brief Internal PadLock AES-CBC buffer en(de)cryption 100 | * 101 | * \note This function is only for internal use by other library 102 | * functions; you must not call it directly. 103 | * 104 | * \param ctx AES context 105 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT 106 | * \param length length of the input data 107 | * \param iv initialization vector (updated after use) 108 | * \param input buffer holding the input data 109 | * \param output buffer holding the output data 110 | * 111 | * \return 0 if success, 1 if operation failed 112 | */ 113 | int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx, 114 | int mode, 115 | size_t length, 116 | unsigned char iv[16], 117 | const unsigned char *input, 118 | unsigned char *output ); 119 | 120 | #ifdef __cplusplus 121 | } 122 | #endif 123 | 124 | #endif /* HAVE_X86 */ 125 | 126 | #endif /* padlock.h */ 127 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/pk_internal.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file pk_internal.h 3 | * 4 | * \brief Public Key abstraction layer: wrapper functions 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | 25 | #ifndef MBEDTLS_PK_WRAP_H 26 | #define MBEDTLS_PK_WRAP_H 27 | 28 | #if !defined(MBEDTLS_CONFIG_FILE) 29 | #include "config.h" 30 | #else 31 | #include MBEDTLS_CONFIG_FILE 32 | #endif 33 | 34 | #include "pk.h" 35 | 36 | struct mbedtls_pk_info_t 37 | { 38 | /** Public key type */ 39 | mbedtls_pk_type_t type; 40 | 41 | /** Type name */ 42 | const char *name; 43 | 44 | /** Get key size in bits */ 45 | size_t (*get_bitlen)( const void * ); 46 | 47 | /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */ 48 | int (*can_do)( mbedtls_pk_type_t type ); 49 | 50 | /** Verify signature */ 51 | int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg, 52 | const unsigned char *hash, size_t hash_len, 53 | const unsigned char *sig, size_t sig_len ); 54 | 55 | /** Make signature */ 56 | int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg, 57 | const unsigned char *hash, size_t hash_len, 58 | unsigned char *sig, size_t *sig_len, 59 | int (*f_rng)(void *, unsigned char *, size_t), 60 | void *p_rng ); 61 | 62 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 63 | /** Verify signature (restartable) */ 64 | int (*verify_rs_func)( void *ctx, mbedtls_md_type_t md_alg, 65 | const unsigned char *hash, size_t hash_len, 66 | const unsigned char *sig, size_t sig_len, 67 | void *rs_ctx ); 68 | 69 | /** Make signature (restartable) */ 70 | int (*sign_rs_func)( void *ctx, mbedtls_md_type_t md_alg, 71 | const unsigned char *hash, size_t hash_len, 72 | unsigned char *sig, size_t *sig_len, 73 | int (*f_rng)(void *, unsigned char *, size_t), 74 | void *p_rng, void *rs_ctx ); 75 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ 76 | 77 | /** Decrypt message */ 78 | int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen, 79 | unsigned char *output, size_t *olen, size_t osize, 80 | int (*f_rng)(void *, unsigned char *, size_t), 81 | void *p_rng ); 82 | 83 | /** Encrypt message */ 84 | int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen, 85 | unsigned char *output, size_t *olen, size_t osize, 86 | int (*f_rng)(void *, unsigned char *, size_t), 87 | void *p_rng ); 88 | 89 | /** Check public-private key pair */ 90 | int (*check_pair_func)( const void *pub, const void *prv ); 91 | 92 | /** Allocate a new context */ 93 | void * (*ctx_alloc_func)( void ); 94 | 95 | /** Free the given context */ 96 | void (*ctx_free_func)( void *ctx ); 97 | 98 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 99 | /** Allocate the restart context */ 100 | void * (*rs_alloc_func)( void ); 101 | 102 | /** Free the restart context */ 103 | void (*rs_free_func)( void *rs_ctx ); 104 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ 105 | 106 | /** Interface with the debug module */ 107 | void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items ); 108 | 109 | }; 110 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) 111 | /* Container for RSA-alt */ 112 | typedef struct 113 | { 114 | void *key; 115 | mbedtls_pk_rsa_alt_decrypt_func decrypt_func; 116 | mbedtls_pk_rsa_alt_sign_func sign_func; 117 | mbedtls_pk_rsa_alt_key_len_func key_len_func; 118 | } mbedtls_rsa_alt_context; 119 | #endif 120 | 121 | #if defined(MBEDTLS_RSA_C) 122 | extern const mbedtls_pk_info_t mbedtls_rsa_info; 123 | #endif 124 | 125 | #if defined(MBEDTLS_ECP_C) 126 | extern const mbedtls_pk_info_t mbedtls_eckey_info; 127 | extern const mbedtls_pk_info_t mbedtls_eckeydh_info; 128 | #endif 129 | 130 | #if defined(MBEDTLS_ECDSA_C) 131 | extern const mbedtls_pk_info_t mbedtls_ecdsa_info; 132 | #endif 133 | 134 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) 135 | extern const mbedtls_pk_info_t mbedtls_rsa_alt_info; 136 | #endif 137 | 138 | #endif /* MBEDTLS_PK_WRAP_H */ 139 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/pkcs5.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file pkcs5.h 3 | * 4 | * \brief PKCS#5 functions 5 | * 6 | * \author Mathias Olsson 7 | */ 8 | /* 9 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * http://www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | * 24 | * This file is part of mbed TLS (https://tls.mbed.org) 25 | */ 26 | #ifndef MBEDTLS_PKCS5_H 27 | #define MBEDTLS_PKCS5_H 28 | 29 | #if !defined(MBEDTLS_CONFIG_FILE) 30 | #include "config.h" 31 | #else 32 | #include MBEDTLS_CONFIG_FILE 33 | #endif 34 | 35 | #include "asn1.h" 36 | #include "md.h" 37 | 38 | #include 39 | #include 40 | 41 | #define MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA -0x2f80 /**< Bad input parameters to function. */ 42 | #define MBEDTLS_ERR_PKCS5_INVALID_FORMAT -0x2f00 /**< Unexpected ASN.1 data. */ 43 | #define MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE -0x2e80 /**< Requested encryption or digest alg not available. */ 44 | #define MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH -0x2e00 /**< Given private key password does not allow for correct decryption. */ 45 | 46 | #define MBEDTLS_PKCS5_DECRYPT 0 47 | #define MBEDTLS_PKCS5_ENCRYPT 1 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | #if defined(MBEDTLS_ASN1_PARSE_C) 54 | 55 | /** 56 | * \brief PKCS#5 PBES2 function 57 | * 58 | * \param pbe_params the ASN.1 algorithm parameters 59 | * \param mode either MBEDTLS_PKCS5_DECRYPT or MBEDTLS_PKCS5_ENCRYPT 60 | * \param pwd password to use when generating key 61 | * \param pwdlen length of password 62 | * \param data data to process 63 | * \param datalen length of data 64 | * \param output output buffer 65 | * 66 | * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails. 67 | */ 68 | int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode, 69 | const unsigned char *pwd, size_t pwdlen, 70 | const unsigned char *data, size_t datalen, 71 | unsigned char *output ); 72 | 73 | #endif /* MBEDTLS_ASN1_PARSE_C */ 74 | 75 | /** 76 | * \brief PKCS#5 PBKDF2 using HMAC 77 | * 78 | * \param ctx Generic HMAC context 79 | * \param password Password to use when generating key 80 | * \param plen Length of password 81 | * \param salt Salt to use when generating key 82 | * \param slen Length of salt 83 | * \param iteration_count Iteration count 84 | * \param key_length Length of generated key in bytes 85 | * \param output Generated key. Must be at least as big as key_length 86 | * 87 | * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails. 88 | */ 89 | int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *password, 90 | size_t plen, const unsigned char *salt, size_t slen, 91 | unsigned int iteration_count, 92 | uint32_t key_length, unsigned char *output ); 93 | 94 | #if defined(MBEDTLS_SELF_TEST) 95 | 96 | /** 97 | * \brief Checkup routine 98 | * 99 | * \return 0 if successful, or 1 if the test failed 100 | */ 101 | int mbedtls_pkcs5_self_test( int verbose ); 102 | 103 | #endif /* MBEDTLS_SELF_TEST */ 104 | 105 | #ifdef __cplusplus 106 | } 107 | #endif 108 | 109 | #endif /* pkcs5.h */ 110 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/platform_time.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file platform_time.h 3 | * 4 | * \brief mbed TLS Platform time abstraction 5 | */ 6 | /* 7 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_PLATFORM_TIME_H 25 | #define MBEDTLS_PLATFORM_TIME_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /** 38 | * \name SECTION: Module settings 39 | * 40 | * The configuration options you can set for this module are in this section. 41 | * Either change them in config.h or define them on the compiler command line. 42 | * \{ 43 | */ 44 | 45 | /* 46 | * The time_t datatype 47 | */ 48 | #if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO) 49 | typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t; 50 | #else 51 | /* For time_t */ 52 | #include 53 | typedef time_t mbedtls_time_t; 54 | #endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */ 55 | 56 | /* 57 | * The function pointers for time 58 | */ 59 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) 60 | extern mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* time ); 61 | 62 | /** 63 | * \brief Set your own time function pointer 64 | * 65 | * \param time_func the time function implementation 66 | * 67 | * \return 0 68 | */ 69 | int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* time ) ); 70 | #else 71 | #if defined(MBEDTLS_PLATFORM_TIME_MACRO) 72 | #define mbedtls_time MBEDTLS_PLATFORM_TIME_MACRO 73 | #else 74 | #define mbedtls_time time 75 | #endif /* MBEDTLS_PLATFORM_TIME_MACRO */ 76 | #endif /* MBEDTLS_PLATFORM_TIME_ALT */ 77 | 78 | #ifdef __cplusplus 79 | } 80 | #endif 81 | 82 | #endif /* platform_time.h */ 83 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/ssl_cache.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file ssl_cache.h 3 | * 4 | * \brief SSL session cache implementation 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_SSL_CACHE_H 25 | #define MBEDTLS_SSL_CACHE_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include "ssl.h" 34 | 35 | #if defined(MBEDTLS_THREADING_C) 36 | #include "threading.h" 37 | #endif 38 | 39 | /** 40 | * \name SECTION: Module settings 41 | * 42 | * The configuration options you can set for this module are in this section. 43 | * Either change them in config.h or define them on the compiler command line. 44 | * \{ 45 | */ 46 | 47 | #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT) 48 | #define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */ 49 | #endif 50 | 51 | #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES) 52 | #define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */ 53 | #endif 54 | 55 | /* \} name SECTION: Module settings */ 56 | 57 | #ifdef __cplusplus 58 | extern "C" { 59 | #endif 60 | 61 | typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context; 62 | typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry; 63 | 64 | /** 65 | * \brief This structure is used for storing cache entries 66 | */ 67 | struct mbedtls_ssl_cache_entry 68 | { 69 | #if defined(MBEDTLS_HAVE_TIME) 70 | mbedtls_time_t timestamp; /*!< entry timestamp */ 71 | #endif 72 | mbedtls_ssl_session session; /*!< entry session */ 73 | #if defined(MBEDTLS_X509_CRT_PARSE_C) 74 | mbedtls_x509_buf peer_cert; /*!< entry peer_cert */ 75 | #endif 76 | mbedtls_ssl_cache_entry *next; /*!< chain pointer */ 77 | }; 78 | 79 | /** 80 | * \brief Cache context 81 | */ 82 | struct mbedtls_ssl_cache_context 83 | { 84 | mbedtls_ssl_cache_entry *chain; /*!< start of the chain */ 85 | int timeout; /*!< cache entry timeout */ 86 | int max_entries; /*!< maximum entries */ 87 | #if defined(MBEDTLS_THREADING_C) 88 | mbedtls_threading_mutex_t mutex; /*!< mutex */ 89 | #endif 90 | }; 91 | 92 | /** 93 | * \brief Initialize an SSL cache context 94 | * 95 | * \param cache SSL cache context 96 | */ 97 | void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache ); 98 | 99 | /** 100 | * \brief Cache get callback implementation 101 | * (Thread-safe if MBEDTLS_THREADING_C is enabled) 102 | * 103 | * \param data SSL cache context 104 | * \param session session to retrieve entry for 105 | */ 106 | int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session ); 107 | 108 | /** 109 | * \brief Cache set callback implementation 110 | * (Thread-safe if MBEDTLS_THREADING_C is enabled) 111 | * 112 | * \param data SSL cache context 113 | * \param session session to store entry for 114 | */ 115 | int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session ); 116 | 117 | #if defined(MBEDTLS_HAVE_TIME) 118 | /** 119 | * \brief Set the cache timeout 120 | * (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day)) 121 | * 122 | * A timeout of 0 indicates no timeout. 123 | * 124 | * \param cache SSL cache context 125 | * \param timeout cache entry timeout in seconds 126 | */ 127 | void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout ); 128 | #endif /* MBEDTLS_HAVE_TIME */ 129 | 130 | /** 131 | * \brief Set the maximum number of cache entries 132 | * (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50)) 133 | * 134 | * \param cache SSL cache context 135 | * \param max cache entry maximum 136 | */ 137 | void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max ); 138 | 139 | /** 140 | * \brief Free referenced items in a cache context and clear memory 141 | * 142 | * \param cache SSL cache context 143 | */ 144 | void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache ); 145 | 146 | #ifdef __cplusplus 147 | } 148 | #endif 149 | 150 | #endif /* ssl_cache.h */ 151 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/ssl_cookie.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file ssl_cookie.h 3 | * 4 | * \brief DTLS cookie callbacks implementation 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_SSL_COOKIE_H 25 | #define MBEDTLS_SSL_COOKIE_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include "ssl.h" 34 | 35 | #if defined(MBEDTLS_THREADING_C) 36 | #include "threading.h" 37 | #endif 38 | 39 | /** 40 | * \name SECTION: Module settings 41 | * 42 | * The configuration options you can set for this module are in this section. 43 | * Either change them in config.h or define them on the compiler command line. 44 | * \{ 45 | */ 46 | #ifndef MBEDTLS_SSL_COOKIE_TIMEOUT 47 | #define MBEDTLS_SSL_COOKIE_TIMEOUT 60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */ 48 | #endif 49 | 50 | /* \} name SECTION: Module settings */ 51 | 52 | #ifdef __cplusplus 53 | extern "C" { 54 | #endif 55 | 56 | /** 57 | * \brief Context for the default cookie functions. 58 | */ 59 | typedef struct mbedtls_ssl_cookie_ctx 60 | { 61 | mbedtls_md_context_t hmac_ctx; /*!< context for the HMAC portion */ 62 | #if !defined(MBEDTLS_HAVE_TIME) 63 | unsigned long serial; /*!< serial number for expiration */ 64 | #endif 65 | unsigned long timeout; /*!< timeout delay, in seconds if HAVE_TIME, 66 | or in number of tickets issued */ 67 | 68 | #if defined(MBEDTLS_THREADING_C) 69 | mbedtls_threading_mutex_t mutex; 70 | #endif 71 | } mbedtls_ssl_cookie_ctx; 72 | 73 | /** 74 | * \brief Initialize cookie context 75 | */ 76 | void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx ); 77 | 78 | /** 79 | * \brief Setup cookie context (generate keys) 80 | */ 81 | int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx, 82 | int (*f_rng)(void *, unsigned char *, size_t), 83 | void *p_rng ); 84 | 85 | /** 86 | * \brief Set expiration delay for cookies 87 | * (Default MBEDTLS_SSL_COOKIE_TIMEOUT) 88 | * 89 | * \param ctx Cookie contex 90 | * \param delay Delay, in seconds if HAVE_TIME, or in number of cookies 91 | * issued in the meantime. 92 | * 0 to disable expiration (NOT recommended) 93 | */ 94 | void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay ); 95 | 96 | /** 97 | * \brief Free cookie context 98 | */ 99 | void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx ); 100 | 101 | /** 102 | * \brief Generate cookie, see \c mbedtls_ssl_cookie_write_t 103 | */ 104 | mbedtls_ssl_cookie_write_t mbedtls_ssl_cookie_write; 105 | 106 | /** 107 | * \brief Verify cookie, see \c mbedtls_ssl_cookie_write_t 108 | */ 109 | mbedtls_ssl_cookie_check_t mbedtls_ssl_cookie_check; 110 | 111 | #ifdef __cplusplus 112 | } 113 | #endif 114 | 115 | #endif /* ssl_cookie.h */ 116 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/ssl_ticket.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file ssl_ticket.h 3 | * 4 | * \brief TLS server ticket callbacks implementation 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_SSL_TICKET_H 25 | #define MBEDTLS_SSL_TICKET_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | /* 34 | * This implementation of the session ticket callbacks includes key 35 | * management, rotating the keys periodically in order to preserve forward 36 | * secrecy, when MBEDTLS_HAVE_TIME is defined. 37 | */ 38 | 39 | #include "ssl.h" 40 | #include "cipher.h" 41 | 42 | #if defined(MBEDTLS_THREADING_C) 43 | #include "threading.h" 44 | #endif 45 | 46 | #ifdef __cplusplus 47 | extern "C" { 48 | #endif 49 | 50 | /** 51 | * \brief Information for session ticket protection 52 | */ 53 | typedef struct mbedtls_ssl_ticket_key 54 | { 55 | unsigned char name[4]; /*!< random key identifier */ 56 | uint32_t generation_time; /*!< key generation timestamp (seconds) */ 57 | mbedtls_cipher_context_t ctx; /*!< context for auth enc/decryption */ 58 | } 59 | mbedtls_ssl_ticket_key; 60 | 61 | /** 62 | * \brief Context for session ticket handling functions 63 | */ 64 | typedef struct mbedtls_ssl_ticket_context 65 | { 66 | mbedtls_ssl_ticket_key keys[2]; /*!< ticket protection keys */ 67 | unsigned char active; /*!< index of the currently active key */ 68 | 69 | uint32_t ticket_lifetime; /*!< lifetime of tickets in seconds */ 70 | 71 | /** Callback for getting (pseudo-)random numbers */ 72 | int (*f_rng)(void *, unsigned char *, size_t); 73 | void *p_rng; /*!< context for the RNG function */ 74 | 75 | #if defined(MBEDTLS_THREADING_C) 76 | mbedtls_threading_mutex_t mutex; 77 | #endif 78 | } 79 | mbedtls_ssl_ticket_context; 80 | 81 | /** 82 | * \brief Initialize a ticket context. 83 | * (Just make it ready for mbedtls_ssl_ticket_setup() 84 | * or mbedtls_ssl_ticket_free().) 85 | * 86 | * \param ctx Context to be initialized 87 | */ 88 | void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx ); 89 | 90 | /** 91 | * \brief Prepare context to be actually used 92 | * 93 | * \param ctx Context to be set up 94 | * \param f_rng RNG callback function 95 | * \param p_rng RNG callback context 96 | * \param cipher AEAD cipher to use for ticket protection. 97 | * Recommended value: MBEDTLS_CIPHER_AES_256_GCM. 98 | * \param lifetime Tickets lifetime in seconds 99 | * Recommended value: 86400 (one day). 100 | * 101 | * \note It is highly recommended to select a cipher that is at 102 | * least as strong as the the strongest ciphersuite 103 | * supported. Usually that means a 256-bit key. 104 | * 105 | * \note The lifetime of the keys is twice the lifetime of tickets. 106 | * It is recommended to pick a reasonnable lifetime so as not 107 | * to negate the benefits of forward secrecy. 108 | * 109 | * \return 0 if successful, 110 | * or a specific MBEDTLS_ERR_XXX error code 111 | */ 112 | int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx, 113 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, 114 | mbedtls_cipher_type_t cipher, 115 | uint32_t lifetime ); 116 | 117 | /** 118 | * \brief Implementation of the ticket write callback 119 | * 120 | * \note See \c mbedlts_ssl_ticket_write_t for description 121 | */ 122 | mbedtls_ssl_ticket_write_t mbedtls_ssl_ticket_write; 123 | 124 | /** 125 | * \brief Implementation of the ticket parse callback 126 | * 127 | * \note See \c mbedlts_ssl_ticket_parse_t for description 128 | */ 129 | mbedtls_ssl_ticket_parse_t mbedtls_ssl_ticket_parse; 130 | 131 | /** 132 | * \brief Free a context's content and zeroize it. 133 | * 134 | * \param ctx Context to be cleaned up 135 | */ 136 | void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx ); 137 | 138 | #ifdef __cplusplus 139 | } 140 | #endif 141 | 142 | #endif /* ssl_ticket.h */ 143 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/threading.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file threading.h 3 | * 4 | * \brief Threading abstraction layer 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_THREADING_H 25 | #define MBEDTLS_THREADING_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | /* MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE is deprecated and should not be 40 | * used. */ 41 | #define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */ 42 | 43 | #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */ 44 | #define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */ 45 | 46 | #if defined(MBEDTLS_THREADING_PTHREAD) 47 | #include 48 | typedef struct mbedtls_threading_mutex_t 49 | { 50 | pthread_mutex_t mutex; 51 | char is_valid; 52 | } mbedtls_threading_mutex_t; 53 | #endif 54 | 55 | #if defined(MBEDTLS_THREADING_ALT) 56 | /* You should define the mbedtls_threading_mutex_t type in your header */ 57 | #include "threading_alt.h" 58 | 59 | /** 60 | * \brief Set your alternate threading implementation function 61 | * pointers and initialize global mutexes. If used, this 62 | * function must be called once in the main thread before any 63 | * other mbed TLS function is called, and 64 | * mbedtls_threading_free_alt() must be called once in the main 65 | * thread after all other mbed TLS functions. 66 | * 67 | * \note mutex_init() and mutex_free() don't return a status code. 68 | * If mutex_init() fails, it should leave its argument (the 69 | * mutex) in a state such that mutex_lock() will fail when 70 | * called with this argument. 71 | * 72 | * \param mutex_init the init function implementation 73 | * \param mutex_free the free function implementation 74 | * \param mutex_lock the lock function implementation 75 | * \param mutex_unlock the unlock function implementation 76 | */ 77 | void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), 78 | void (*mutex_free)( mbedtls_threading_mutex_t * ), 79 | int (*mutex_lock)( mbedtls_threading_mutex_t * ), 80 | int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ); 81 | 82 | /** 83 | * \brief Free global mutexes. 84 | */ 85 | void mbedtls_threading_free_alt( void ); 86 | #endif /* MBEDTLS_THREADING_ALT */ 87 | 88 | #if defined(MBEDTLS_THREADING_C) 89 | /* 90 | * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock 91 | * 92 | * All these functions are expected to work or the result will be undefined. 93 | */ 94 | extern void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t *mutex ); 95 | extern void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t *mutex ); 96 | extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex ); 97 | extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex ); 98 | 99 | /* 100 | * Global mutexes 101 | */ 102 | #if defined(MBEDTLS_FS_IO) 103 | extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex; 104 | #endif 105 | 106 | #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) 107 | /* This mutex may or may not be used in the default definition of 108 | * mbedtls_platform_gmtime_r(), but in order to determine that, 109 | * we need to check POSIX features, hence modify _POSIX_C_SOURCE. 110 | * With the current approach, this declaration is orphaned, lacking 111 | * an accompanying definition, in case mbedtls_platform_gmtime_r() 112 | * doesn't need it, but that's not a problem. */ 113 | extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex; 114 | #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */ 115 | 116 | #endif /* MBEDTLS_THREADING_C */ 117 | 118 | #ifdef __cplusplus 119 | } 120 | #endif 121 | 122 | #endif /* threading.h */ 123 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/version.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file version.h 3 | * 4 | * \brief Run-time version information 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | /* 25 | * This set of compile-time defines and run-time variables can be used to 26 | * determine the version number of the mbed TLS library used. 27 | */ 28 | #ifndef MBEDTLS_VERSION_H 29 | #define MBEDTLS_VERSION_H 30 | 31 | #if !defined(MBEDTLS_CONFIG_FILE) 32 | #include "config.h" 33 | #else 34 | #include MBEDTLS_CONFIG_FILE 35 | #endif 36 | 37 | /** 38 | * The version number x.y.z is split into three parts. 39 | * Major, Minor, Patchlevel 40 | */ 41 | #define MBEDTLS_VERSION_MAJOR 2 42 | #define MBEDTLS_VERSION_MINOR 16 43 | #define MBEDTLS_VERSION_PATCH 2 44 | 45 | /** 46 | * The single version number has the following structure: 47 | * MMNNPP00 48 | * Major version | Minor version | Patch version 49 | */ 50 | #define MBEDTLS_VERSION_NUMBER 0x02100200 51 | #define MBEDTLS_VERSION_STRING "2.16.2" 52 | #define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.16.2" 53 | 54 | #if defined(MBEDTLS_VERSION_C) 55 | 56 | #ifdef __cplusplus 57 | extern "C" { 58 | #endif 59 | 60 | /** 61 | * Get the version number. 62 | * 63 | * \return The constructed version number in the format 64 | * MMNNPP00 (Major, Minor, Patch). 65 | */ 66 | unsigned int mbedtls_version_get_number( void ); 67 | 68 | /** 69 | * Get the version string ("x.y.z"). 70 | * 71 | * \param string The string that will receive the value. 72 | * (Should be at least 9 bytes in size) 73 | */ 74 | void mbedtls_version_get_string( char *string ); 75 | 76 | /** 77 | * Get the full version string ("mbed TLS x.y.z"). 78 | * 79 | * \param string The string that will receive the value. The mbed TLS version 80 | * string will use 18 bytes AT MOST including a terminating 81 | * null byte. 82 | * (So the buffer should be at least 18 bytes to receive this 83 | * version string). 84 | */ 85 | void mbedtls_version_get_string_full( char *string ); 86 | 87 | /** 88 | * \brief Check if support for a feature was compiled into this 89 | * mbed TLS binary. This allows you to see at runtime if the 90 | * library was for instance compiled with or without 91 | * Multi-threading support. 92 | * 93 | * \note only checks against defines in the sections "System 94 | * support", "mbed TLS modules" and "mbed TLS feature 95 | * support" in config.h 96 | * 97 | * \param feature The string for the define to check (e.g. "MBEDTLS_AES_C") 98 | * 99 | * \return 0 if the feature is present, 100 | * -1 if the feature is not present and 101 | * -2 if support for feature checking as a whole was not 102 | * compiled in. 103 | */ 104 | int mbedtls_version_check_feature( const char *feature ); 105 | 106 | #ifdef __cplusplus 107 | } 108 | #endif 109 | 110 | #endif /* MBEDTLS_VERSION_C */ 111 | 112 | #endif /* version.h */ 113 | -------------------------------------------------------------------------------- /network/mbedtls/include/mbedtls/xtea.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file xtea.h 3 | * 4 | * \brief XTEA block cipher (32-bit) 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_XTEA_H 25 | #define MBEDTLS_XTEA_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | #include 35 | 36 | #define MBEDTLS_XTEA_ENCRYPT 1 37 | #define MBEDTLS_XTEA_DECRYPT 0 38 | 39 | #define MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */ 40 | 41 | /* MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED is deprecated and should not be used. */ 42 | #define MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED -0x0029 /**< XTEA hardware accelerator failed. */ 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | #if !defined(MBEDTLS_XTEA_ALT) 49 | // Regular implementation 50 | // 51 | 52 | /** 53 | * \brief XTEA context structure 54 | */ 55 | typedef struct mbedtls_xtea_context 56 | { 57 | uint32_t k[4]; /*!< key */ 58 | } 59 | mbedtls_xtea_context; 60 | 61 | #else /* MBEDTLS_XTEA_ALT */ 62 | #include "xtea_alt.h" 63 | #endif /* MBEDTLS_XTEA_ALT */ 64 | 65 | /** 66 | * \brief Initialize XTEA context 67 | * 68 | * \param ctx XTEA context to be initialized 69 | */ 70 | void mbedtls_xtea_init( mbedtls_xtea_context *ctx ); 71 | 72 | /** 73 | * \brief Clear XTEA context 74 | * 75 | * \param ctx XTEA context to be cleared 76 | */ 77 | void mbedtls_xtea_free( mbedtls_xtea_context *ctx ); 78 | 79 | /** 80 | * \brief XTEA key schedule 81 | * 82 | * \param ctx XTEA context to be initialized 83 | * \param key the secret key 84 | */ 85 | void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] ); 86 | 87 | /** 88 | * \brief XTEA cipher function 89 | * 90 | * \param ctx XTEA context 91 | * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT 92 | * \param input 8-byte input block 93 | * \param output 8-byte output block 94 | * 95 | * \return 0 if successful 96 | */ 97 | int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, 98 | int mode, 99 | const unsigned char input[8], 100 | unsigned char output[8] ); 101 | 102 | #if defined(MBEDTLS_CIPHER_MODE_CBC) 103 | /** 104 | * \brief XTEA CBC cipher function 105 | * 106 | * \param ctx XTEA context 107 | * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT 108 | * \param length the length of input, multiple of 8 109 | * \param iv initialization vector for CBC mode 110 | * \param input input block 111 | * \param output output block 112 | * 113 | * \return 0 if successful, 114 | * MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0 115 | */ 116 | int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, 117 | int mode, 118 | size_t length, 119 | unsigned char iv[8], 120 | const unsigned char *input, 121 | unsigned char *output); 122 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ 123 | 124 | #if defined(MBEDTLS_SELF_TEST) 125 | 126 | /** 127 | * \brief Checkup routine 128 | * 129 | * \return 0 if successful, or 1 if the test failed 130 | */ 131 | int mbedtls_xtea_self_test( int verbose ); 132 | 133 | #endif /* MBEDTLS_SELF_TEST */ 134 | 135 | #ifdef __cplusplus 136 | } 137 | #endif 138 | 139 | #endif /* xtea.h */ 140 | -------------------------------------------------------------------------------- /network/mbedtls/library/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | libmbed* 3 | *.sln 4 | *.vcxproj 5 | -------------------------------------------------------------------------------- /network/mbedtls/library/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | aux_source_directory(. MBEDTLS_SRCS) 2 | 3 | add_library("mbedtls" ${CMAKE_LIB_TYPE} ${MBEDTLS_SRCS}) 4 | target_link_libraries("mbedtls" "wrapper") 5 | -------------------------------------------------------------------------------- /network/mbedtls/library/version.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Version information 3 | * 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 | * not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * This file is part of mbed TLS (https://tls.mbed.org) 20 | */ 21 | 22 | #if !defined(MBEDTLS_CONFIG_FILE) 23 | #include "mbedtls/config.h" 24 | #else 25 | #include MBEDTLS_CONFIG_FILE 26 | #endif 27 | 28 | #if defined(MBEDTLS_VERSION_C) 29 | 30 | #include "mbedtls/version.h" 31 | #include 32 | 33 | unsigned int mbedtls_version_get_number( void ) 34 | { 35 | return( MBEDTLS_VERSION_NUMBER ); 36 | } 37 | 38 | void mbedtls_version_get_string( char *string ) 39 | { 40 | memcpy( string, MBEDTLS_VERSION_STRING, 41 | sizeof( MBEDTLS_VERSION_STRING ) ); 42 | } 43 | 44 | void mbedtls_version_get_string_full( char *string ) 45 | { 46 | memcpy( string, MBEDTLS_VERSION_STRING_FULL, 47 | sizeof( MBEDTLS_VERSION_STRING_FULL ) ); 48 | } 49 | 50 | #endif /* MBEDTLS_VERSION_C */ 51 | -------------------------------------------------------------------------------- /network/mbedtls/wrapper/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | aux_source_directory(. WARAPPER_SRCS) 2 | 3 | add_library("wrapper" ${CMAKE_LIB_TYPE} ${WARAPPER_SRCS}) 4 | target_link_libraries("wrapper" "platform") 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /network/mbedtls/wrapper/entropy_hardware_alt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-01-11 02:04:49 5 | * @LastEditTime: 2020-02-19 23:53:22 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include "mbedtls/entropy.h" 9 | #include "random.h" 10 | 11 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) 12 | 13 | static int mbedtls_get_random(unsigned char *buf, size_t len) 14 | { 15 | int i, j; 16 | unsigned long tmp; 17 | 18 | for (i = 0; i < ((len + 3) & ~3) / 4; i++) { 19 | tmp = random_number(); 20 | 21 | for (j = 0; j < 4; j++) { 22 | if ((i * 4 + j) < len) { 23 | buf[i * 4 + j] = (unsigned char)(tmp >> (j * 8)); 24 | } else { 25 | break; 26 | } 27 | } 28 | } 29 | 30 | return 0; 31 | } 32 | 33 | int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) 34 | { 35 | mbedtls_get_random(output, len); 36 | *olen = len; 37 | 38 | return 0; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /network/mbedtls/wrapper/net_sockets_alt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-01-08 21:48:09 5 | * @LastEditTime : 2020-01-12 00:23:42 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | 9 | #if !defined(MBEDTLS_CONFIG_FILE) 10 | #include "mbedtls/config.h" 11 | #else 12 | #include MBEDTLS_CONFIG_FILE 13 | #endif 14 | 15 | #if !defined(MBEDTLS_NET_C) 16 | 17 | #if defined(MBEDTLS_PLATFORM_C) 18 | #include "mbedtls/platform.h" 19 | #else 20 | #include 21 | #endif 22 | 23 | #include "mbedtls/net_sockets.h" 24 | #include "platform_net_socket.h" 25 | #include "platform_timer.h" 26 | 27 | /* 28 | * Initialize a context 29 | */ 30 | void mbedtls_net_init(mbedtls_net_context *ctx) 31 | { 32 | if (!ctx) { 33 | return; 34 | } 35 | 36 | ctx->fd = -1; 37 | } 38 | 39 | /* 40 | * Initiate a TCP connection with host:port and the given protocol 41 | */ 42 | int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto) 43 | { 44 | int net_proto; 45 | 46 | net_proto = (proto == MBEDTLS_NET_PROTO_UDP) ? PLATFORM_NET_PROTO_UDP : PLATFORM_NET_PROTO_TCP; 47 | 48 | ctx->fd = platform_net_socket_connect(host, port, net_proto); 49 | 50 | if (ctx->fd < 0) { 51 | return ctx->fd; 52 | } 53 | 54 | return 0; 55 | } 56 | 57 | /* 58 | * Set the socket blocking or non-blocking 59 | */ 60 | int mbedtls_net_set_block( mbedtls_net_context *ctx ) 61 | { 62 | return platform_net_socket_set_block(ctx->fd); 63 | } 64 | 65 | int mbedtls_net_set_nonblock( mbedtls_net_context *ctx ) 66 | { 67 | return platform_net_socket_set_nonblock(ctx->fd); 68 | } 69 | 70 | /* 71 | * Read at most 'len' characters 72 | */ 73 | int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len) 74 | { 75 | int ret; 76 | int fd = ((mbedtls_net_context *)ctx)->fd; 77 | 78 | if (fd < 0) { 79 | return MBEDTLS_ERR_NET_INVALID_CONTEXT; 80 | } 81 | 82 | ret = platform_net_socket_recv(fd, buf, len, 0); 83 | 84 | if (ret == 0) { 85 | return MBEDTLS_ERR_SSL_WANT_READ; 86 | } else if (ret < 0) { 87 | return MBEDTLS_ERR_NET_RECV_FAILED; 88 | } 89 | 90 | return ret; 91 | } 92 | 93 | /* 94 | * Read at most 'len' characters, blocking for at most 'timeout' ms 95 | */ 96 | int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout) 97 | { 98 | int ret; 99 | int fd = ((mbedtls_net_context *)ctx)->fd; 100 | 101 | if (fd < 0) { 102 | return MBEDTLS_ERR_NET_INVALID_CONTEXT; 103 | } 104 | 105 | ret = platform_net_socket_recv_timeout(fd, buf, len, timeout); 106 | 107 | if (ret == 0) { 108 | return MBEDTLS_ERR_SSL_TIMEOUT; 109 | } else if (ret < 0) { 110 | return MBEDTLS_ERR_NET_RECV_FAILED; 111 | } 112 | 113 | return ret; 114 | } 115 | 116 | /* 117 | * Write at most 'len' characters 118 | */ 119 | 120 | int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len) 121 | { 122 | int ret; 123 | int fd = ((mbedtls_net_context *) ctx)->fd; 124 | 125 | if (fd < 0) { 126 | return MBEDTLS_ERR_NET_INVALID_CONTEXT; 127 | } 128 | 129 | ret = platform_net_socket_write(fd, (unsigned char*)buf, len); 130 | 131 | if (ret == 0) { 132 | return MBEDTLS_ERR_SSL_WANT_WRITE; 133 | } else if (ret < 0) { 134 | return MBEDTLS_ERR_NET_SEND_FAILED; 135 | } 136 | 137 | return ret; 138 | } 139 | 140 | /* 141 | * Gracefully close the connection 142 | */ 143 | 144 | void mbedtls_net_free(mbedtls_net_context *ctx) 145 | { 146 | if (ctx->fd < 0) { 147 | return; 148 | } 149 | 150 | // shutdown(ctx->fd, 2); 151 | platform_net_socket_close(ctx->fd); 152 | 153 | ctx->fd = -1; 154 | } 155 | 156 | /* 157 | * Portable usleep helper 158 | */ 159 | void mbedtls_net_usleep( unsigned long usec ) 160 | { 161 | platform_timer_usleep(usec); 162 | } 163 | 164 | /* dtls */ 165 | void mbedtls_dtls_net_init(mbedtls_net_context *ctx) 166 | { 167 | mbedtls_net_init(ctx); 168 | } 169 | 170 | int mbedtls_dtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto) 171 | { 172 | return mbedtls_net_connect(ctx, host, port, proto); 173 | } 174 | 175 | void mbedtls_dtls_net_usleep(unsigned long usec) 176 | { 177 | mbedtls_net_usleep(usec); 178 | } 179 | 180 | int mbedtls_dtls_net_recv(void *ctx, unsigned char *buf, size_t len) 181 | { 182 | return mbedtls_net_recv(ctx, buf, len); 183 | } 184 | 185 | int mbedtls_dtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout) 186 | { 187 | return mbedtls_net_recv_timeout(ctx, buf, len, timeout); 188 | } 189 | 190 | int mbedtls_dtls_net_send(void *ctx, const unsigned char *buf, size_t len) 191 | { 192 | return mbedtls_net_send(ctx, buf, len); 193 | } 194 | 195 | void mbedtls_dtls_net_free(mbedtls_net_context *ctx) 196 | { 197 | mbedtls_net_free(ctx); 198 | } 199 | 200 | #endif /* MBEDTLS_NET_C */ 201 | -------------------------------------------------------------------------------- /network/mbedtls/wrapper/timing_alt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-01-08 19:44:56 5 | * @LastEditTime : 2020-01-13 01:01:39 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #if !defined(MBEDTLS_CONFIG_FILE) 9 | #include "mbedtls/config.h" 10 | #else 11 | #include MBEDTLS_CONFIG_FILE 12 | #endif 13 | 14 | #include "platform_timer.h" 15 | #include "timing_alt.h" 16 | 17 | unsigned long mbedtls_timing_hardclock( void ) 18 | { 19 | return 1600*1000*1000; 20 | } 21 | 22 | unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) 23 | { 24 | struct mbedtls_timing_hr_time now; 25 | 26 | now.timer_ms = platform_timer_now(); 27 | 28 | if (reset) { 29 | val->timer_ms = now.timer_ms; 30 | } 31 | 32 | return (unsigned long)(now.timer_ms - val->timer_ms); 33 | } 34 | 35 | /* 36 | * Set delays to watch 37 | */ 38 | void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms) 39 | { 40 | mbedtls_timing_delay_context *ctx; 41 | 42 | if (!data) { 43 | return; 44 | } 45 | 46 | ctx = (mbedtls_timing_delay_context*)data; 47 | 48 | ctx->int_ms = int_ms; 49 | ctx->fin_ms = fin_ms; 50 | 51 | if (fin_ms != 0) { 52 | (void)mbedtls_timing_get_timer(&ctx->timer, 1); 53 | } 54 | } 55 | 56 | /* 57 | * Get number of delays expired 58 | */ 59 | int mbedtls_timing_get_delay(void *data) 60 | { 61 | unsigned long elapsed_ms; 62 | mbedtls_timing_delay_context *ctx; 63 | 64 | if (!data) { 65 | return -1; 66 | } 67 | 68 | ctx = (mbedtls_timing_delay_context*)data; 69 | 70 | if (ctx->fin_ms == 0) { 71 | return -1; 72 | } 73 | 74 | elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0); 75 | 76 | if (elapsed_ms >= ctx->fin_ms) { 77 | return 2; 78 | } 79 | 80 | if (elapsed_ms >= ctx->int_ms) { 81 | return 1; 82 | } 83 | 84 | return 0; 85 | } 86 | 87 | -------------------------------------------------------------------------------- /network/mbedtls/wrapper/timing_alt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-01-08 19:41:14 5 | * @LastEditTime: 2020-01-08 19:44:11 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | /** 9 | * \file timing_alt.h 10 | * 11 | * \brief Portable interface to the CPU cycle counter 12 | * 13 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 14 | * SPDX-License-Identifier: Apache-2.0 15 | * 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 17 | * not use this file except in compliance with the License. 18 | * You may obtain a copy of the License at 19 | * 20 | * http://www.apache.org/licenses/LICENSE-2.0 21 | * 22 | * Unless required by applicable law or agreed to in writing, software 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 25 | * See the License for the specific language governing permissions and 26 | * limitations under the License. 27 | * 28 | * This file is part of mbed TLS (https://tls.mbed.org) 29 | */ 30 | 31 | #ifndef _TIMING_ALT_H_ 32 | #define _TIMING_ALT_H_ 33 | 34 | #if !defined(MBEDTLS_CONFIG_FILE) 35 | #include "config.h" 36 | #else 37 | #include MBEDTLS_CONFIG_FILE 38 | #endif 39 | 40 | #include "stdint.h" 41 | 42 | /** 43 | * @brief timer structure 44 | */ 45 | struct mbedtls_timing_hr_time { 46 | uint64_t timer_ms; 47 | }; 48 | 49 | /** 50 | * @brief Context for mbedtls_timing_set/get_delay() 51 | */ 52 | typedef struct { 53 | struct mbedtls_timing_hr_time timer; 54 | uint32_t int_ms; 55 | uint32_t fin_ms; 56 | } mbedtls_timing_delay_context; 57 | 58 | /** 59 | * @brief Return the elapsed time in milliseconds 60 | * 61 | * @param val points to a timer structure 62 | * @param reset if set to 1, the timer is restarted 63 | */ 64 | unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset); 65 | 66 | /** 67 | * @brief Set a pair of delays to watch 68 | * Must point to a valid mbedtls_timing_delay_context struct. 69 | * 70 | * @param data Pointer to timing data 71 | * @param int_ms First (intermediate) delay in milliseconds. 72 | * @param fin_ms Second (final) delay in milliseconds. 73 | * Pass 0 to cancel the current delay. 74 | */ 75 | void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms); 76 | 77 | /** 78 | * @brief Get the status of delays 79 | * (Memory helper: number of delays passed.) 80 | * 81 | * @param data Pointer to timing data 82 | * Must point to a valid mbedtls_timing_delay_context struct. 83 | * 84 | * @return -1 if cancelled (fin_ms = 0) 85 | * 0 if none of the delays are passed, 86 | * 1 if only the intermediate delay is passed, 87 | * 2 if the final delay is passed. 88 | */ 89 | int mbedtls_timing_get_delay(void *data); 90 | 91 | #endif 92 | 93 | -------------------------------------------------------------------------------- /network/nettype_tcp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 13:38:52 5 | * @LastEditTime: 2020-05-25 10:13:41 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include "nettype_tcp.h" 9 | #include "mqtt_log.h" 10 | 11 | int nettype_tcp_read(network_t *n, unsigned char *read_buf, int len, int timeout) 12 | { 13 | return platform_net_socket_recv_timeout(n->socket, read_buf, len, timeout); 14 | } 15 | 16 | int nettype_tcp_write(network_t *n, unsigned char *write_buf, int len, int timeout) 17 | { 18 | return platform_net_socket_write_timeout(n->socket, write_buf, len, timeout); 19 | } 20 | 21 | int nettype_tcp_connect(network_t* n) 22 | { 23 | n->socket = platform_net_socket_connect(n->host, n->port, PLATFORM_NET_PROTO_TCP); 24 | if (n->socket < 0) 25 | RETURN_ERROR(n->socket); 26 | 27 | RETURN_ERROR(KAWAII_MQTT_SUCCESS_ERROR); 28 | } 29 | 30 | void nettype_tcp_disconnect(network_t* n) 31 | { 32 | if (NULL != n) 33 | platform_net_socket_close(n->socket); 34 | n->socket = -1; 35 | } 36 | -------------------------------------------------------------------------------- /network/nettype_tcp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 13:39:00 5 | * @LastEditTime: 2020-05-24 15:49:46 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _NETTYPE_TCP_H_ 9 | #define _NETTYPE_TCP_H_ 10 | 11 | #include "platform_net_socket.h" 12 | #include "network.h" 13 | #include "mqtt_error.h" 14 | 15 | int nettype_tcp_read(network_t *n, unsigned char *buf, int len, int timeout); 16 | int nettype_tcp_write(network_t *n, unsigned char *buf, int len, int timeout); 17 | int nettype_tcp_connect(network_t* n); 18 | void nettype_tcp_disconnect(network_t* n); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /network/nettype_tls.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-01-11 19:45:44 5 | * @LastEditTime: 2020-05-24 17:03:13 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | 9 | #ifndef _NETTYPE_TLS_H_ 10 | #define _NETTYPE_TLS_H_ 11 | 12 | #include "mqtt_defconfig.h" 13 | #include "network.h" 14 | #include "mqtt_error.h" 15 | #include "mqtt_log.h" 16 | 17 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 18 | 19 | typedef struct nettype_tls_params { 20 | mbedtls_net_context socket_fd; /**< mbed TLS network context. */ 21 | mbedtls_entropy_context entropy; /**< mbed TLS entropy. */ 22 | mbedtls_ctr_drbg_context ctr_drbg; /**< mbed TLS ctr_drbg. */ 23 | mbedtls_ssl_context ssl; /**< mbed TLS control context. */ 24 | mbedtls_ssl_config ssl_conf; /**< mbed TLS configuration context. */ 25 | #if defined(MBEDTLS_X509_CRT_PARSE_C) 26 | mbedtls_x509_crt ca_cert; /**< mbed TLS CA certification. */ 27 | mbedtls_x509_crt client_cert; /**< mbed TLS Client certification. */ 28 | #endif 29 | mbedtls_pk_context private_key; /**< mbed TLS Client key. */ 30 | } nettype_tls_params_t; 31 | 32 | int nettype_tls_read(network_t *n, unsigned char *buf, int len, int timeout); 33 | int nettype_tls_write(network_t *n, unsigned char *buf, int len, int timeout); 34 | int nettype_tls_connect(network_t* n); 35 | void nettype_tls_disconnect(network_t* n); 36 | 37 | #endif /* KAWAII_MQTT_NETWORK_TYPE_TLS */ 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /network/network.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-09 21:30:54 5 | * @LastEditTime: 2020-06-05 17:17:48 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include "platform_timer.h" 9 | #include "platform_memory.h" 10 | #include "nettype_tcp.h" 11 | 12 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 13 | #include "nettype_tls.h" 14 | #endif 15 | 16 | int network_read(network_t *n, unsigned char *buf, int len, int timeout) 17 | { 18 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 19 | if (n->channel) 20 | return nettype_tls_read(n, buf, len, timeout); 21 | #endif 22 | return nettype_tcp_read(n, buf, len, timeout); 23 | } 24 | 25 | int network_write(network_t *n, unsigned char *buf, int len, int timeout) 26 | { 27 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 28 | if (n->channel) 29 | return nettype_tls_write(n, buf, len, timeout); 30 | #endif 31 | return nettype_tcp_write(n, buf, len, timeout); 32 | } 33 | 34 | int network_connect(network_t *n) 35 | { 36 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 37 | if (n->channel) 38 | return nettype_tls_connect(n); 39 | #endif 40 | return nettype_tcp_connect(n); 41 | 42 | } 43 | 44 | void network_disconnect(network_t *n) 45 | { 46 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 47 | if (n->channel) 48 | nettype_tls_disconnect(n); 49 | else 50 | #endif 51 | nettype_tcp_disconnect(n); 52 | } 53 | 54 | int network_init(network_t *n, const char *host, const char *port, const char *ca) 55 | { 56 | if (NULL == n) 57 | RETURN_ERROR(KAWAII_MQTT_NULL_VALUE_ERROR); 58 | 59 | n->socket = -1; 60 | n->host = host; 61 | n->port = port; 62 | 63 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 64 | n->channel = 0; 65 | 66 | if (NULL != ca) { 67 | network_set_ca(n, ca); 68 | } 69 | #endif 70 | RETURN_ERROR(KAWAII_MQTT_SUCCESS_ERROR); 71 | } 72 | 73 | void network_release(network_t* n) 74 | { 75 | if (n->socket >= 0) 76 | network_disconnect(n); 77 | 78 | } 79 | 80 | void network_set_channel(network_t *n, int channel) 81 | { 82 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 83 | n->channel = channel; 84 | #endif 85 | } 86 | 87 | int network_set_ca(network_t *n, const char *ca) 88 | { 89 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 90 | if ((NULL == n) || (NULL == ca)) 91 | RETURN_ERROR(KAWAII_MQTT_NULL_VALUE_ERROR); 92 | 93 | n->ca_crt = ca; 94 | n->ca_crt_len = strlen(ca); 95 | n->channel = NETWORK_CHANNEL_TLS; 96 | n->timeout_ms = KAWAII_MQTT_TLS_HANDSHAKE_TIMEOUT; 97 | #endif 98 | RETURN_ERROR(KAWAII_MQTT_SUCCESS_ERROR); 99 | } 100 | 101 | int network_set_host_port(network_t* n, char *host, char *port) 102 | { 103 | if (!(n && host && port)) 104 | RETURN_ERROR(KAWAII_MQTT_NULL_VALUE_ERROR); 105 | 106 | n->host = host; 107 | n->port = port; 108 | 109 | RETURN_ERROR(KAWAII_MQTT_SUCCESS_ERROR); 110 | } 111 | 112 | -------------------------------------------------------------------------------- /network/network.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-09 21:31:02 5 | * @LastEditTime: 2020-05-21 01:09:29 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _NETWORK_H_ 9 | #define _NETWORK_H_ 10 | 11 | #include "mqtt_defconfig.h" 12 | 13 | #define NETWORK_CHANNEL_TCP 0 14 | #define NETWORK_CHANNEL_TLS 1 15 | 16 | typedef struct network { 17 | const char *host; 18 | const char *port; 19 | int socket; 20 | #ifdef KAWAII_MQTT_NETWORK_TYPE_TLS 21 | int channel; /* tcp or tls */ 22 | const char *ca_crt; 23 | unsigned int ca_crt_len; 24 | unsigned int timeout_ms; // SSL handshake timeout in millisecond 25 | void *nettype_tls_params; 26 | #endif 27 | } network_t; 28 | 29 | int network_init(network_t *n, const char *host, const char *port, const char *ca); 30 | int network_set_ca(network_t *n, const char *ca); 31 | void network_set_channel(network_t *n, int channel); 32 | int network_set_host_port(network_t* n, char *host, char *port); 33 | int network_read(network_t* n, unsigned char* buf, int len, int timeout); 34 | int network_write(network_t* n, unsigned char* buf, int len, int timeout); 35 | int network_connect(network_t* n); 36 | void network_disconnect(network_t *n); 37 | void network_release(network_t* n); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /platform/RT-Thread/platform_memory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-14 22:02:07 5 | * @LastEditTime: 2020-02-19 23:53:50 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include "platform_memory.h" 9 | #include "string.h" 10 | 11 | void *platform_memory_alloc(size_t size) 12 | { 13 | char *ptr; 14 | ptr = rt_malloc(size); 15 | memset(ptr, 0, size); 16 | return (void *)ptr; 17 | } 18 | 19 | void *platform_memory_calloc(size_t num, size_t size) 20 | { 21 | return rt_calloc(num, size); 22 | } 23 | 24 | void platform_memory_free(void *ptr) 25 | { 26 | rt_free(ptr); 27 | } 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /platform/RT-Thread/platform_memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-14 22:06:35 5 | * @LastEditTime: 2020-02-19 23:54:02 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _PLATFORM_MEMORY_H_ 9 | #define _PLATFORM_MEMORY_H_ 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | 16 | void *platform_memory_alloc(size_t size); 17 | void *platform_memory_calloc(size_t num, size_t size); 18 | void platform_memory_free(void *ptr); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /platform/RT-Thread/platform_mutex.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 18:27:19 5 | * @LastEditTime : 2020-01-08 20:23:13 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include "platform_mutex.h" 9 | 10 | int platform_mutex_init(platform_mutex_t* m) 11 | { 12 | m->mutex = rt_mutex_create("platform_mutex", RT_IPC_FLAG_PRIO); 13 | return 0; 14 | } 15 | 16 | int platform_mutex_lock(platform_mutex_t* m) 17 | { 18 | return rt_mutex_take((m->mutex), RT_WAITING_FOREVER); 19 | } 20 | 21 | int platform_mutex_trylock(platform_mutex_t* m) 22 | { 23 | return rt_mutex_take((m->mutex), 0); 24 | } 25 | 26 | int platform_mutex_unlock(platform_mutex_t* m) 27 | { 28 | return rt_mutex_release((m->mutex)); 29 | } 30 | 31 | int platform_mutex_destroy(platform_mutex_t* m) 32 | { 33 | return rt_mutex_delete((m->mutex)); 34 | } 35 | -------------------------------------------------------------------------------- /platform/RT-Thread/platform_mutex.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 18:31:33 5 | * @LastEditTime : 2020-01-16 00:19:10 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _PLATFORM_MUTEX_H_ 9 | #define _PLATFORM_MUTEX_H_ 10 | 11 | #include 12 | 13 | typedef struct platform_mutex { 14 | rt_mutex_t mutex; 15 | } platform_mutex_t; 16 | 17 | int platform_mutex_init(platform_mutex_t* m); 18 | int platform_mutex_lock(platform_mutex_t* m); 19 | int platform_mutex_trylock(platform_mutex_t* m); 20 | int platform_mutex_unlock(platform_mutex_t* m); 21 | int platform_mutex_destroy(platform_mutex_t* m); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /platform/RT-Thread/platform_net_socket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-01-10 23:45:59 5 | * @LastEditTime: 2020-04-25 17:50:58 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include "platform_net_socket.h" 9 | 10 | int platform_net_socket_connect(const char *host, const char *port, int proto) 11 | { 12 | int fd, ret = KAWAII_MQTT_SOCKET_UNKNOWN_HOST_ERROR; 13 | struct addrinfo hints, *addr_list, *cur; 14 | 15 | /* Do name resolution with both IPv6 and IPv4 */ 16 | memset(&hints, 0, sizeof(hints)); 17 | hints.ai_family = AF_UNSPEC; 18 | hints.ai_socktype = (proto == PLATFORM_NET_PROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; 19 | hints.ai_protocol = (proto == PLATFORM_NET_PROTO_UDP) ? IPPROTO_UDP : IPPROTO_TCP; 20 | 21 | if (getaddrinfo(host, port, &hints, &addr_list) != 0) { 22 | return ret; 23 | } 24 | 25 | for (cur = addr_list; cur != NULL; cur = cur->ai_next) { 26 | fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol); 27 | if (fd < 0) { 28 | ret = KAWAII_MQTT_SOCKET_FAILED_ERROR; 29 | continue; 30 | } 31 | 32 | if (connect(fd, cur->ai_addr, cur->ai_addrlen) == 0) { 33 | ret = fd; 34 | break; 35 | } 36 | 37 | platform_net_socket_close(fd); 38 | ret = KAWAII_MQTT_CONNECT_FAILED_ERROR; 39 | } 40 | 41 | freeaddrinfo(addr_list); 42 | return ret; 43 | } 44 | 45 | int platform_net_socket_recv(int fd, void *buf, size_t len, int flags) 46 | { 47 | return recv(fd, buf, len, flags); 48 | } 49 | 50 | int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout) 51 | { 52 | int nread; 53 | int nleft = len; 54 | unsigned char *ptr; 55 | ptr = buf; 56 | 57 | struct timeval tv = { 58 | timeout / 1000, 59 | (timeout % 1000) * 1000 60 | }; 61 | 62 | if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec <= 0)) { 63 | tv.tv_sec = 0; 64 | tv.tv_usec = 100; 65 | } 66 | 67 | platform_net_socket_setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); 68 | 69 | while (nleft > 0) { 70 | nread = platform_net_socket_recv(fd, ptr, nleft, 0); 71 | if (nread < 0) { 72 | return -1; 73 | } else if (nread == 0) { 74 | break; 75 | } 76 | 77 | nleft -= nread; 78 | ptr += nread; 79 | } 80 | return len - nleft; 81 | } 82 | 83 | int platform_net_socket_write(int fd, void *buf, size_t len) 84 | { 85 | return send(fd, buf, len, 0); 86 | } 87 | 88 | int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout) 89 | { 90 | struct timeval tv = { 91 | timeout / 1000, 92 | (timeout % 1000) * 1000 93 | }; 94 | 95 | if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec <= 0)) { 96 | tv.tv_sec = 0; 97 | tv.tv_usec = 100; 98 | } 99 | 100 | setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval)); 101 | 102 | return send(fd, buf, len, 0); 103 | } 104 | 105 | int platform_net_socket_close(int fd) 106 | { 107 | return closesocket(fd); 108 | } 109 | 110 | int platform_net_socket_set_block(int fd) 111 | { 112 | unsigned long mode = 0; 113 | return ioctlsocket(fd, FIONBIO, &mode); 114 | } 115 | 116 | int platform_net_socket_set_nonblock(int fd) 117 | { 118 | unsigned long mode = 1; 119 | return ioctlsocket(fd, FIONBIO, &mode); 120 | } 121 | 122 | int platform_net_socket_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) 123 | { 124 | return setsockopt(fd, level, optname, optval, optlen); 125 | } 126 | 127 | -------------------------------------------------------------------------------- /platform/RT-Thread/platform_net_socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 13:39:00 5 | * @LastEditTime: 2020-04-25 10:17:08 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _PLATFORM_NET_SOCKET_H_ 9 | #define _PLATFORM_NET_SOCKET_H_ 10 | 11 | #include "network.h" 12 | #include "mqtt_error.h" 13 | 14 | #ifdef KAWAII_MQTT_NETSOCKET_USE_SAL 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #else 25 | 26 | #include "lwip/opt.h" 27 | #include "lwip/sys.h" 28 | #include "lwip/api.h" 29 | #include 30 | #include "lwip/netdb.h" 31 | 32 | #endif // KAWAII_MQTT_NETSOCKET_USE_SAL 33 | 34 | #define PLATFORM_NET_PROTO_TCP 0 /**< The TCP transport protocol */ 35 | #define PLATFORM_NET_PROTO_UDP 1 /**< The UDP transport protocol */ 36 | 37 | int platform_net_socket_connect(const char *host, const char *port, int proto); 38 | int platform_net_socket_recv(int fd, void *buf, size_t len, int flags); 39 | int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout); 40 | int platform_net_socket_write(int fd, void *buf, size_t len); 41 | int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout); 42 | int platform_net_socket_close(int fd); 43 | int platform_net_socket_set_block(int fd); 44 | int platform_net_socket_set_nonblock(int fd); 45 | int platform_net_socket_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen); 46 | 47 | #endif /* _PLATFORM_NET_SOCKET_H_ */ 48 | -------------------------------------------------------------------------------- /platform/RT-Thread/platform_thread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-23 19:26:27 5 | * @LastEditTime: 2020-04-25 08:44:24 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include "platform_thread.h" 9 | #include "platform_memory.h" 10 | 11 | platform_thread_t *platform_thread_init( const char *name, 12 | void (*entry)(void *), 13 | void * const param, 14 | unsigned int stack_size, 15 | unsigned int priority, 16 | unsigned int tick) 17 | { 18 | platform_thread_t *thread; 19 | 20 | thread = platform_memory_alloc(sizeof(platform_thread_t)); 21 | 22 | if(RT_NULL == thread) 23 | { 24 | return RT_NULL; 25 | } 26 | 27 | /*modify thread creation method is dynamic creation, so thread exit rtos can recylcle the resource!*/ 28 | thread->thread = rt_thread_create((const char *)name, 29 | entry, param, 30 | stack_size, priority, tick); 31 | 32 | if (thread->thread == RT_NULL) 33 | { 34 | return RT_NULL; 35 | } 36 | else 37 | { 38 | return thread; 39 | } 40 | 41 | } 42 | 43 | void platform_thread_startup(platform_thread_t* thread) 44 | { 45 | rt_thread_startup(thread->thread); 46 | } 47 | 48 | 49 | void platform_thread_stop(platform_thread_t* thread) 50 | { 51 | rt_thread_suspend(thread->thread); 52 | 53 | } 54 | 55 | void platform_thread_start(platform_thread_t* thread) 56 | { 57 | rt_thread_resume(thread->thread); 58 | } 59 | 60 | void platform_thread_destroy(platform_thread_t* thread) 61 | { 62 | platform_memory_free(thread); 63 | } 64 | 65 | 66 | -------------------------------------------------------------------------------- /platform/RT-Thread/platform_thread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 18:31:44 5 | * @LastEditTime : 2020-01-08 20:24:17 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _PLATFORM_THREAD_H_ 9 | #define _PLATFORM_THREAD_H_ 10 | 11 | #include 12 | 13 | typedef struct platform_thread { 14 | rt_thread_t thread; 15 | } platform_thread_t; 16 | 17 | platform_thread_t *platform_thread_init( const char *name, 18 | void (*entry)(void *), 19 | void * const param, 20 | unsigned int stack_size, 21 | unsigned int priority, 22 | unsigned int tick); 23 | void platform_thread_startup(platform_thread_t* thread); 24 | void platform_thread_stop(platform_thread_t* thread); 25 | void platform_thread_start(platform_thread_t* thread); 26 | void platform_thread_destroy(platform_thread_t* thread); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /platform/RT-Thread/platform_timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-10 22:16:41 5 | * @LastEditTime : 2020-01-11 01:19:35 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | 9 | #include "platform_timer.h" 10 | 11 | static uint32_t platform_uptime_ms(void) 12 | { 13 | #if (RT_TICK_PER_SECOND == 1000) 14 | return (uint32_t)rt_tick_get(); 15 | #else 16 | rt_tick_t tick = 0u; 17 | 18 | tick = rt_tick_get() * 1000; 19 | return (uint32_t)((tick + RT_TICK_PER_SECOND - 1) / RT_TICK_PER_SECOND); 20 | #endif 21 | } 22 | 23 | void platform_timer_init(platform_timer_t* timer) 24 | { 25 | timer->time = 0; 26 | } 27 | 28 | void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout) 29 | { 30 | timer->time = platform_uptime_ms(); 31 | timer->time += timeout; 32 | } 33 | 34 | char platform_timer_is_expired(platform_timer_t* timer) 35 | { 36 | return platform_uptime_ms() > timer->time ? 1 : 0; 37 | } 38 | 39 | int platform_timer_remain(platform_timer_t* timer) 40 | { 41 | uint32_t now; 42 | 43 | now = platform_uptime_ms(); 44 | if (timer->time <= now) { 45 | return 0; 46 | } 47 | 48 | return timer->time - now; 49 | } 50 | 51 | unsigned long platform_timer_now(void) 52 | { 53 | return (unsigned long) platform_uptime_ms(); 54 | } 55 | 56 | void platform_timer_usleep(unsigned long usec) 57 | { 58 | uint32_t ms = 0; 59 | if(usec != 0) { 60 | ms = usec / 1000; 61 | if (ms == 0) { 62 | ms = 1; 63 | } 64 | } 65 | rt_thread_mdelay(ms); 66 | } 67 | -------------------------------------------------------------------------------- /platform/RT-Thread/platform_timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-10 22:18:32 5 | * @LastEditTime : 2020-01-11 01:20:44 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #ifndef _PLATFORM_TIMER_H_ 9 | #define _PLATFORM_TIMER_H_ 10 | 11 | #include 12 | 13 | typedef struct platform_timer { 14 | uint32_t time; 15 | } platform_timer_t; 16 | 17 | void platform_timer_init(platform_timer_t* timer); 18 | void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout); 19 | char platform_timer_is_expired(platform_timer_t* timer); 20 | int platform_timer_remain(platform_timer_t* timer); 21 | unsigned long platform_timer_now(void); 22 | void platform_timer_usleep(unsigned long usec); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /png/mqtt-tool.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiejieTop/kawaii-mqtt/99543a03586d52cab111d864a0e3cfb7456b8fa0/png/mqtt-tool.png -------------------------------------------------------------------------------- /png/mqttclient.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiejieTop/kawaii-mqtt/99543a03586d52cab111d864a0e3cfb7456b8fa0/png/mqttclient.png -------------------------------------------------------------------------------- /test/test.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * @Author: jiejie 4 | * @Github: https://github.com/jiejieTop 5 | * @LastEditTime: 2020-06-17 14:35:29 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "mqttclient.h" 14 | 15 | #ifndef KAWAII_MQTT_HOST 16 | #define KAWAII_MQTT_HOST "jiejie01.top" 17 | #endif 18 | #ifndef KAWAII_MQTT_PORT 19 | #define KAWAII_MQTT_PORT "1883" 20 | #endif 21 | #ifndef KAWAII_MQTT_CLIENTID 22 | #define KAWAII_MQTT_CLIENTID "rtthread001" 23 | #endif 24 | #ifndef KAWAII_MQTT_USERNAME 25 | #define KAWAII_MQTT_USERNAME "rt-thread" 26 | #endif 27 | #ifndef KAWAII_MQTT_PASSWORD 28 | #define KAWAII_MQTT_PASSWORD "rt-thread" 29 | #endif 30 | #ifndef KAWAII_MQTT_SUBTOPIC 31 | #define KAWAII_MQTT_SUBTOPIC "rtt-sub" 32 | #endif 33 | #ifndef KAWAII_MQTT_PUBTOPIC 34 | #define KAWAII_MQTT_PUBTOPIC "rtt-pub" 35 | #endif 36 | 37 | static void sub_topic_handle1(void* client, message_data_t* msg) 38 | { 39 | (void) client; 40 | KAWAII_MQTT_LOG_I("-----------------------------------------------------------------------------------"); 41 | KAWAII_MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char*)msg->message->payload); 42 | KAWAII_MQTT_LOG_I("-----------------------------------------------------------------------------------"); 43 | } 44 | 45 | 46 | static int mqtt_publish_handle1(mqtt_client_t *client) 47 | { 48 | mqtt_message_t msg; 49 | memset(&msg, 0, sizeof(msg)); 50 | 51 | msg.qos = QOS0; 52 | msg.payload = (void *)"this is a kawaii mqtt test ..."; 53 | 54 | return mqtt_publish(client, KAWAII_MQTT_PUBTOPIC, &msg); 55 | } 56 | 57 | static void kawaii_mqtt_demo(void *parameter) 58 | { 59 | mqtt_client_t *client = NULL; 60 | 61 | rt_thread_delay(6000); 62 | 63 | mqtt_log_init(); 64 | 65 | client = mqtt_lease(); 66 | 67 | mqtt_set_host(client, KAWAII_MQTT_HOST); 68 | mqtt_set_port(client, KAWAII_MQTT_PORT); 69 | mqtt_set_user_name(client, KAWAII_MQTT_USERNAME); 70 | mqtt_set_password(client, KAWAII_MQTT_PASSWORD); 71 | mqtt_set_client_id(client, KAWAII_MQTT_CLIENTID); 72 | mqtt_set_clean_session(client, 1); 73 | 74 | KAWAII_MQTT_LOG_I("The ID of the Kawaii client is: %s ", KAWAII_MQTT_CLIENTID); 75 | 76 | mqtt_connect(client); 77 | 78 | mqtt_subscribe(client, KAWAII_MQTT_SUBTOPIC, QOS0, sub_topic_handle1); 79 | 80 | while (1) { 81 | mqtt_publish_handle1(client); 82 | 83 | mqtt_sleep_ms(4 * 1000); 84 | } 85 | } 86 | 87 | int ka_mqtt(void) 88 | { 89 | rt_thread_t tid_mqtt; 90 | 91 | tid_mqtt = rt_thread_create("kawaii_demo", kawaii_mqtt_demo, RT_NULL, 2048, 17, 10); 92 | if (tid_mqtt == RT_NULL) { 93 | return -RT_ERROR; 94 | } 95 | 96 | rt_thread_startup(tid_mqtt); 97 | 98 | return RT_EOK; 99 | } 100 | MSH_CMD_EXPORT(ka_mqtt, Kawaii MQTT client test program); 101 | -------------------------------------------------------------------------------- /test/test_ca.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-01-12 10:51:11 5 | * @LastEditTime: 2020-04-25 09:21:55 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | static const char *test_ca_crt = { 9 | "-----BEGIN CERTIFICATE-----\r\n" 10 | "MIIFrTCCBJWgAwIBAgIQDApSGhCHMtFicAaXgcO0vjANBgkqhkiG9w0BAQsFADBy\r\n" 11 | "MQswCQYDVQQGEwJDTjElMCMGA1UEChMcVHJ1c3RBc2lhIFRlY2hub2xvZ2llcywg\r\n" 12 | "SW5jLjEdMBsGA1UECxMURG9tYWluIFZhbGlkYXRlZCBTU0wxHTAbBgNVBAMTFFRy\r\n" 13 | "dXN0QXNpYSBUTFMgUlNBIENBMB4XDTIwMDEyOTAwMDAwMFoXDTIxMDQyODEyMDAw\r\n" 14 | "MFowGzEZMBcGA1UEAxMQd3d3LmppZWppZTAxLnRvcDCCASIwDQYJKoZIhvcNAQEB\r\n" 15 | "BQADggEPADCCAQoCggEBAKIDMo45w085LaCMG1LWY5b8V94zDqdt+weVhKolgsLZ\r\n" 16 | "htAQTDrafBx1sNJtOpa8ADeQkFbWOTEy3tgViOBvBr+8Qhl6vYsESJrg7DXeCVRm\r\n" 17 | "04pk+cFrdWYRE70AUz8RXRuaWLv1Fu2L+qlymrnZB/WBJFnETINh6yzqY8FNETUV\r\n" 18 | "EL08eE0LoXt/4b7iAJYEFRYTyBKjLpkr04e92SQIuL/l42j92lYNOjYfYOlpWZUC\r\n" 19 | "cp4WHXEDd1YXdTMmXpfsU3VlYS3RTusOFsgXpLFET9xGRvtDSu5qw6rPGf/y/PhT\r\n" 20 | "1LFB/xlL2E6Rpo/6VWuQ8A5rA+H3D1I/fIBB97orMYUCAwEAAaOCApQwggKQMB8G\r\n" 21 | "A1UdIwQYMBaAFH/TmfOgRw4xAFZWIo63zJ7dygGKMB0GA1UdDgQWBBQyzyOTN1l5\r\n" 22 | "Rg1Ih1tQ0TPYMqfw+jApBgNVHREEIjAgghB3d3cuamllamllMDEudG9wggxqaWVq\r\n" 23 | "aWUwMS50b3AwDgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggr\r\n" 24 | "BgEFBQcDAjBMBgNVHSAERTBDMDcGCWCGSAGG/WwBAjAqMCgGCCsGAQUFBwIBFhxo\r\n" 25 | "dHRwczovL3d3dy5kaWdpY2VydC5jb20vQ1BTMAgGBmeBDAECATCBkgYIKwYBBQUH\r\n" 26 | "AQEEgYUwgYIwNAYIKwYBBQUHMAGGKGh0dHA6Ly9zdGF0dXNlLmRpZ2l0YWxjZXJ0\r\n" 27 | "dmFsaWRhdGlvbi5jb20wSgYIKwYBBQUHMAKGPmh0dHA6Ly9jYWNlcnRzLmRpZ2l0\r\n" 28 | "YWxjZXJ0dmFsaWRhdGlvbi5jb20vVHJ1c3RBc2lhVExTUlNBQ0EuY3J0MAkGA1Ud\r\n" 29 | "EwQCMAAwggEEBgorBgEEAdZ5AgQCBIH1BIHyAPAAdQB9PvL4j/+IVWgkwsDKnlKJ\r\n" 30 | "eSvFDngJfy5ql2iZfiLw1wAAAW/wKSTgAAAEAwBGMEQCIGFq0FdvZfXf4lV20Am1\r\n" 31 | "HRP6F7wxzkesK0r1566sNqvxAiBp5W3iTLWEgeJa/PfH5hX/d+K5CIyXScLa4qqa\r\n" 32 | "MCLHwwB3AFzcQ5L+5qtFRLFemtRW5hA3+9X6R9yhc5SyXub2xw7KAAABb/ApJJIA\r\n" 33 | "AAQDAEgwRgIhANEsjShyRf0GGpwJ6ZTQKBHo933rlSpaNIvor7cG8RBQAiEAkeDf\r\n" 34 | "7+n+zyEGZUMOYI0E0R2chjPBJGvtw1yD12sxekowDQYJKoZIhvcNAQELBQADggEB\r\n" 35 | "AIzaZ5X1So+xVe2JWkMfmJA8IQhdp9WghCDLRORcIggcY9BtYxFSBdusxIa1bhdt\r\n" 36 | "rCY6RoepCwTrhV9PotwMgZtSOu8szHHRlqX8zNUhIh628yzPSTDDZ4xgeJvlGAkT\r\n" 37 | "Zlv0XrezkDLRZkKN9R6KX1ccaPNbn9PD6SMtpHPbE7UEZYfrV3wAJhFzsyhp2JF4\r\n" 38 | "KLLNPaeDgDM3Lu6tUm/bznDEyxi7/ZoR+7fSQAMF5Jo1ysKUAOC00I9Ne+7eSbTh\r\n" 39 | "flV//8NBN+Z2ShCV0uFedf6ugDUMOuOUCtp0c7N+sM1IVE5MOhLRDAGRIUyi7/43\r\n" 40 | "dJ0Okust1fXo4UTDGJtyp30=\r\n" 41 | "-----END CERTIFICATE-----\r\n" 42 | "-----BEGIN CERTIFICATE-----\r\n" 43 | "MIIErjCCA5agAwIBAgIQBYAmfwbylVM0jhwYWl7uLjANBgkqhkiG9w0BAQsFADBh\r\n" 44 | "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\r\n" 45 | "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\r\n" 46 | "QTAeFw0xNzEyMDgxMjI4MjZaFw0yNzEyMDgxMjI4MjZaMHIxCzAJBgNVBAYTAkNO\r\n" 47 | "MSUwIwYDVQQKExxUcnVzdEFzaWEgVGVjaG5vbG9naWVzLCBJbmMuMR0wGwYDVQQL\r\n" 48 | "ExREb21haW4gVmFsaWRhdGVkIFNTTDEdMBsGA1UEAxMUVHJ1c3RBc2lhIFRMUyBS\r\n" 49 | "U0EgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCgWa9X+ph+wAm8\r\n" 50 | "Yh1Fk1MjKbQ5QwBOOKVaZR/OfCh+F6f93u7vZHGcUU/lvVGgUQnbzJhR1UV2epJa\r\n" 51 | "e+m7cxnXIKdD0/VS9btAgwJszGFvwoqXeaCqFoP71wPmXjjUwLT70+qvX4hdyYfO\r\n" 52 | "JcjeTz5QKtg8zQwxaK9x4JT9CoOmoVdVhEBAiD3DwR5fFgOHDwwGxdJWVBvktnoA\r\n" 53 | "zjdTLXDdbSVC5jZ0u8oq9BiTDv7jAlsB5F8aZgvSZDOQeFrwaOTbKWSEInEhnchK\r\n" 54 | "ZTD1dz6aBlk1xGEI5PZWAnVAba/ofH33ktymaTDsE6xRDnW97pDkimCRak6CEbfe\r\n" 55 | "3dXw6OV5AgMBAAGjggFPMIIBSzAdBgNVHQ4EFgQUf9OZ86BHDjEAVlYijrfMnt3K\r\n" 56 | "AYowHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUwDgYDVR0PAQH/BAQD\r\n" 57 | "AgGGMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjASBgNVHRMBAf8ECDAG\r\n" 58 | "AQH/AgEAMDQGCCsGAQUFBwEBBCgwJjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3Au\r\n" 59 | "ZGlnaWNlcnQuY29tMEIGA1UdHwQ7MDkwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2lj\r\n" 60 | "ZXJ0LmNvbS9EaWdpQ2VydEdsb2JhbFJvb3RDQS5jcmwwTAYDVR0gBEUwQzA3Bglg\r\n" 61 | "hkgBhv1sAQIwKjAoBggrBgEFBQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQuY29t\r\n" 62 | "L0NQUzAIBgZngQwBAgEwDQYJKoZIhvcNAQELBQADggEBAK3dVOj5dlv4MzK2i233\r\n" 63 | "lDYvyJ3slFY2X2HKTYGte8nbK6i5/fsDImMYihAkp6VaNY/en8WZ5qcrQPVLuJrJ\r\n" 64 | "DSXT04NnMeZOQDUoj/NHAmdfCBB/h1bZ5OGK6Sf1h5Yx/5wR4f3TUoPgGlnU7EuP\r\n" 65 | "ISLNdMRiDrXntcImDAiRvkh5GJuH4YCVE6XEntqaNIgGkRwxKSgnU3Id3iuFbW9F\r\n" 66 | "UQ9Qqtb1GX91AJ7i4153TikGgYCdwYkBURD8gSVe8OAco6IfZOYt/TEwii1Ivi1C\r\n" 67 | "qnuUlWpsF1LdQNIdfbW3TSe0BhQa7ifbVIfvPWHYOu3rkg1ZeMo6XRU9B4n5VyJY\r\n" 68 | "RmE=\r\n" 69 | "-----END CERTIFICATE-----" 70 | }; 71 | 72 | const char *test_ca_get() 73 | { 74 | return test_ca_crt; 75 | } 76 | --------------------------------------------------------------------------------