├── .github └── workflows │ ├── codeql-buildscript.sh │ ├── codeql.yml │ └── fail_on_error.py ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── README_CN.md ├── build.sh ├── build_deb.sh ├── cmake ├── color.cmake ├── config.cmake.in ├── install.cmake └── module.cmake ├── common ├── CMakeLists.txt ├── log │ ├── CMakeLists.txt │ ├── arch │ │ ├── CMakeLists.txt │ │ ├── freertos │ │ │ └── arch.c │ │ ├── linux │ │ │ ├── CMakeLists.txt │ │ │ └── arch.c │ │ ├── rt-thread │ │ │ └── arch.c │ │ └── tencentos-tiny │ │ │ └── arch.c │ ├── build.sh │ ├── fifo.c │ ├── fifo.h │ ├── format.c │ ├── format.h │ ├── salof.c │ ├── salof.h │ ├── salof_config.h │ └── salof_defconfig.h ├── mqtt_defconfig.h ├── mqtt_error.h ├── mqtt_list.c ├── mqtt_list.h ├── mqtt_log.h ├── random.c └── random.h ├── config └── mqtt_config.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 ├── example ├── CMakeLists.txt ├── ali │ ├── CMakeLists.txt │ ├── ali.c │ └── mqtt_config.h ├── baidu │ ├── CMakeLists.txt │ ├── baidu.c │ └── mqtt_config.h ├── emqx │ ├── CMakeLists.txt │ ├── ca.inc │ ├── emqx.c │ └── mqtt_config.h ├── mqttclient.cmake └── onenet │ ├── CMakeLists.txt │ ├── mqtt_config.h │ └── onenet.c ├── make-libmqttclient.sh ├── make_deb.sh ├── 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 ├── 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 ├── CMakeLists.txt ├── FreeRTOS │ ├── 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 ├── 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 ├── TencentOS-tiny │ ├── 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 │ └── sal │ │ ├── README.md │ │ ├── platform_net_socket.c │ │ └── platform_net_socket.h └── linux │ ├── CMakeLists.txt │ ├── 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 /.github/workflows/codeql-buildscript.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ./build.sh 4 | -------------------------------------------------------------------------------- /.github/workflows/fail_on_error.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import json 4 | import sys 5 | 6 | # Return whether SARIF file contains error-level results 7 | def codeql_sarif_contain_error(filename): 8 | with open(filename, 'r') as f: 9 | s = json.load(f) 10 | 11 | for run in s.get('runs', []): 12 | rules_metadata = run['tool']['driver']['rules'] 13 | if not rules_metadata: 14 | rules_metadata = run['tool']['extensions'][0]['rules'] 15 | 16 | for res in run.get('results', []): 17 | if 'ruleIndex' in res: 18 | rule_index = res['ruleIndex'] 19 | elif 'rule' in res and 'index' in res['rule']: 20 | rule_index = res['rule']['index'] 21 | else: 22 | continue 23 | try: 24 | rule_level = rules_metadata[rule_index]['defaultConfiguration']['level'] 25 | except IndexError as e: 26 | print(e, rule_index, len(rules_metadata)) 27 | else: 28 | if rule_level == 'error': 29 | return True 30 | return False 31 | 32 | if __name__ == "__main__": 33 | if codeql_sarif_contain_error(sys.argv[1]): 34 | sys.exit(1) 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | build/ 3 | libmqttclient/ 4 | mqtt-client/ 5 | *.deb 6 | *.bak 7 | *.zip 8 | *.tar 9 | *.db 10 | ~/ -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(mqtt-client) 4 | 5 | set(TARGETS "mqtt-client") 6 | set(SUBDIRS "common" "mqtt" "platform" "network" "mqttclient") 7 | 8 | # 库文件前缀 9 | set(LIBRARY_PREFIX "mc") 10 | 11 | # 编译debug版本 12 | set(CMAKE_BUILD_TYPE "Debug") 13 | 14 | # 设置版本信息 15 | set(PROJECT_MAJOR_VERSION 1) # 架版本信息 16 | set(PROJECT_MINOR_VERSION 2) # 主版本信息 17 | set(PROJECT_PATCH_VERSION 0) # 次版本信息 18 | set(PROJECT_VERSION ${PROJECT_MAJOR_VERSION}.${PROJECT_MINOR_VERSION}.${PROJECT_PATCH_VERSION}) 19 | 20 | set(PROJECT_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 21 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_ROOT_DIR}/build/bin) #设置可执行文件的输出目录 22 | set(LIBRARY_OUTPUT_PATH ${PROJECT_ROOT_DIR}/build/lib) #设置库文件的输出目录 23 | link_directories(${LIBRARY_OUTPUT_PATH}) 24 | 25 | 26 | set(CMAKE_C_FLAGS "-Wall") 27 | set(CMAKE_C_FLAGS "-lpthread") 28 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O0 -g -no-pie -ggdb") 29 | set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3 -DNDEBUG") 30 | 31 | if(CMAKE_COMPILER_IS_GNUCXX) 32 | set(CMAKE_CXX_FLAGS "-std=c++11") 33 | set(CMAKE_CXX_FLAGS "-lpthread") 34 | set(CMAKE_CXX_FLAGS "-Wall") 35 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -g -no-pie -ggdb") 36 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3") 37 | endif(CMAKE_COMPILER_IS_GNUCXX) 38 | 39 | 40 | ######################################### 41 | ## load cmake config and load 3rdparty ## 42 | ######################################### 43 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/color.cmake) 44 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/module.cmake) 45 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/install.cmake) 46 | 47 | # foreach(incdir ${INCDIRS}) 48 | # include_directories(${incdir}) 49 | # endforeach() 50 | 51 | include_directories(${PROJECT_ROOT_DIR}/config) 52 | 53 | foreach(subdir ${SUBDIRS}) 54 | add_subdirectory(${PROJECT_ROOT_DIR}/${subdir}) 55 | endforeach() 56 | 57 | 58 | get_module_info() 59 | show_module_info() 60 | install_project() 61 | 62 | option(BUILD_EXAMPLES "Build examples" ON) 63 | 64 | if(BUILD_EXAMPLES) 65 | add_subdirectory(example) 66 | endif() 67 | -------------------------------------------------------------------------------- /build_deb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # info 4 | author="jiejie" 5 | email="jiejietop@gmail.com" 6 | github="https://github.com/jiejieTop" 7 | description="is a communication library" 8 | 9 | # variable 10 | package_dir="" 11 | package_name="" 12 | info_name="" 13 | arch="x86" 14 | system="all" 15 | major_version="1" 16 | minor_version="2.0" 17 | version="$major_version.$minor_version" 18 | 19 | current_pwd=$( 20 | cd "$(dirname "$0")" 21 | pwd 22 | ) 23 | 24 | fun_do_generate_depend() { 25 | date=$(date) 26 | changelog_file="author : ${author} 27 | VERSION : ${version} 28 | DATE: ${date} 29 | " 30 | 31 | copyright_file=" 32 | ****************************************************************** 33 | * @attention 34 | * 35 | * github : ${github} 36 | * author : ${author} 37 | * 38 | * * ****************************************************************** 39 | " 40 | 41 | postinst_file="#!/bin/sh 42 | sudo ldconfig 43 | echo '******************************************************************' 44 | echo 'welcome to use ${info_name} v${version}!' 45 | echo '******************************************************************' 46 | " 47 | 48 | control_file="Source: ${author} 49 | Package: ${info_name} 50 | Version: ${version} 51 | Section: develop / communication 52 | Priority: optional 53 | Architecture: all 54 | Depends : 55 | Maintainer: ${author}[${email}] 56 | Description: ${description}, git hash : ${git_hash} 57 | " 58 | 59 | echo "$control_file" >$package_dir/DEBIAN/control 60 | echo "$changelog_file" >$package_dir/DEBIAN/changelog 61 | echo "$copyright_file" >$package_dir/DEBIAN/copyright 62 | echo "$postinst_file" >$package_dir/DEBIAN/postinst 63 | } 64 | 65 | fun_do_get_system_id() 66 | { 67 | source /etc/os-release 68 | system="$ID-$VERSION_ID" 69 | } 70 | 71 | fun_do_generate_arch() { 72 | hostnamectl | grep -i "Architecture: x86" 73 | if [ $? -ne 0 ]; then 74 | hostnamectl | grep -i "Architecture: arm" 75 | if [ $? -ne 0 ]; then 76 | exit 1 77 | fi 78 | arch="arm" 79 | else 80 | arch="x86" 81 | fi 82 | } 83 | 84 | fun_do_config() { 85 | if [ " $1" == " " ]; then 86 | echo "the construction path of the deb package must be specified" 87 | echo "$0 [path]" 88 | exit -1 89 | fi 90 | 91 | package_dir=$1 92 | 93 | if [ " $2" == " " ]; then 94 | # take the name of the deb package according to the path 95 | package_name=${package_dir##*/}.deb 96 | else 97 | package_name=$2 98 | fi 99 | 100 | info_name=${package_name%.*} 101 | 102 | if [ ! -d "${package_dir}/DEBIAN" ]; then 103 | mkdir -p ${package_dir}/DEBIAN 104 | fun_do_generate_depend 105 | echo "please create a simulated root directory in ${package_dir}, and put the content in this directory" 106 | exit -1 107 | fi 108 | 109 | echo "build $package_name in $package_dir" 110 | 111 | } 112 | 113 | fun_do_make_deb() { 114 | package_name="${package_name%.*}-$system-$arch-$version.${package_name##*.}" 115 | sudo chmod 775 $package_dir/DEBIAN/postinst 116 | dpkg -b $package_dir $package_name 117 | } 118 | 119 | main() { 120 | cd $current_pwd 121 | fun_do_config $1 $2 122 | fun_do_generate_arch 123 | fun_do_get_system_id 124 | fun_do_generate_depend 125 | fun_do_make_deb 126 | cd - 127 | } 128 | 129 | main "$@" 130 | -------------------------------------------------------------------------------- /cmake/color.cmake: -------------------------------------------------------------------------------- 1 | if(NOT WIN32) 2 | string(ASCII 27 ESC) 3 | set(COLOUR_RESET "${ESC}[m") 4 | set(COLOUR_BOLD "${ESC}[1m") 5 | set(RED "${ESC}[31m") 6 | set(GREEN "${ESC}[32m") 7 | set(YELLOW "${ESC}[33m") 8 | set(BLUE "${ESC}[34m") 9 | set(MAGENTA "${ESC}[35m") 10 | set(CYAN "${ESC}[36m") 11 | set(WHITE "${ESC}[37m") 12 | set(BOLD_RED "${ESC}[1;31m") 13 | set(BOLD_GREEN "${ESC}[1;32m") 14 | set(BOLD_YELLOW "${ESC}[1;33m") 15 | set(BOLD_BLUE "${ESC}[1;34m") 16 | set(BOLD_MAGENTA "${ESC}[1;35m") 17 | set(BOLD_CYAN "${ESC}[1;36m") 18 | set(BOLD_WHITE "${ESC}[1;37m") 19 | endif() 20 | 21 | function(message) 22 | list(GET ARGV 0 MESSAGE_TYPE) 23 | if(MESSAGE_TYPE STREQUAL FATAL_ERROR OR MESSAGE_TYPE STREQUAL SEND_ERROR) 24 | list(REMOVE_AT ARGV 0) 25 | _message(${MESSAGE_TYPE} "${BOLD_RED}${ARGV}${COLOUR_RESET}") 26 | elseif(MESSAGE_TYPE STREQUAL WARNING) 27 | list(REMOVE_AT ARGV 0) 28 | _message(${MESSAGE_TYPE} 29 | "${BOLD_YELLOW}${ARGV}${COLOUR_RESET}") 30 | elseif(MESSAGE_TYPE STREQUAL AUTHOR_WARNING) 31 | list(REMOVE_AT ARGV 0) 32 | _message(${MESSAGE_TYPE} "${BOLD_CYAN}${ARGV}${COLOUR_RESET}") 33 | elseif(MESSAGE_TYPE STREQUAL STATUS) 34 | list(REMOVE_AT ARGV 0) 35 | _message(${MESSAGE_TYPE} "${GREEN}${ARGV}${COLOUR_RESET}") 36 | elseif(MESSAGE_TYPE STREQUAL INFO) 37 | list(REMOVE_AT ARGV 0) 38 | _message("${BLUE}${ARGV}${COLOUR_RESET}") 39 | else() 40 | _message("${ARGV}") 41 | endif() 42 | ENDFUNCTION() 43 | 44 | -------------------------------------------------------------------------------- /cmake/config.cmake.in: -------------------------------------------------------------------------------- 1 | # - Config file for the Project package 2 | # It defines the following variables 3 | # ${PROJECT_NAME}_INCLUDE_DIRS - include directories for Project 4 | # ${PROJECT_NAME}_LIBRARIES - libraries to link against 5 | # ${PROJECT_NAME}_EXECUTABLE - the bar executable 6 | 7 | set(${PROJECT_NAME}_INCLUDE_DIRS ${${PROJECT_NAME}_INCLUDE_DIRS_INSTALL}) 8 | set(${PROJECT_NAME}_LINK_DIRS ${${PROJECT_NAME}_LINK_DIRS_INSTALL}) 9 | set(${PROJECT_NAME}_LIBRARIES ${${PROJECT_NAME}_LIBRARIES_INSTALL}) 10 | set(${PROJECT_NAME}_EXECUTABLE ${${PROJECT_NAME}_EXECUTABLE_INSTALL}) 11 | 12 | -------------------------------------------------------------------------------- /cmake/install.cmake: -------------------------------------------------------------------------------- 1 | 2 | macro(install_project) 3 | get_module_info() 4 | foreach(dir ${MODULE_INCLUDE_DIRS}) 5 | string(REGEX REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" dir ${dir}) 6 | set(install_dir ${CMAKE_INSTALL_PREFIX}/include/${dir}) 7 | string(REGEX REPLACE "//" ";/" install_dir ${install_dir}) 8 | set(${PROJECT_NAME}_INCLUDE_DIRS_INSTALL ${${PROJECT_NAME}_INCLUDE_DIRS_INSTALL};\n${install_dir}) 9 | endforeach() 10 | 11 | set(${PROJECT_NAME}_LINK_DIRS_INSTALL ${CMAKE_INSTALL_PREFIX}/lib) 12 | set(${PROJECT_NAME}_LIBRARIES_INSTALL ${MODULE_NAME}) 13 | set(${PROJECT_NAME}_EXECUTABLE_INSTALL ${PROJECT_NAME}) 14 | 15 | configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.cmake.in 16 | ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake) 17 | 18 | install(FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake 19 | DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake) 20 | endmacro() 21 | 22 | -------------------------------------------------------------------------------- /cmake/module.cmake: -------------------------------------------------------------------------------- 1 | set(MODULE_NAME "") 2 | set(STATIC_MODULE_NAME "") 3 | set(MODULE_INCLUDE_DIRS "") 4 | 5 | # describe property 6 | define_property(GLOBAL PROPERTY MODULE_NAME_LIST BRIEF_DOCS "MODULE_NAME_LIST" FULL_DOCS "module name list") 7 | define_property(GLOBAL PROPERTY MODULE_INCLUDE_DIRS_LIST BRIEF_DOCS "MODULE_INCLUDE_DIRS_LIST" FULL_DOCS "module include directory list") 8 | 9 | # initialize property 10 | set_property(GLOBAL PROPERTY MODULE_NAME_LIST "") 11 | set_property(GLOBAL PROPERTY MODULE_INCLUDE_DIRS_LIST "") 12 | 13 | 14 | # macro for add values into the list 15 | macro(set_module_info module var) 16 | set_property(GLOBAL APPEND PROPERTY MODULE_NAME_LIST ${module}) 17 | set_property(GLOBAL APPEND PROPERTY MODULE_INCLUDE_DIRS_LIST ${var}) 18 | endmacro(set_module_info) 19 | 20 | macro(set_header_info var) 21 | set_property(GLOBAL APPEND PROPERTY MODULE_INCLUDE_DIRS_LIST ${var}) 22 | endmacro(set_header_info) 23 | 24 | macro(get_module_info) 25 | get_property(TMP_MODULE_NAME GLOBAL PROPERTY MODULE_NAME_LIST) 26 | get_property(TMP_INC_DIR GLOBAL PROPERTY MODULE_INCLUDE_DIRS_LIST) 27 | set(MODULE_NAME ${TMP_MODULE_NAME}) 28 | set(MODULE_INCLUDE_DIRS ${TMP_INC_DIR}) 29 | foreach(module ${MODULE_NAME}) 30 | # 将 *.so 替换为 *.a 31 | string(REGEX REPLACE ".so" ".a" module ${module}) 32 | set(STATIC_MODULE_NAME "${STATIC_MODULE_NAME};lib${module}") 33 | endforeach() 34 | 35 | include_directories(${MODULE_INCLUDE_DIRS}) 36 | endmacro(get_module_info) 37 | 38 | macro(show_module_info) 39 | message(INFO "module :") 40 | foreach(module ${MODULE_NAME}) 41 | message(STATUS ${module}) 42 | endforeach() 43 | 44 | message(INFO "static module :") 45 | foreach(module ${STATIC_MODULE_NAME}) 46 | message(STATUS ${module}) 47 | endforeach() 48 | 49 | message(INFO "module include directories is :") 50 | foreach(include_dir ${MODULE_INCLUDE_DIRS}) 51 | message(STATUS ${include_dir}) 52 | endforeach() 53 | endmacro(show_module_info) 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /common/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 2 | 3 | set(CURRENT_LIB_NAME ${LIBRARY_PREFIX}-${CURRENT_LIB_NAME}) 4 | 5 | set(SUBDIRS "log") 6 | 7 | foreach(subdir ${SUBDIRS}) 8 | add_subdirectory(${subdir}) 9 | endforeach() 10 | 11 | 12 | set(DEPEND_LIBRARY_INTERNAL "log") 13 | 14 | # 指定头文件搜索目录 15 | set(${CURRENT_LIB_NAME}_INC_DIRS 16 | ${CMAKE_CURRENT_SOURCE_DIR} 17 | ) 18 | 19 | # 源文件目录 20 | set(${CURRENT_LIB_NAME}_SRC_DIRS 21 | ${CMAKE_CURRENT_SOURCE_DIR} 22 | ) 23 | 24 | set_module_info(${CURRENT_LIB_NAME} "${${CURRENT_LIB_NAME}_INC_DIRS}") 25 | get_module_info() 26 | 27 | 28 | foreach(FOREACH_SRC_DIR ${${CURRENT_LIB_NAME}_SRC_DIRS}) 29 | aux_source_directory(${FOREACH_SRC_DIR} ${CURRENT_LIB_NAME}_SOURCES) 30 | endforeach() 31 | 32 | foreach(lib ${DEPEND_LIBRARY_INTERNAL}) 33 | list(APPEND DEPEND_LIBRARY ${LIBRARY_PREFIX}-${lib}) 34 | list(APPEND DEPEND_LIBRARY_STATIC lib${LIBRARY_PREFIX}-${lib}.a) 35 | endforeach() 36 | 37 | 38 | # 生成动态库 39 | add_library(${CURRENT_LIB_NAME} SHARED ${${CURRENT_LIB_NAME}_SOURCES}) 40 | target_link_libraries(${CURRENT_LIB_NAME} ${DEPEND_LIBRARY}) 41 | # 生成静态库 42 | add_library(${CURRENT_LIB_NAME}_static STATIC ${${CURRENT_LIB_NAME}_SOURCES}) 43 | target_link_libraries(${CURRENT_LIB_NAME}_static ${DEPEND_LIBRARY_STATIC}) 44 | 45 | # 进行必要的配置,以生成动态库和静态库 46 | # 指定静态库的输出名称 47 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES OUTPUT_NAME ${CURRENT_LIB_NAME}) 48 | # 使动态库和静态库同时存在 49 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1) 50 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 51 | # 指定动态库版本 52 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) 53 | 54 | # 打包相关 install package 55 | install(TARGETS ${CURRENT_LIB_NAME} ${CURRENT_LIB_NAME}_static 56 | EXPORT ${CURRENT_LIB_NAME}-targets 57 | RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin 58 | LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 59 | ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 60 | PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include 61 | ) 62 | 63 | install(DIRECTORY ${${CURRENT_LIB_NAME}_INC_DIRS} 64 | DESTINATION "${CMAKE_INSTALL_PREFIX}/include" 65 | FILES_MATCHING 66 | PATTERN "*.h" 67 | PATTERN "*.hpp" 68 | PATTERN "CMakeLists.txt" EXCLUDE) 69 | -------------------------------------------------------------------------------- /common/log/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 2 | 3 | set(CURRENT_LIB_NAME ${LIBRARY_PREFIX}-${CURRENT_LIB_NAME}) 4 | 5 | # 指定头文件搜索目录 6 | set(${CURRENT_LIB_NAME}_INC_DIRS 7 | ${CMAKE_CURRENT_SOURCE_DIR}/ 8 | ) 9 | 10 | # 源文件目录 11 | set(${CURRENT_LIB_NAME}_SRC_DIRS 12 | ${CMAKE_CURRENT_SOURCE_DIR}/ 13 | ${CMAKE_CURRENT_SOURCE_DIR}/arch/linux 14 | ) 15 | 16 | set_module_info(${CURRENT_LIB_NAME} "${${CURRENT_LIB_NAME}_INC_DIRS}") 17 | get_module_info() 18 | 19 | foreach(FOREACH_SRC_DIR ${${CURRENT_LIB_NAME}_SRC_DIRS}) 20 | aux_source_directory(${FOREACH_SRC_DIR} ${CURRENT_LIB_NAME}_SOURCES) 21 | endforeach() 22 | 23 | 24 | # 生成动态库 25 | add_library(${CURRENT_LIB_NAME} SHARED ${${CURRENT_LIB_NAME}_SOURCES}) 26 | target_link_libraries(${CURRENT_LIB_NAME} ${CMAKE_THREAD_LIBS_INIT}) 27 | # 生成静态库 28 | add_library(${CURRENT_LIB_NAME}_static STATIC ${${CURRENT_LIB_NAME}_SOURCES}) 29 | target_link_libraries(${CURRENT_LIB_NAME}_static ${CMAKE_THREAD_LIBS_INIT}) 30 | 31 | # 进行必要的配置,以生成动态库和静态库 32 | # 指定静态库的输出名称 33 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES OUTPUT_NAME ${CURRENT_LIB_NAME}) 34 | # 使动态库和静态库同时存在 35 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1) 36 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 37 | # 指定动态库版本 38 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) 39 | 40 | # 打包相关 install package 41 | install(TARGETS ${CURRENT_LIB_NAME} ${CURRENT_LIB_NAME}_static 42 | EXPORT ${CURRENT_LIB_NAME}-targets 43 | RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin 44 | LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 45 | ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 46 | PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include 47 | ) 48 | -------------------------------------------------------------------------------- /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/freertos/arch.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-26 19:11:40 5 | * @LastEditTime : 2019-12-28 01:15:29 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 pvPortMalloc(size); 15 | } 16 | 17 | 18 | void salof_free(void *mem) 19 | { 20 | vPortFree(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 | (void)tick; 32 | 33 | xTaskCreate(task_entry, name, stack_size, param, priority, &task); 34 | return task; 35 | } 36 | 37 | salof_mutex salof_mutex_create(void) 38 | { 39 | return xSemaphoreCreateMutex(); 40 | } 41 | 42 | 43 | void salof_mutex_delete(salof_mutex mutex) 44 | { 45 | vSemaphoreDelete(mutex); 46 | } 47 | 48 | 49 | int salof_mutex_pend(salof_mutex mutex, unsigned int timeout) 50 | { 51 | if(xSemaphoreTake(mutex, timeout) != pdPASS) 52 | return -1; 53 | return 0; 54 | } 55 | 56 | int salof_mutex_post(salof_mutex mutex) 57 | { 58 | if(xSemaphoreGive(mutex) != pdPASS) 59 | return -1; 60 | return 0; 61 | } 62 | 63 | 64 | salof_sem salof_sem_create(void) 65 | { 66 | return xSemaphoreCreateBinary(); 67 | } 68 | 69 | void salof_sem_delete(salof_sem sem) 70 | { 71 | vSemaphoreDelete(sem); 72 | } 73 | 74 | int salof_sem_pend(salof_sem sem, unsigned int timeout) 75 | { 76 | if(xSemaphoreTake(sem, timeout) != pdPASS) 77 | return -1; 78 | return 0; 79 | } 80 | 81 | int salof_sem_post(salof_sem sem) 82 | { 83 | if(xSemaphoreGive(sem) != pdPASS) 84 | return -1; 85 | return 0; 86 | } 87 | 88 | unsigned int salof_get_tick(void) 89 | { 90 | return xTaskGetTickCount(); 91 | } 92 | 93 | char *salof_get_task_name(void) 94 | { 95 | return pcTaskGetName(xTaskGetCurrentTaskHandle()); 96 | } 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /common/log/arch/linux/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | aux_source_directory(. CURRENT_DIR_SRCS) 2 | 3 | get_filename_component(PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) 4 | 5 | string(REGEX REPLACE ".*/(.*)" "\\1" LIB_NAME ${PARENT_DIR}) 6 | 7 | if (CURRENT_DIR_SRCS) 8 | foreach(libname ${LIBNAMES}) 9 | if (${LIB_NAME} STREQUAL ${libname}) 10 | add_library(${libname} ${CMAKE_LIB_TYPE} ${CURRENT_DIR_SRCS}) 11 | find_package("Threads") 12 | target_link_libraries(${libname} ${CMAKE_THREAD_LIBS_INIT}) 13 | endif() 14 | endforeach() 15 | 16 | else() 17 | message(WARNING "not find is src file!") 18 | endif() 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /common/log/arch/linux/arch.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-26 19:11:37 5 | * @LastEditTime: 2020-02-25 04:01:18 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 malloc((size_t)size); 15 | } 16 | 17 | void salof_free(void *mem) 18 | { 19 | free(mem); 20 | } 21 | 22 | salof_tcb salof_task_create(const char *name, 23 | void (*task_entry)(void *param), 24 | void * const param, 25 | unsigned int stack_size, 26 | unsigned int priority, 27 | unsigned int tick) 28 | { 29 | int res; 30 | salof_tcb task; 31 | void *(*__start_routine) (void *); 32 | 33 | __start_routine = (void *(*)(void*))task_entry; 34 | task = salof_alloc(sizeof(pthread_t)); 35 | res = pthread_create(task, NULL, __start_routine, param); 36 | if(res != 0) { 37 | salof_free(task); 38 | } 39 | 40 | return task; 41 | } 42 | 43 | salof_mutex salof_mutex_create(void) 44 | { 45 | salof_mutex mutex; 46 | mutex = salof_alloc(sizeof(pthread_mutex_t)); 47 | 48 | if (NULL != mutex) 49 | pthread_mutex_init(mutex, NULL); 50 | 51 | return mutex; 52 | } 53 | 54 | void salof_mutex_delete(salof_mutex mutex) 55 | { 56 | pthread_mutex_destroy(mutex); 57 | } 58 | 59 | int salof_mutex_pend(salof_mutex mutex, unsigned int timeout) 60 | { 61 | if (timeout == 0) 62 | return pthread_mutex_trylock(mutex); 63 | 64 | return pthread_mutex_lock(mutex); 65 | } 66 | 67 | int salof_mutex_post(salof_mutex mutex) 68 | { 69 | return pthread_mutex_unlock(mutex); 70 | } 71 | 72 | salof_sem salof_sem_create(void) 73 | { 74 | salof_sem sem; 75 | sem = salof_alloc(sizeof(sem_t)); 76 | 77 | if (NULL != sem) 78 | sem_init(sem, 0, 0); 79 | 80 | return sem; 81 | } 82 | 83 | void salof_sem_delete(salof_sem sem) 84 | { 85 | sem_destroy(sem); 86 | } 87 | 88 | int salof_sem_pend(salof_sem sem, unsigned int timeout) 89 | { 90 | (void) timeout; 91 | return sem_wait(sem); 92 | } 93 | 94 | int salof_sem_post(salof_sem sem) 95 | { 96 | return sem_post(sem); 97 | } 98 | 99 | unsigned int salof_get_tick(void) 100 | { 101 | return (unsigned int)time(NULL); 102 | } 103 | 104 | char *salof_get_task_name(void) 105 | { 106 | return NULL; 107 | } 108 | 109 | 110 | int send_buff(char *buf, int len) 111 | { 112 | fputs(buf, stdout); 113 | fflush(stdout); 114 | return len; 115 | } 116 | 117 | #endif 118 | -------------------------------------------------------------------------------- /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: 2020-06-17 16:25:18 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/arch/tencentos-tiny/arch.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-26 19:11:34 5 | * @LastEditTime: 2020-03-02 01:32:23 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 tos_mmheap_alloc(size); 15 | } 16 | 17 | 18 | void salof_free(void *mem) 19 | { 20 | tos_mmheap_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 | k_err_t err; 32 | k_stack_t *task_stack; 33 | task = salof_alloc(sizeof(k_task_t)); 34 | task_stack = salof_alloc(stack_size); 35 | err = tos_task_create(task, 36 | (char*)name, 37 | task_entry, 38 | param, 39 | priority, 40 | task_stack, 41 | stack_size, 42 | tick); 43 | if(err != K_ERR_NONE) 44 | { 45 | tos_mmheap_free(task); 46 | tos_mmheap_free(task_stack); 47 | } 48 | 49 | return task; 50 | } 51 | 52 | salof_mutex salof_mutex_create(void) 53 | { 54 | salof_mutex mutex; 55 | mutex = salof_alloc(sizeof(k_mutex_t)); 56 | tos_mutex_create((salof_mutex)mutex); 57 | return mutex; 58 | } 59 | 60 | void salof_mutex_delete(salof_mutex mutex) 61 | { 62 | tos_mutex_destroy((salof_mutex)mutex); 63 | tos_mmheap_free(mutex); 64 | } 65 | 66 | 67 | int salof_mutex_pend(salof_mutex mutex, unsigned int timeout) 68 | { 69 | 70 | if(tos_mutex_pend_timed((salof_mutex)mutex, timeout) != K_ERR_NONE) 71 | return -1; 72 | return 0; 73 | } 74 | 75 | int salof_mutex_post(salof_mutex mutex) 76 | { 77 | if(tos_mutex_post((salof_mutex)mutex) != K_ERR_NONE) 78 | return -1; 79 | return 0; 80 | } 81 | 82 | salof_sem salof_sem_create(void) 83 | { 84 | salof_sem sem; 85 | sem = salof_alloc(sizeof(k_sem_t)); 86 | tos_sem_create((salof_sem)sem, 0); 87 | return sem; 88 | } 89 | 90 | void salof_sem_delete(salof_sem sem) 91 | { 92 | tos_sem_destroy((salof_sem)sem); 93 | tos_mmheap_free(sem); 94 | } 95 | 96 | 97 | int salof_sem_pend(salof_sem sem, unsigned int timeout) 98 | { 99 | 100 | if(tos_sem_pend((salof_sem)sem, timeout) != K_ERR_NONE) 101 | return -1; 102 | return 0; 103 | } 104 | 105 | int salof_sem_post(salof_sem sem) 106 | { 107 | if(tos_sem_post((salof_sem)sem) != K_ERR_NONE) 108 | return -1; 109 | return 0; 110 | } 111 | 112 | 113 | unsigned int salof_get_tick(void) 114 | { 115 | return tos_systick_get(); 116 | } 117 | 118 | char *salof_get_task_name(void) 119 | { 120 | return k_curr_task->name; 121 | } 122 | 123 | #endif 124 | -------------------------------------------------------------------------------- /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 | #if 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 | #if 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 | #if 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 | #if 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 | #if 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 | #if 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 | #if 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 SALOF_LOG_TS && SALOF_LOG_TAR 47 | #define SALOF_LOG_T SALOF_PRINT_LOG("[TS: %d] [TAR: %s] ",salof_get_tick(), salof_get_task_name()) 48 | #elif SALOF_LOG_TS 49 | #define SALOF_LOG_T SALOF_PRINT_LOG("[TS: %d] ", salof_get_tick()) 50 | #elif 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 "mqtt_config.h" 13 | 14 | #endif /* _SALOF_CONFIG_H_ */ 15 | -------------------------------------------------------------------------------- /common/mqtt_defconfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author : jiejie 3 | * @GitHub : https://github.com/jiejieTop 4 | * @Date : 2021-02-26 12:00:24 5 | * @LastEditors : jiejie 6 | * @LastEditTime : 2022-06-15 23:22:10 7 | * @FilePath : /mqttclient/mqttclient/mqtt_defconfig.h 8 | * Copyright (c) 2022 jiejie, All Rights Reserved. Please keep the author information and source code according to the license. 9 | */ 10 | /* 11 | * @Author: jiejie 12 | * @Github: https://github.com/jiejieTop 13 | * @Date: 2020-02-25 03:36:09 14 | * @LastEditTime: 2020-06-17 19:59:41 15 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 16 | */ 17 | 18 | #ifndef _DEFCONFIG_H_ 19 | #define _DEFCONFIG_H_ 20 | 21 | #include "mqtt_config.h" 22 | 23 | #ifndef MQTT_LOG_LEVEL 24 | #define MQTT_LOG_LEVEL MQTT_LOG_DEBUG_LEVEL //MQTT_LOG_WARN_LEVEL MQTT_LOG_DEBUG_LEVEL 25 | #endif // !MQTT_LOG_LEVEL 26 | 27 | #ifndef MQTT_MAX_PACKET_ID 28 | #define MQTT_MAX_PACKET_ID (0xFFFF - 1) 29 | #endif // !MQTT_MAX_PACKET_ID 30 | 31 | #ifndef MQTT_TOPIC_LEN_MAX 32 | #define MQTT_TOPIC_LEN_MAX 64 33 | #endif // !MQTT_TOPIC_LEN_MAX 34 | 35 | #ifndef MQTT_ACK_HANDLER_NUM_MAX 36 | #define MQTT_ACK_HANDLER_NUM_MAX 64 37 | #endif // !MQTT_ACK_HANDLER_NUM_MAX 38 | 39 | #ifndef MQTT_DEFAULT_BUF_SIZE 40 | #define MQTT_DEFAULT_BUF_SIZE 1024 41 | #endif // !MQTT_DEFAULT_BUF_SIZE 42 | 43 | #ifndef MQTT_DEFAULT_CMD_TIMEOUT 44 | #define MQTT_DEFAULT_CMD_TIMEOUT 4000 45 | #endif // !MQTT_DEFAULT_CMD_TIMEOUT 46 | 47 | #ifndef MQTT_MAX_CMD_TIMEOUT 48 | #define MQTT_MAX_CMD_TIMEOUT 20000 49 | #endif // !MQTT_MAX_CMD_TIMEOUT 50 | 51 | #ifndef MQTT_MIN_CMD_TIMEOUT 52 | #define MQTT_MIN_CMD_TIMEOUT 1000 53 | #endif // !MQTT_MIN_CMD_TIMEOUT 54 | 55 | #ifndef MQTT_KEEP_ALIVE_INTERVAL 56 | #define MQTT_KEEP_ALIVE_INTERVAL 100 // unit: second 57 | #endif // !MQTT_KEEP_ALIVE_INTERVAL 58 | 59 | #ifndef MQTT_VERSION 60 | #define MQTT_VERSION 4 // 4 is mqtt 3.1.1 61 | #endif // !MQTT_VERSION 62 | 63 | #ifndef MQTT_RECONNECT_DEFAULT_DURATION 64 | #define MQTT_RECONNECT_DEFAULT_DURATION 1000 65 | #endif // !MQTT_RECONNECT_DEFAULT_DURATION 66 | 67 | #ifndef MQTT_THREAD_STACK_SIZE 68 | #define MQTT_THREAD_STACK_SIZE 4096 69 | #endif // !MQTT_THREAD_STACK_SIZE 70 | 71 | #ifndef MQTT_THREAD_PRIO 72 | #define MQTT_THREAD_PRIO 5 73 | #endif // !MQTT_THREAD_PRIO 74 | 75 | #ifndef MQTT_THREAD_TICK 76 | #define MQTT_THREAD_TICK 50 77 | #endif // !MQTT_THREAD_TICK 78 | 79 | 80 | #ifndef MQTT_NETWORK_TYPE_NO_TLS 81 | 82 | #ifndef MQTT_TLS_HANDSHAKE_TIMEOUT 83 | #define MQTT_TLS_HANDSHAKE_TIMEOUT (5 * 1000) 84 | #endif // !MQTT_TLS_HANDSHAKE_TIMEOUT 85 | 86 | #endif /* MQTT_NETWORK_TYPE_NO_TLS */ 87 | 88 | #endif /* _DEFCONFIG_H_ */ 89 | -------------------------------------------------------------------------------- /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-10-17 14:16:15 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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | typedef enum mqtt_error { 16 | MQTT_SSL_CERT_ERROR = -0x001C, /* cetr parse failed */ 17 | MQTT_SOCKET_FAILED_ERROR = -0x001B, /* socket fd failed */ 18 | MQTT_SOCKET_UNKNOWN_HOST_ERROR = -0x001A, /* socket unknown host ip or domain */ 19 | MQTT_SET_PUBLISH_DUP_FAILED_ERROR = -0x0019, /* mqtt publish packet set udp bit failed */ 20 | MQTT_CLEAN_SESSION_ERROR = -0x0018, /* mqtt clean session error */ 21 | MQTT_ACK_NODE_IS_EXIST_ERROR = -0x0017, /* mqtt ack list is exist ack node */ 22 | MQTT_ACK_HANDLER_NUM_TOO_MUCH_ERROR = -0x0016, /* mqtt ack handler number is too much */ 23 | MQTT_RESUBSCRIBE_ERROR = -0x0015, /* mqtt resubscribe error */ 24 | MQTT_SUBSCRIBE_ERROR = -0x0014, /* mqtt subscribe error */ 25 | MQTT_SEND_PACKET_ERROR = -0x0013, /* mqtt send a packet */ 26 | MQTT_SERIALIZE_PUBLISH_ACK_PACKET_ERROR = -0x0012, /* mqtt serialize publish ack packet error */ 27 | MQTT_PUBLISH_PACKET_ERROR = -0x0011, /* mqtt publish packet error */ 28 | MQTT_RECONNECT_TIMEOUT_ERROR = -0x0010, /* mqtt try reconnect, but timeout */ 29 | MQTT_SUBSCRIBE_NOT_ACK_ERROR = -0x000F, /* mqtt subscribe, but not ack */ 30 | MQTT_NOT_CONNECT_ERROR = -0x000E, /* mqtt not connect */ 31 | MQTT_SUBSCRIBE_ACK_PACKET_ERROR = -0x000D, /* mqtt subscribe, but ack packet error */ 32 | MQTT_UNSUBSCRIBE_ACK_PACKET_ERROR = -0x000C, /* mqtt unsubscribe, but ack packet error */ 33 | MQTT_PUBLISH_ACK_PACKET_ERROR = -0x000B, /* mqtt pubilsh ack packet error */ 34 | MQTT_PUBLISH_ACK_TYPE_ERROR = -0x000A, /* mqtt pubilsh ack type error */ 35 | MQTT_PUBREC_PACKET_ERROR = -0x0009, /* mqtt pubrec packet error */ 36 | MQTT_BUFFER_TOO_SHORT_ERROR = -0x0008, /* mqtt buffer too short */ 37 | MQTT_NOTHING_TO_READ_ERROR = -0x0007, /* mqtt nothing to read */ 38 | MQTT_SUBSCRIBE_QOS_ERROR = -0x0006, /* mqtt subsrcibe qos error */ 39 | MQTT_BUFFER_OVERFLOW_ERROR = -0x0005, /* mqtt buffer overflow */ 40 | MQTT_CONNECT_FAILED_ERROR = -0x0004, /* mqtt connect failed */ 41 | MQTT_MEM_NOT_ENOUGH_ERROR = -0x0003, /* mqtt memory not enough */ 42 | MQTT_NULL_VALUE_ERROR = -0x0002, /* mqtt value is null */ 43 | MQTT_FAILED_ERROR = -0x0001, /* failed */ 44 | MQTT_SUCCESS_ERROR = 0x0000 /* success */ 45 | } mqtt_error_t; 46 | 47 | #define RETURN_ERROR(x) { return x; } 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /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-10-17 14:18:02 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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | typedef struct mqtt_list_node { 16 | struct mqtt_list_node *next; 17 | struct mqtt_list_node *prev; 18 | } mqtt_list_t; 19 | 20 | #define OFFSET_OF_FIELD(type, field) \ 21 | ((size_t)&(((type *)0)->field)) 22 | 23 | #define CONTAINER_OF_FIELD(ptr, type, field) \ 24 | ((type *)((unsigned char *)(ptr) - OFFSET_OF_FIELD(type, field))) 25 | 26 | #define LIST_NODE(node) \ 27 | { &(node), &(node) } 28 | 29 | #define LIST_DEFINE(list) \ 30 | mqtt_list_t list = { &(list), &(list) } 31 | 32 | #define LIST_ENTRY(list, type, field) \ 33 | CONTAINER_OF_FIELD(list, type, field) 34 | 35 | #define LIST_FIRST_ENTRY(list, type, field) \ 36 | LIST_ENTRY((list)->next, type, field) 37 | 38 | #define LIST_FIRST_ENTRY_OR_NULL(list, type, field) \ 39 | (mqtt_list_is_empty(list) ? NULL : LIST_FIRST_ENTRY(list, type, field)) 40 | 41 | #define LIST_FOR_EACH(curr, list) \ 42 | for (curr = (list)->next; curr != (list); curr = curr->next) 43 | 44 | #define LIST_FOR_EACH_PREV(curr, list) \ 45 | for (curr = (list)->prev; curr != (list); curr = curr->prev) 46 | 47 | #define LIST_FOR_EACH_SAFE(curr, next, list) \ 48 | for (curr = (list)->next, next = curr->next; curr != (list); \ 49 | curr = next, next = curr->next) 50 | 51 | #define LIST_FOR_EACH_PREV_SAFE(curr, next, list) \ 52 | for (curr = (list)->prev, next = curr->prev; \ 53 | curr != (list); \ 54 | curr = next, next = curr->prev) 55 | 56 | void mqtt_list_init(mqtt_list_t *list); 57 | void mqtt_list_add(mqtt_list_t *node, mqtt_list_t *list); 58 | void mqtt_list_add_tail(mqtt_list_t *node, mqtt_list_t *list); 59 | void mqtt_list_del(mqtt_list_t *entry); 60 | void mqtt_list_del_init(mqtt_list_t *entry); 61 | void mqtt_list_move(mqtt_list_t *node, mqtt_list_t *list); 62 | void mqtt_list_move_tail(mqtt_list_t *node, mqtt_list_t *list); 63 | int mqtt_list_is_empty(mqtt_list_t *list); 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif /* _LIST_H_ */ 70 | 71 | -------------------------------------------------------------------------------- /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-10-17 14:15:55 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 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | #define MQTT_LOG_BASE_LEVEL (0) 18 | #define MQTT_LOG_ERR_LEVEL (MQTT_LOG_BASE_LEVEL + 1) 19 | #define MQTT_LOG_WARN_LEVEL (MQTT_LOG_ERR_LEVEL + 1) 20 | #define MQTT_LOG_INFO_LEVEL (MQTT_LOG_WARN_LEVEL + 1) 21 | #define MQTT_LOG_DEBUG_LEVEL (MQTT_LOG_INFO_LEVEL + 1) 22 | 23 | #ifdef MQTT_LOG_IS_SALOF 24 | #include "salof.h" 25 | 26 | #define MQTT_LOG_D(fmt, ...) SALOF_LOG_DEBUG(fmt, ##__VA_ARGS__) 27 | #define MQTT_LOG_I(fmt, ...) SALOF_LOG_INFO(fmt, ##__VA_ARGS__) 28 | #define MQTT_LOG_W(fmt, ...) SALOF_LOG_WARN(fmt, ##__VA_ARGS__) 29 | #define MQTT_LOG_E(fmt, ...) SALOF_LOG_ERR(fmt, ##__VA_ARGS__) 30 | #define mqtt_log_init salof_init 31 | #else 32 | #include 33 | 34 | #if MQTT_LOG_LEVEL < MQTT_LOG_DEBUG_LEVEL 35 | #define MQTT_LOG_D(fmt, ...) 36 | #else 37 | #define MQTT_LOG_D(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");} 38 | #endif 39 | 40 | #if MQTT_LOG_LEVEL < MQTT_LOG_INFO_LEVEL 41 | #define MQTT_LOG_I(fmt, ...) 42 | #else 43 | #define MQTT_LOG_I(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");} 44 | #endif 45 | 46 | #if MQTT_LOG_LEVEL < MQTT_LOG_WARN_LEVEL 47 | #define MQTT_LOG_W(fmt, ...) 48 | #else 49 | #define MQTT_LOG_W(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");} 50 | #endif 51 | 52 | #if MQTT_LOG_LEVEL < MQTT_LOG_ERR_LEVEL 53 | #define MQTT_LOG_E(fmt, ...) 54 | #else 55 | #define MQTT_LOG_E(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");} 56 | #endif 57 | 58 | #if MQTT_LOG_LEVEL < MQTT_LOG_BASE_LEVEL 59 | #define MQTT_LOG(fmt, ...) 60 | #else 61 | #define MQTT_LOG(fmt, ...) { printf(fmt, ##__VA_ARGS__); printf("\n");} 62 | #endif 63 | 64 | #define mqtt_log_init() 65 | #endif 66 | 67 | #ifdef __cplusplus 68 | } 69 | #endif 70 | 71 | #endif /* _LOG_H_ */ 72 | -------------------------------------------------------------------------------- /common/random.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author : jiejie 3 | * @GitHub : https://github.com/jiejieTop 4 | * @Date : 2021-02-26 12:00:24 5 | * @LastEditors : jiejie 6 | * @LastEditTime : 2022-06-15 19:44:22 7 | * @FilePath : /mqttclient/common/random.c 8 | * Copyright (c) 2022 jiejie, All Rights Reserved. Please keep the author information and source code according to the license. 9 | */ 10 | 11 | #include 12 | #include "random.h" 13 | 14 | extern int platform_timer_now(); 15 | static unsigned int last_seed = 1; 16 | 17 | 18 | int __attribute__((weak)) platform_timer_now() 19 | { 20 | return 1; 21 | } 22 | 23 | static int do_random(unsigned int seed) 24 | { 25 | srand(seed); 26 | return rand(); 27 | } 28 | 29 | int random_number(void) 30 | { 31 | unsigned int seed = (unsigned int) platform_timer_now(); 32 | last_seed += (seed >> ((seed ^ last_seed) % 3)); 33 | return do_random(last_seed ^ seed); 34 | } 35 | 36 | // random number range interval [min, max) 37 | int random_number_range(unsigned int min, unsigned int max) 38 | { 39 | return (random_number() % (max - min)) + min; 40 | } 41 | 42 | int random_string(char *buffer, int len) 43 | { 44 | unsigned int i, flag, seed, random; 45 | 46 | if (NULL == buffer) 47 | return 0; 48 | 49 | seed = (unsigned int) random_number(); 50 | seed += (unsigned int) ((size_t)buffer ^ seed); 51 | 52 | random = (unsigned int)do_random(seed); 53 | 54 | for (i = 0; i < len; i++) { 55 | random = do_random(seed ^ random); 56 | flag = (unsigned int)random % 3; 57 | switch (flag) { 58 | case 0: 59 | buffer[i] = 'A' + do_random(random ^ (i & flag)) % 26; 60 | break; 61 | case 1: 62 | buffer[i] = 'a' + do_random(random ^ (i & flag)) % 26; 63 | break; 64 | case 2: 65 | buffer[i] = '0' + do_random(random ^ (i & flag)) % 10; 66 | break; 67 | default: 68 | buffer[i] = 'x'; 69 | break; 70 | } 71 | random += ((0xb433e5c6 ^ random) << (i & flag)); 72 | } 73 | 74 | buffer[len] = '\0'; 75 | return len; 76 | } 77 | 78 | -------------------------------------------------------------------------------- /common/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author : jiejie 3 | * @GitHub : https://github.com/jiejieTop 4 | * @Date : 2021-02-26 12:00:24 5 | * @LastEditors : jiejie 6 | * @LastEditTime : 2022-06-15 19:40:10 7 | * @FilePath : /mqttclient/common/random.h 8 | * Copyright (c) 2022 jiejie, All Rights Reserved. Please keep the author information and source code according to the license. 9 | */ 10 | 11 | 12 | #ifndef _RANDOM_H_ 13 | #define _RANDOM_H_ 14 | 15 | #ifdef __cplusplus 16 | extern "C" { 17 | #endif 18 | 19 | #define RANDOM_MAX 0x7FFFFFFF 20 | 21 | int random_number(void); 22 | int random_number_range(unsigned int min, unsigned int max); 23 | int random_string(char *buffer, int len); 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | 29 | #endif /* _RANDOM_H_ */ 30 | -------------------------------------------------------------------------------- /config/mqtt_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @LastEditTime: 2020-06-17 19:31:41 5 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 6 | */ 7 | #ifndef _MQTT_CONFIG_H_ 8 | #define _MQTT_CONFIG_H_ 9 | 10 | #define MQTT_LOG_IS_SALOF 11 | 12 | #define MQTT_LOG_LEVEL MQTT_LOG_INFO_LEVEL //MQTT_LOG_WARN_LEVEL MQTT_LOG_DEBUG_LEVEL 13 | 14 | #ifdef MQTT_LOG_IS_SALOF 15 | #define SALOF_USING_LOG (1U) 16 | #define SALOF_USING_SALOF (1U) 17 | #define SALOF_LOG_LEVEL MQTT_LOG_LEVEL 18 | #define SALOF_OS SALOF_USING_LINUX 19 | #define SALOF_USING_IDLE_HOOK (0U) 20 | #define SALOF_LOG_COLOR (1U) 21 | #define SALOF_LOG_TS (0U) 22 | #define SALOF_LOG_TAR (0U) 23 | #define SALOF_BUFF_SIZE 512 24 | #define SALOF_FIFO_SIZE 4096 25 | #define SALOF_TASK_STACK_SIZE 1024 26 | #define SALOF_TASK_TICK 50 27 | #endif 28 | 29 | #define MQTT_MAX_PACKET_ID (0xFFFF - 1) 30 | #define MQTT_TOPIC_LEN_MAX 64 31 | #define MQTT_ACK_HANDLER_NUM_MAX 64 32 | #define MQTT_DEFAULT_BUF_SIZE 1024 33 | #define MQTT_DEFAULT_CMD_TIMEOUT 5000 34 | #define MQTT_MAX_CMD_TIMEOUT 20000 35 | #define MQTT_MIN_CMD_TIMEOUT 1000 36 | #define MQTT_KEEP_ALIVE_INTERVAL 50 // unit: second 37 | #define MQTT_VERSION 4 // 4 is mqtt 3.1.1 38 | #define MQTT_RECONNECT_DEFAULT_DURATION 1000 39 | #define MQTT_THREAD_STACK_SIZE 2048 40 | #define MQTT_THREAD_PRIO 5 41 | #define MQTT_THREAD_TICK 50 42 | 43 | 44 | // #define MQTT_NETWORK_TYPE_NO_TLS 45 | 46 | #endif /* _MQTT_CONFIG_H_ */ 47 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project(mqttclient_example) 3 | 4 | set(EXAMPLE_DIRS "emqx" "onenet" "baidu" "ali") 5 | 6 | # ######################### 7 | # ## find depend package ## 8 | # ######################### 9 | if(NOT BUILD_EXAMPLES) 10 | include(${CMAKE_CURRENT_SOURCE_DIR}/mqttclient.cmake) 11 | set(PACKAGE_DEPEND ${MQTTCLIENT_PACKAGE_LIBS}) 12 | endif() 13 | 14 | ################ 15 | ## add subdir ## 16 | ################ 17 | foreach(example ${EXAMPLE_DIRS}) 18 | add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${example}) 19 | endforeach() 20 | -------------------------------------------------------------------------------- /example/ali/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # aux_source_directory(. DIR_SRCS) 2 | 3 | # set(INCDIRS ${CMAKE_CURRENT_SOURCE_DIR}) 4 | 5 | # add_executable("ali" ${DIR_SRCS}) 6 | 7 | # foreach(findlib ${LIBNAMES}) 8 | # target_link_libraries("ali" ${findlib}) 9 | # endforeach() 10 | 11 | # find_package("Threads") 12 | # target_link_libraries("ali" ${CMAKE_THREAD_LIBS_INIT}) 13 | 14 | 15 | cmake_minimum_required(VERSION 2.8) 16 | project(mqttclient_example) 17 | 18 | ################## 19 | ## set arg info ## 20 | ################## 21 | set(CMAKE_C_FLAGS "-Wall") 22 | set(CMAKE_C_FLAGS "-lpthread") 23 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O0 -g -no-pie -ggdb") 24 | set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3 -DNDEBUG") 25 | 26 | if(CMAKE_COMPILER_IS_GNUCXX) 27 | set(CMAKE_CXX_FLAGS "-std=c++11") 28 | set(CMAKE_CXX_FLAGS "-lpthread") 29 | set(CMAKE_CXX_FLAGS "-Wall") 30 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -g -no-pie -ggdb") 31 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3") 32 | endif(CMAKE_COMPILER_IS_GNUCXX) 33 | 34 | ######################### 35 | ## find depend package ## 36 | ######################### 37 | if(BUILD_EXAMPLES) 38 | set(PACKAGE_DEPEND ${MODULE_NAME}) 39 | endif() 40 | 41 | find_package("Threads") 42 | 43 | ########### 44 | ## build ## 45 | ########### 46 | 47 | # 指定头文件搜索目录 48 | set(INC_DIRS 49 | ${CMAKE_CURRENT_SOURCE_DIR} 50 | ) 51 | 52 | # 指定依赖文件搜索目录 53 | set(DEPEND_SRC_DIRS 54 | ) 55 | 56 | # 指定应用文件搜索目录 57 | set(EXAMPLE_SRC_DIRS 58 | ${CMAKE_CURRENT_SOURCE_DIR} 59 | ) 60 | 61 | foreach(FOREACH_SRC_DIR ${DEPEND_SRC_DIRS}) 62 | aux_source_directory(${FOREACH_SRC_DIR} DEPEND_SOURCES) 63 | endforeach() 64 | 65 | foreach(FOREACH_SRC_DIR ${EXAMPLE_SRC_DIRS}) 66 | aux_source_directory(${FOREACH_SRC_DIR} EXAMPLE_SOURCES) 67 | endforeach() 68 | 69 | include_directories(${INC_DIRS}) 70 | 71 | foreach(example_src_name ${EXAMPLE_SOURCES}) 72 | get_filename_component(example_name ${example_src_name} NAME_WE) 73 | add_executable(${example_name}_example ${example_src_name} ${DEPEND_SOURCES}) 74 | target_link_libraries(${example_name}_example ${CMAKE_THREAD_LIBS_INIT} ${PACKAGE_DEPEND}) 75 | message(STATUS "build example : ${example_name} ") 76 | endforeach() 77 | 78 | 79 | -------------------------------------------------------------------------------- /example/ali/ali.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-11 21:53:07 5 | * @LastEditTime: 2020-06-08 20:40:47 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 | static void topic1_handler(void* client, message_data_t* msg) 16 | { 17 | (void) client; 18 | MQTT_LOG_I("-----------------------------------------------------------------------------------"); 19 | MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char*)msg->message->payload); 20 | MQTT_LOG_I("-----------------------------------------------------------------------------------"); 21 | } 22 | 23 | void *mqtt_publish_thread(void *arg) 24 | { 25 | mqtt_client_t *client = (mqtt_client_t *)arg; 26 | 27 | char buf[100] = { 0 }; 28 | mqtt_message_t msg; 29 | memset(&msg, 0, sizeof(msg)); 30 | sprintf(buf, "welcome to mqttclient, this is a publish test..."); 31 | 32 | sleep(2); 33 | 34 | mqtt_list_subscribe_topic(client); 35 | 36 | msg.payload = (void *) buf; 37 | msg.qos = 0; 38 | while(1) { 39 | sprintf(buf, "welcome to mqttclient, this is a publish test, a rand number: %d ...", random_number()); 40 | mqtt_publish(client, "/a1w7XupONEX/test1/user/topic1", &msg); 41 | sleep(4); 42 | } 43 | } 44 | 45 | int main(void) 46 | { 47 | int res; 48 | pthread_t thread1; 49 | mqtt_client_t *client = NULL; 50 | 51 | printf("\nwelcome to mqttclient test...\n"); 52 | 53 | mqtt_log_init(); 54 | 55 | client = mqtt_lease(); 56 | 57 | mqtt_set_port(client, "1883"); 58 | mqtt_set_host(client, "a1w7XupONEX.iot-as-mqtt.cn-shanghai.aliyuncs.com"); 59 | mqtt_set_client_id(client, "123456|securemode=3,signmethod=hmacsha1|"); 60 | mqtt_set_user_name(client, "test1&a1w7XupONEX"); 61 | mqtt_set_password(client, "A9EFF34CCA05EABAE560373CBED3E43AC88956CF"); 62 | mqtt_set_clean_session(client, 1); 63 | 64 | mqtt_connect(client); 65 | 66 | mqtt_subscribe(client, "/a1w7XupONEX/test1/user/topic1", QOS0, topic1_handler); 67 | 68 | res = pthread_create(&thread1, NULL, mqtt_publish_thread, client); 69 | if(res != 0) { 70 | MQTT_LOG_E("create mqtt publish thread fail"); 71 | exit(res); 72 | } 73 | 74 | while (1) { 75 | sleep(100); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /example/ali/mqtt_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @LastEditTime: 2020-06-17 19:31:41 5 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 6 | */ 7 | #ifndef _MQTT_CONFIG_H_ 8 | #define _MQTT_CONFIG_H_ 9 | 10 | #define MQTT_LOG_IS_SALOF 11 | 12 | #define MQTT_LOG_LEVEL MQTT_LOG_INFO_LEVEL //MQTT_LOG_WARN_LEVEL MQTT_LOG_DEBUG_LEVEL 13 | 14 | #ifdef MQTT_LOG_IS_SALOF 15 | #define SALOF_USING_LOG (1U) 16 | #define SALOF_USING_SALOF (1U) 17 | #define SALOF_LOG_LEVEL MQTT_LOG_LEVEL 18 | #define SALOF_OS SALOF_USING_LINUX 19 | #define SALOF_USING_IDLE_HOOK (0U) 20 | #define SALOF_LOG_COLOR (1U) 21 | #define SALOF_LOG_TS (0U) 22 | #define SALOF_LOG_TAR (0U) 23 | #define SALOF_BUFF_SIZE 512 24 | #define SALOF_FIFO_SIZE 4096 25 | #define SALOF_TASK_STACK_SIZE 1024 26 | #define SALOF_TASK_TICK 50 27 | #endif 28 | 29 | #define MQTT_MAX_PACKET_ID (0xFFFF - 1) 30 | #define MQTT_TOPIC_LEN_MAX 64 31 | #define MQTT_ACK_HANDLER_NUM_MAX 64 32 | #define MQTT_DEFAULT_BUF_SIZE 1024 33 | #define MQTT_DEFAULT_CMD_TIMEOUT 5000 34 | #define MQTT_MAX_CMD_TIMEOUT 20000 35 | #define MQTT_MIN_CMD_TIMEOUT 1000 36 | #define MQTT_KEEP_ALIVE_INTERVAL 50 // unit: second 37 | #define MQTT_VERSION 4 // 4 is mqtt 3.1.1 38 | #define MQTT_RECONNECT_DEFAULT_DURATION 1000 39 | #define MQTT_THREAD_STACK_SIZE 2048 40 | #define MQTT_THREAD_PRIO 5 41 | #define MQTT_THREAD_TICK 50 42 | 43 | 44 | // #define MQTT_NETWORK_TYPE_NO_TLS 45 | 46 | #endif /* _MQTT_CONFIG_H_ */ 47 | -------------------------------------------------------------------------------- /example/baidu/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # aux_source_directory(. DIR_SRCS) 2 | 3 | # set(INCDIRS ${CMAKE_CURRENT_SOURCE_DIR}) 4 | 5 | # add_executable("baidu" ${DIR_SRCS}) 6 | 7 | # foreach(findlib ${LIBNAMES}) 8 | # target_link_libraries("baidu" ${findlib}) 9 | # endforeach() 10 | 11 | # find_package("Threads") 12 | # target_link_libraries("baidu" ${CMAKE_THREAD_LIBS_INIT}) 13 | 14 | 15 | cmake_minimum_required(VERSION 2.8) 16 | project(mqttclient_example) 17 | 18 | ################## 19 | ## set arg info ## 20 | ################## 21 | set(CMAKE_C_FLAGS "-Wall") 22 | set(CMAKE_C_FLAGS "-lpthread") 23 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O0 -g -no-pie -ggdb") 24 | set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3 -DNDEBUG") 25 | 26 | if(CMAKE_COMPILER_IS_GNUCXX) 27 | set(CMAKE_CXX_FLAGS "-std=c++11") 28 | set(CMAKE_CXX_FLAGS "-lpthread") 29 | set(CMAKE_CXX_FLAGS "-Wall") 30 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -g -no-pie -ggdb") 31 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3") 32 | endif(CMAKE_COMPILER_IS_GNUCXX) 33 | 34 | ######################### 35 | ## find depend package ## 36 | ######################### 37 | if(BUILD_EXAMPLES) 38 | set(PACKAGE_DEPEND ${MODULE_NAME}) 39 | endif() 40 | 41 | find_package("Threads") 42 | 43 | ########### 44 | ## build ## 45 | ########### 46 | 47 | # 指定头文件搜索目录 48 | set(INC_DIRS 49 | ${CMAKE_CURRENT_SOURCE_DIR} 50 | ) 51 | 52 | # 指定依赖文件搜索目录 53 | set(DEPEND_SRC_DIRS 54 | ) 55 | 56 | # 指定应用文件搜索目录 57 | set(EXAMPLE_SRC_DIRS 58 | ${CMAKE_CURRENT_SOURCE_DIR} 59 | ) 60 | 61 | foreach(FOREACH_SRC_DIR ${DEPEND_SRC_DIRS}) 62 | aux_source_directory(${FOREACH_SRC_DIR} DEPEND_SOURCES) 63 | endforeach() 64 | 65 | foreach(FOREACH_SRC_DIR ${EXAMPLE_SRC_DIRS}) 66 | aux_source_directory(${FOREACH_SRC_DIR} EXAMPLE_SOURCES) 67 | endforeach() 68 | 69 | include_directories(${INC_DIRS}) 70 | 71 | foreach(example_src_name ${EXAMPLE_SOURCES}) 72 | get_filename_component(example_name ${example_src_name} NAME_WE) 73 | add_executable(${example_name}_example ${example_src_name} ${DEPEND_SOURCES}) 74 | target_link_libraries(${example_name}_example ${CMAKE_THREAD_LIBS_INIT} ${PACKAGE_DEPEND}) 75 | message(STATUS "build example : ${example_name} ") 76 | endforeach() 77 | 78 | 79 | -------------------------------------------------------------------------------- /example/baidu/mqtt_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @LastEditTime: 2020-06-17 19:31:41 5 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 6 | */ 7 | #ifndef _MQTT_CONFIG_H_ 8 | #define _MQTT_CONFIG_H_ 9 | 10 | #define MQTT_LOG_IS_SALOF 11 | 12 | #define MQTT_LOG_LEVEL MQTT_LOG_INFO_LEVEL //MQTT_LOG_WARN_LEVEL MQTT_LOG_DEBUG_LEVEL 13 | 14 | #ifdef MQTT_LOG_IS_SALOF 15 | #define SALOF_USING_LOG (1U) 16 | #define SALOF_USING_SALOF (1U) 17 | #define SALOF_LOG_LEVEL MQTT_LOG_LEVEL 18 | #define SALOF_OS SALOF_USING_LINUX 19 | #define SALOF_USING_IDLE_HOOK (0U) 20 | #define SALOF_LOG_COLOR (1U) 21 | #define SALOF_LOG_TS (0U) 22 | #define SALOF_LOG_TAR (0U) 23 | #define SALOF_BUFF_SIZE 512 24 | #define SALOF_FIFO_SIZE 4096 25 | #define SALOF_TASK_STACK_SIZE 1024 26 | #define SALOF_TASK_TICK 50 27 | #endif 28 | 29 | #define MQTT_MAX_PACKET_ID (0xFFFF - 1) 30 | #define MQTT_TOPIC_LEN_MAX 64 31 | #define MQTT_ACK_HANDLER_NUM_MAX 64 32 | #define MQTT_DEFAULT_BUF_SIZE 1024 33 | #define MQTT_DEFAULT_CMD_TIMEOUT 5000 34 | #define MQTT_MAX_CMD_TIMEOUT 20000 35 | #define MQTT_MIN_CMD_TIMEOUT 1000 36 | #define MQTT_KEEP_ALIVE_INTERVAL 50 // unit: second 37 | #define MQTT_VERSION 4 // 4 is mqtt 3.1.1 38 | #define MQTT_RECONNECT_DEFAULT_DURATION 1000 39 | #define MQTT_THREAD_STACK_SIZE 2048 40 | #define MQTT_THREAD_PRIO 5 41 | #define MQTT_THREAD_TICK 50 42 | 43 | 44 | // #define MQTT_NETWORK_TYPE_NO_TLS 45 | 46 | #endif /* _MQTT_CONFIG_H_ */ 47 | -------------------------------------------------------------------------------- /example/emqx/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project(mqttclient_example) 3 | 4 | ################## 5 | ## set arg info ## 6 | ################## 7 | set(CMAKE_C_FLAGS "-Wall") 8 | set(CMAKE_C_FLAGS "-lpthread") 9 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O0 -g -no-pie -ggdb") 10 | set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3 -DNDEBUG") 11 | 12 | if(CMAKE_COMPILER_IS_GNUCXX) 13 | set(CMAKE_CXX_FLAGS "-std=c++11") 14 | set(CMAKE_CXX_FLAGS "-lpthread") 15 | set(CMAKE_CXX_FLAGS "-Wall") 16 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -g -no-pie -ggdb") 17 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3") 18 | endif(CMAKE_COMPILER_IS_GNUCXX) 19 | 20 | ######################### 21 | ## find depend package ## 22 | ######################### 23 | if(BUILD_EXAMPLES) 24 | set(PACKAGE_DEPEND ${MODULE_NAME}) 25 | endif() 26 | 27 | find_package("Threads") 28 | 29 | ########### 30 | ## build ## 31 | ########### 32 | 33 | # 指定头文件搜索目录 34 | set(INC_DIRS 35 | ${CMAKE_CURRENT_SOURCE_DIR} 36 | ) 37 | 38 | # 指定依赖文件搜索目录 39 | set(DEPEND_SRC_DIRS 40 | ) 41 | 42 | # 指定应用文件搜索目录 43 | set(EXAMPLE_SRC_DIRS 44 | ${CMAKE_CURRENT_SOURCE_DIR} 45 | ) 46 | 47 | foreach(FOREACH_SRC_DIR ${DEPEND_SRC_DIRS}) 48 | aux_source_directory(${FOREACH_SRC_DIR} DEPEND_SOURCES) 49 | endforeach() 50 | 51 | foreach(FOREACH_SRC_DIR ${EXAMPLE_SRC_DIRS}) 52 | aux_source_directory(${FOREACH_SRC_DIR} EXAMPLE_SOURCES) 53 | endforeach() 54 | 55 | include_directories(${INC_DIRS}) 56 | 57 | foreach(example_src_name ${EXAMPLE_SOURCES}) 58 | get_filename_component(example_name ${example_src_name} NAME_WE) 59 | add_executable(${example_name}_example ${example_src_name} ${DEPEND_SOURCES}) 60 | target_link_libraries(${example_name}_example ${CMAKE_THREAD_LIBS_INIT} ${PACKAGE_DEPEND}) 61 | message(STATUS "build example : ${example_name} ") 62 | endforeach() 63 | 64 | 65 | -------------------------------------------------------------------------------- /example/emqx/ca.inc: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-01-12 10:51:11 5 | * @LastEditTime : 2022-06-12 17:47:53 6 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 7 | */ 8 | 9 | static const char *test_ca_crt = { 10 | "-----BEGIN CERTIFICATE-----\r\n" 11 | "MIIDEzCCAfugAwIBAgIBAjANBgkqhkiG9w0BAQsFADA/MQswCQYDVQQGEwJDTjER\r\n" 12 | "MA8GA1UECAwIaGFuZ3pob3UxDDAKBgNVBAoMA0VNUTEPMA0GA1UEAwwGUm9vdENB\r\n" 13 | "MB4XDTIwMDUwODA4MDcwNVoXDTMwMDUwNjA4MDcwNVowPzELMAkGA1UEBhMCQ04x\r\n" 14 | "ETAPBgNVBAgMCGhhbmd6aG91MQwwCgYDVQQKDANFTVExDzANBgNVBAMMBlNlcnZl\r\n" 15 | "cjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALNeWT3pE+QFfiRJzKmn\r\n" 16 | "AMUrWo3K2j/Tm3+Xnl6WLz67/0rcYrJbbKvS3uyRP/stXyXEKw9CepyQ1ViBVFkW\r\n" 17 | "Aoy8qQEOWFDsZc/5UzhXUnb6LXr3qTkFEjNmhj+7uzv/lbBxlUG1NlYzSeOB6/RT\r\n" 18 | "8zH/lhOeKhLnWYPXdXKsa1FL6ij4X8DeDO1kY7fvAGmBn/THh1uTpDizM4YmeI+7\r\n" 19 | "4dmayA5xXvARte5h4Vu5SIze7iC057N+vymToMk2Jgk+ZZFpyXrnq+yo6RaD3ANc\r\n" 20 | "lrc4FbeUQZ5a5s5Sxgs9a0Y3WMG+7c5VnVXcbjBRz/aq2NtOnQQjikKKQA8GF080\r\n" 21 | "BQkCAwEAAaMaMBgwCQYDVR0TBAIwADALBgNVHQ8EBAMCBeAwDQYJKoZIhvcNAQEL\r\n" 22 | "BQADggEBAJefnMZpaRDHQSNUIEL3iwGXE9c6PmIsQVE2ustr+CakBp3TZ4l0enLt\r\n" 23 | "iGMfEVFju69cO4oyokWv+hl5eCMkHBf14Kv51vj448jowYnF1zmzn7SEzm5Uzlsa\r\n" 24 | "sqjtAprnLyof69WtLU1j5rYWBuFX86yOTwRAFNjm9fvhAcrEONBsQtqipBWkMROp\r\n" 25 | "iUYMkRqbKcQMdwxov+lHBYKq9zbWRoqLROAn54SRqgQk6c15JdEfgOOjShbsOkIH\r\n" 26 | "UhqcwRkQic7n1zwHVGVDgNIZVgmJ2IdIWBlPEC7oLrRrBD/X1iEEXtKab6p5o22n\r\n" 27 | "KB5mN+iQaE+Oe2cpGKZJiJRdM+IqDDQ=\r\n" 28 | "-----END CERTIFICATE-----\r\n" 29 | }; 30 | 31 | const char *test_ca_get() 32 | { 33 | return test_ca_crt; 34 | } 35 | -------------------------------------------------------------------------------- /example/emqx/emqx.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-11 21:53:07 5 | * @LastEditTime : 2022-06-15 23:03:30 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 | 14 | #include "mqtt_config.h" 15 | #include "mqtt_log.h" 16 | #include "mqttclient.h" 17 | 18 | #include "ca.inc" 19 | 20 | // #define TEST_USEING_TLS 21 | 22 | static void topic1_handler(void* client, message_data_t* msg) 23 | { 24 | (void) client; 25 | MQTT_LOG_I("-----------------------------------------------------------------------------------"); 26 | MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char*)msg->message->payload); 27 | MQTT_LOG_I("-----------------------------------------------------------------------------------"); 28 | } 29 | 30 | void *mqtt_publish_thread(void *arg) 31 | { 32 | mqtt_client_t *client = (mqtt_client_t *)arg; 33 | 34 | char buf[100] = { 0 }; 35 | mqtt_message_t msg; 36 | memset(&msg, 0, sizeof(msg)); 37 | sprintf(buf, "welcome to mqttclient, this is a publish test..."); 38 | 39 | sleep(2); 40 | 41 | mqtt_list_subscribe_topic(client); 42 | 43 | msg.payload = (void *) buf; 44 | 45 | while(1) { 46 | sprintf(buf, "welcome to mqttclient, this is a publish test, a rand number: %d ...", random_number()); 47 | 48 | msg.qos = 0; 49 | mqtt_publish(client, "topic1", &msg); 50 | 51 | msg.qos = 1; 52 | mqtt_publish(client, "topic2", &msg); 53 | 54 | msg.qos = 2; 55 | mqtt_publish(client, "topic3", &msg); 56 | 57 | sleep(4); 58 | } 59 | } 60 | 61 | int main(void) 62 | { 63 | int res; 64 | pthread_t thread1; 65 | mqtt_client_t *client = NULL; 66 | char client_id[32]; 67 | char user_name[32]; 68 | char password[32]; 69 | 70 | printf("\nwelcome to mqttclient test...\n"); 71 | 72 | random_string(client_id, 10); 73 | random_string(user_name, 10); 74 | random_string(password, 10); 75 | 76 | mqtt_log_init(); 77 | 78 | client = mqtt_lease(); 79 | 80 | #ifdef TEST_USEING_TLS 81 | mqtt_set_port(client, "8883"); 82 | mqtt_set_ca(client, (char*)test_ca_get()); 83 | #else 84 | mqtt_set_port(client, "1883"); 85 | #endif 86 | 87 | mqtt_set_host(client, "120.25.213.14"); 88 | mqtt_set_client_id(client, client_id); 89 | mqtt_set_user_name(client, user_name); 90 | mqtt_set_password(client, password); 91 | mqtt_set_clean_session(client, 1); 92 | 93 | mqtt_connect(client); 94 | 95 | mqtt_subscribe(client, "topic1", QOS0, topic1_handler); 96 | mqtt_subscribe(client, "topic2", QOS1, NULL); 97 | mqtt_subscribe(client, "topic3", QOS2, NULL); 98 | 99 | res = pthread_create(&thread1, NULL, mqtt_publish_thread, client); 100 | if(res != 0) { 101 | MQTT_LOG_E("create mqtt publish thread fail"); 102 | exit(res); 103 | } 104 | 105 | while (1) { 106 | sleep(100); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /example/emqx/mqtt_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @LastEditTime: 2020-06-17 19:31:41 5 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 6 | */ 7 | #ifndef _MQTT_CONFIG_H_ 8 | #define _MQTT_CONFIG_H_ 9 | 10 | #define MQTT_LOG_IS_SALOF 11 | 12 | #define MQTT_LOG_LEVEL MQTT_LOG_INFO_LEVEL //MQTT_LOG_WARN_LEVEL MQTT_LOG_DEBUG_LEVEL 13 | 14 | #ifdef MQTT_LOG_IS_SALOF 15 | #define SALOF_USING_LOG (1U) 16 | #define SALOF_USING_SALOF (1U) 17 | #define SALOF_LOG_LEVEL MQTT_LOG_LEVEL 18 | #define SALOF_OS SALOF_USING_LINUX 19 | #define SALOF_USING_IDLE_HOOK (0U) 20 | #define SALOF_LOG_COLOR (1U) 21 | #define SALOF_LOG_TS (0U) 22 | #define SALOF_LOG_TAR (0U) 23 | #define SALOF_BUFF_SIZE 512 24 | #define SALOF_FIFO_SIZE 4096 25 | #define SALOF_TASK_STACK_SIZE 1024 26 | #define SALOF_TASK_TICK 50 27 | #endif 28 | 29 | #define MQTT_MAX_PACKET_ID (0xFFFF - 1) 30 | #define MQTT_TOPIC_LEN_MAX 64 31 | #define MQTT_ACK_HANDLER_NUM_MAX 64 32 | #define MQTT_DEFAULT_BUF_SIZE 1024 33 | #define MQTT_DEFAULT_CMD_TIMEOUT 5000 34 | #define MQTT_MAX_CMD_TIMEOUT 20000 35 | #define MQTT_MIN_CMD_TIMEOUT 1000 36 | #define MQTT_KEEP_ALIVE_INTERVAL 50 // unit: second 37 | #define MQTT_VERSION 4 // 4 is mqtt 3.1.1 38 | #define MQTT_RECONNECT_DEFAULT_DURATION 1000 39 | #define MQTT_THREAD_STACK_SIZE 2048 40 | #define MQTT_THREAD_PRIO 5 41 | #define MQTT_THREAD_TICK 50 42 | 43 | 44 | // #define MQTT_NETWORK_TYPE_NO_TLS 45 | 46 | #endif /* _MQTT_CONFIG_H_ */ 47 | -------------------------------------------------------------------------------- /example/mqttclient.cmake: -------------------------------------------------------------------------------- 1 | set(MQTTCLIENT_PACKAGE "mqtt-client") 2 | set(MQTTCLIENT_PACKAGE_PATH "/usr/local/mqttclient") 3 | set(MQTTCLIENT_PACKAGE_LIBS "") 4 | set(MQTTCLIENT_PACKAGE_INC_DIRS "") 5 | set(MQTTCLIENT_PACKAGE_LINK_DIRS "") 6 | 7 | macro(find_depend_package package) 8 | find_package(${package} REQUIRED) 9 | if (NOT ${package}_FOUND) 10 | message(FATAL_ERROR "${package} not found!") 11 | endif (NOT ${package}_FOUND) 12 | endmacro(find_depend_package package) 13 | 14 | macro(find_depend_package_by_path package package_path) 15 | set(${package}_DIR ${package_path}) 16 | find_depend_package(${package}) 17 | endmacro(find_depend_package_by_path package package_path) 18 | 19 | foreach(depend_package ${MQTTCLIENT_PACKAGE}) 20 | foreach(depend_package_path ${MQTTCLIENT_PACKAGE_PATH}) 21 | find_depend_package_by_path(${depend_package} ${depend_package_path}/lib/cmake) 22 | endforeach() 23 | list(APPEND MQTTCLIENT_PACKAGE_LIBS ${${depend_package}_LIBRARIES}) 24 | list(APPEND MQTTCLIENT_PACKAGE_INC_DIRS ${${depend_package}_INCLUDE_DIRS}) 25 | list(APPEND MQTTCLIENT_PACKAGE_LINK_DIRS ${${depend_package}_LINK_DIRS}) 26 | endforeach() 27 | 28 | message("\n------------ find package info ------------") 29 | 30 | message("--> depend packages : ") 31 | foreach(depend_package ${MQTTCLIENT_PACKAGE_LIBS}) 32 | message(" " ${depend_package}) 33 | endforeach() 34 | 35 | message("--> depend packages : ") 36 | foreach(depend_package_link_dir ${MQTTCLIENT_PACKAGE_LINK_DIRS}) 37 | message(" " ${depend_package_link_dir}) 38 | endforeach() 39 | 40 | message("--> depend packages : ") 41 | foreach(depend_package_include_dir ${MQTTCLIENT_PACKAGE_INC_DIRS}) 42 | message(" " ${depend_package_include_dir}) 43 | endforeach() 44 | 45 | include_directories(${MQTTCLIENT_PACKAGE_INC_DIRS}) 46 | link_directories(${MQTTCLIENT_PACKAGE_LINK_DIRS}) 47 | -------------------------------------------------------------------------------- /example/onenet/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # aux_source_directory(. DIR_SRCS) 2 | 3 | # set(INCDIRS ${CMAKE_CURRENT_SOURCE_DIR}) 4 | 5 | # add_executable("onenet" ${DIR_SRCS}) 6 | 7 | # foreach(findlib ${LIBNAMES}) 8 | # target_link_libraries("onenet" ${findlib}) 9 | # endforeach() 10 | 11 | # find_package("Threads") 12 | # target_link_libraries("onenet" ${CMAKE_THREAD_LIBS_INIT}) 13 | 14 | cmake_minimum_required(VERSION 2.8) 15 | project(mqttclient_example) 16 | 17 | ################## 18 | ## set arg info ## 19 | ################## 20 | set(CMAKE_C_FLAGS "-Wall") 21 | set(CMAKE_C_FLAGS "-lpthread") 22 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O0 -g -no-pie -ggdb") 23 | set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3 -DNDEBUG") 24 | 25 | if(CMAKE_COMPILER_IS_GNUCXX) 26 | set(CMAKE_CXX_FLAGS "-std=c++11") 27 | set(CMAKE_CXX_FLAGS "-lpthread") 28 | set(CMAKE_CXX_FLAGS "-Wall") 29 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -g -no-pie -ggdb") 30 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3") 31 | endif(CMAKE_COMPILER_IS_GNUCXX) 32 | 33 | ######################### 34 | ## find depend package ## 35 | ######################### 36 | if(BUILD_EXAMPLES) 37 | set(PACKAGE_DEPEND ${MODULE_NAME}) 38 | endif() 39 | 40 | find_package("Threads") 41 | 42 | ########### 43 | ## build ## 44 | ########### 45 | 46 | # 指定头文件搜索目录 47 | set(INC_DIRS 48 | ${CMAKE_CURRENT_SOURCE_DIR} 49 | ) 50 | 51 | # 指定依赖文件搜索目录 52 | set(DEPEND_SRC_DIRS 53 | ) 54 | 55 | # 指定应用文件搜索目录 56 | set(EXAMPLE_SRC_DIRS 57 | ${CMAKE_CURRENT_SOURCE_DIR} 58 | ) 59 | 60 | foreach(FOREACH_SRC_DIR ${DEPEND_SRC_DIRS}) 61 | aux_source_directory(${FOREACH_SRC_DIR} DEPEND_SOURCES) 62 | endforeach() 63 | 64 | foreach(FOREACH_SRC_DIR ${EXAMPLE_SRC_DIRS}) 65 | aux_source_directory(${FOREACH_SRC_DIR} EXAMPLE_SOURCES) 66 | endforeach() 67 | 68 | include_directories(${INC_DIRS}) 69 | 70 | foreach(example_src_name ${EXAMPLE_SOURCES}) 71 | get_filename_component(example_name ${example_src_name} NAME_WE) 72 | add_executable(${example_name}_example ${example_src_name} ${DEPEND_SOURCES}) 73 | target_link_libraries(${example_name}_example ${CMAKE_THREAD_LIBS_INIT} ${PACKAGE_DEPEND}) 74 | message(STATUS "build example : ${example_name} ") 75 | endforeach() 76 | 77 | 78 | -------------------------------------------------------------------------------- /example/onenet/mqtt_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @LastEditTime: 2020-06-17 19:31:41 5 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 6 | */ 7 | #ifndef _MQTT_CONFIG_H_ 8 | #define _MQTT_CONFIG_H_ 9 | 10 | #define MQTT_LOG_IS_SALOF 11 | 12 | #define MQTT_LOG_LEVEL MQTT_LOG_INFO_LEVEL //MQTT_LOG_WARN_LEVEL MQTT_LOG_DEBUG_LEVEL 13 | 14 | #ifdef MQTT_LOG_IS_SALOF 15 | #define SALOF_USING_LOG (1U) 16 | #define SALOF_USING_SALOF (1U) 17 | #define SALOF_LOG_LEVEL MQTT_LOG_LEVEL 18 | #define SALOF_OS SALOF_USING_LINUX 19 | #define SALOF_USING_IDLE_HOOK (0U) 20 | #define SALOF_LOG_COLOR (1U) 21 | #define SALOF_LOG_TS (0U) 22 | #define SALOF_LOG_TAR (0U) 23 | #define SALOF_BUFF_SIZE 512 24 | #define SALOF_FIFO_SIZE 4096 25 | #define SALOF_TASK_STACK_SIZE 1024 26 | #define SALOF_TASK_TICK 50 27 | #endif 28 | 29 | #define MQTT_MAX_PACKET_ID (0xFFFF - 1) 30 | #define MQTT_TOPIC_LEN_MAX 64 31 | #define MQTT_ACK_HANDLER_NUM_MAX 64 32 | #define MQTT_DEFAULT_BUF_SIZE 1024 33 | #define MQTT_DEFAULT_CMD_TIMEOUT 5000 34 | #define MQTT_MAX_CMD_TIMEOUT 20000 35 | #define MQTT_MIN_CMD_TIMEOUT 1000 36 | #define MQTT_KEEP_ALIVE_INTERVAL 50 // unit: second 37 | #define MQTT_VERSION 4 // 4 is mqtt 3.1.1 38 | #define MQTT_RECONNECT_DEFAULT_DURATION 1000 39 | #define MQTT_THREAD_STACK_SIZE 2048 40 | #define MQTT_THREAD_PRIO 5 41 | #define MQTT_THREAD_TICK 50 42 | 43 | 44 | // #define MQTT_NETWORK_TYPE_NO_TLS 45 | 46 | #endif /* _MQTT_CONFIG_H_ */ 47 | -------------------------------------------------------------------------------- /example/onenet/onenet.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2020-04-18 12:37:34 5 | * @LastEditTime: 2020-06-08 20:32: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 10 | #include 11 | #include 12 | #include 13 | #include "mqttclient.h" 14 | 15 | extern const char *test_ca_get(); 16 | 17 | 18 | static void interceptor_handler(void* client, message_data_t* msg) 19 | { 20 | (void) client; 21 | MQTT_LOG_I("-----------------------------------------------------------------------------------"); 22 | MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char*)msg->message->payload); 23 | MQTT_LOG_I("-----------------------------------------------------------------------------------"); 24 | } 25 | 26 | void *mqtt_publish_thread(void *arg) 27 | { 28 | mqtt_client_t *client = (mqtt_client_t *)arg; 29 | 30 | char buf[100] = { 0 }; 31 | mqtt_message_t msg; 32 | memset(&msg, 0, sizeof(msg)); 33 | sprintf(buf, "welcome to mqttclient, this is a publish test..."); 34 | 35 | msg.qos = 0; 36 | msg.payload = (void *) buf; 37 | while(1) { 38 | sprintf(buf, "welcome to mqttclient, this is a publish test, a rand number: %d ...", random_number()); 39 | mqtt_publish(client, "topic1", &msg); 40 | sleep(4); 41 | } 42 | } 43 | 44 | int main(void) 45 | { 46 | int res; 47 | pthread_t thread1; 48 | mqtt_client_t *client = NULL; 49 | 50 | printf("\nwelcome to mqttclient test...\n"); 51 | 52 | mqtt_log_init(); 53 | 54 | client = mqtt_lease(); 55 | 56 | mqtt_set_port(client, "6002"); 57 | mqtt_set_host(client, "183.230.40.39"); 58 | mqtt_set_client_id(client, "599908192"); 59 | mqtt_set_user_name(client, "348547"); 60 | mqtt_set_password(client, "mqttclienttest1"); 61 | mqtt_set_clean_session(client, 1); 62 | 63 | mqtt_connect(client); 64 | 65 | mqtt_subscribe(client, "topic1", QOS0, NULL); 66 | 67 | mqtt_set_interceptor_handler(client, interceptor_handler); // set interceptor handler 68 | 69 | res = pthread_create(&thread1, NULL, mqtt_publish_thread, client); 70 | if(res != 0) { 71 | MQTT_LOG_E("create mqtt publish thread fail"); 72 | exit(res); 73 | } 74 | 75 | while (1) { 76 | sleep(100); 77 | } 78 | } -------------------------------------------------------------------------------- /make-libmqttclient.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | current_pwd=$( 4 | cd "$(dirname "$0")" 5 | pwd 6 | ) 7 | 8 | mkdir -p $current_pwd/libmqttclient $current_pwd/libmqttclient/include $current_pwd/libmqttclient/lib 9 | 10 | cd $current_pwd/libmqttclient 11 | 12 | if [ ! -f "Makefile" ]; then 13 | 14 | cat <<-EOF >Makefile 15 | CC=\$(CROSS_COMPILE)gcc 16 | 17 | SRC = \$(wildcard $current_pwd/*.c \\ 18 | $current_pwd/common/*.c \\ 19 | $current_pwd/common/log/*.c \\ 20 | $current_pwd/common/log/arch/linux/*.c \\ 21 | $current_pwd/network/mbedtls/library/*.c \\ 22 | $current_pwd/network/mbedtls/wrapper/*.c \\ 23 | $current_pwd/mqtt/*.c \\ 24 | $current_pwd/mqttclient/*.c \\ 25 | $current_pwd/network/*.c \\ 26 | $current_pwd/platform/linux/*.c \\ 27 | ) 28 | 29 | INC = -lpthread \\ 30 | -I$current_pwd/common \\ 31 | -I$current_pwd/common/log \\ 32 | -I$current_pwd/network/mbedtls/include \\ 33 | -I$current_pwd/network/mbedtls/include/mbedtls \\ 34 | -I$current_pwd/network/mbedtls/wrapper \\ 35 | -I$current_pwd/mqtt \\ 36 | -I$current_pwd/mqttclient \\ 37 | -I$current_pwd/network \\ 38 | -I$current_pwd/platform/linux \\ 39 | -I$current_pwd/config 40 | 41 | OBJS = \$(patsubst %.c, %.o, \$(SRC)) 42 | 43 | FLAG = -g -fpic -I. -Iinclude \$(INC) 44 | TARGET = libmqttclient.so 45 | 46 | EOF 47 | 48 | echo -e "\n\$(TARGET):\$(OBJS)" >>Makefile 49 | echo -e "\t\$(CC) -shared \$^ -o \$@" >>Makefile 50 | 51 | echo -e "\n%.o:%.c" >>Makefile 52 | echo -e "\t\$(CC) -c \$(FLAG) \$^ -o \$@" >>Makefile 53 | 54 | echo -e "\nclean:" >>Makefile 55 | echo -e "\trm -rf \$(TARGET) \$(OBJS)" >>Makefile 56 | 57 | echo -e "\ninstall:" >>Makefile 58 | echo -e "\tsudo cp -rdf \$(TARGET) /usr/lib/." >>Makefile 59 | 60 | echo -e "\nremove:" >>Makefile 61 | echo -e "\tsudo rm -rdf /usr/lib/\$(TARGET)" >>Makefile 62 | 63 | echo -e "\n.PHONY:clean" >>Makefile 64 | 65 | fi 66 | 67 | mkdir -p $current_pwd/libmqttclient/include/config/. 68 | mkdir -p $current_pwd/libmqttclient/include/mqtt/. 69 | mkdir -p $current_pwd/libmqttclient/include/common/. 70 | mkdir -p $current_pwd/libmqttclient/include/network/. 71 | mkdir -p $current_pwd/libmqttclient/include/mqttclient/. 72 | mkdir -p $current_pwd/libmqttclient/include/common/log/. 73 | mkdir -p $current_pwd/libmqttclient/include/platform/linux/. 74 | mkdir -p $current_pwd/libmqttclient/include/mbedtls/. 75 | mkdir -p $current_pwd/libmqttclient/include/mbedtls/wrapper/. 76 | 77 | cp -r $current_pwd/config/*.h $current_pwd/libmqttclient/include/config/. 78 | cp -r $current_pwd/mqtt/*.h $current_pwd/libmqttclient/include/mqtt/. 79 | cp -r $current_pwd/common/*.h $current_pwd/libmqttclient/include/common/. 80 | cp -r $current_pwd/network/*.h $current_pwd/libmqttclient/include/network/. 81 | cp -r $current_pwd/mqttclient/*.h $current_pwd/libmqttclient/include/mqttclient/. 82 | cp -r $current_pwd/common/log/*.h $current_pwd/libmqttclient/include/common/log/. 83 | cp -r $current_pwd/platform/linux/*.h $current_pwd/libmqttclient/include/platform/linux/. 84 | cp -r $current_pwd/network/mbedtls/include/mbedtls/* $current_pwd/libmqttclient/include/mbedtls/. 85 | cp -r $current_pwd/network/mbedtls/wrapper/*.h $current_pwd/libmqttclient/include/mbedtls/wrapper/. 86 | 87 | if [ " $1" == " " ]; then 88 | cd $current_pwd/libmqttclient 89 | make 90 | mv libmqttclient.so $current_pwd/libmqttclient/lib/. 91 | make clean 92 | 93 | elif [ "$1" == "remove" ]; then 94 | make remove 95 | fi 96 | -------------------------------------------------------------------------------- /mqtt/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 2 | 3 | set(CURRENT_LIB_NAME ${LIBRARY_PREFIX}-${CURRENT_LIB_NAME}) 4 | 5 | # 指定头文件搜索目录 6 | set(${CURRENT_LIB_NAME}_INC_DIRS 7 | ${CMAKE_CURRENT_SOURCE_DIR} 8 | ) 9 | 10 | # 源文件目录 11 | set(${CURRENT_LIB_NAME}_SRC_DIRS 12 | ${CMAKE_CURRENT_SOURCE_DIR} 13 | ) 14 | 15 | set_module_info(${CURRENT_LIB_NAME} "${${CURRENT_LIB_NAME}_INC_DIRS}") 16 | 17 | foreach(FOREACH_SRC_DIR ${${CURRENT_LIB_NAME}_SRC_DIRS}) 18 | aux_source_directory(${FOREACH_SRC_DIR} ${CURRENT_LIB_NAME}_SOURCES) 19 | endforeach() 20 | 21 | 22 | # 生成动态库 23 | add_library(${CURRENT_LIB_NAME} SHARED ${${CURRENT_LIB_NAME}_SOURCES}) 24 | target_link_libraries(${CURRENT_LIB_NAME}) 25 | # 生成静态库 26 | add_library(${CURRENT_LIB_NAME}_static STATIC ${${CURRENT_LIB_NAME}_SOURCES}) 27 | target_link_libraries(${CURRENT_LIB_NAME}_static) 28 | link_directories(${LIBRARY_OUTPUT_PATH}) 29 | 30 | # 进行必要的配置,以生成动态库和静态库 31 | # 指定静态库的输出名称 32 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES OUTPUT_NAME ${CURRENT_LIB_NAME}) 33 | # 使动态库和静态库同时存在 34 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1) 35 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 36 | # 指定动态库版本 37 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) 38 | 39 | # 打包相关 install package 40 | install(TARGETS ${CURRENT_LIB_NAME} ${CURRENT_LIB_NAME}_static 41 | EXPORT ${CURRENT_LIB_NAME}-targets 42 | RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin 43 | LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 44 | ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 45 | PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include 46 | ) 47 | 48 | install(DIRECTORY ${${CURRENT_LIB_NAME}_INC_DIRS} 49 | DESTINATION "${CMAKE_INSTALL_PREFIX}/include" 50 | FILES_MATCHING 51 | PATTERN "*.h" 52 | PATTERN "*.hpp" 53 | PATTERN "CMakeLists.txt" EXCLUDE) 54 | -------------------------------------------------------------------------------- /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/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/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/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 | string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 2 | 3 | set(CURRENT_LIB_NAME ${LIBRARY_PREFIX}-${CURRENT_LIB_NAME}) 4 | 5 | set(DEPEND_LIBRARY_INTERNAL "common" "mqtt" "platform" "network" ) 6 | 7 | # 指定头文件搜索目录 8 | set(${CURRENT_LIB_NAME}_INC_DIRS 9 | ${CMAKE_CURRENT_SOURCE_DIR} 10 | ) 11 | 12 | # 源文件目录 13 | set(${CURRENT_LIB_NAME}_SRC_DIRS 14 | ${CMAKE_CURRENT_SOURCE_DIR} 15 | ) 16 | 17 | set_module_info(${CURRENT_LIB_NAME} "${${CURRENT_LIB_NAME}_INC_DIRS}") 18 | get_module_info() 19 | 20 | 21 | 22 | foreach(FOREACH_SRC_DIR ${${CURRENT_LIB_NAME}_SRC_DIRS}) 23 | aux_source_directory(${FOREACH_SRC_DIR} ${CURRENT_LIB_NAME}_SOURCES) 24 | endforeach() 25 | 26 | 27 | foreach(lib ${DEPEND_LIBRARY_INTERNAL}) 28 | list(APPEND DEPEND_LIBRARY ${LIBRARY_PREFIX}-${lib}) 29 | list(APPEND DEPEND_LIBRARY_STATIC lib${LIBRARY_PREFIX}-${lib}.a) 30 | endforeach() 31 | 32 | # 生成动态库 33 | add_library(${CURRENT_LIB_NAME} SHARED ${${CURRENT_LIB_NAME}_SOURCES}) 34 | target_link_libraries(${CURRENT_LIB_NAME} ${DEPEND_LIBRARY}) 35 | # 生成静态库 36 | add_library(${CURRENT_LIB_NAME}_static STATIC ${${CURRENT_LIB_NAME}_SOURCES}) 37 | target_link_libraries(${CURRENT_LIB_NAME}_static ${DEPEND_LIBRARY_STATIC}) 38 | 39 | # 进行必要的配置,以生成动态库和静态库 40 | # 指定静态库的输出名称 41 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES OUTPUT_NAME ${CURRENT_LIB_NAME}) 42 | # 使动态库和静态库同时存在 43 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1) 44 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 45 | # 指定动态库版本 46 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) 47 | 48 | # 打包相关 install package 49 | install(TARGETS ${CURRENT_LIB_NAME} ${CURRENT_LIB_NAME}_static 50 | EXPORT ${CURRENT_LIB_NAME}-targets 51 | RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin 52 | LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 53 | ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 54 | PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include 55 | ) 56 | 57 | install(DIRECTORY ${${CURRENT_LIB_NAME}_INC_DIRS} 58 | DESTINATION "${CMAKE_INSTALL_PREFIX}/include" 59 | FILES_MATCHING 60 | PATTERN "*.h" 61 | PATTERN "*.hpp" 62 | PATTERN "CMakeLists.txt" EXCLUDE) 63 | 64 | -------------------------------------------------------------------------------- /network/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 2 | 3 | set(CURRENT_LIB_NAME ${LIBRARY_PREFIX}-${CURRENT_LIB_NAME}) 4 | 5 | set(SUBDIRS "mbedtls") 6 | 7 | foreach(subdir ${SUBDIRS}) 8 | add_subdirectory(${subdir}) 9 | endforeach() 10 | 11 | set(DEPEND_LIBRARY_INTERNAL "common" "log" "platform" "mbedtls") 12 | 13 | # 指定头文件搜索目录 14 | set(${CURRENT_LIB_NAME}_INC_DIRS 15 | ${CMAKE_CURRENT_SOURCE_DIR} 16 | ) 17 | 18 | # 源文件目录 19 | set(${CURRENT_LIB_NAME}_SRC_DIRS 20 | ${CMAKE_CURRENT_SOURCE_DIR} 21 | ) 22 | 23 | set_module_info(${CURRENT_LIB_NAME} "${${CURRENT_LIB_NAME}_INC_DIRS}") 24 | get_module_info() 25 | 26 | 27 | 28 | foreach(FOREACH_SRC_DIR ${${CURRENT_LIB_NAME}_SRC_DIRS}) 29 | aux_source_directory(${FOREACH_SRC_DIR} ${CURRENT_LIB_NAME}_SOURCES) 30 | endforeach() 31 | 32 | 33 | foreach(lib ${DEPEND_LIBRARY_INTERNAL}) 34 | list(APPEND DEPEND_LIBRARY ${LIBRARY_PREFIX}-${lib}) 35 | list(APPEND DEPEND_LIBRARY_STATIC lib${LIBRARY_PREFIX}-${lib}.a) 36 | endforeach() 37 | 38 | # 生成动态库 39 | add_library(${CURRENT_LIB_NAME} SHARED ${${CURRENT_LIB_NAME}_SOURCES}) 40 | target_link_libraries(${CURRENT_LIB_NAME} ${DEPEND_LIBRARY}) 41 | # 生成静态库 42 | add_library(${CURRENT_LIB_NAME}_static STATIC ${${CURRENT_LIB_NAME}_SOURCES}) 43 | target_link_libraries(${CURRENT_LIB_NAME}_static ${DEPEND_LIBRARY_STATIC}) 44 | 45 | # 进行必要的配置,以生成动态库和静态库 46 | # 指定静态库的输出名称 47 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES OUTPUT_NAME ${CURRENT_LIB_NAME}) 48 | # 使动态库和静态库同时存在 49 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1) 50 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 51 | # 指定动态库版本 52 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) 53 | 54 | # 打包相关 install package 55 | install(TARGETS ${CURRENT_LIB_NAME} ${CURRENT_LIB_NAME}_static 56 | EXPORT ${CURRENT_LIB_NAME}-targets 57 | RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin 58 | LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 59 | ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 60 | PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include 61 | ) 62 | 63 | install(DIRECTORY ${${CURRENT_LIB_NAME}_INC_DIRS} 64 | DESTINATION "${CMAKE_INSTALL_PREFIX}/include" 65 | FILES_MATCHING 66 | PATTERN "*.h" 67 | PATTERN "*.hpp" 68 | PATTERN "CMakeLists.txt" EXCLUDE) 69 | -------------------------------------------------------------------------------- /network/mbedtls/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 2 | 3 | set(CURRENT_LIB_NAME ${LIBRARY_PREFIX}-${CURRENT_LIB_NAME}) 4 | 5 | set(DEPEND_LIBRARY_INTERNAL "platform") 6 | 7 | # 指定头文件搜索目录 8 | set(${CURRENT_LIB_NAME}_INC_DIRS 9 | ${CMAKE_CURRENT_SOURCE_DIR} 10 | ${CMAKE_CURRENT_SOURCE_DIR}/wrapper 11 | ${CMAKE_CURRENT_SOURCE_DIR}/configs 12 | ${CMAKE_CURRENT_SOURCE_DIR}/include 13 | ${CMAKE_CURRENT_SOURCE_DIR}/include/mbedtls 14 | ) 15 | 16 | # 源文件目录 17 | set(${CURRENT_LIB_NAME}_SRC_DIRS 18 | ${CMAKE_CURRENT_SOURCE_DIR} 19 | ${CMAKE_CURRENT_SOURCE_DIR}/wrapper 20 | ${CMAKE_CURRENT_SOURCE_DIR}/library 21 | ) 22 | 23 | set_module_info(${CURRENT_LIB_NAME} "${${CURRENT_LIB_NAME}_INC_DIRS}") 24 | get_module_info() 25 | 26 | foreach(FOREACH_SRC_DIR ${${CURRENT_LIB_NAME}_SRC_DIRS}) 27 | aux_source_directory(${FOREACH_SRC_DIR} ${CURRENT_LIB_NAME}_SOURCES) 28 | endforeach() 29 | 30 | foreach(lib ${DEPEND_LIBRARY_INTERNAL}) 31 | list(APPEND DEPEND_LIBRARY ${LIBRARY_PREFIX}-${lib}) 32 | list(APPEND DEPEND_LIBRARY_STATIC lib${LIBRARY_PREFIX}-${lib}.a) 33 | endforeach() 34 | 35 | # 生成动态库 36 | add_library(${CURRENT_LIB_NAME} SHARED ${${CURRENT_LIB_NAME}_SOURCES}) 37 | target_link_libraries(${CURRENT_LIB_NAME} ${DEPEND_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) 38 | # 生成静态库 39 | add_library(${CURRENT_LIB_NAME}_static STATIC ${${CURRENT_LIB_NAME}_SOURCES}) 40 | target_link_libraries(${CURRENT_LIB_NAME}_static ${DEPEND_LIBRARY_STATIC} ${CMAKE_THREAD_LIBS_INIT}) 41 | 42 | # 进行必要的配置,以生成动态库和静态库 43 | # 指定静态库的输出名称 44 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES OUTPUT_NAME ${CURRENT_LIB_NAME}) 45 | # 使动态库和静态库同时存在 46 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1) 47 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 48 | # 指定动态库版本 49 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) 50 | 51 | 52 | # 打包相关 install package 53 | install(TARGETS ${CURRENT_LIB_NAME} ${CURRENT_LIB_NAME}_static 54 | EXPORT ${CURRENT_LIB_NAME}-targets 55 | RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin 56 | LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 57 | ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 58 | PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include 59 | ) 60 | 61 | -------------------------------------------------------------------------------- /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-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/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/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/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/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_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/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/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 | #include "platform_net_socket.h" 11 | 12 | int nettype_tcp_read(network_t *n, unsigned char *read_buf, int len, int timeout) 13 | { 14 | return platform_net_socket_recv_timeout(n->socket, read_buf, len, timeout); 15 | } 16 | 17 | int nettype_tcp_write(network_t *n, unsigned char *write_buf, int len, int timeout) 18 | { 19 | return platform_net_socket_write_timeout(n->socket, write_buf, len, timeout); 20 | } 21 | 22 | int nettype_tcp_connect(network_t* n) 23 | { 24 | n->socket = platform_net_socket_connect(n->host, n->port, PLATFORM_NET_PROTO_TCP); 25 | if (n->socket < 0) 26 | RETURN_ERROR(n->socket); 27 | 28 | RETURN_ERROR(MQTT_SUCCESS_ERROR); 29 | } 30 | 31 | void nettype_tcp_disconnect(network_t* n) 32 | { 33 | if (NULL != n) 34 | platform_net_socket_close(n->socket); 35 | n->socket = -1; 36 | } 37 | -------------------------------------------------------------------------------- /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-10-17 14:17:10 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 "network.h" 12 | #include "mqtt_error.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | int nettype_tcp_read(network_t *n, unsigned char *buf, int len, int timeout); 19 | int nettype_tcp_write(network_t *n, unsigned char *buf, int len, int timeout); 20 | int nettype_tcp_connect(network_t* n); 21 | void nettype_tcp_disconnect(network_t* n); 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /network/nettype_tls.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author : jiejie 3 | * @GitHub : https://github.com/jiejieTop 4 | * @Date : 2021-02-26 12:00:24 5 | * @LastEditors : jiejie 6 | * @LastEditTime : 2022-06-15 19:48:43 7 | * @FilePath : /mqttclient/network/nettype_tls.h 8 | * Copyright (c) 2022 jiejie, All Rights Reserved. Please keep the author information and source code according to the license. 9 | */ 10 | /* 11 | * @Author: jiejie 12 | * @Github: https://github.com/jiejieTop 13 | * @Date: 2020-01-11 19:45:44 14 | * @LastEditTime: 2020-10-17 14:14:11 15 | * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. 16 | */ 17 | 18 | #ifndef _NETTYPE_TLS_H_ 19 | #define _NETTYPE_TLS_H_ 20 | 21 | #include "mqtt_defconfig.h" 22 | #include "network.h" 23 | #include "mqtt_error.h" 24 | #include "mqtt_log.h" 25 | 26 | #ifndef MQTT_NETWORK_TYPE_NO_TLS 27 | 28 | #include "mbedtls/config.h" 29 | #include "mbedtls/ssl.h" 30 | #include "mbedtls/entropy.h" 31 | #include "mbedtls/net_sockets.h" 32 | #include "mbedtls/ctr_drbg.h" 33 | #include "mbedtls/error.h" 34 | #include "mbedtls/debug.h" 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | typedef struct nettype_tls_params { 41 | mbedtls_net_context socket_fd; /**< mbed TLS network context. */ 42 | mbedtls_entropy_context entropy; /**< mbed TLS entropy. */ 43 | mbedtls_ctr_drbg_context ctr_drbg; /**< mbed TLS ctr_drbg. */ 44 | mbedtls_ssl_context ssl; /**< mbed TLS control context. */ 45 | mbedtls_ssl_config ssl_conf; /**< mbed TLS configuration context. */ 46 | #if defined(MBEDTLS_X509_CRT_PARSE_C) 47 | mbedtls_x509_crt ca_cert; /**< mbed TLS CA certification. */ 48 | mbedtls_x509_crt client_cert; /**< mbed TLS Client certification. */ 49 | #endif 50 | mbedtls_pk_context private_key; /**< mbed TLS Client key. */ 51 | } nettype_tls_params_t; 52 | 53 | int nettype_tls_read(network_t *n, unsigned char *buf, int len, int timeout); 54 | int nettype_tls_write(network_t *n, unsigned char *buf, int len, int timeout); 55 | int nettype_tls_connect(network_t* n); 56 | void nettype_tls_disconnect(network_t* n); 57 | 58 | #endif /* MQTT_NETWORK_TYPE_NO_TLS */ 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /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 | #ifndef MQTT_NETWORK_TYPE_NO_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 | #ifndef MQTT_NETWORK_TYPE_NO_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 | #ifndef MQTT_NETWORK_TYPE_NO_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 | #ifndef MQTT_NETWORK_TYPE_NO_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 | #ifndef MQTT_NETWORK_TYPE_NO_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(MQTT_NULL_VALUE_ERROR); 58 | 59 | n->socket = -1; 60 | n->host = host; 61 | n->port = port; 62 | 63 | #ifndef MQTT_NETWORK_TYPE_NO_TLS 64 | n->channel = 0; 65 | 66 | if (NULL != ca) { 67 | network_set_ca(n, ca); 68 | } 69 | #endif 70 | RETURN_ERROR(MQTT_SUCCESS_ERROR); 71 | } 72 | 73 | void network_release(network_t* n) 74 | { 75 | if (n->socket >= 0) 76 | { 77 | network_disconnect(n); 78 | memset(n, 0, sizeof(network_t)); 79 | } 80 | } 81 | 82 | void network_set_channel(network_t *n, int channel) 83 | { 84 | #ifndef MQTT_NETWORK_TYPE_NO_TLS 85 | n->channel = channel; 86 | #endif 87 | } 88 | 89 | int network_set_ca(network_t *n, const char *ca) 90 | { 91 | #ifndef MQTT_NETWORK_TYPE_NO_TLS 92 | if ((NULL == n) || (NULL == ca)) 93 | RETURN_ERROR(MQTT_NULL_VALUE_ERROR); 94 | 95 | n->ca_crt = ca; 96 | n->ca_crt_len = strlen(ca); 97 | n->channel = NETWORK_CHANNEL_TLS; 98 | n->timeout_ms = MQTT_TLS_HANDSHAKE_TIMEOUT; 99 | #endif 100 | RETURN_ERROR(MQTT_SUCCESS_ERROR); 101 | } 102 | 103 | int network_set_host_port(network_t* n, char *host, char *port) 104 | { 105 | if (!(n && host && port)) 106 | RETURN_ERROR(MQTT_NULL_VALUE_ERROR); 107 | 108 | n->host = host; 109 | n->port = port; 110 | 111 | RETURN_ERROR(MQTT_SUCCESS_ERROR); 112 | } 113 | 114 | -------------------------------------------------------------------------------- /network/network.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-09 21:31:02 5 | * @LastEditTime: 2020-10-17 14:14:41 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 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | #define NETWORK_CHANNEL_TCP 0 18 | #define NETWORK_CHANNEL_TLS 1 19 | 20 | typedef struct network { 21 | const char *host; 22 | const char *port; 23 | int socket; 24 | #ifndef MQTT_NETWORK_TYPE_NO_TLS 25 | int channel; /* tcp or tls */ 26 | const char *ca_crt; 27 | unsigned int ca_crt_len; 28 | unsigned int timeout_ms; // SSL handshake timeout in millisecond 29 | void *nettype_tls_params; 30 | #endif 31 | } network_t; 32 | 33 | int network_init(network_t *n, const char *host, const char *port, const char *ca); 34 | int network_set_ca(network_t *n, const char *ca); 35 | void network_set_channel(network_t *n, int channel); 36 | int network_set_host_port(network_t* n, char *host, char *port); 37 | int network_read(network_t* n, unsigned char* buf, int len, int timeout); 38 | int network_write(network_t* n, unsigned char* buf, int len, int timeout); 39 | int network_connect(network_t* n); 40 | void network_disconnect(network_t *n); 41 | void network_release(network_t* n); 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /platform/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR}) 2 | 3 | set(CURRENT_LIB_NAME ${LIBRARY_PREFIX}-${CURRENT_LIB_NAME}) 4 | 5 | set(DEPEND_LIBRARY_INTERNAL "common") 6 | 7 | # 指定头文件搜索目录 8 | set(${CURRENT_LIB_NAME}_INC_DIRS 9 | ${CMAKE_CURRENT_SOURCE_DIR}/linux 10 | ) 11 | 12 | # 源文件目录 13 | set(${CURRENT_LIB_NAME}_SRC_DIRS 14 | ${CMAKE_CURRENT_SOURCE_DIR}/linux 15 | ) 16 | 17 | find_package("Threads") 18 | 19 | set_module_info(${CURRENT_LIB_NAME} "${${CURRENT_LIB_NAME}_INC_DIRS}") 20 | get_module_info() 21 | 22 | foreach(FOREACH_SRC_DIR ${${CURRENT_LIB_NAME}_SRC_DIRS}) 23 | aux_source_directory(${FOREACH_SRC_DIR} ${CURRENT_LIB_NAME}_SOURCES) 24 | endforeach() 25 | 26 | foreach(lib ${DEPEND_LIBRARY_INTERNAL}) 27 | list(APPEND DEPEND_LIBRARY ${LIBRARY_PREFIX}-${lib}) 28 | list(APPEND DEPEND_LIBRARY_STATIC lib${LIBRARY_PREFIX}-${lib}.a) 29 | endforeach() 30 | 31 | # 生成动态库 32 | add_library(${CURRENT_LIB_NAME} SHARED ${${CURRENT_LIB_NAME}_SOURCES}) 33 | target_link_libraries(${CURRENT_LIB_NAME} ${DEPEND_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) 34 | # 生成静态库 35 | add_library(${CURRENT_LIB_NAME}_static STATIC ${${CURRENT_LIB_NAME}_SOURCES}) 36 | target_link_libraries(${CURRENT_LIB_NAME}_static ${DEPEND_LIBRARY_STATIC} ${CMAKE_THREAD_LIBS_INIT}) 37 | 38 | # 进行必要的配置,以生成动态库和静态库 39 | # 指定静态库的输出名称 40 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES OUTPUT_NAME ${CURRENT_LIB_NAME}) 41 | # 使动态库和静态库同时存在 42 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1) 43 | set_target_properties(${CURRENT_LIB_NAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 44 | # 指定动态库版本 45 | set_target_properties(${CURRENT_LIB_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) 46 | 47 | # 打包相关 install package 48 | install(TARGETS ${CURRENT_LIB_NAME} ${CURRENT_LIB_NAME}_static 49 | EXPORT ${CURRENT_LIB_NAME}-targets 50 | RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin 51 | LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 52 | ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 53 | PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include 54 | ) 55 | 56 | install(DIRECTORY ${${CURRENT_LIB_NAME}_INC_DIRS} 57 | DESTINATION "${CMAKE_INSTALL_PREFIX}/include/platform" 58 | FILES_MATCHING 59 | PATTERN "*.h" 60 | PATTERN "*.hpp" 61 | PATTERN "CMakeLists.txt" EXCLUDE) 62 | 63 | 64 | -------------------------------------------------------------------------------- /platform/FreeRTOS/platform_memory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-14 22:02:07 5 | * @LastEditTime: 2020-04-27 16:32:58 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 | #include "FreeRTOS.h" 12 | 13 | void *platform_memory_alloc(size_t size) 14 | { 15 | char *ptr; 16 | ptr = pvPortMalloc(size); 17 | memset(ptr, 0, size); 18 | return (void *)ptr; 19 | } 20 | 21 | void *platform_memory_calloc(size_t num, size_t size) 22 | { 23 | return pvPortMalloc(num * size); 24 | } 25 | 26 | void platform_memory_free(void *ptr) 27 | { 28 | vPortFree(ptr); 29 | } 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /platform/FreeRTOS/platform_memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-14 22:06:35 5 | * @LastEditTime: 2020-04-27 16:27:29 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 | void *platform_memory_alloc(size_t size); 15 | void *platform_memory_calloc(size_t num, size_t size); 16 | void platform_memory_free(void *ptr); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /platform/FreeRTOS/platform_mutex.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 18:27:19 5 | * @LastEditTime: 2020-04-27 22:22:27 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 = xSemaphoreCreateMutex(); 13 | return 0; 14 | } 15 | 16 | int platform_mutex_lock(platform_mutex_t* m) 17 | { 18 | return xSemaphoreTake(m->mutex, portMAX_DELAY); 19 | } 20 | 21 | int platform_mutex_trylock(platform_mutex_t* m) 22 | { 23 | return xSemaphoreTake(m->mutex, 0); 24 | } 25 | 26 | int platform_mutex_unlock(platform_mutex_t* m) 27 | { 28 | return xSemaphoreGive(m->mutex); 29 | } 30 | 31 | int platform_mutex_destroy(platform_mutex_t* m) 32 | { 33 | vSemaphoreDelete(m->mutex); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /platform/FreeRTOS/platform_mutex.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 18:31:33 5 | * @LastEditTime: 2020-04-27 17:04:46 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 "FreeRTOS.h" 12 | #include "semphr.h" 13 | 14 | typedef struct platform_mutex { 15 | SemaphoreHandle_t mutex; 16 | } platform_mutex_t; 17 | 18 | int platform_mutex_init(platform_mutex_t* m); 19 | int platform_mutex_lock(platform_mutex_t* m); 20 | int platform_mutex_trylock(platform_mutex_t* m); 21 | int platform_mutex_unlock(platform_mutex_t* m); 22 | int platform_mutex_destroy(platform_mutex_t* m); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /platform/FreeRTOS/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-27 23:46:54 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 | #include "lwip/opt.h" 15 | #include "lwip/sys.h" 16 | #include "lwip/api.h" 17 | #include 18 | #include "lwip/netdb.h" 19 | 20 | #define PLATFORM_NET_PROTO_TCP 0 /**< The TCP transport protocol */ 21 | #define PLATFORM_NET_PROTO_UDP 1 /**< The UDP transport protocol */ 22 | 23 | int platform_net_socket_connect(const char *host, const char *port, int proto); 24 | int platform_net_socket_recv(int fd, void *buf, size_t len, int flags); 25 | int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout); 26 | int platform_net_socket_write(int fd, void *buf, size_t len); 27 | int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout); 28 | int platform_net_socket_close(int fd); 29 | int platform_net_socket_set_block(int fd); 30 | int platform_net_socket_set_nonblock(int fd); 31 | int platform_net_socket_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen); 32 | 33 | #endif /* _PLATFORM_NET_SOCKET_H_ */ 34 | -------------------------------------------------------------------------------- /platform/FreeRTOS/platform_thread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-23 19:26:27 5 | * @LastEditTime: 2020-09-23 08:53:43 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 | BaseType_t err; 19 | platform_thread_t *thread; 20 | 21 | thread = platform_memory_alloc(sizeof(platform_thread_t)); 22 | 23 | (void)tick; 24 | 25 | err = xTaskCreate(entry, name, stack_size, param, priority, &thread->thread); 26 | 27 | if(pdPASS != err) { 28 | platform_memory_free(thread); 29 | return NULL; 30 | } 31 | 32 | return thread; 33 | } 34 | 35 | void platform_thread_startup(platform_thread_t* thread) 36 | { 37 | (void)thread; 38 | } 39 | 40 | 41 | void platform_thread_stop(platform_thread_t* thread) 42 | { 43 | vTaskSuspend(thread->thread); 44 | } 45 | 46 | void platform_thread_start(platform_thread_t* thread) 47 | { 48 | vTaskResume(thread->thread); 49 | } 50 | 51 | void platform_thread_destroy(platform_thread_t* thread) 52 | { 53 | if (NULL != thread) 54 | vTaskDelete(thread->thread); 55 | 56 | platform_memory_free(thread); 57 | } 58 | 59 | 60 | -------------------------------------------------------------------------------- /platform/FreeRTOS/platform_thread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 18:31:44 5 | * @LastEditTime: 2020-04-27 17:04:25 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 "FreeRTOS.h" 12 | #include "task.h" 13 | 14 | typedef struct platform_thread { 15 | TaskHandle_t thread; 16 | } platform_thread_t; 17 | 18 | platform_thread_t *platform_thread_init( const char *name, 19 | void (*entry)(void *), 20 | void * const param, 21 | unsigned int stack_size, 22 | unsigned int priority, 23 | unsigned int tick); 24 | void platform_thread_startup(platform_thread_t* thread); 25 | void platform_thread_stop(platform_thread_t* thread); 26 | void platform_thread_start(platform_thread_t* thread); 27 | void platform_thread_destroy(platform_thread_t* thread); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /platform/FreeRTOS/platform_timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-10 22:16:41 5 | * @LastEditTime: 2020-04-27 22:35:34 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 | #include "FreeRTOS.h" 11 | #include "task.h" 12 | 13 | static uint32_t platform_uptime_ms(void) 14 | { 15 | #if (configTICK_RATE_HZ == 1000) 16 | return (uint32_t)xTaskGetTickCount(); 17 | #else 18 | TickType_t tick = 0u; 19 | 20 | tick = xTaskGetTickCount() * 1000; 21 | return (uint32_t)((tick + configTICK_RATE_HZ - 1) / configTICK_RATE_HZ); 22 | #endif 23 | } 24 | 25 | void platform_timer_init(platform_timer_t* timer) 26 | { 27 | timer->time = 0; 28 | } 29 | 30 | void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout) 31 | { 32 | timer->time = platform_uptime_ms(); 33 | timer->time += timeout; 34 | } 35 | 36 | char platform_timer_is_expired(platform_timer_t* timer) 37 | { 38 | return platform_uptime_ms() > timer->time ? 1 : 0; 39 | } 40 | 41 | int platform_timer_remain(platform_timer_t* timer) 42 | { 43 | uint32_t now; 44 | 45 | now = platform_uptime_ms(); 46 | if (timer->time <= now) { 47 | return 0; 48 | } 49 | 50 | return timer->time - now; 51 | } 52 | 53 | unsigned long platform_timer_now(void) 54 | { 55 | return (unsigned long) platform_uptime_ms(); 56 | } 57 | 58 | void platform_timer_usleep(unsigned long usec) 59 | { 60 | 61 | TickType_t tick; 62 | 63 | if(usec != 0) { 64 | tick = usec / portTICK_PERIOD_MS; 65 | 66 | if (tick == 0) 67 | tick = 1; 68 | } 69 | 70 | vTaskDelay(tick); 71 | } 72 | 73 | -------------------------------------------------------------------------------- /platform/FreeRTOS/platform_timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-10 22:18:32 5 | * @LastEditTime: 2020-04-27 17:15:58 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 "FreeRTOS.h" 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 | -------------------------------------------------------------------------------- /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.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 MQTT_NETSOCKET_USE_SAL 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #else 24 | 25 | #include "lwip/opt.h" 26 | #include "lwip/sys.h" 27 | #include "lwip/api.h" 28 | #include 29 | #include "lwip/netdb.h" 30 | 31 | #endif // MQTT_NETSOCKET_USE_SAL 32 | 33 | #define PLATFORM_NET_PROTO_TCP 0 /**< The TCP transport protocol */ 34 | #define PLATFORM_NET_PROTO_UDP 1 /**< The UDP transport protocol */ 35 | 36 | int platform_net_socket_connect(const char *host, const char *port, int proto); 37 | int platform_net_socket_recv(int fd, void *buf, size_t len, int flags); 38 | int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout); 39 | int platform_net_socket_write(int fd, void *buf, size_t len); 40 | int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout); 41 | int platform_net_socket_close(int fd); 42 | int platform_net_socket_set_block(int fd); 43 | int platform_net_socket_set_nonblock(int fd); 44 | int platform_net_socket_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen); 45 | 46 | #endif /* _PLATFORM_NET_SOCKET_H_ */ 47 | -------------------------------------------------------------------------------- /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-09-20 14:30:08 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-09-20 14:30:25 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-06-17 21:24:24 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 | -------------------------------------------------------------------------------- /platform/TencentOS-tiny/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 "tos_k.h" 9 | 10 | 11 | void *platform_memory_alloc(size_t size) 12 | { 13 | return tos_mmheap_alloc(size); 14 | } 15 | 16 | void *platform_memory_calloc(size_t num, size_t size) 17 | { 18 | return tos_mmheap_calloc(num, size); 19 | } 20 | 21 | void platform_memory_free(void *ptr) 22 | { 23 | tos_mmheap_free(ptr); 24 | } 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /platform/TencentOS-tiny/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 | void *platform_memory_alloc(size_t size); 15 | void *platform_memory_calloc(size_t num, size_t size); 16 | void platform_memory_free(void *ptr); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /platform/TencentOS-tiny/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 | return tos_mutex_create(&(m->mutex)); 13 | } 14 | 15 | int platform_mutex_lock(platform_mutex_t* m) 16 | { 17 | return tos_mutex_pend(&(m->mutex)); 18 | } 19 | 20 | int platform_mutex_trylock(platform_mutex_t* m) 21 | { 22 | return tos_mutex_pend_timed(&(m->mutex), 0); 23 | } 24 | 25 | int platform_mutex_unlock(platform_mutex_t* m) 26 | { 27 | return tos_mutex_post(&(m->mutex)); 28 | } 29 | 30 | int platform_mutex_destroy(platform_mutex_t* m) 31 | { 32 | return tos_mutex_destroy(&(m->mutex)); 33 | } 34 | -------------------------------------------------------------------------------- /platform/TencentOS-tiny/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 "tos_k.h" 12 | 13 | typedef struct platform_mutex { 14 | k_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/TencentOS-tiny/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-06-18 20:12:15 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 MQTT_NETSOCKET_USING_AT 15 | 16 | #include "sal_module_wrapper.h" 17 | 18 | #else 19 | 20 | #include "lwip/opt.h" 21 | #include "lwip/sys.h" 22 | #include "lwip/api.h" 23 | #include 24 | #include "lwip/netdb.h" 25 | 26 | #endif 27 | 28 | #define PLATFORM_NET_PROTO_TCP 0 /**< The TCP transport protocol */ 29 | #define PLATFORM_NET_PROTO_UDP 1 /**< The UDP transport protocol */ 30 | 31 | int platform_net_socket_connect(const char *host, const char *port, int proto); 32 | int platform_net_socket_recv(int fd, void *buf, size_t len, int flags); 33 | int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout); 34 | int platform_net_socket_write(int fd, void *buf, size_t len); 35 | int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout); 36 | int platform_net_socket_close(int fd); 37 | 38 | #ifndef MQTT_NETSOCKET_USING_AT 39 | int platform_net_socket_set_block(int fd); 40 | int platform_net_socket_set_nonblock(int fd); 41 | int platform_net_socket_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen); 42 | #endif 43 | 44 | #endif /* _PLATFORM_NET_SOCKET_H_ */ 45 | -------------------------------------------------------------------------------- /platform/TencentOS-tiny/platform_thread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-23 19:26:27 5 | * @LastEditTime : 2020-02-15 23:32:25 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 | k_err_t err; 20 | k_stack_t *thread_stack; 21 | thread = platform_memory_alloc(sizeof(platform_thread_t)); 22 | thread_stack = (k_stack_t*) platform_memory_alloc(stack_size); 23 | 24 | err = tos_task_create(&(thread->thread), 25 | (char*)name, 26 | entry, 27 | param, 28 | priority, 29 | thread_stack, 30 | stack_size, 31 | tick); 32 | 33 | if(err != K_ERR_NONE) { 34 | platform_memory_free(thread); 35 | platform_memory_free(thread_stack); 36 | } 37 | 38 | return thread; 39 | } 40 | 41 | void platform_thread_startup(platform_thread_t* thread) 42 | { 43 | (void)thread; 44 | } 45 | 46 | 47 | void platform_thread_stop(platform_thread_t* thread) 48 | { 49 | tos_task_suspend(&(thread->thread)); 50 | } 51 | 52 | void platform_thread_start(platform_thread_t* thread) 53 | { 54 | tos_task_resume(&(thread->thread)); 55 | } 56 | 57 | 58 | void platform_thread_destroy(platform_thread_t* thread) 59 | { 60 | if (NULL != thread) 61 | tos_task_destroy(&(thread->thread)); 62 | platform_memory_free(&(thread->thread)); 63 | platform_memory_free(&(thread->thread.stk_size)); 64 | } 65 | 66 | 67 | -------------------------------------------------------------------------------- /platform/TencentOS-tiny/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 "tos_k.h" 12 | 13 | typedef struct platform_thread { 14 | k_task_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/TencentOS-tiny/platform_timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-10 22:16:41 5 | * @LastEditTime: 2020-04-27 22:37:33 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 (TOS_CFG_CPU_TICK_PER_SECOND == 1000) 14 | return (uint32_t)tos_systick_get(); 15 | #else 16 | k_tick_t tick = 0u; 17 | 18 | tick = tos_systick_get() * 1000; 19 | return (uint32_t)((tick + TOS_CFG_CPU_TICK_PER_SECOND - 1) / TOS_CFG_CPU_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; 59 | k_tick_t tick; 60 | 61 | if(usec != 0) { 62 | 63 | ms = usec / TOS_CFG_CPU_TICK_PER_SECOND; 64 | 65 | if (ms == 0) { 66 | ms = 1; 67 | } 68 | } 69 | 70 | tick = tos_millisec2tick(ms); 71 | 72 | tos_sleep_ms(tick); 73 | } 74 | -------------------------------------------------------------------------------- /platform/TencentOS-tiny/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 "tos_k.h" 12 | 13 | 14 | typedef struct platform_timer { 15 | uint32_t time; 16 | } platform_timer_t; 17 | 18 | void platform_timer_init(platform_timer_t* timer); 19 | void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout); 20 | char platform_timer_is_expired(platform_timer_t* timer); 21 | int platform_timer_remain(platform_timer_t* timer); 22 | unsigned long platform_timer_now(void); 23 | void platform_timer_usleep(unsigned long usec); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /platform/TencentOS-tiny/sal/README.md: -------------------------------------------------------------------------------- 1 | # 注意 2 | 3 | 如果用的是AT模组,可以结合TencentOS tiny的AT框架与SAL层使用platform_net_socket.c,替换原有BSD socket 接口即可。 -------------------------------------------------------------------------------- /platform/TencentOS-tiny/sal/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-01-13 02:48:53 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; 13 | 14 | fd = tos_sal_module_connect(host, port, TOS_SAL_PROTO_TCP); 15 | 16 | if (fd < 0) { 17 | return MQTT_CONNECT_FAILED_ERROR; 18 | } 19 | 20 | return fd; 21 | } 22 | 23 | int platform_net_socket_recv(int fd, void *buf, size_t len, int flags) 24 | { 25 | return tos_sal_module_recv(fd, buf, len); 26 | } 27 | 28 | int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout) 29 | { 30 | return tos_sal_module_recv_timeout(fd, buf, len, timeout); 31 | } 32 | 33 | int platform_net_socket_write(int fd, void *buf, size_t len) 34 | { 35 | return tos_sal_module_send(fd, buf, len); 36 | } 37 | 38 | int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout) 39 | { 40 | return tos_sal_module_send(fd, buf, len); 41 | } 42 | 43 | int platform_net_socket_close(int fd) 44 | { 45 | return tos_sal_module_close(fd); 46 | } 47 | 48 | -------------------------------------------------------------------------------- /platform/TencentOS-tiny/sal/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-02-19 01:02:51 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 | #include "stddef.h" 14 | 15 | #define PLATFORM_NET_PROTO_TCP 0 /**< The TCP transport protocol */ 16 | #define PLATFORM_NET_PROTO_UDP 1 /**< The UDP transport protocol */ 17 | 18 | int platform_net_socket_connect(const char *host, const char *port, int proto); 19 | int platform_net_socket_recv(int fd, void *buf, size_t len, int flags); 20 | int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout); 21 | int platform_net_socket_write(int fd, void *buf, size_t len); 22 | int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout); 23 | int platform_net_socket_close(int fd); 24 | 25 | #endif /* _PLATFORM_NET_SOCKET_H_ */ 26 | -------------------------------------------------------------------------------- /platform/linux/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /platform/linux/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 20:26:04 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 | 10 | void *platform_memory_alloc(size_t size) 11 | { 12 | return malloc(size); 13 | } 14 | 15 | void *platform_memory_calloc(size_t num, size_t size) 16 | { 17 | return calloc(num, size); 18 | } 19 | 20 | void platform_memory_free(void *ptr) 21 | { 22 | free(ptr); 23 | } 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /platform/linux/platform_memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-14 22:06:35 5 | * @LastEditTime: 2020-10-17 14:17:24 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 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | void *platform_memory_alloc(size_t size); 19 | void *platform_memory_calloc(size_t num, size_t size); 20 | void platform_memory_free(void *ptr); 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /platform/linux/platform_mutex.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 18:27:19 5 | * @LastEditTime: 2020-02-23 15:01:06 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 | return pthread_mutex_init(&(m->mutex), NULL); 13 | } 14 | 15 | int platform_mutex_lock(platform_mutex_t* m) 16 | { 17 | return pthread_mutex_lock(&(m->mutex)); 18 | } 19 | 20 | int platform_mutex_trylock(platform_mutex_t* m) 21 | { 22 | return pthread_mutex_trylock(&(m->mutex)); 23 | } 24 | 25 | int platform_mutex_unlock(platform_mutex_t* m) 26 | { 27 | return pthread_mutex_unlock(&(m->mutex)); 28 | } 29 | 30 | int platform_mutex_destroy(platform_mutex_t* m) 31 | { 32 | return pthread_mutex_destroy(&(m->mutex)); 33 | } 34 | -------------------------------------------------------------------------------- /platform/linux/platform_mutex.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 18:31:33 5 | * @LastEditTime: 2020-10-17 14:17:31 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 | #include 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | typedef struct platform_mutex { 17 | pthread_mutex_t mutex; 18 | } platform_mutex_t; 19 | 20 | int platform_mutex_init(platform_mutex_t* m); 21 | int platform_mutex_lock(platform_mutex_t* m); 22 | int platform_mutex_trylock(platform_mutex_t* m); 23 | int platform_mutex_unlock(platform_mutex_t* m); 24 | int platform_mutex_destroy(platform_mutex_t* m); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /platform/linux/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-10-17 14:17:45 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 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | #define PLATFORM_NET_PROTO_TCP 0 /**< The TCP transport protocol */ 33 | #define PLATFORM_NET_PROTO_UDP 1 /**< The UDP transport protocol */ 34 | 35 | int platform_net_socket_connect(const char *host, const char *port, int proto); 36 | int platform_net_socket_recv(int fd, void *buf, size_t len, int flags); 37 | int platform_net_socket_recv_timeout(int fd, unsigned char *buf, int len, int timeout); 38 | int platform_net_socket_write(int fd, void *buf, size_t len); 39 | int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int timeout); 40 | int platform_net_socket_close(int fd); 41 | int platform_net_socket_set_block(int fd); 42 | int platform_net_socket_set_nonblock(int fd); 43 | int platform_net_socket_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen); 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | 49 | #endif /* _PLATFORM_NET_SOCKET_H_ */ 50 | -------------------------------------------------------------------------------- /platform/linux/platform_thread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-23 19:26:27 5 | * @LastEditTime: 2020-02-23 16:19:07 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 | int res; 19 | platform_thread_t *thread; 20 | void *(*thread_entry) (void *); 21 | 22 | thread_entry = (void *(*)(void*))entry; 23 | thread = platform_memory_alloc(sizeof(platform_thread_t)); 24 | 25 | res = pthread_create(&thread->thread, NULL, thread_entry, param); 26 | if(res != 0) { 27 | platform_memory_free(thread); 28 | } 29 | 30 | thread->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; 31 | thread->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; 32 | 33 | return thread; 34 | } 35 | 36 | void platform_thread_startup(platform_thread_t* thread) 37 | { 38 | (void) thread; 39 | } 40 | 41 | void platform_thread_stop(platform_thread_t* thread) 42 | { 43 | pthread_mutex_lock(&(thread->mutex)); 44 | pthread_cond_wait(&(thread->cond), &(thread->mutex)); 45 | pthread_mutex_unlock(&(thread->mutex)); 46 | } 47 | 48 | void platform_thread_start(platform_thread_t* thread) 49 | { 50 | pthread_mutex_lock(&(thread->mutex)); 51 | pthread_cond_signal(&(thread->cond)); 52 | pthread_mutex_unlock(&(thread->mutex)); 53 | } 54 | 55 | void platform_thread_destroy(platform_thread_t* thread) 56 | { 57 | if (NULL != thread) 58 | pthread_detach(thread->thread); 59 | } 60 | 61 | 62 | -------------------------------------------------------------------------------- /platform/linux/platform_thread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-15 18:31:44 5 | * @LastEditTime: 2020-10-17 14:15:21 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 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | typedef struct platform_thread { 18 | pthread_t thread; 19 | pthread_mutex_t mutex; 20 | pthread_cond_t cond; 21 | } platform_thread_t; 22 | 23 | platform_thread_t *platform_thread_init( const char *name, 24 | void (*entry)(void *), 25 | void * const param, 26 | unsigned int stack_size, 27 | unsigned int priority, 28 | unsigned int tick); 29 | void platform_thread_startup(platform_thread_t* thread); 30 | void platform_thread_stop(platform_thread_t* thread); 31 | void platform_thread_start(platform_thread_t* thread); 32 | void platform_thread_destroy(platform_thread_t* thread); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /platform/linux/platform_timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-10 22:16:41 5 | * @LastEditTime: 2020-06-05 17:18:48 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 | void platform_timer_init(platform_timer_t* timer) 12 | { 13 | timer->time = (struct timeval){0, 0}; 14 | } 15 | 16 | void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout) 17 | { 18 | struct timeval now; 19 | gettimeofday(&now, NULL); 20 | struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000}; 21 | timeradd(&now, &interval, &timer->time); 22 | } 23 | 24 | char platform_timer_is_expired(platform_timer_t* timer) 25 | { 26 | struct timeval now, res; 27 | gettimeofday(&now, NULL); 28 | timersub(&timer->time, &now, &res); 29 | return ((res.tv_sec < 0) || (res.tv_sec == 0 && res.tv_usec <= 0)); 30 | } 31 | 32 | int platform_timer_remain(platform_timer_t* timer) 33 | { 34 | struct timeval now, res; 35 | gettimeofday(&now, NULL); 36 | timersub(&timer->time, &now, &res); 37 | return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000; 38 | } 39 | 40 | unsigned long platform_timer_now(void) 41 | { 42 | return (unsigned long) time(NULL); 43 | } 44 | 45 | void platform_timer_usleep(unsigned long usec) 46 | { 47 | usleep(usec); 48 | } 49 | -------------------------------------------------------------------------------- /platform/linux/platform_timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: jiejie 3 | * @Github: https://github.com/jiejieTop 4 | * @Date: 2019-12-10 22:18:32 5 | * @LastEditTime: 2020-10-17 14:17:55 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 | #include 13 | #include 14 | #include 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | typedef struct platform_timer { 21 | struct timeval time; 22 | } platform_timer_t; 23 | 24 | void platform_timer_init(platform_timer_t* timer); 25 | void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout); 26 | char platform_timer_is_expired(platform_timer_t* timer); 27 | int platform_timer_remain(platform_timer_t* timer); 28 | unsigned long platform_timer_now(void); 29 | void platform_timer_usleep(unsigned long usec); 30 | 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /png/mqtt-tool.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiejieTop/mqttclient/956e0c8dcac02d4b107ccb49f1990c7718b6b585/png/mqtt-tool.png -------------------------------------------------------------------------------- /png/mqttclient.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiejieTop/mqttclient/956e0c8dcac02d4b107ccb49f1990c7718b6b585/png/mqttclient.png --------------------------------------------------------------------------------