二、移植sdk库至板子 可以参照文档《无AliOS Things的SDK适配指南》来交叉编译libiot_sdk.a库。 https://help.aliyun.com/document_detail/178333.htm?spm=a2c4g.11186623.0.0.1b046fdbIkFmIT#task-1935127
1、配置交叉编译器。 (1)修改文件build-rules/settings.mk中的TOOLCHAIN_DLDIR,比如我这里修改为:
(2)修改build-rules/funcs.mk里面的函数Relative_TcPath增加编译器的相对路径,比如我这里修改为:
(3)增加我们板子的config文件。在src/board/目录下增加一个新的config配置文件,里面的内容可以参照已有的配置文件,比如我参照的是config.ubuntu.x86来新增我的板子的配置文件config.linux.rv1126: CONFIG_ENV_CFLAGS += \
-Os -Wall \
-g3 --coverage \
-D_PLATFORM_IS_LINUX_ \
-D__UBUNTU_SDK_DEMO__ \
CONFIG_ENV_CFLAGS += \
-DWITH_MQTT_DYN_BUF=1 \
-DWITH_MEM_STATS=0 \
-DWITH_MEM_STATS_PER_MODULE=0 \
-DWITH_MQTT_JSON_FLOW=1 \
-DWITH_MQTT_ZIP_TOPIC=1 \
-DWITH_MQTT_SUB_SHORTCUT=1
CONFIG_ENV_CFLAGS += \
-DCONFIG_HTTP_AUTH_TIMEOUT=10000 \
-DCONFIG_MID_HTTP_TIMEOUT=10000 \
-DCONFIG_GUIDER_AUTH_TIMEOUT=10000 \
-DCONFIG_MQTT_RX_MAXLEN=10000 \
-DCONFIG_MBEDTLS_DEBUG_LEVEL=0 \
-DCOMPATIBLE_LK_KV
ifneq (Darwin,$(strip $(shell uname)))
CONFIG_ENV_CFLAGS += -rdynamic
CONFIG_ENV_CFLAGS += -Werror
CONFIG_ENV_CFLAGS += -Wno-missing-braces
else
CONFIG_src/ref-impl/hal :=
CONFIG_examples :=
CONFIG_src/tools/linkkit_tsl_convert :=
endif
CONFIG_src/ref-impl/tls :=
CONFIG_src/ref-impl/hal :=
CONFIG_ENV_LDFLAGS += -lpthread -lrt
#交叉编译器的前缀,这里不要带路径
CROSS_PREFIX := arm-linux-gnueabihf-
2、配置SDK的功能 我们可以直接编辑根目录下面的make.settings文件,也可以执行make menuconfig命令进行图形化配置,比如配置配网的方式等,这里我使用设备AP配网,所以我使能设备AP配网:
3、编译libiot_sdk.a库 先执行make clean命令进行清理操作,再执行make reconfig选择配置。
刚才我们添加的板子处于序号3的配置,输入3,然后回车:
然后执行make命令进行编译,编译无误的结果:
如果没有编译错误,生成的库文件libiot_sdk.a在output/release/lib目录下面。 4、交叉编译demo工程 我们准备一个工程,然后把sdk里的必要文件到我们的工程里,比如我这里新建一个ali_smartliving文件夹存放sdk相关的代码,ali_smartliving里面的内容如:
其中include及src文件夹就是sdk里对应的文件夹,lib里放的是我们上面交叉编译得到的libiot_sdk.a。 把sdk开发包examples/linkkit/living_platform的整个文件夹复制到我们的工程里,然后编写CMakeLists.txt文件: cmake_minimum_required(VERSION 3.1)
message(STATUS "######################################")
message(STATUS "Build on target platform: ${TARGET_PLATFORM}")
message(STATUS "######################################")
####################################### 编译模式设置 #######################################
set(DEBUG_VERSION "DebugMode")
set(RELEASE_VERSION "ReleaseMode")
set(COMPILE_MODE ${RELEASE_VERSION}) # 编译模式
set(TARGET_NAME "ali_sdk_test") # 目标可执行程序名称
string(TIMESTAMP COMPILE_TIME %m%d_%H%M) # 生成编译时间
# Debug模式(目标:工程名+编译时间)
if(${COMPILE_MODE} MATCHES ${DEBUG_VERSION})
message(STATUS "Compile mode: ${COMPILE_MODE}")
set(USE_RELEASE_MODE 0)
set(CMAKE_BUILD_TYPE "Debug")
set(target ${TARGET_NAME}_${COMPILE_TIME})
# Release模式(目标:工程名+版本号)
else()
message(STATUS "Compile mode: ${COMPILE_MODE}")
set(USE_RELEASE_MODE 1)
set(CMAKE_BUILD_TYPE "Release")
set(VERSION_MAJOR 1) # 一级版本号
set(VERSION_MINOR 0) # 二级版本号
set(VERSION_LEVEL3 0) # 三级版本号
set(target ${TARGET_NAME}_V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_LEVEL3})
endif()
project(${target})
####################################### 编译选项配置 #######################################
set(TARGET_PLATFORM "x86-64" CACHE STRING "which platform the source code should be compiled on")
message(STATUS "Build on target platform: ${TARGET_PLATFORM}")
if(TARGET_PLATFORM MATCHES "rv1126")
include(cmake/rv1126_crosscompile_toolchain.cmake) #交叉编译环境设置
elseif(TARGET_PALTFORM MATCHES "x86-64")
message(STATUS "PC_DEBUG: ${PC_DEBUG}")
endif()
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall") #-g for debug
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-Map=out.map")
if(TARGET_PLATFORM MATCHES "rv1126")
set(CMAKE_C_FLAGS "-fdata-sections -g -rdynamic -mapcs-frame -funwind-tables -ffunction-sections -Wall -Os")
set(CMAKE_C_FLAGS "$ENV{CMAKE_C_FLAGS} -DDLL_HAL_EXPORTS -DCOMPATIBLE_LK_KV -lrt")
elseif(TARGET_PALTFORM MATCHES "x86-64")
set(CMAKE_C_FLAGS "-fdata-sections -g -rdynamic -mapcs-frame -funwind-tables -ffunction-sections -Wall -Os")
set(CMAKE_C_FLAGS "$ENV{CMAKE_C_FLAGS} -DDLL_HAL_EXPORTS -DCOMPATIBLE_LK_KV -lrt")
endif()
set(CMAKE_CXX_FLAGS "-fdata-sections -Wno-psabi")
####################################### 共享库 ############################################
set(ALI_IOT_LIBDIR ali_smartliving/lib)
link_directories(${ALI_IOT_LIBDIR})
set(ALI_IOT_LIBS libiot_sdk.a)
add_definitions (-DDLL_IOT_EXPORTS)
add_definitions (-DAWSS_DISABLE_ENROLLEE)
add_definitions (-DAWSS_DISABLE_REGISTRAR)
add_definitions (-DAWSS_SUPPORT_ADHA)
add_definitions (-DAWSS_SUPPORT_AHA)
add_definitions (-DAWSS_SUPPORT_APLIST)
add_definitions (-DAWSS_SUPPORT_DEV_AP)
add_definitions (-DAWSS_SUPPORT_PHONEASAP)
add_definitions (-DAWSS_SUPPORT_ROUTER)
add_definitions (-DAWSS_SUPPORT_SMARTCONFIG)
add_definitions (-DAWSS_SUPPORT_SMARTCONFIG_WPS)
add_definitions (-DAWSS_SUPPORT_ZEROCONFIG)
add_definitions (-DCOAP_SERV_MULTITHREAD)
add_definitions (-DCONFIG_HTTP_AUTH_TIMEOUT=5000)
add_definitions (-DCONFIG_MID_HTTP_TIMEOUT=5000)
add_definitions (-DCONFIG_GUIDER_AUTH_TIMEOUT=10000)
add_definitions (-DDEVICE_MODEL_ENABLED)
add_definitions (-DDEV_BIND_ENABLED)
add_definitions (-DFORCE_SSL_VERIFY)
add_definitions (-DMQTT_COMM_ENABLED)
add_definitions (-DMQTT_DIRECT)
add_definitions (-DOTA_ENABLED)
add_definitions (-DOTA_SIGNAL_CHANNEL=1)
add_definitions (-DSUPPORT_TLS)
add_definitions (-DWIFI_PROVISION_ENABLED)
add_definitions (-D_PLATFORM_IS_HOST_)
include_directories(build)
include_directories(ali_smartliving)
include_directories(ali_smartliving/include)
include_directories(ali_smartliving/include/exports)
include_directories(ali_smartliving/include/imports)
include_directories(ali_smartliving/linkkit/living_platform)
include_directories(ali_smartliving/src/ref-impl/hal)
include_directories(ali_smartliving/src/ref-impl/hal/os/ubuntu)
include_directories(ali_smartliving/src/infra/log)
include_directories(ali_smartliving/src/ref-impl/tls/include)
include_directories(ali_smartliving/src/infra/utils/digest)
include_directories(living_platform)
####################################### 源文件 ############################################
aux_source_directory(living_platform living_platform_src)
file(GLOB hal_srcs ${PROJECT_SOURCE_DIR}/ali_smartliving/src/ref-impl/hal/os/ubuntu/*.c)
file(GLOB ssl_mbedtls_srcs ${PROJECT_SOURCE_DIR}/ali_smartliving/src/ref-impl/hal/ssl/mbedtls/*.c)
file(GLOB tls_srcs ${PROJECT_SOURCE_DIR}/ali_smartliving/src/ref-impl/tls/library/*.c)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
message(STATUS "################## Compile time:${COMPILE_TIME} ##################")
add_executable(${PROJECT_NAME}
${living_platform_src}
${hal_srcs}
${ssl_mbedtls_srcs}
${tls_srcs}
main.c
)
target_link_libraries(${PROJECT_NAME}
${ALI_IOT_LIBS}
Threads::Threads
m
)
我们工程的结构如:
注意: 大家自己移植、编译,可能会遇到如下两个问题: (1)很多系统函数找不到,加上编译参数-lrt即可。 (2)softap_decrypt_password函数找不到。这个函数是在libawss_security.a库里的,而这个库我们并没有交叉编译,demo的文档也有提到:
|