littlebot
Published on 2025-04-12 / 1 Visits
0

【源码】基于C和Python的LWM2M协议开发与测试项目

项目简介

本项目聚焦于Open Mobile Alliance的LightWeight M2M(LWM2M)协议。运用C语言实现LWM2M协议相关功能,同时借助Python构建测试套件对协议实现进行全面测试。项目涵盖LWM2M引擎、CoAP栈适配、数据格式序列化/反序列化等核心模块,还提供命令行形式的LWM2M客户端、服务器和引导服务器示例。测试套件覆盖注册、观察、资源访问控制、固件更新等多方面,可有效验证协议实现的正确性与稳定性。

项目的主要特性和功能

  1. 多角色支持:支持LWM2M客户端、服务器和引导服务器三种角色,可灵活搭建物联网通信系统。
  2. 数据格式处理:支持TLV、JSON、SenML JSON等多种数据格式的序列化和反序列化。
  3. 多平台适配:可依据目标平台的字节序(大端或小端)进行编译配置。
  4. 测试套件丰富:采用Python的CUnit测试框架,提供多个测试用例,对LWM2M协议不同特性进行全面测试。
  5. 内存管理检查:通过内存跟踪检查内存使用情况,确保LWM2M数据创建和释放时内存管理正确。

安装使用步骤

环境准备

  1. 安装必要依赖:在Ubuntu 20.04系统中,执行以下命令安装依赖: bash apt install build-essential clang-format clang-format-10 clang-tools-10 cmake gcovr git libcunit1-dev ninja-build python3-pip pip3 install gitlint
  2. 复制项目源码bash cd wakaama

编译项目

Wakaama不是一个库,而是需要与应用程序一起构建的文件,使用CMake >= 3.13进行编译。根据需求定义编译开关,如LWM2M_CLIENT_MODELWM2M_SERVER_MODE等。例如,要编译LWM2M服务器示例: bash mkdir build_server cd build_server cmake [wakaama directory]/examples/server make

运行示例程序

服务器

bash ./lwm2mserver [Options] 选项说明: ``` Usage: lwm2mserver [OPTION] Launch a LWM2M server on localhost.

Options: -4 Use IPv4 connection. Default: IPv6 connection -l PORT Set the local UDP port of the Server. Default: 5683 -S BYTES CoAP block size. Options: 16, 32, 64, 128, 256, 512, 1024. Default: 1024 ```

客户端

bash ./lwm2mclient [Options] 选项说明: Usage: lwm2mclient [OPTION] Launch a LWM2M client. Options: -n NAME Set the endpoint name of the Client. Default: testlwm2mclient -l PORT Set the local UDP port of the Client. Default: 56830 -h HOST Set the hostname of the LWM2M Server to connect to. Default: localhost -p PORT Set the port of the LWM2M Server to connect to. Default: 5683 -4 Use IPv4 connection. Default: IPv6 connection -t TIME Set the lifetime of the Client. Default: 300 -b Bootstrap requested. -c Change battery level over time. -S BYTES CoAP block size. Options: 16, 32, 64, 128, 256, 512, 1024. Default: 1024 若启用DTLS功能,还需额外设置相关参数: -i Set the device management or bootstrap server PSK identity. If not set use none secure mode -s Set the device management or bootstrap server Pre-Shared-Key. If not set use none secure mode

运行测试

集成测试

bash cd wakaama tools/ci/run_ci.sh --run-build pytest -v tests/integration

下载地址

点击下载 【提取码: 4003】【解压密码: www.makuang.net】